OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPU] Fix memory map (#31855)
Bogdan Pereanu0d56f7c486c1
diff --git a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cppindex f73bc958f1a..19b9a69293a 100644--- a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp@@ -117,15 +117,6 @@ ZeroInferRequest::ZeroInferRequest(const std::shared_ptr<ZeroInitStructsHolder>&_levelZeroOutputTensors(_metadata.outputs.size(), nullptr) {_logger.debug("ZeroInferRequest::ZeroInferRequest - SyncInferRequest");- ze_device_external_memory_properties_t desc = {};- desc.stype = ZE_STRUCTURE_TYPE_DEVICE_EXTERNAL_MEMORY_PROPERTIES;- auto res = zeDeviceGetExternalMemoryProperties(_initStructs->getDevice(), &desc);- if (res == ZE_RESULT_SUCCESS) {- if (desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_STANDARD_ALLOCATION) {- _externalMemoryStandardAllocationSupported = true;- }- }-_outputAllocator = std::make_shared<const zeroMemory::HostMemAllocator>(_initStructs);_inputAllocator =std::make_shared<const zeroMemory::HostMemAllocator>(_initStructs, ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED);diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hppindex 735c5ac6157..cf52f367bb7 100644--- a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp+++ b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp@@ -20,10 +20,10 @@ namespace intel_npu {using SerializedIR = std::pair<size_t, std::shared_ptr<uint8_t>>;struct GraphDescriptor {- GraphDescriptor(ze_graph_handle_t handle = nullptr, void* data = nullptr);+ GraphDescriptor(ze_graph_handle_t handle = nullptr, bool memoryPersistent = false);ze_graph_handle_t _handle = nullptr;- void* _data = nullptr;+ bool _memoryPersistent = false;};/**@@ -74,8 +74,7 @@ private:void initializeGraphThroughCommandList(ze_graph_handle_t graphHandle, uint32_t commandQueueGroupOrdinal) const;- void* importCpuVa(void* data, size_t size, const uint32_t flags = 0) const;- bool releaseCpuVa(void* data);+ bool canCpuVaBeImported(void* data, size_t size, const uint32_t flags = 0) const;std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;uint32_t _graphExtVersion;diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cppindex 2ca5df2649f..c63e39a11d3 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp@@ -243,7 +243,7 @@ void Graph::initialize(const Config& config) {}bool Graph::release_blob(const Config& config) {- if (_graphDesc._data || _blobIsPersistent || _blob == std::nullopt ||+ if (_graphDesc._memoryPersistent || _blobIsPersistent || _blob == std::nullopt ||_zeroInitStruct->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 8) || config.get<PERF_COUNT>()) {return false;}diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cppindex e187dc0470a..99f6f7e8af5 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp@@ -598,7 +598,7 @@ void WeightlessGraph::set_weights_inputs() {}void WeightlessGraph::release_init_blob(const size_t initIndex) {- if (_initsGraphDesc.at(initIndex)._data || _blobIsPersistent || _initBlobs == std::nullopt ||+ if (_initsGraphDesc.at(initIndex)._memoryPersistent || _blobIsPersistent || _initBlobs == std::nullopt ||_zeroInitStruct->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 8)) {return;}diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cppindex c46647b9ed8..7760ea45307 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp@@ -41,7 +41,9 @@namespace intel_npu {-GraphDescriptor::GraphDescriptor(ze_graph_handle_t handle, void* data) : _handle(handle), _data(data) {}+GraphDescriptor::GraphDescriptor(ze_graph_handle_t handle, bool memoryPersistent)+ : _handle(handle),+ _memoryPersistent(memoryPersistent) {}ZeGraphExtWrappers::ZeGraphExtWrappers(const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct): _zeroInitStruct(zeroInitStruct),@@ -63,22 +65,6 @@ ZeGraphExtWrappers::~ZeGraphExtWrappers() {_logger.debug("Obj destroyed");}-bool ZeGraphExtWrappers::releaseCpuVa(void* data) {- _logger.debug("destroyGraph - perform zeMemFree");-- auto result = zeMemFree(_zeroInitStruct->getContext(), data);- if (ZE_RESULT_SUCCESS != result) {- _logger.error("failed to free L0 memory result: %s, code %#X - %s",- ze_result_to_string(result).c_str(),- uint64_t(result),- ze_result_to_description(result).c_str());-- return false;- }-- return true;-}-void ZeGraphExtWrappers::destroyGraph(GraphDescriptor& graphDescriptor) {if (graphDescriptor._handle) {_logger.debug("destroyGraph - perform pfnDestroy");@@ -91,12 +77,6 @@ void ZeGraphExtWrappers::destroyGraph(GraphDescriptor& graphDescriptor) {} else {graphDescriptor._handle = nullptr;}-- if (graphDescriptor._data != nullptr) {- if (releaseCpuVa(graphDescriptor._data)) {- graphDescriptor._data = nullptr;- }- }}}@@ -310,10 +290,10 @@ std::unordered_set<std::string> ZeGraphExtWrappers::queryGraph(std::pair<size_t,return std::unordered_set<std::string>();}-void* ZeGraphExtWrappers::importCpuVa(void* data, size_t size, const uint32_t flags) const {+bool ZeGraphExtWrappers::canCpuVaBeImported(void* data, size_t size, const uint32_t flags) const {if (_graphExtVersion < ZE_MAKE_VERSION(1, 13) ||!utils::memory_and_size_aligned_to_standard_page_size(data, size)) {- return nullptr;+ return false;}ze_device_external_memory_properties_t externalMemorydDesc = {};@@ -322,29 +302,10 @@ void* ZeGraphExtWrappers::importCpuVa(void* data, size_t size, const uint32_t flauto res = zeDeviceGetExternalMemoryProperties(_zeroInitStruct->getDevice(), &externalMemorydDesc);if ((res != ZE_RESULT_SUCCESS) ||((externalMemorydDesc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_STANDARD_ALLOCATION) == 0)) {- return nullptr;- }-- _ze_external_memory_import_system_memory_t memory_import = {ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_SYSTEM_MEMORY,- nullptr,- data,- size};-- void* importedNpuData = nullptr;-- ze_host_mem_alloc_desc_t memAllocDesc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC, &memory_import, flags};- res =- zeMemAllocHost(_zeroInitStruct->getContext(), &memAllocDesc, size, utils::STANDARD_PAGE_SIZE, &importedNpuData);-- if (res == ZE_RESULT_SUCCESS) {- return importedNpuData;+ return false;}- _logger.debug("Got an error when importing a CPUVA: %s, code %#X - %s",- ze_result_to_string(res).c_str(),- uint64_t(res),- ze_result_to_description(res).c_str());- return nullptr;+ return true;}GraphDescriptor ZeGraphExtWrappers::getGraphDescriptor(std::pair<size_t, std::shared_ptr<uint8_t>> serializedIR,@@ -397,9 +358,9 @@ GraphDescriptor ZeGraphExtWrappers::getGraphDescriptor(void* blobData, size_t bluint32_t flags = 0;- void* npuMemory = importCpuVa(blobData, blobSize, ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED);+ bool setPersistentFlag = canCpuVaBeImported(blobData, blobSize, ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED);- if (npuMemory) {+ if (setPersistentFlag) {_logger.debug("getGraphDescriptor - set ZE_GRAPH_FLAG_INPUT_GRAPH_PERSISTENT");flags = ZE_GRAPH_FLAG_INPUT_GRAPH_PERSISTENT;}@@ -420,7 +381,7 @@ GraphDescriptor ZeGraphExtWrappers::getGraphDescriptor(void* blobData, size_t bl&graphHandle);THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnCreate2", result, _zeroInitStruct->getGraphDdiTable());- return GraphDescriptor{graphHandle, npuMemory};+ return GraphDescriptor{graphHandle, setPersistentFlag};}/**