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
NPUW GQA: Properly auto-configure for new models (#39294)
Dmitry Matveev9d72fa66c720
diff --git a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cppindex 1b6e39cbe7d..1181517cfce 100644--- a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp@@ -133,7 +133,8 @@ bool has_2bit_group_quantized_weights(const std::shared_ptr<const ov::Model>& mo}std::pair<ov::AnyMap, GQAModelStage> with_gqa_defaults(const std::shared_ptr<ov::Model>& model,- const ov::AnyMap& properties) {+ const ov::AnyMap& properties,+ ov::npuw::GQACompiledModel::Case gqa_case) {const auto detect_gqa_model_stage = [&]() {// The activation tensor ("input_hidden_states") is what the embedding// model stage feeds into the transformer. For the generate (iter) model@@ -219,29 +220,38 @@ std::pair<ov::AnyMap, GQAModelStage> with_gqa_defaults(const std::shared_ptr<ov:};// V0/V1 are pre-assembled GQA graphs that online partitioning would only get in the way- // of. V2 (2-bit group-quantized weights) still benefits from online partitioning, so it- // keeps the default pipeline (REG) instead of being forced to NONE.- ov::AnyMap config = {{"NPU_COMPILER_DYNAMIC_QUANTIZATION", "YES"},+ // of. V2 (2-bit group-quantized weights) still benefits from online partitioning: its+ // prefill stage switches to the REP (repeated-block) pipeline below, and both stages+ // compile their partitioned blocks in parallel via NPUW_PARALLEL_COMPILE; V0/V1 don't+ // use online partitioning at all, so that option would have no effect for them.+ const bool is_v2 = gqa_case == ov::npuw::GQACompiledModel::Case::V2;+ ov::AnyMap config = {{std::string(::intel_npu::COMPILER_DYNAMIC_QUANTIZATION::key()), "YES"},{std::string(::intel_npu::NPUW_DEVICES::key()), "NPU"},{ov::cache_mode.name(), ov::CacheMode::OPTIMIZE_SPEED},- {std::string(::intel_npu::NPUW_UNQDQ::key()), "YES"}};- if (!has_2bit_group_quantized_weights(model)) {- config.emplace("NPUW_ONLINE_PIPELINE", "NONE");+ {std::string(::intel_npu::NPUW_UNQDQ::key()), "YES"},+ {std::string(::intel_npu::COMPILATION_MODE_PARAMS::key()), "optimization-level=3"}};+ if (!is_v2) {+ config.emplace(std::string(::intel_npu::NPUW_ONLINE_PIPELINE::key()), "NONE");+ } else {+ config.emplace(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key()), "YES");}const auto stage = detect_gqa_model_stage();if (stage == GQAModelStage::PREFILL) {merge_config_with(config,{{std::string(::intel_npu::NPUW_FOLD::key()), "YES"},- {"NPUW_FOLD_ONLY", "attn"},- {"NPUW_ONLINE_ISOLATE", "ATTN"},- {"NPUW_ONLINE_KEEP_BLOCKS_TAGGED", "attn"},- {"NPUW_ATTN", "STATIC"}});+ {std::string(::intel_npu::NPUW_FOLD_ONLY::key()), "attn"},+ {std::string(::intel_npu::NPUW_ONLINE_ISOLATE::key()), "ATTN"},+ {std::string(::intel_npu::NPUW_ONLINE_KEEP_BLOCKS_TAGGED::key()), "attn"},+ {std::string(::intel_npu::NPUW_ATTN::key()), "STATIC"}});+ if (is_v2) {+ merge_config_with(config, {{std::string(::intel_npu::NPUW_ONLINE_PIPELINE::key()), "REP"}});+ }LOG_INFO("Detected prefill-style GQA model; applying FOLD with ATTN isolation");} else if (stage == GQAModelStage::GENERATE) {merge_config_with(config,{{std::string(::intel_npu::NPUW_FOLD::key()), "YES"},- {"NPUW_FOLD_ONLY", "attn"},+ {std::string(::intel_npu::NPUW_FOLD_ONLY::key()), "attn"},{std::string(::intel_npu::NPUW_FUNCALL_ASYNC::key()), "YES"},{std::string(::intel_npu::NPUW_UNFOLD_IREQS::key()), "YES"}});@@ -282,7 +292,7 @@ std::pair<ov::AnyMap, GQAModelStage> with_gqa_defaults(const std::shared_ptr<ov:ov::npuw::GQACompiledModel::PreparedState ov::npuw::GQACompiledModel::prepare(const std::shared_ptr<ov::Model>& model,const ov::AnyMap& properties) {- auto [prepared_properties, stage] = with_gqa_defaults(model, properties);+ auto [prepared_properties, stage] = with_gqa_defaults(model, properties, identify_case(model));static std::atomic<std::size_t> mcount{0u};diff --git a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cppindex eafd61998ae..215660059af 100644--- a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp@@ -11,6 +11,7 @@#include <vector>#include "gqa_compiled_model.hpp"+#include "intel_npu/config/npuw.hpp"#include "llm_test_helpers.hpp"#include "npuw_transformations/reshape_to_static_gqa.hpp"#include "openvino/core/node_vector.hpp"@@ -745,6 +746,9 @@ TEST_F(GQACompiledModelTest, DisablesOnlinePipelineForCaseV1Models) {const auto& call = recorder.only_call();EXPECT_EQ(call.props.at("NPUW_ONLINE_PIPELINE").as<std::string>(), "NONE");+ // V1 doesn't use online partitioning at all, so parallelizing its compilation+ // wouldn't do anything.+ EXPECT_EQ(call.props.count(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key())), 0u);}TEST_F(GQACompiledModelTest, DisablesOnlinePipelineForCaseV0Models) {@@ -759,9 +763,12 @@ TEST_F(GQACompiledModelTest, DisablesOnlinePipelineForCaseV0Models) {const auto& call = recorder.only_call();EXPECT_EQ(call.props.at("NPUW_ONLINE_PIPELINE").as<std::string>(), "NONE");+ // V0 doesn't use online partitioning at all, so parallelizing its compilation+ // wouldn't do anything.+ EXPECT_EQ(call.props.count(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key())), 0u);}-TEST_F(GQACompiledModelTest, KeepsDefaultOnlinePipelineForCaseV2Models) {+TEST_F(GQACompiledModelTest, UsesRepOnlinePipelineForCaseV2PrefillModels) {RecordingFactory recorder;std::unique_ptr<ov::npuw::GQACompiledModel> compiled;@@ -775,10 +782,34 @@ TEST_F(GQACompiledModelTest, KeepsDefaultOnlinePipelineForCaseV2Models) {recorder));ASSERT_NE(compiled, nullptr);- // Unlike V0/V1, a 2-bit group-quantized (V2) model isn't forced to NONE -- it's left- // unset so the plugin-wide default (REG) applies.+ // Unlike V0/V1, a 2-bit group-quantized (V2) model isn't forced to NONE -- its prefill+ // stage still benefits from online partitioning, so it switches to the REP+ // (repeated-block) pipeline, and compiles the resulting blocks in parallel.+ const auto& call = recorder.only_call();+ EXPECT_EQ(call.props.at("NPUW_ONLINE_PIPELINE").as<std::string>(), "REP");+ EXPECT_EQ(call.props.at(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key())).as<std::string>(), "YES");+}++TEST_F(GQACompiledModelTest, UsesParallelCompileWithDefaultPipelineForCaseV2GenerateModels) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::GQACompiledModel> compiled;++ // num_heads == kv_num_heads == 1 so input_hidden_states' token dim is 1 -> generate stage.+ ASSERT_NO_THROW(compiled = create_compiled_model(+ build_full_gqa_transformer_model(1,+ 1,+ true,+ PositionSignal::SeqLenPair,+ /*with_2bit_weights=*/true),+ {},+ recorder));+ ASSERT_NE(compiled, nullptr);++ // REP is a prefill-only optimization -- the generate stage keeps the plugin-wide+ // default pipeline (REG), but still compiles in parallel like the prefill stage.const auto& call = recorder.only_call();EXPECT_EQ(call.props.count("NPUW_ONLINE_PIPELINE"), 0u);+ EXPECT_EQ(call.props.at(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key())).as<std::string>(), "YES");}TEST_F(GQACompiledModelTest, AppliesFoldOnlyAttnForGenerateStyleModels) {