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: detect V2 by u2 weights before position_ids (#39978)
Koryun Khachatryancec54fea222a
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 570bf785247..7438452dd17 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@@ -13,6 +13,7 @@#include <iostream>#include <optional>#include <sstream>+#include <string_view>#include <typeinfo>#include <unordered_set>#include <utility>@@ -293,6 +294,20 @@ std::pair<ov::AnyMap, GQAModelStage> with_gqa_defaults(const std::shared_ptr<ov:return {config, stage};}+std::string_view gqa_case_name(ov::npuw::GQACompiledModel::Case gqa_case) {+ switch (gqa_case) {+ case ov::npuw::GQACompiledModel::Case::V0:+ return "V0";+ case ov::npuw::GQACompiledModel::Case::V1:+ return "V1";+ case ov::npuw::GQACompiledModel::Case::V2:+ return "V2";+ case ov::npuw::GQACompiledModel::Case::Unknown:+ break;+ }+ return "Unknown";+}+} // namespace// Prints a "[GQA-TRACE]" line when OPENVINO_NPUW_GQA_LOG=1 is set; no-op otherwise.@@ -310,7 +325,9 @@ 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, identify_case(model));+ const auto gqa_case = identify_case(model);+ LOG_INFO("Detected GQA model version: " << gqa_case_name(gqa_case));+ auto [prepared_properties, stage] = with_gqa_defaults(model, properties, gqa_case);static std::atomic<std::size_t> mcount{0u};@@ -468,8 +485,8 @@ ov::npuw::GQACompiledModel::Case ov::npuw::GQACompiledModel::identify_case(// Each matched name must also be forward-reachable to the GQA op (not just same-named).const auto* gqa_node = gqa.get();- // V1 uses `position_ids` for RoPE; V0/V2 use `past_seq_len`/`total_seq_len` instead.- // V2 is further distinguished by 2-bit group-quantized (u2) weights.+ // V2 is identified by 2-bit group-quantized (u2) weights, whatever its position signal.+ // Otherwise V1 uses `position_ids` for RoPE and V0 uses `past_seq_len`/`total_seq_len`.bool has_activation_input = false;bool has_position_ids = false;bool has_seq_len_signal = false;@@ -501,10 +518,10 @@ ov::npuw::GQACompiledModel::Case ov::npuw::GQACompiledModel::identify_case(return Case::Unknown;}- if (has_position_ids) {- return Case::V1;+ if (has_2bit_group_quantized_weights(model)) {+ return Case::V2;}- return has_2bit_group_quantized_weights(model) ? Case::V2 : Case::V0;+ return has_position_ids ? Case::V1 : Case::V0;}bool ov::npuw::GQACompiledModel::has_dynamic_max_seq_len(const std::shared_ptr<const ov::Model>& model) {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 bc243acf4a4..40bfa2fd3ba 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@@ -943,6 +943,25 @@ TEST_F(GQACompiledModelTest, UsesRepOnlinePipelineForCaseV2PrefillModels) {EXPECT_EQ(call.props.at(std::string(::intel_npu::NPUW_PARALLEL_COMPILE::key())).as<std::string>(), "YES");}+TEST_F(GQACompiledModelTest, UsesRepOnlinePipelineForCaseV2PositionIdsPrefillModels) {+ RecordingFactory recorder;+ std::unique_ptr<ov::npuw::GQACompiledModel> compiled;++ ASSERT_NO_THROW(compiled = create_compiled_model(build_full_gqa_transformer_model(4,+ 2,+ true,+ PositionSignal::PositionIds,+ /*with_2bit_weights=*/true),+ {},+ recorder));+ ASSERT_NE(compiled, nullptr);++ // u2 weights take priority over position_ids, so this must not get V1's NONE pipeline.+ 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;@@ -1260,6 +1279,17 @@ TEST(GQACompiledModelSupportsTest, IdentifiesCaseV2ForSeqLenPairModelWith2BitWeiEXPECT_TRUE(ov::npuw::GQACompiledModel::supports(model));}+TEST(GQACompiledModelSupportsTest, IdentifiesCaseV2ForPositionIdsModelWith2BitWeights) {+ auto model = build_full_gqa_transformer_model(4,+ 2,+ true,+ PositionSignal::PositionIds,+ /*with_2bit_weights=*/true);+ EXPECT_EQ(ov::npuw::GQACompiledModel::identify_case(model), ov::npuw::GQACompiledModel::Case::V2);+ make_kv_cache_dynamic(model, 2);+ EXPECT_TRUE(ov::npuw::GQACompiledModel::supports(model));+}+TEST(GQACompiledModelSupportsTest, IdentifiesCaseUnknownWithoutAnyPositionSignal) {auto model = build_full_gqa_transformer_model(4, 2, true, PositionSignal::PositionIds);for (const auto& parameter : model->get_parameters()) {