Skip to content

Commit 3aa3eff

Browse files
committed
fix predict issue in PR36682
1 parent 6f4aa22 commit 3aa3eff

3 files changed

Lines changed: 47 additions & 49 deletions

File tree

src/plugins/intel_npu/src/backend/include/zero_dynamic_pipeline.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ struct DynamicArguments {
1818
std::vector<npu_vm_runtime_mem_ref_handle_t> _inputMemRefHandles;
1919
std::vector<npu_vm_runtime_mem_ref_handle_t> _outputMemRefHandles;
2020

21-
// Share runtime_execution_context during VM execution and forecasting
22-
npu_vm_runtime_execution_context_handle_t _executionContext = nullptr;
23-
// Set by the caller after the first successful @c npuVMRuntimeExecute.
24-
bool _executedOnce = false;
21+
// Share runtime_execution_context in param during VM execution and forecasting
22+
npu_vm_runtime_execute_params_t _executeParams = {};
2523

2624
DynamicArguments() = default;
2725
DynamicArguments(const DynamicArguments&) = delete;

src/plugins/intel_npu/src/backend/src/zero_dynamic_pipeline.cpp

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,18 @@ void DynamicArguments::setArgumentProperties(uint32_t argi,
153153
}
154154

155155
DynamicArguments::~DynamicArguments() {
156-
if (_executionContext != nullptr) {
157-
npuVMRuntimeDestroyExecutionContext(_executionContext);
158-
_executionContext = nullptr;
156+
if (_executeParams.executionContext != nullptr) {
157+
npuVMRuntimeDestroyExecutionContext(_executeParams.executionContext);
158+
_executeParams.executionContext = nullptr;
159159
}
160160
}
161161

162162
void DynamicArguments::ensureExecutionContext(npu_vm_runtime_handle_t vmRuntime) {
163-
if (_executionContext != nullptr) {
163+
if (_executeParams.executionContext != nullptr) {
164164
return;
165165
}
166-
if (npuVMRuntimeCreateExecutionContext(vmRuntime, &_executionContext) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
166+
if (npuVMRuntimeCreateExecutionContext(vmRuntime, &_executeParams.executionContext) !=
167+
NPU_VM_RUNTIME_RESULT_SUCCESS) {
167168
OPENVINO_THROW("Failed to create a VM execution context");
168169
}
169170
}
@@ -340,8 +341,8 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
340341
ze_event_handle_t event) {
341342
_logger.debug("Start to execute graph with runtime engine");
342343
bool noTensorChange = true;
343-
// _executedOnce is true only after a successful npuVMRuntimeExecute below
344-
const bool firstExecution = !args._executedOnce;
344+
npu_vm_runtime_execute_params_t* params = &args._executeParams;
345+
bool firstInference = params->graphDdiTableExt == nullptr;
345346

346347
auto processMemRefs = [&](auto& memRefs, auto& targetMemRefHandles) {
347348
targetMemRefHandles.clear();
@@ -353,9 +354,10 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
353354
memref._impl = impl;
354355
}
355356
impl->UpdateMemRefHandleStatus(memref);
356-
targetMemRefHandles.push_back(impl->_memRef);
357357

358-
if (impl->_ptrUpdated || impl->_shapeUpdated || impl->_strideUpdated) {
358+
if (firstInference) {
359+
targetMemRefHandles.push_back(impl->_memRef);
360+
} else if (impl->_ptrUpdated || impl->_shapeUpdated || impl->_strideUpdated) {
359361
noTensorChange = false;
360362
}
361363
}
@@ -364,7 +366,7 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
364366
processMemRefs(args._inputsMemRef, args._inputMemRefHandles);
365367
processMemRefs(args._outputsMemRef, args._outputMemRefHandles);
366368

367-
if (!firstExecution && noTensorChange) {
369+
if (!firstInference && noTensorChange) {
368370
_logger.debug("Reuse command list without update since no tensor change detected");
369371
auto result = zeCommandQueueExecuteCommandLists(commandQueue,
370372
static_cast<uint32_t>(commandLists.size()),
@@ -389,30 +391,27 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
389391
// Create the VM execution context (owned by args._impl, destroyed with it).
390392
args.ensureExecutionContext(vmRuntime);
391393

392-
npu_vm_runtime_execute_params_t params{};
393-
params.executionContext = args._executionContext;
394-
params.pInputs = args._inputMemRefHandles.data();
395-
params.numOfInputs = static_cast<uint32_t>(args._inputMemRefHandles.size());
396-
params.pOutputs = args._outputMemRefHandles.data();
397-
params.numOfOutputs = static_cast<uint32_t>(args._outputMemRefHandles.size());
398-
params.ctx = _init_structs->getContext();
399-
params.device = _init_structs->getDevice();
400-
params.graphDdiTableExt = _init_structs->getGraphDdiTable().getImpl();
401-
params.commandLists = commandLists.data();
402-
params.numCommandLists = static_cast<uint64_t>(commandLists.size());
403-
params.commandQueue = commandQueue;
404-
params.inferenceFence = fence;
405-
params.event = event;
394+
params->executionContext = args._executeParams.executionContext;
395+
params->pInputs = args._inputMemRefHandles.data();
396+
params->numOfInputs = static_cast<uint32_t>(args._inputMemRefHandles.size());
397+
params->pOutputs = args._outputMemRefHandles.data();
398+
params->numOfOutputs = static_cast<uint32_t>(args._outputMemRefHandles.size());
399+
params->ctx = _init_structs->getContext();
400+
params->device = _init_structs->getDevice();
401+
params->graphDdiTableExt = _init_structs->getGraphDdiTable().getImpl();
402+
params->commandLists = commandLists.data();
403+
params->numCommandLists = static_cast<uint64_t>(commandLists.size());
404+
params->commandQueue = commandQueue;
405+
params->inferenceFence = fence;
406+
params->event = event;
406407

407408
_logger.debug("Execute graph with runtime engine");
408-
if (npuVMRuntimeExecute(vmRuntime, &params) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
409+
if (npuVMRuntimeExecute(vmRuntime, params) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
409410
OPENVINO_THROW("Failed to execute VM runtime engine");
410411
} else {
411412
_logger.debug("Execution runtime engine is created successfully.");
412413
}
413414

414-
args._executedOnce = true;
415-
416415
_logger.debug("Completed to execute graph with runtime engine");
417416
}
418417

@@ -423,26 +422,28 @@ void DynamicPipeline::predict_output_shape(const IGraph& graph,
423422
Logger logger("DynamicPipeline::predict_output_shape", Logger::global().level());
424423
logger.debug("predict_output_shape - started");
425424

425+
std::vector<std::shared_ptr<MemRefTypeImpl>> inputMemRefImpls;
426+
std::vector<std::shared_ptr<MemRefTypeImpl>> outputMemRefImpls;
427+
426428
const npu_vm_runtime_handle_t vmRuntime = static_cast<npu_vm_runtime_handle_t>(graph.get_handle());
427429
OPENVINO_ASSERT(vmRuntime != nullptr, "predict_output_shape requires a valid VM runtime engine");
428430

429-
auto processMemRefs = [&](auto& memRefs, auto& targetMemRefHandles) {
430-
targetMemRefHandles.clear();
431-
targetMemRefHandles.reserve(memRefs.size());
431+
auto processMemRefs = [&](auto& memRefs, auto& destMemRefHandles, auto& destMemRefImpls) {
432+
destMemRefHandles.clear();
433+
destMemRefHandles.reserve(memRefs.size());
434+
destMemRefImpls.clear();
435+
destMemRefImpls.reserve(memRefs.size());
432436

433437
for (auto& memref : memRefs) {
434-
auto impl = std::static_pointer_cast<MemRefTypeImpl>(memref._impl);
435-
if (impl == nullptr) {
436-
impl = std::make_shared<MemRefTypeImpl>();
437-
memref._impl = impl;
438-
}
438+
std::shared_ptr<MemRefTypeImpl> impl = std::make_shared<MemRefTypeImpl>();
439439
impl->UpdateMemRefHandleStatus(memref);
440-
targetMemRefHandles.push_back(impl->_memRef);
440+
destMemRefHandles.push_back(impl->_memRef);
441+
destMemRefImpls.push_back(impl);
441442
}
442443
};
443444

444-
processMemRefs(inputsMemRefs, args._inputMemRefHandles);
445-
processMemRefs(outputsMemRefs, args._outputMemRefHandles);
445+
processMemRefs(inputsMemRefs, args._inputMemRefHandles, inputMemRefImpls);
446+
processMemRefs(outputsMemRefs, args._outputMemRefHandles, outputMemRefImpls);
446447

447448
npu_vm_runtime_result_t result = NPU_VM_RUNTIME_RESULT_SUCCESS;
448449
npu_vm_runtime_version_t version{};
@@ -467,27 +468,25 @@ void DynamicPipeline::predict_output_shape(const IGraph& graph,
467468
params.numOfInputs = static_cast<uint32_t>(args._inputMemRefHandles.size());
468469
params.pOutputs = args._outputMemRefHandles.data();
469470
params.numOfOutputs = static_cast<uint32_t>(args._outputMemRefHandles.size());
470-
params.executionContext = args._executionContext;
471+
params.executionContext = args._executeParams.executionContext;
471472

472473
result = npuVMRuntimePredictOutputShape2(vmRuntime, &params);
473474
}
474475

475476
if (result != NPU_VM_RUNTIME_RESULT_SUCCESS) {
476477
OPENVINO_THROW("Failed to predict output shape with VM runtime engine, error code: ", result);
477478
} else {
478-
for (auto& out : outputsMemRefs) {
479-
std::shared_ptr<MemRefTypeImpl> outImpl = std::static_pointer_cast<MemRefTypeImpl>(out._impl);
479+
for (size_t i = 0; i < outputsMemRefs.size(); ++i) {
480+
auto& out = outputsMemRefs[i];
481+
std::shared_ptr<MemRefTypeImpl> outImpl = outputMemRefImpls[i];
482+
480483
if (outImpl == nullptr) {
481484
OPENVINO_THROW("MemRefType implementation is broken, unknown error happens in shape prediction.");
482485
}
483486
outImpl->alignWithHandle(out);
484487
}
485488
logger.debug("Output shape prediction is done successfully.");
486489
}
487-
488-
// Clear memref handles after shape prediction to avoid the next execution using wrong memref handles
489-
args._inputMemRefHandles.clear();
490-
args._outputMemRefHandles.clear();
491490
}
492491

493492
void DynamicPipeline::pull() {

src/plugins/intel_npu/src/utils/include/intel_npu/utils/vm/npu_vm_runtime.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
#include <level_zero/ze_api.h>
13+
1314
#include "ze_graph_ext.h"
1415

1516
#if defined(__cplusplus)

0 commit comments

Comments
 (0)