forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pre-alloc remote memory; rebase from vpux/2021/3
- Loading branch information
1 parent
6f860dd
commit 4cdb4bb
Showing
9 changed files
with
242 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
inference-engine/samples/benchmark_app/remotecontext_helper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (C) 2018-2020 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#ifdef USE_REMOTE_MEM | ||
#include <string> | ||
#include <algorithm> | ||
#include <utility> | ||
#include <vector> | ||
#include <map> | ||
#include <regex> | ||
|
||
#include <inference_engine.hpp> | ||
|
||
#include <samples/common.hpp> | ||
#include <samples/slog.hpp> | ||
|
||
#include "utils.hpp" | ||
#include "remotecontext_helper.hpp" | ||
#include "WorkloadContext.h" | ||
#include "RemoteMemory.h" | ||
#include "hddl2/hddl2_params.hpp" | ||
#include "ie_compound_blob.h" | ||
|
||
using namespace InferenceEngine; | ||
|
||
class RemoteContextHelper::Impl { | ||
WorkloadID _workloadId = -1; | ||
HddlUnite::WorkloadContext::Ptr _context; | ||
RemoteContext::Ptr _contextPtr; | ||
bool _init = false; | ||
|
||
public: | ||
void Init(InferenceEngine::Core& ie) { | ||
_context = HddlUnite::createWorkloadContext(); | ||
_context->setContext(_workloadId); | ||
auto ret = registerWorkloadContext(_context); | ||
if (ret != HddlStatusCode::HDDL_OK) { | ||
THROW_IE_EXCEPTION << "registerWorkloadContext failed with " << ret; | ||
} | ||
|
||
// init context map and create context based on it | ||
ParamMap paramMap = { {HDDL2_PARAM_KEY(WORKLOAD_CONTEXT_ID), _workloadId} }; | ||
_contextPtr = ie.CreateContext("VPUX", paramMap); | ||
_init = true; | ||
} | ||
|
||
HddlUnite::RemoteMemory::Ptr allocateRemoteMemory(const void* data, const size_t& dataSize) { | ||
auto remoteFrame = std::make_shared<HddlUnite::RemoteMemory>(*_context, | ||
HddlUnite::RemoteMemoryDesc(dataSize, 1, dataSize, 1)); | ||
|
||
if (remoteFrame == nullptr) { | ||
THROW_IE_EXCEPTION << "Failed to allocate remote memory."; | ||
} | ||
|
||
if (remoteFrame->syncToDevice(data, dataSize) != HDDL_OK) { | ||
THROW_IE_EXCEPTION << "Failed to sync memory to device."; | ||
} | ||
return remoteFrame; | ||
} | ||
|
||
void PreallocRemoteMem(InferReqWrap::Ptr& request, | ||
const std::string& inputBlobName, | ||
const Blob::Ptr& inputBlob) { | ||
if (_init == false) | ||
THROW_IE_EXCEPTION << "RemoteContextHelper did not init."; | ||
MemoryBlob::Ptr minput = as<MemoryBlob>(inputBlob); | ||
const TensorDesc& inputTensor = minput->getTensorDesc(); | ||
// locked memory holder should be alive all time while access to its buffer happens | ||
auto minputHolder = minput->rmap(); | ||
auto inputBlobData = minputHolder.as<uint8_t*>(); | ||
|
||
// 1, allocate memory with HddlUnite on device | ||
auto remoteMemory = allocateRemoteMemory(inputBlobData, minput->byteSize()); | ||
|
||
// 2, create remote blob by using already exists remote memory and specify color format of it | ||
ParamMap blobParamMap = { {HDDL2_PARAM_KEY(REMOTE_MEMORY), remoteMemory} }; | ||
RemoteBlob::Ptr remoteBlobPtr = _contextPtr->CreateBlob(inputTensor, blobParamMap); | ||
if (remoteBlobPtr == nullptr) { | ||
THROW_IE_EXCEPTION << "CreateBlob failed."; | ||
} | ||
|
||
// 3, set remote blob | ||
request->setBlob(inputBlobName, remoteBlobPtr); | ||
} | ||
|
||
RemoteContext::Ptr getRemoteContext() { | ||
if (_init == false) | ||
THROW_IE_EXCEPTION << "RemoteContextHelper did not init."; | ||
return _contextPtr; | ||
} | ||
}; | ||
|
||
RemoteContextHelper::RemoteContextHelper() : _impl(new RemoteContextHelper::Impl()) { | ||
} | ||
|
||
RemoteContextHelper::~RemoteContextHelper() { | ||
} | ||
|
||
void RemoteContextHelper::Init(InferenceEngine::Core& ie) { | ||
_impl->Init(ie); | ||
} | ||
|
||
void RemoteContextHelper::PreallocRemoteMem(InferReqWrap::Ptr& request, const std::string& inputBlobName, const Blob::Ptr& inputBlob) { | ||
_impl->PreallocRemoteMem(request, inputBlobName, inputBlob); | ||
} | ||
|
||
InferenceEngine::RemoteContext::Ptr RemoteContextHelper::getRemoteContext() { | ||
return _impl->getRemoteContext(); | ||
} | ||
#endif |
26 changes: 26 additions & 0 deletions
26
inference-engine/samples/benchmark_app/remotecontext_helper.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (C) 2018-2020 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
#include <memory> | ||
#include <inference_engine.hpp> | ||
#include "infer_request_wrap.hpp" | ||
|
||
class RemoteContextHelper { | ||
class Impl; | ||
std::unique_ptr<Impl> _impl; | ||
public: | ||
RemoteContextHelper(); | ||
~RemoteContextHelper(); | ||
|
||
void Init(InferenceEngine::Core& ie); | ||
void PreallocRemoteMem(InferReqWrap::Ptr& request, | ||
const std::string& inputBlobName, | ||
const InferenceEngine::Blob::Ptr& inputBlob); | ||
InferenceEngine::RemoteContext::Ptr getRemoteContext(); | ||
}; |