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`] Move `allocate_tensor` and `create_tensor` into `ZeroInferRequest` class (#31957)
Mircea-Aurelian Dan179f722d36f2
diff --git a/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp b/src/plugins/intel_npu/src/backend/include/zero_infer_request.hppindex ea56512dc8a..d954e0d1d68 100644--- a/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp+++ b/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp@@ -61,9 +61,18 @@ private:std::shared_ptr<ov::ITensor>& get_level_zero_input(size_t index, size_t tensorNo = 0) const;std::vector<std::shared_ptr<ov::ITensor>>& get_level_zero_inputs(size_t index) const;- std::shared_ptr<ov::ITensor> create_tensor(ov::element::Type type,- const ov::Shape& shape,- const ov::Allocator& allocator = {}) const override;+ /**+ * @brief Allocates a tensor on host and stores the reference inside multiple attributes.+ * @param index The index which the allocated tensor shall use.+ * @param isInput Determines the containers in which the newly allocated tensors will be stored.+ * @param allocator If provided, the tensor uses the custom allocator instead of using the default one.+ * @param batchSize If provided, the value of the shape on the 0th axis is overriden with this value.+ * @return Pointer towards the allocated tensor+ */+ std::shared_ptr<ov::ITensor> allocate_tensor(const size_t index,+ const bool isInput,+ const ov::Allocator& allocator,+ const std::optional<std::size_t> batchSize = std::nullopt) const;void add_state(const IODescriptor& descriptor, size_t tensorIndex) const override;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 4324eaddfa3..f14da4b4fbb 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@@ -132,7 +132,7 @@ ZeroInferRequest::ZeroInferRequest(const std::shared_ptr<ZeroInitStructsHolder>&continue;}- get_level_zero_input(ioIndex) = allocate_tensor(inputDescriptor, ioIndex, INPUT, *_inputAllocator);+ get_level_zero_input(ioIndex) = allocate_tensor(ioIndex, INPUT, *_inputAllocator);++ioIndex;}@@ -146,7 +146,7 @@ ZeroInferRequest::ZeroInferRequest(const std::shared_ptr<ZeroInitStructsHolder>&continue;}- _levelZeroOutputTensors.at(ioIndex) = allocate_tensor(outputDescriptor, ioIndex, OUTPUT, *_outputAllocator);+ _levelZeroOutputTensors.at(ioIndex) = allocate_tensor(ioIndex, OUTPUT, *_outputAllocator);++ioIndex;}@@ -178,7 +178,7 @@ void ZeroInferRequest::create_pipeline() {batchSize.value());get_level_zero_input(inputIndex, i) =- allocate_tensor(_metadata.inputs.at(inputIndex), inputIndex, true, *_inputAllocator, batchSize);+ allocate_tensor(inputIndex, true, *_inputAllocator, batchSize);}}continue;@@ -199,8 +199,7 @@ void ZeroInferRequest::create_pipeline() {continue;}- get_level_zero_input(inputIndex) =- allocate_tensor(_metadata.inputs.at(inputIndex), inputIndex, INPUT, *_inputAllocator, batchSize);+ get_level_zero_input(inputIndex) = allocate_tensor(inputIndex, INPUT, *_inputAllocator, batchSize);_logger.debug("ZeroInferRequest::create_pipeline - new input tensor %s allocated, size: %zu",_metadata.inputs.at(inputIndex).nodeFriendlyName.c_str(),get_level_zero_input(inputIndex)->get_byte_size());@@ -224,8 +223,7 @@ void ZeroInferRequest::create_pipeline() {_logger.debug("ZeroInferRequest::create_pipeline - allocate new output tensor %s",_metadata.outputs.at(outputIndex).nodeFriendlyName.c_str());- _levelZeroOutputTensors.at(outputIndex) =- allocate_tensor(_metadata.outputs.at(outputIndex), outputIndex, OUTPUT, *_outputAllocator, batchSize);+ _levelZeroOutputTensors.at(outputIndex) = allocate_tensor(outputIndex, OUTPUT, *_outputAllocator, batchSize);if (_dynamicBatchValueChanged && !_userOutputTensors.at(outputIndex)->get_shape().empty() &&_userOutputTensors.at(outputIndex)->get_shape()[utils::BATCH_AXIS] !=@@ -357,11 +355,8 @@ void ZeroInferRequest::set_tensor_data(const std::shared_ptr<ov::ITensor>& tensoauto batch = _graph->get_batch_size();- levelZeroTensors = allocate_tensor(isInput ? _metadata.inputs.at(index) : _metadata.outputs.at(index),- index,- isInput,- isInput ? *_inputAllocator : *_outputAllocator,- batch);+ levelZeroTensors =+ allocate_tensor(index, isInput, isInput ? *_inputAllocator : *_outputAllocator, batch);updateCommandListArg = true;}@@ -571,11 +566,8 @@ void ZeroInferRequest::set_tensors(const ov::Output<const ov::Node>& port,_logger.debug("ZeroInferRequest::set_tensors - tensor wasn't created in the same L0 context, ""create a L0 tensor");- get_level_zero_input(foundPort.idx, i) = allocate_tensor(_metadata.inputs.at(foundPort.idx),- foundPort.idx,- true,- *_inputAllocator,- batchSize);+ get_level_zero_input(foundPort.idx, i) =+ allocate_tensor(foundPort.idx, true, *_inputAllocator, batchSize);}data = get_level_zero_input(foundPort.idx, i)->data();@@ -657,8 +649,7 @@ ov::SoPtr<ov::ITensor> ZeroInferRequest::get_tensor(const ov::Output<const ov::Nauto& levelZeroTensors = isInput ? get_level_zero_input(ioIndex) : _levelZeroOutputTensors.at(ioIndex);- levelZeroTensors =- allocate_tensor(metadata, ioIndex, isInput, isInput ? *_inputAllocator : *_outputAllocator, batchSize);+ levelZeroTensors = allocate_tensor(ioIndex, isInput, isInput ? *_inputAllocator : *_outputAllocator, batchSize);if (!_dynamicBatchValueChanged) {userTensors = levelZeroTensors;@@ -677,6 +668,48 @@ ov::SoPtr<ov::ITensor> ZeroInferRequest::get_tensor(const ov::Output<const ov::Nreturn levelZeroTensors;}+std::shared_ptr<ov::ITensor> ZeroInferRequest::allocate_tensor(const size_t index,+ const bool isInput,+ const ov::Allocator& allocator,+ const std::optional<std::size_t> batchSize) const {+ const auto& descriptor = isInput ? _metadata.inputs.at(index) : _metadata.outputs.at(index);+ check_network_precision(descriptor.precision);++ std::shared_ptr<ov::ITensor> tensor;+ ov::Shape allocatedTensorShape = descriptor.shapeFromCompiler.get_max_shape();++ if (batchSize.has_value()) {+ allocatedTensorShape[utils::BATCH_AXIS] = *batchSize;+ }++ if (descriptor.isStateOutput) {+ // Only one buffer is required for each (state input, state output) pair, acting as an input before running the+ // inference and as an output after performing it. Thus both the "state input" and "state output" entries shall+ // point to the same buffer.+ OPENVINO_ASSERT(descriptor.relatedDescriptorIndex.has_value(),+ "The link between state descriptors is missing, state name: ",+ descriptor.nameFromCompiler);+ tensor = get_user_input(*descriptor.relatedDescriptorIndex)._ptr;+ } else {+ tensor =+ std::make_shared<ZeroTensor>(_initStructs, _config, descriptor.precision, allocatedTensorShape, allocator);+ }++ if (isInput) {+ if (get_user_input(index) == nullptr) {+ get_user_input(index) = tensor;+ }++ if (descriptor.isStateInput) {+ add_state(descriptor, index);+ }+ } else if (_userOutputTensors.at(index) == nullptr) {+ _userOutputTensors.at(index) = tensor;+ }++ return tensor;+}+void ZeroInferRequest::update_pipeline_if_memory_changed() {size_t ioIndex = 0;@@ -795,7 +828,9 @@ void ZeroInferRequest::update_states_if_memory_changed() {auto zeroTensor = std::dynamic_pointer_cast<ZeroTensor>(levelZeroInput);if (zeroTensor == nullptr || (zeroTensor != nullptr && zeroTensor->tensor_was_shared_with_user())) {- levelZeroInput = create_tensor(+ levelZeroInput = std::make_shared<ZeroTensor>(+ _initStructs,+ _config,_metadata.inputs.at(zeroState->get_tensor_index()).precision,_metadata.inputs.at(zeroState->get_tensor_index()).shapeFromCompiler.get_max_shape(),*_inputAllocator);@@ -1062,14 +1097,6 @@ std::vector<ov::ProfilingInfo> ZeroInferRequest::get_profiling_info() const {return _pipeline->get_profiling_info();}-std::shared_ptr<ov::ITensor> ZeroInferRequest::create_tensor(ov::element::Type type,- const ov::Shape& shape,- const ov::Allocator& allocator) const {- OPENVINO_ASSERT(allocator, "Allocator mush be provided when creating a zero tensor!");-- return std::make_shared<ZeroTensor>(_initStructs, _config, type, shape, allocator);-}-void ZeroInferRequest::add_state(const IODescriptor& descriptor, size_t tensorIndex) const {OPENVINO_ASSERT(descriptor.relatedDescriptorIndex.has_value(),"The link between state descriptors is missing, state name: ",diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/sync_infer_request.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/sync_infer_request.hppindex f7406413c9f..4d0e0d9b7e1 100644--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/sync_infer_request.hpp+++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/sync_infer_request.hpp@@ -148,25 +148,6 @@ protected:*/virtual void check_network_precision(const ov::element::Type_t precision) const = 0;- /**- * @brief Allocates a tensor on host and stores the reference inside multiple attributes.- * @param descriptor Tensor's metadata- * @param index The index which the allocated tensor shall use.- * @param isInput Determines the containers in which the newly allocated tensors will be stored.- * @param allocator If provided, the tensor uses the custom allocator instead of using the default one.- * @param batchSize If provided, the value of the shape on the 0th axis is overriden with this value.- * @return Pointer towards the allocated tensor- */- std::shared_ptr<ov::ITensor> allocate_tensor(const IODescriptor& descriptor,- const size_t index,- const bool isInput,- const ov::Allocator& allocator = {},- const std::optional<std::size_t> batchSize = std::nullopt) const;-- virtual std::shared_ptr<ov::ITensor> create_tensor(ov::element::Type type,- const ov::Shape& shape,- const ov::Allocator& allocator = {}) const;-virtual void add_state(const IODescriptor& descriptor, const size_t tensorIndex) const;bool is_batched_input(size_t idx) const;diff --git a/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp b/src/plugins/intel_npu/src/common/src/sync_infer_request.cppindex ce1576c31a2..baa32e61288 100644--- a/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp+++ b/src/plugins/intel_npu/src/common/src/sync_infer_request.cpp@@ -323,53 +323,6 @@ void SyncInferRequest::check_tensors() const {}}-std::shared_ptr<ov::ITensor> SyncInferRequest::allocate_tensor(const IODescriptor& descriptor,- const size_t index,- const bool isInput,- const ov::Allocator& allocator,- const std::optional<std::size_t> batchSize) const {- check_network_precision(descriptor.precision);-- std::shared_ptr<ov::ITensor> tensor;- ov::Shape allocatedTensorShape = descriptor.shapeFromCompiler.get_max_shape();-- if (batchSize.has_value()) {- allocatedTensorShape[utils::BATCH_AXIS] = *batchSize;- }-- if (descriptor.isStateOutput) {- // Only one buffer is required for each (state input, state output) pair, acting as an input before running the- // inference and as an output after performing it. Thus both the "state input" and "state output" entries shall- // point to the same buffer.- OPENVINO_ASSERT(descriptor.relatedDescriptorIndex.has_value(),- "The link between state descriptors is missing, state name: ",- descriptor.nameFromCompiler);- tensor = get_user_input(*descriptor.relatedDescriptorIndex)._ptr;- } else {- tensor = create_tensor(descriptor.precision, allocatedTensorShape, allocator);- }-- if (isInput) {- if (get_user_input(index) == nullptr) {- get_user_input(index) = tensor;- }-- if (descriptor.isStateInput) {- add_state(descriptor, index);- }- } else if (_userOutputTensors.at(index) == nullptr) {- _userOutputTensors.at(index) = tensor;- }-- return tensor;-}--std::shared_ptr<ov::ITensor> SyncInferRequest::create_tensor(ov::element::Type type,- const ov::Shape& shape,- const ov::Allocator& allocator) const {- return ov::make_tensor(type, shape, allocator);-}-void SyncInferRequest::add_state(const IODescriptor& descriptor, const size_t tensorIndex) const {_variableStates.push_back(std::make_shared<VariableState>(descriptor.nameFromCompiler, get_user_input(tensorIndex)));