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 - support no-slice case (#36545)
Dmitry Matveeve088c971888c
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 babe2fbd457..bf00ca75889 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@@ -38,58 +38,34 @@ ov::AnyMap with_gqa_defaults(const std::shared_ptr<ov::Model>& model, const ov::};const auto detect_gqa_model_stage = [&]() {- const auto to_lower = [](std::string value) {- std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {- return static_cast<char>(std::tolower(ch));- });- return value;- };-- const auto is_cache_parameter_name = [&](const std::string& name) {- const auto lower_name = to_lower(name);- return lower_name.find("past") != std::string::npos || lower_name.find("present") != std::string::npos ||- lower_name.find("cache") != std::string::npos;- };-- bool saw_generate_input = false;- bool saw_prefill_input = false;-+ // The activation tensor ("input_hidden_states") is what the embedding+ // model stage feeds into the transformer. For the generate (iter) model+ // the seq dim is specialized to 1 via a free-dim override; for the+ // prefill (ctx) model the seq dim is either >1 or left dynamic (the+ // context length varies at runtime). KV-cache slicing has no effect on+ // this input at all.for (const auto& parameter : model->get_parameters()) {- if (is_cache_parameter_name(parameter->get_friendly_name())) {- continue;- }-- const auto element_type = parameter->get_element_type();- if (!element_type.is_real()) {+ const auto& name = parameter->get_friendly_name();+ if (name != "input_hidden_states" && name != "input_ids") {continue;}const auto& partial_shape = parameter->get_partial_shape();- if (partial_shape.rank().is_dynamic() || partial_shape.rank().get_length() != 3) {+ if (partial_shape.rank().is_dynamic() || partial_shape.rank().get_length() < 2) {continue;}const auto& token_dim = partial_shape[1];++ // Dynamic seq dim → prefill model with variable context length.if (token_dim.is_dynamic()) {- continue;+ return GQAModelStage::PREFILL;}const auto token_count = token_dim.get_length();if (token_count == 1) {- saw_generate_input = true;- } else if (token_count > 1) {- saw_prefill_input = true;- }-- if (saw_generate_input && saw_prefill_input) {- return GQAModelStage::UNKNOWN;+ return GQAModelStage::GENERATE;}- }-- if (saw_generate_input) {- return GQAModelStage::GENERATE;- }- if (saw_prefill_input) {return GQAModelStage::PREFILL;}return GQAModelStage::UNKNOWN;@@ -98,22 +74,28 @@ ov::AnyMap with_gqa_defaults(const std::shared_ptr<ov::Model>& model, const ov::ov::AnyMap config = {{"NPUW_ONLINE_PIPELINE", "REP"},{std::string(::intel_npu::NPUW_DEVICES::key()), "NPU"},- {std::string(::intel_npu::NPUW_FOLD::key()), "YES"},{ov::cache_mode.name(), ov::CacheMode::OPTIMIZE_SPEED},{std::string(::intel_npu::NPUW_UNQDQ::key()), "YES"},};- if (detect_gqa_model_stage() != GQAModelStage::GENERATE) {+ const auto stage = detect_gqa_model_stage();+ if (stage == GQAModelStage::PREFILL) {merge_config_with(config,- {{"NPUW_ONLINE_ISOLATE", "ATTN"},+ {{std::string(::intel_npu::NPUW_FOLD::key()), "YES"},{"NPUW_FOLD_ONLY", "attn"},+ {"NPUW_ONLINE_ISOLATE", "ATTN"},{"NPUW_ATTN", "STATIC"},- {"NPUW_ONLINE_KEEP_BLOCK_SIZE", "9"}});- } else {+ {"NPUW_ONLINE_KEEP_BLOCK_SIZE", "2"}});+ 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_FUNCALL_ASYNC::key()), "YES"},+ {{std::string(::intel_npu::NPUW_FOLD::key()), "YES"},+ {"NPUW_FOLD_ONLY", "attn"},+ {std::string(::intel_npu::NPUW_FUNCALL_ASYNC::key()), "YES"},{std::string(::intel_npu::NPUW_UNFOLD_IREQS::key()), "YES"}});- LOG_INFO("Detected generate-style GQA model; skipping ATTN isolation defaults");+ LOG_INFO("Detected generate-style GQA model; applying FOLD with async funcall");+ } else {+ LOG_INFO("GQA model stage unknown; FOLD disabled");}merge_config_with(config, properties);return config;diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/gqa.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/gqa.cppindex 2c70b344f1f..90f75e2a29b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/gqa.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/gqa.cpp@@ -73,25 +73,29 @@ GQA::GQA(const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot, const std:// output[1] (present_key): Slice -> Result (with Broadcast+ShapeOf harness).if (matched_gqa->get_output_size() > 1) {for (auto& target : matched_gqa->output(1).get_target_inputs()) {- auto slice = target.get_node()->shared_from_this();- if (ov::is_type<ov::op::v8::Slice>(slice)) {- isolate_slice_with_harness(slice);+ auto node = target.get_node()->shared_from_this();+ if (ov::is_type<ov::op::v8::Slice>(node)) {+ isolate_slice_with_harness(node);}}}// output[2] (present_value): Slice -> Transpose -> Result (with Broadcast+ShapeOf harness).+ // Also handles bare Transpose -> Result when no Slice is present (no slice_kv).if (matched_gqa->get_output_size() > 2) {for (auto& target : matched_gqa->output(2).get_target_inputs()) {- auto slice = target.get_node()->shared_from_this();- if (ov::is_type<ov::op::v8::Slice>(slice)) {- isolate_slice_with_harness(slice);- for (auto& t2 : slice->output(0).get_target_inputs()) {+ auto node = target.get_node()->shared_from_this();+ if (ov::is_type<ov::op::v8::Slice>(node)) {+ isolate_slice_with_harness(node);+ for (auto& t2 : node->output(0).get_target_inputs()) {auto trans = t2.get_node()->shared_from_this();if (ov::is_type<ov::op::v1::Transpose>(trans)) {isolate(trans);}}+ } else if (ov::is_type<ov::op::v1::Transpose>(node)) {+ // No Slice (slice_kv not used): bare Transpose on present_value.+ isolate(node);}}}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 79b2e700aeb..577bc8d63e3 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@@ -267,9 +267,8 @@ TEST_F(GQACompiledModelTest, AddsExpectedNpuwDefaultsBeforeInnerCompilation) {EXPECT_EQ(call.props.at("NPUW_ONLINE_PIPELINE").as<std::string>(), "REP");EXPECT_EQ(call.props.at("NPUW_DEVICES").as<std::string>(), "NPU");EXPECT_EQ(call.props.at("NPUW_ONLINE_ISOLATE").as<std::string>(), "ATTN");- EXPECT_EQ(call.props.at("NPUW_FOLD_ONLY").as<std::string>(), "attn");EXPECT_EQ(call.props.at("NPUW_ATTN").as<std::string>(), "STATIC");- EXPECT_EQ(call.props.at("NPUW_ONLINE_KEEP_BLOCK_SIZE").as<std::string>(), "9");+ EXPECT_EQ(call.props.at("NPUW_ONLINE_KEEP_BLOCK_SIZE").as<std::string>(), "2");EXPECT_EQ(call.props.at("NPUW_FOLD").as<std::string>(), "YES");EXPECT_EQ(call.props.at(ov::cache_mode.name()).as<ov::CacheMode>(), ov::CacheMode::OPTIMIZE_SPEED);EXPECT_EQ(call.props.at("NPUW_UNQDQ").as<std::string>(), "YES");@@ -277,7 +276,7 @@ TEST_F(GQACompiledModelTest, AddsExpectedNpuwDefaultsBeforeInnerCompilation) {EXPECT_EQ(call.props.count("NPUW_UNFOLD_IREQS"), 0u);}-TEST_F(GQACompiledModelTest, SkipsAttnIsolationDefaultsForGenerateStyleModels) {+TEST_F(GQACompiledModelTest, AppliesFoldOnlyAttnForGenerateStyleModels) {RecordingFactory recorder;std::unique_ptr<ov::npuw::GQACompiledModel> compiled;@@ -292,8 +291,8 @@ TEST_F(GQACompiledModelTest, SkipsAttnIsolationDefaultsForGenerateStyleModels) {EXPECT_EQ(call.props.at("NPUW_UNQDQ").as<std::string>(), "YES");EXPECT_EQ(call.props.at("NPUW_FUNCALL_ASYNC").as<std::string>(), "YES");EXPECT_EQ(call.props.at("NPUW_UNFOLD_IREQS").as<std::string>(), "YES");+ EXPECT_EQ(call.props.at("NPUW_FOLD_ONLY").as<std::string>(), "attn");EXPECT_EQ(call.props.count("NPUW_ONLINE_ISOLATE"), 0u);- EXPECT_EQ(call.props.count("NPUW_FOLD_ONLY"), 0u);EXPECT_EQ(call.props.count("NPUW_ATTN"), 0u);EXPECT_EQ(call.props.count("NPUW_ONLINE_KEEP_BLOCK_SIZE"), 0u);}@@ -308,7 +307,6 @@ TEST_F(GQACompiledModelTest, KeepsAttnIsolationDefaultsForPrefillStyleModels) {const auto& call = recorder.only_call();EXPECT_EQ(call.props.at("NPUW_DEVICES").as<std::string>(), "NPU");EXPECT_EQ(call.props.at("NPUW_ONLINE_ISOLATE").as<std::string>(), "ATTN");- EXPECT_EQ(call.props.at("NPUW_FOLD_ONLY").as<std::string>(), "attn");EXPECT_EQ(call.props.at("NPUW_FOLD").as<std::string>(), "YES");EXPECT_EQ(call.props.count("NPUW_FUNCALL_ASYNC"), 0u);EXPECT_EQ(call.props.count("NPUW_UNFOLD_IREQS"), 0u);