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] Use the correct path for dynamic batching (#31880)
Bogdan Pereanue4ae09c185cc
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 f14da4b4fbb..b1ce63585d2 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@@ -424,18 +424,23 @@ void ZeroInferRequest::set_tensor(const ov::Output<const ov::Node>& port, constauto batchSizeCandidate =determine_dynamic_batch_size(_metadata.inputs.at(foundPort.idx), tensor._ptr, std::nullopt);- // Check if batch has been changedif (batchSizeCandidate.has_value()) {- if (!_dynamicBatchValueChanged ||- (get_user_input(foundPort.idx)._ptr != nullptr &&- get_user_input(foundPort.idx)->get_byte_size() * get_user_inputs(foundPort.idx).size() !=- tensor->get_byte_size())) {- _dynamicBatchValueChanged = true;- _graph->set_batch_size(batchSizeCandidate.value());- } else {- if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {- OPENVINO_THROW("Batching size is not matching all the tensors.");+ if (!_dynamicBatchValueChanged) {+ if (get_user_input(foundPort.idx)._ptr != nullptr &&+ get_user_input(foundPort.idx)->get_byte_size() * get_user_inputs(foundPort.idx).size() !=+ tensor->get_byte_size()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ } else if (_graph->get_batch_size().has_value()) {+ if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ }+ } else {+ _graph->set_batch_size(batchSizeCandidate.value());}+ } else if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ OPENVINO_THROW("Batching size is not matching all the tensors.");}}@@ -458,17 +463,22 @@ void ZeroInferRequest::set_tensor(const ov::Output<const ov::Node>& port, constauto batchSizeCandidate =determine_dynamic_batch_size(_metadata.outputs.at(foundPort.idx), tensor._ptr, std::nullopt);- // Check if batch has been changedif (batchSizeCandidate.has_value()) {- if (!_dynamicBatchValueChanged ||- (_userOutputTensors.at(foundPort.idx)._ptr != nullptr &&- _userOutputTensors.at(foundPort.idx)->get_byte_size() != tensor->get_byte_size())) {- _dynamicBatchValueChanged = true;- _graph->set_batch_size(batchSizeCandidate.value());- } else {- if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {- OPENVINO_THROW("Batching size is not matching all the tensors.");+ if (!_dynamicBatchValueChanged) {+ if (_userOutputTensors.at(foundPort.idx)._ptr != nullptr &&+ _userOutputTensors.at(foundPort.idx)->get_byte_size() != tensor->get_byte_size()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ } else if (_graph->get_batch_size().has_value()) {+ if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ }+ } else {+ _graph->set_batch_size(batchSizeCandidate.value());}+ } else if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ OPENVINO_THROW("Batching size is not matching all the tensors.");}}@@ -506,26 +516,33 @@ void ZeroInferRequest::set_tensors(const ov::Output<const ov::Node>& port,_logger.debug("ZeroInferRequest::set_tensors: %zu", tensors.size());- auto batchSize = determine_dynamic_batch_size(_metadata.inputs.at(foundPort.idx), nullptr, tensors.size());+ auto batchSizeCandidate = determine_dynamic_batch_size(_metadata.inputs.at(foundPort.idx), nullptr, tensors.size());// Check if batch has been changed- if (batchSize.has_value()) {- if (!_dynamicBatchValueChanged || get_user_inputs(foundPort.idx).size() != tensors.size()) {- _dynamicBatchValueChanged = true;- _graph->set_batch_size(batchSize.value());- } else {- if (batchSize.value() != _graph->get_batch_size().value()) {- OPENVINO_THROW("Batching size is not matching all the tensors.");+ if (batchSizeCandidate.has_value()) {+ if (!_dynamicBatchValueChanged) {+ if (get_user_inputs(foundPort.idx).size() != tensors.size()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ } else if (_graph->get_batch_size().has_value()) {+ if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ _dynamicBatchValueChanged = true;+ _graph->set_batch_size(batchSizeCandidate.value());+ }+ } else {+ _graph->set_batch_size(batchSizeCandidate.value());}+ } else if (batchSizeCandidate.value() != _graph->get_batch_size().value()) {+ OPENVINO_THROW("Batching size is not matching all the tensors.");}} else {- batchSize = _graph->get_batch_size();+ batchSizeCandidate = _graph->get_batch_size();}get_user_inputs(foundPort.idx).resize(tensors.size());get_user_inputs(foundPort.idx) = tensors;- if (_initStructs->getMutableCommandListExtVersion() >= ZE_MAKE_VERSION(1, 0) && batchSize.has_value()) {+ if (_initStructs->getMutableCommandListExtVersion() >= ZE_MAKE_VERSION(1, 0) && batchSizeCandidate.has_value()) {for (size_t i = 0; i < tensors.size(); i++) {auto remoteTensor = std::dynamic_pointer_cast<ZeroRemoteTensor>(tensors[i]._ptr);@@ -567,7 +584,7 @@ void ZeroInferRequest::set_tensors(const ov::Output<const ov::Node>& port,"create a L0 tensor");get_level_zero_input(foundPort.idx, i) =- allocate_tensor(foundPort.idx, true, *_inputAllocator, batchSize);+ allocate_tensor(foundPort.idx, true, *_inputAllocator, batchSizeCandidate);}data = get_level_zero_input(foundPort.idx, i)->data();@@ -902,7 +919,12 @@ void ZeroInferRequest::infer_async() {}}- if (is_batched_input(inputIndex) || batch_size.has_value()) {+ // This path is used only for batched input when set_tensors is called and userTensor vector size is greater+ // than 1.+ // There are two cases:+ // 1. Batch size is set and batching is handled by the plugin.+ // 2. Batch size isn't set and batching is handled by the compiler.+ if (is_batched_input(inputIndex)) {if (batch_size.has_value()) {for (size_t i = 0; i < userTensor.size(); i++) {if (!is_remote_tensor(get_level_zero_input(inputIndex, i))) {diff --git a/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hppindex 5181d6892cc..71a9b09f7d0 100644--- a/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp@@ -1184,6 +1184,42 @@ TEST_P(DynamicBatchingTests, DynamicCheckMultipleBatchingRun1) {}}+TEST_P(DynamicBatchingTests, DynamicCheckMultipleBatchingRun2) {+ auto modelShape = PartialShape{-1, 2, 64, 64};+ size_t batch_size = 3;+ auto shape = Shape{batch_size, 2, 64, 64};+ auto shape_size = ov::shape_size(shape);+ auto model = createModel(element::f32, modelShape, "N...");++ compiled_model = core->compile_model(model, target_device, configuration);++ ov::InferRequest inference_request;+ ov::Tensor input_tensor;++ input_tensor = ov::Tensor(ov::element::f32, shape);++ inference_request = compiled_model.create_infer_request();++ inference_request.set_input_tensor(input_tensor);++ auto* input_data = reinterpret_cast<float*>(input_tensor.data());+ for (size_t i = 0; i < shape_size; ++i) {+ input_data[i] = static_cast<float>(batch_size);+ }++ inference_request.infer(); // Adds '1' to each element++ auto output_tensor = inference_request.get_output_tensor();++ float expected_result = static_cast<float>(batch_size) + 1.f;+ auto* output_tensor_data = reinterpret_cast<float*>(output_tensor.data());+ for (size_t j = 0; j < shape_size; ++j) {+ EXPECT_NEAR(output_tensor_data[j], expected_result, 1e-5)+ << "Run=" << batch_size << " Expected=" << expected_result << ", actual=" << output_tensor_data[j]+ << " for index " << j;+ }+}+TEST_P(DynamicBatchingTests, DynamicCheckMultipleBatchingRunsSeq) {auto modelShape = PartialShape{ov::Dimension(1, 10), 2, 64, 64};auto shape = Shape{4, 2, 64, 64};