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
Enable HostCompile_Interpreter when compilation mode is not set for models with dynamic inputs and outputs (#36395)
Insoo Woodb9c5949aabc
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cppindex 789eeb8d2b8..d7d5b16be08 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp@@ -65,11 +65,44 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<conconst FilteredConfig& config) const {OV_ITT_TASK_CHAIN(COMPILE_BLOB, itt::domains::NPUPlugin, "PluginCompilerAdapter", "compile");+ // specify HostCompile_Interpreter mode for dynamic model (inputs and outputs both dynamic) if NPU_COMPILATION_MODE+ // is not set+ FilteredConfig effectiveConfig = config;+ if (!config.has<COMPILATION_MODE>() &&+ !(config.has<DYNAMIC_SHAPE_TO_STATIC>() && config.get<DYNAMIC_SHAPE_TO_STATIC>())) {+ const auto isDynamic = [](const auto& port) {+ auto& shape = port.get_partial_shape();+ return shape.is_dynamic() && (shape.rank().get_length() == 4) && shape[0].is_static();+ };++ if (model) {+ const auto& inputs = model->inputs();+ const auto& outputs = model->outputs();+ const bool inputsDynamic = std::any_of(inputs.begin(), inputs.end(), isDynamic);+ const bool outputsDynamic = std::any_of(outputs.begin(), outputs.end(), isDynamic);+ if (inputsDynamic && outputsDynamic) {+ _logger.info("NPU_COMPILATION_MODE not set; selecting 'HostCompile_Interpreter' "+ "for fully-dynamic model (inputs and outputs both dynamic)");+ effectiveConfig.update({{ov::intel_npu::compilation_mode.name(), "HostCompile_Interpreter"}});+ }+ }+ }+_logger.debug("compile start");- auto [tensor, compatibilityDescriptor] = _compiler->compile(model, config);+ auto [tensor, compatibilityDescriptor] = _compiler->compile(model, effectiveConfig);_logger.debug("compile end");- if (config.get<COMPILATION_MODE>().find("HostCompile") == 0) {+ auto isHostCompiledBlob = [&](ov::Tensor& tensor) {+ const size_t headerSize = std::min(tensor.get_byte_size(), size_t{20});+ const std::string_view header(static_cast<const char*>(tensor.data()), headerSize);+ if (header.find("llvm") != std::string_view::npos || header.find("NPUByte\x00") != std::string_view::npos) {+ _logger.debug("HostCompile mode is detected based on blob header, use internal function to get metadata!");+ return true;+ }+ return false;+ };++ if (isHostCompiledBlob(tensor)) {NPUVMRuntimeApi::initializeFromBlob(tensor.data(), tensor.get_byte_size());// metadata will be obtained in initialze() of DynamicGraphdiff --git a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.cpp b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.cppindex 5bfe26d06c5..4226484011f 100644--- a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.cpp+++ b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.cpp@@ -32,3 +32,18 @@ INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTests,::testing::ValuesIn(configs),::testing::ValuesIn(modelNames)),ov::test::utils::appendPlatformTypeTestName<InferWithHostCompileTests>);++const std::vector<ov::AnyMap> defaultHostCompileconfigs = {+ {+ {"NPU_COMPILER_TYPE", "PLUGIN"},+ {"NPU_CREATE_EXECUTOR", "0"},+ }+};++const std::vector<std::string> defaultHCModelNames = {"MaxPool_NCHW", "MaxPool_NCHW_DynBatch"};+INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTests,+ InferWithDefaultHostCompileTests,+ ::testing::Combine(::testing::ValuesIn(devices),+ ::testing::ValuesIn(defaultHostCompileconfigs),+ ::testing::ValuesIn(defaultHCModelNames)),+ ov::test::utils::appendPlatformTypeTestName<InferWithDefaultHostCompileTests>);diff --git a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hppindex 4e286b6c103..f4a97d9d7c2 100644--- a/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/dynamic_host_pipeline/infer_with_host_compile.hpp@@ -26,11 +26,20 @@ namespace ov {namespace test {namespace behavior {-inline std::shared_ptr<ov::Model> createMaxPoolModel() {- auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16,+inline std::shared_ptr<ov::Model> createMaxPoolModel(bool dynamicBatch = false, bool nhwcLayout = true) {+ std::shared_ptr<ov::op::v0::Parameter> input;+ if (dynamicBatch) {+ input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16,+ ov::PartialShape{ov::Dimension(1, 10), 16, 720, 1280});+ } else {+ input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16,ov::PartialShape{1, 16, ov::Dimension(10, 720), ov::Dimension(10, 1280)});- input->set_friendly_name("input1");+ }+ std::string inputName = "input1";+ input->set_friendly_name(inputName);+ input->get_output_tensor(0).set_names({inputName});+ if (!nhwcLayout) input->set_layout("NCHW");auto maxpool = std::make_shared<ov::op::v1::MaxPool>(input,Strides{1, 1},Shape{0, 0},@@ -41,16 +50,23 @@ inline std::shared_ptr<ov::Model> createMaxPoolModel() {maxpool->set_friendly_name("MaxPool_2");auto result = std::make_shared<ov::op::v0::Result>(maxpool);- result->set_friendly_name("output");+ std::string outputName = "output";+ if (!nhwcLayout) result->set_layout("NCHW");+ result->set_friendly_name(outputName);+ result->get_output_tensor(0).set_names({outputName});+auto model = std::make_shared<Model>(ResultVector{result}, ParameterVector{input}, "MaxPool");+// making input and output to be NHWC- auto preProc = ov::preprocess::PrePostProcessor(model);- preProc.input(0).tensor().set_layout("NHWC");- preProc.input(0).model().set_layout("NCHW");- preProc.output(0).tensor().set_layout("NHWC");- preProc.output(0).model().set_layout("NCHW");-- model = preProc.build();+ if (nhwcLayout) {+ auto preProc = ov::preprocess::PrePostProcessor(model);+ preProc.input(0).tensor().set_layout("NHWC");+ preProc.input(0).model().set_layout("NCHW");+ preProc.output(0).tensor().set_layout("NHWC");+ preProc.output(0).model().set_layout("NCHW");++ model = preProc.build();+ }return model;}@@ -321,6 +337,12 @@ std::shared_ptr<ov::Model> InferWithHostCompileTests::createModelByName(const stif (modelName == "MaxPool") {return createMaxPoolModel();}+ if (modelName == "MaxPool_NCHW") {+ return createMaxPoolModel(false, false);+ }+ if (modelName == "MaxPool_NCHW_DynBatch") {+ return createMaxPoolModel(true, false);+ }OPENVINO_THROW("Unknown model name for InferWithHostCompileTests: ", modelName);}@@ -651,6 +673,64 @@ TEST_P(InferWithHostCompileTests, CompileAndInferWithZeroTensor) {<< logCapture.str();}+using InferWithDefaultHostCompileTests = InferWithHostCompileTests;++inline bool isByteCodeBlob(const std::string& blob) {+ const size_t headerSize = std::min(blob.size(), size_t{20});+ const std::string_view header(blob.data(), headerSize);+ return header.find("NPUByte\x00") != std::string_view::npos;+};++inline bool isElfBlob(const std::string& blob) {+ const size_t headerSize = std::min(blob.size(), size_t{20});+ const std::string_view header(blob.data(), headerSize);+ return header.find("ELF\x00") != std::string_view::npos;+};++TEST_P(InferWithDefaultHostCompileTests, CompileDynamicModelWithNoHostCompileMode) {+ // Skip test according to plugin specific disabledTestPatterns() (if any)+ SKIP_IF_CURRENT_TEST_IS_DISABLED()+ if (!isTargetDevice) {+ GTEST_SKIP() << "Skip test for current device";+ }++ auto model = createModelByName(selectedModelName);++ ov::CompiledModel compiledModel;+ // Compilation shall pass since load of npu_mlir_runtime is deffered with NPU_CREATE_EXECUTOR=0+ OV_ASSERT_NO_THROW(compiledModel = core->compile_model(model, target_device, configuration));++ std::stringstream modelStream;+ OV_ASSERT_NO_THROW(compiledModel.export_model(modelStream));++ if (modelStream.str().empty()) {+ FAIL() << "Exported model stream is empty";+ }++ if (selectedModelName == "MaxPool_NCHW_DynBatch") {+ ASSERT_TRUE(isElfBlob(modelStream.str()))+ << "Expected exported model to be an ELF blob";+ }+ else if (selectedModelName == "MaxPool_NCHW") {+ ASSERT_TRUE(isByteCodeBlob(modelStream.str()))+ << "Expected exported model to be a bytecode";+ }++ ov::InferRequest reqDynamic;+ try {+ ov::CompiledModel importedModel = core->import_model(modelStream, target_device);+ reqDynamic = importedModel.create_infer_request();+ } catch (const ov::Exception& e) {+ if (std::string(e.what()).find("Cannot load library") == std::string::npos) {+ FAIL() << "Expected exception message to contain 'Cannot load library', but got: " << e.what();+ } else {+ GTEST_SKIP() << "Cannot load library, skip test.";+ }+ }++ OV_ASSERT_NO_THROW(reqDynamic.infer());+}+} // namespace behavior} // namespace test} // namespace ovdiff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hppindex 10ba7a2180a..652e691a4ea 100644--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp@@ -2580,7 +2580,14 @@ TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameRawMemoryMultipleTimes::operator delete(output_data, std::align_val_t(4096));}-using DynamicBoundsTests = InferRequestRunTests;+class DynamicBoundsTests : public InferRequestRunTests+{+public:+ void SetUp() override {+ InferRequestRunTests::SetUp();+ configuration.insert({ov::intel_npu::compilation_mode.name(), ov::Any("DefaultHW")});+ }+};TEST_P(DynamicBoundsTests, ChangeShapeAfterTensorIsSet) {SKIP_IF_CURRENT_TEST_IS_DISABLED();