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 case w/o V tensor transpose (#36638)
Ekaterina Shiryaeva0a27e2d1f12d
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.incindex 3a74ea04010..081be721abc 100644--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/npuw_option_defs.inc@@ -8,6 +8,7 @@ INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_NO_FOLD, std::string, "", ov::intel_npu::nINTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_MIN_SIZE, std::size_t, 10, ov::intel_npu::npuw::partitioning::online, min_size, "NPUW_ONLINE_MIN_SIZE", ROOT, EXPOSED, CACHED, ALL)INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_KEEP_BLOCKS, std::size_t, 5, ov::intel_npu::npuw::partitioning::online, keep_blocks, "NPUW_ONLINE_KEEP_BLOCKS", ROOT, EXPOSED, CACHED, ALL)INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_KEEP_BLOCK_SIZE, std::size_t, 10, ov::intel_npu::npuw::partitioning::online, keep_block_size, "NPUW_ONLINE_KEEP_BLOCK_SIZE", ROOT, EXPOSED, CACHED, ALL)+INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_KEEP_BLOCKS_TAGGED, std::string, "", ov::intel_npu::npuw::partitioning::online, keep_block_tag, "NPUW_ONLINE_KEEP_BLOCKS_TAGGED", ROOT, EXPOSED, CACHED, ALL)INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_ONLINE_DUMP_PLAN, std::string, "", ov::intel_npu::npuw::partitioning::online, dump_plan, "NPUW_ONLINE_DUMP_PLAN", ROOT, HIDDEN, UNCACHED, ALL)INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_PLAN, std::string, "", ov::intel_npu::npuw::partitioning, plan, "NPUW_PLAN", ROOT, HIDDEN, UNCACHED, ALL)INTEL_NPU_NPUW_SIMPLE_OPT(NPUW_FOLD, bool, false, ov::intel_npu::npuw::partitioning, fold, "NPUW_FOLD", ROOT, EXPOSED, CACHED, ALL)diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex ac9d8560074..577355d9ffa 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -2440,6 +2440,7 @@ void ov::npuw::CompiledModel::implement_properties() {BIND(npuw::partitioning::online::min_size, NPUW_ONLINE_MIN_SIZE),BIND(npuw::partitioning::online::keep_blocks, NPUW_ONLINE_KEEP_BLOCKS),BIND(npuw::partitioning::online::keep_block_size, NPUW_ONLINE_KEEP_BLOCK_SIZE),+ BIND(npuw::partitioning::online::keep_block_tag, NPUW_ONLINE_KEEP_BLOCKS_TAGGED),BIND(npuw::partitioning::online::avoid, NPUW_ONLINE_AVOID),BIND(npuw::partitioning::online::isolate, NPUW_ONLINE_ISOLATE),BIND(npuw::partitioning::online::nofold, NPUW_ONLINE_NO_FOLD),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 bf00ca75889..243c687a353 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@@ -30,13 +30,14 @@ void merge_config_with(ov::AnyMap& lhs, const ov::AnyMap& rhs) {}}-ov::AnyMap with_gqa_defaults(const std::shared_ptr<ov::Model>& model, const ov::AnyMap& properties) {- enum class GQAModelStage {- UNKNOWN,- PREFILL,- GENERATE,- };-+enum class GQAModelStage {+ UNKNOWN,+ PREFILL,+ GENERATE,+};++std::pair<ov::AnyMap, GQAModelStage> with_gqa_defaults(const std::shared_ptr<ov::Model>& model,+ const ov::AnyMap& properties) {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@@ -84,8 +85,8 @@ ov::AnyMap with_gqa_defaults(const std::shared_ptr<ov::Model>& model, const ov::{{std::string(::intel_npu::NPUW_FOLD::key()), "YES"},{"NPUW_FOLD_ONLY", "attn"},{"NPUW_ONLINE_ISOLATE", "ATTN"},- {"NPUW_ATTN", "STATIC"},- {"NPUW_ONLINE_KEEP_BLOCK_SIZE", "2"}});+ {"NPUW_ONLINE_KEEP_BLOCKS_TAGGED", "attn"},+ {"NPUW_ATTN", "STATIC"}});LOG_INFO("Detected prefill-style GQA model; applying FOLD with ATTN isolation");} else if (stage == GQAModelStage::GENERATE) {merge_config_with(config,@@ -98,14 +99,20 @@ ov::AnyMap with_gqa_defaults(const std::shared_ptr<ov::Model>& model, const ov::LOG_INFO("GQA model stage unknown; FOLD disabled");}merge_config_with(config, properties);- return config;+ return {config, stage};}} // namespaceov::npuw::GQACompiledModel::PreparedState ov::npuw::GQACompiledModel::prepare(const std::shared_ptr<ov::Model>& model,const ov::AnyMap& properties) {- auto prepared_properties = with_gqa_defaults(model, properties);+ auto [prepared_properties, stage] = with_gqa_defaults(model, properties);++ model->set_friendly_name(model->get_friendly_name() + "_gqa_" ++ (stage == GQAModelStage::PREFILL ? "prefill"+ : stage == GQAModelStage::GENERATE ? "generate"+ : "unknown"));+// Untangle shared scale constants so every DequantizeLinear Multiply// gets its own copy. Some exporters reuse a single scale node across// multiple layers; NPUW's FOLD pass requires per-instance scalars.diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cppindex bd48b8bef36..88774901e6b 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp@@ -1406,8 +1406,19 @@ bool Snapshot::cleanUpUniquesImpl(const GPtrSet& gptrs) {// Another special case, actually a workaround. Keep it// FIXME: slightly different from Ensemble since we don't check flops and keep it by size onlyauto block_layer_size = (*(gptrs.begin()))->size();- if (gptrs.size() >= m_ctx.keep_blocks && block_layer_size >= m_ctx.keep_block_size) {- LOG_VERB("Keeping a repeated block of " << gptrs.size() << " groups with " << block_layer_size << " layers.");+ std::string isolate_tag = (*(gptrs.begin()))->isolatedTag();+ NPUW_ASSERT(std::all_of(gptrs.begin(), gptrs.end(), [&](const auto& g) {+ return g->isolatedTag() == isolate_tag;+ }));++ const bool keep_by_size = gptrs.size() >= m_ctx.keep_blocks && block_layer_size >= m_ctx.keep_block_size;+ const bool keep_by_isolate_tag =+ !isolate_tag.empty() && std::find(m_ctx.keep_block_tags.begin(), m_ctx.keep_block_tags.end(), isolate_tag) !=+ m_ctx.keep_block_tags.end();+ if (keep_by_size || keep_by_isolate_tag) {+ LOG_VERB("Keeping a repeated block of " << gptrs.size() << " groups with " << block_layer_size+ << " layers tagged '" << isolate_tag << "', "+ << "by_size=" << keep_by_size << ", by_tag=" << keep_by_isolate_tag);for (const auto& g : gptrs) {g->freeze();}diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.cppindex 707699e8f1e..2cb40596c52 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.cpp@@ -137,6 +137,11 @@ size_t ov::npuw::online::util::getMinRepBlockSize(const ::intel_npu::Config& cfgreturn min_size;}+std::unordered_set<std::string> ov::npuw::online::util::getKeepBlockTags(const ::intel_npu::Config& cfg) {+ auto tags = splitByComma(cfg.getString<::intel_npu::NPUW_ONLINE_KEEP_BLOCKS_TAGGED>());+ return std::unordered_set<std::string>(tags.begin(), tags.end());+}+std::vector<ov::npuw::online::Avoid> ov::npuw::online::util::getAvoids(const ::intel_npu::Config& cfg) {std::vector<ov::npuw::online::Avoid> avoids;@@ -232,6 +237,7 @@ ov::npuw::online::PassContext::PassContext(const ::intel_npu::Config& cfg) {min_graph_size = ov::npuw::online::util::getMinGraphSize(cfg);keep_blocks = ov::npuw::online::util::getMinRepBlocks(cfg);keep_block_size = ov::npuw::online::util::getMinRepBlockSize(cfg);+ keep_block_tags = ov::npuw::online::util::getKeepBlockTags(cfg);avoids = ov::npuw::online::util::getAvoids(cfg);isolates = ov::npuw::online::util::getIsolates(cfg);nofolds = ov::npuw::online::util::getNoFolds(cfg);diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hppindex d15e18872df..a5c5e18bb9e 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp@@ -40,6 +40,7 @@ struct PassContext {size_t min_graph_size = 10;size_t keep_blocks = 10;size_t keep_block_size = 10;+ std::unordered_set<std::string> keep_block_tags;std::vector<Avoid> avoids;std::vector<Isolate> isolates;std::vector<std::string> nofolds;@@ -86,6 +87,7 @@ std::vector<std::string> splitByComma(const std::string& s);size_t getMinGraphSize(const ::intel_npu::Config& cfg);size_t getMinRepBlocks(const ::intel_npu::Config& cfg);size_t getMinRepBlockSize(const ::intel_npu::Config& cfg);+std::unordered_set<std::string> getKeepBlockTags(const ::intel_npu::Config& cfg);std::vector<Avoid> getAvoids(const ::intel_npu::Config& cfg);std::vector<Isolate> getIsolates(const ::intel_npu::Config& cfg);std::vector<Isolate> getIsolates(const std::string& isolates_unparsed);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 577bc8d63e3..9b0f5d9280a 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,8 +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_ONLINE_KEEP_BLOCKS_TAGGED").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>(), "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");@@ -293,8 +293,8 @@ TEST_F(GQACompiledModelTest, AppliesFoldOnlyAttnForGenerateStyleModels) {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_ONLINE_KEEP_BLOCKS_TAGGED"), 0u);EXPECT_EQ(call.props.count("NPUW_ATTN"), 0u);- EXPECT_EQ(call.props.count("NPUW_ONLINE_KEEP_BLOCK_SIZE"), 0u);}TEST_F(GQACompiledModelTest, KeepsAttnIsolationDefaultsForPrefillStyleModels) {diff --git a/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cppindex 45625ae1414..a117c07d75c 100644--- a/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp@@ -466,6 +466,27 @@ TEST(PartitioningOptionsTest, FoldOnlyWithIsolatedTagsProducesExpectedSubgraphCoEXPECT_EQ(folded, N);}+TEST(PartitioningOptionsTest, OnlyAttnIsolatedFamiliesIgnoreKeepBlockSizeThreshold) {+ // With KEEP_BLOCK_SIZE=2 and KEEP_BLOCK_TAG=attn, only the attn-tagged+ // single-node repeated family bypasses the size threshold. Non-attn+ // isolated single-node families are left unfrozen and fuse into adjacent remnants.+ constexpr std::size_t N = 30;+ auto ext_cfg = abc_attn_base_cfg;+ ext_cfg["NPUW_ONLINE_KEEP_BLOCK_SIZE"] = "2";+ ext_cfg["NPUW_ONLINE_KEEP_BLOCKS_TAGGED"] = "attn";+ auto cfg = make_cfg(ext_cfg);+ auto partitioning = ov::npuw::getPartitioning(build_abc_attn_model(N), cfg);++ EXPECT_EQ(partitioning.subgraphs.size(), 2u * N);++ std::size_t folded = std::count_if(partitioning.subgraphs.begin(),+ partitioning.subgraphs.end(),+ [](const ov::npuw::Subgraph& sg) {+ return !sg._funcall.empty();+ });+ EXPECT_EQ(folded, N);+}+TEST(PartitioningOptionsTest, FuseUnfoldedMergesNonFoldOnlyRepeatedBlocks) {// With NPUW_FUSE_UNFOLDED, blockA (Relu) and blockB (Sigmoid) groups lose// their reptag and are merged by fuseRemnants (frozen attn blocks act as