Skip to content

Commit 5eb56d0

Browse files
committed
fix predict issue in PR36682
1 parent 6f4aa22 commit 5eb56d0

3 files changed

Lines changed: 48 additions & 40 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: 45 additions & 36 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
}
@@ -339,20 +340,24 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
339340
ze_fence_handle_t fence,
340341
ze_event_handle_t event) {
341342
_logger.debug("Start to execute graph with runtime engine");
343+
344+
npu_vm_runtime_execute_params_t* params = &args._executeParams;
345+
bool firstInference = params->graphDdiTableExt == nullptr;
342346
bool noTensorChange = true;
343-
// _executedOnce is true only after a successful npuVMRuntimeExecute below
344-
const bool firstExecution = !args._executedOnce;
345347

346348
auto processMemRefs = [&](auto& memRefs, auto& targetMemRefHandles) {
347349
targetMemRefHandles.clear();
348350
targetMemRefHandles.reserve(memRefs.size());
351+
349352
for (auto& memref : memRefs) {
350-
auto impl = std::static_pointer_cast<MemRefTypeImpl>(memref._impl);
353+
std::shared_ptr<MemRefTypeImpl> impl = std::static_pointer_cast<MemRefTypeImpl>(memref._impl);
351354
if (impl == nullptr) {
352355
impl = std::make_shared<MemRefTypeImpl>();
353356
memref._impl = impl;
354357
}
355358
impl->UpdateMemRefHandleStatus(memref);
359+
360+
// VM runtime execute path always needs current memref handles.
356361
targetMemRefHandles.push_back(impl->_memRef);
357362

358363
if (impl->_ptrUpdated || impl->_shapeUpdated || impl->_strideUpdated) {
@@ -364,7 +369,7 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
364369
processMemRefs(args._inputsMemRef, args._inputMemRefHandles);
365370
processMemRefs(args._outputsMemRef, args._outputMemRefHandles);
366371

367-
if (!firstExecution && noTensorChange) {
372+
if (!firstInference && noTensorChange) {
368373
_logger.debug("Reuse command list without update since no tensor change detected");
369374
auto result = zeCommandQueueExecuteCommandLists(commandQueue,
370375
static_cast<uint32_t>(commandLists.size()),
@@ -388,31 +393,28 @@ void DynamicPipeline::execute_vm_runtime(npu_vm_runtime_handle_t vmRuntime,
388393

389394
// Create the VM execution context (owned by args._impl, destroyed with it).
390395
args.ensureExecutionContext(vmRuntime);
391-
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;
396+
params->executionContext = args._executeParams.executionContext;
397+
398+
params->pInputs = args._inputMemRefHandles.data();
399+
params->numOfInputs = static_cast<uint32_t>(args._inputMemRefHandles.size());
400+
params->pOutputs = args._outputMemRefHandles.data();
401+
params->numOfOutputs = static_cast<uint32_t>(args._outputMemRefHandles.size());
402+
params->ctx = _init_structs->getContext();
403+
params->device = _init_structs->getDevice();
404+
params->graphDdiTableExt = _init_structs->getGraphDdiTable().getImpl();
405+
params->commandLists = commandLists.data();
406+
params->numCommandLists = static_cast<uint64_t>(commandLists.size());
407+
params->commandQueue = commandQueue;
408+
params->inferenceFence = fence;
409+
params->event = event;
406410

407411
_logger.debug("Execute graph with runtime engine");
408-
if (npuVMRuntimeExecute(vmRuntime, &params) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
412+
if (npuVMRuntimeExecute(vmRuntime, params) != NPU_VM_RUNTIME_RESULT_SUCCESS) {
409413
OPENVINO_THROW("Failed to execute VM runtime engine");
410414
} else {
411415
_logger.debug("Execution runtime engine is created successfully.");
412416
}
413417

414-
args._executedOnce = true;
415-
416418
_logger.debug("Completed to execute graph with runtime engine");
417419
}
418420

@@ -426,9 +428,13 @@ void DynamicPipeline::predict_output_shape(const IGraph& graph,
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+
std::vector<std::shared_ptr<MemRefTypeImpl>> inputMemRefImpls, outputMemRefImpls;
432+
433+
auto processMemRefs = [&](auto& memRefs, auto& destMemRefHandles, auto& destMemRefImpls) {
434+
destMemRefHandles.clear();
435+
destMemRefHandles.reserve(memRefs.size());
436+
destMemRefImpls.clear();
437+
destMemRefImpls.reserve(memRefs.size());
432438

433439
for (auto& memref : memRefs) {
434440
auto impl = std::static_pointer_cast<MemRefTypeImpl>(memref._impl);
@@ -437,12 +443,13 @@ void DynamicPipeline::predict_output_shape(const IGraph& graph,
437443
memref._impl = impl;
438444
}
439445
impl->UpdateMemRefHandleStatus(memref);
440-
targetMemRefHandles.push_back(impl->_memRef);
446+
destMemRefHandles.push_back(impl->_memRef);
447+
destMemRefImpls.push_back(impl);
441448
}
442449
};
443450

444-
processMemRefs(inputsMemRefs, args._inputMemRefHandles);
445-
processMemRefs(outputsMemRefs, args._outputMemRefHandles);
451+
processMemRefs(inputsMemRefs, args._inputMemRefHandles, inputMemRefImpls);
452+
processMemRefs(outputsMemRefs, args._outputMemRefHandles, outputMemRefImpls);
446453

447454
npu_vm_runtime_result_t result = NPU_VM_RUNTIME_RESULT_SUCCESS;
448455
npu_vm_runtime_version_t version{};
@@ -467,16 +474,18 @@ void DynamicPipeline::predict_output_shape(const IGraph& graph,
467474
params.numOfInputs = static_cast<uint32_t>(args._inputMemRefHandles.size());
468475
params.pOutputs = args._outputMemRefHandles.data();
469476
params.numOfOutputs = static_cast<uint32_t>(args._outputMemRefHandles.size());
470-
params.executionContext = args._executionContext;
477+
params.executionContext = args._executeParams.executionContext;
471478

472479
result = npuVMRuntimePredictOutputShape2(vmRuntime, &params);
473480
}
474481

475482
if (result != NPU_VM_RUNTIME_RESULT_SUCCESS) {
476483
OPENVINO_THROW("Failed to predict output shape with VM runtime engine, error code: ", result);
477484
} else {
478-
for (auto& out : outputsMemRefs) {
479-
std::shared_ptr<MemRefTypeImpl> outImpl = std::static_pointer_cast<MemRefTypeImpl>(out._impl);
485+
for (size_t i = 0; i < outputsMemRefs.size(); ++i) {
486+
auto& out = outputsMemRefs[i];
487+
std::shared_ptr<MemRefTypeImpl> outImpl = outputMemRefImpls[i];
488+
480489
if (outImpl == nullptr) {
481490
OPENVINO_THROW("MemRefType implementation is broken, unknown error happens in shape prediction.");
482491
}

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)