Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPUW] Fix Whisper V-tensor optimization with weight sharing (#38120)

Ding Zhang9cd720f9f08b
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index 94407598eb3..2ae9515076c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -104,6 +104,14 @@ bool is_cw_compressed(const std::shared_ptr<ov::Model>& model) {
return false;
}
+bool is_int8_compressed(const std::shared_ptr<ov::Model>& model) {
+ const std::vector<std::string> rt_info_path = {"nncf", "weight_compression", "mode"};
+ if (!model->has_rt_info(rt_info_path)) {
+ return false;
+ }
+ return model->get_rt_info<std::string>(rt_info_path).find("int8") != std::string::npos;
+}
+
struct NPUDesc {
std::string arch;
int64_t max_tiles = 0;
@@ -393,7 +401,7 @@ void update_config_for_whisper(ov::AnyMap& config) {
config.erase("NPUW_SLICE_OUT");
}
-void disable_ws_for_whisper(ov::AnyMap& config) {
+void disable_ws_for_int8_whisper(ov::AnyMap& config) {
config.erase("NPUW_FUNCALL_FOR_ALL");
config.erase("NPUW_FOLD");
config.erase("NPUW_FOLD_ONLY");
@@ -1332,9 +1340,11 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
if (m_is_whisper) {
update_config_for_whisper(prefill_config);
- disable_ws_for_whisper(prefill_config);
- disable_ws_for_whisper(generate_config);
- LOG_INFO("NPUW_FOLD and NPUW_FUNCALL_FOR_ALL are disabled for Whisper model.");
+ if (is_int8_compressed(model)) {
+ disable_ws_for_int8_whisper(prefill_config);
+ disable_ws_for_int8_whisper(generate_config);
+ LOG_INFO("WS is disabled for INT8 Whisper model.");
+ }
}
if (m_is_embedding) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp
index 2004fc785d8..686009fb499 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/optimize_value_tensors.cpp
@@ -5,6 +5,7 @@
#include "optimize_value_tensors.hpp"
#include <transformations/op_conversions/scaled_dot_product_attention_decomposition.hpp>
+#include <unordered_set>
#include "../llm_compiled_model_utils.hpp"
#include "../logging.hpp"
@@ -32,6 +33,7 @@ public:
struct Context {
using Ref = std::reference_wrapper<Context>;
bool bTransposed = false;
+ std::unordered_set<const ov::op::v0::Parameter*> transposed_params;
};
protected:
@@ -41,18 +43,17 @@ protected:
const std::shared_ptr<ov::op::v0::Concat>& matched_concat,
const std::shared_ptr<ov::op::v1::Transpose>& matched_transpose,
const std::shared_ptr<ov::op::v0::MatMul>& matched_matmul) {
- // NB: The same param->concat pair may be matched multiple times when the
- // V-concat output feeds more than one downstream branch (e.g. shared KV-cache
- // across attention layers in Gemma4). Guard the shared-state mutations so they
- // are applied exactly once; per-branch matmul transpose_b is always set.
+ // A V Parameter may feed multiple matched attention branches. Guard its shape mutation
+ // so it is applied exactly once; per-branch updates are always applied.
if (matched_concat->get_axis() != 3u) {
- auto param_shape = matched_param->get_partial_shape();
- NPUW_ASSERT(param_shape.size() == 4u);
- // NB: Transpose Parameter that correspond to V-tensor it will
- // speed-up its multiplication with attention scores
- std::swap(param_shape[2], param_shape[3]);
-
- matched_param->set_partial_shape(param_shape);
+ if (ctx.get().transposed_params.insert(matched_param.get()).second) {
+ auto param_shape = matched_param->get_partial_shape();
+ NPUW_ASSERT(param_shape.size() == 4u);
+ // NB: Transpose Parameter that correspond to V-tensor it will
+ // speed-up its multiplication with attention scores
+ std::swap(param_shape[2], param_shape[3]);
+ matched_param->set_partial_shape(param_shape);
+ }
auto order_cst = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 2, 3, 1});
matched_transpose->set_argument(1, order_cst);
@@ -132,13 +133,16 @@ public:
return false;
}
- auto shape = matched_param->get_partial_shape();
+ const auto shape = matched_param->get_partial_shape();
if (shape.rank().is_dynamic() || shape.rank().get_length() != 4) {
return false;
}
- std::swap(shape[2], shape[3]);
- matched_param->set_partial_shape(shape);
+ if (ctx.get().transposed_params.insert(matched_param.get()).second) {
+ auto transposed_shape = shape;
+ std::swap(transposed_shape[2], transposed_shape[3]);
+ matched_param->set_partial_shape(transposed_shape);
+ }
matched_matmul->set_transpose_b(true);
ctx.get().bTransposed = true;
return true;
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
index 9925e934613..ff5170d67ba 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
@@ -1181,11 +1181,13 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperOptionOptimizesSelfAndCrossAtt
EXPECT_EQ(encoder_value_shape[3], whisper_config.get_encoder_seq_len());
}
-TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperDisablesFoldAndFuncallStageOptions) {
+TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperNonInt8PreservesFoldAndFuncallStageOptions) {
RecordingFactory recorder;
std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+ auto model = build_whisper_decoder_model();
+ model->set_rt_info("int4", "nncf", "weight_compression", "mode");
- ASSERT_NO_THROW(compiled = create_compiled_model(build_whisper_decoder_model(),
+ ASSERT_NO_THROW(compiled = create_compiled_model(model,
{{"NPUW_WHISPER", "YES"},
{"NPUW_WHISPER_EOS_TOKEN", "42"},
{"NPUW_FOLD", "YES"},
@@ -1196,14 +1198,42 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperDisablesFoldAndFuncallStageOpt
const auto& prefill = require_call(recorder, "_prefill");
const auto& generate = require_call_containing(recorder, "_kv");
- expect_missing_prop(prefill.props, "NPUW_FOLD");
- expect_missing_prop(prefill.props, "NPUW_FUNCALL_FOR_ALL");
+ expect_prop(prefill.props, "NPUW_FOLD", "YES");
+ expect_prop(prefill.props, "NPUW_FUNCALL_FOR_ALL", "YES");
expect_prop(prefill.props, "NPUW_WEIGHTS_BANK", "whisper-shared");
- expect_missing_prop(generate.props, "NPUW_FOLD");
- expect_missing_prop(generate.props, "NPUW_FUNCALL_FOR_ALL");
+ expect_prop(generate.props, "NPUW_FOLD", "YES");
+ expect_prop(generate.props, "NPUW_FUNCALL_FOR_ALL", "YES");
expect_prop(generate.props, "NPUW_WEIGHTS_BANK", "whisper-shared");
}
+TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperInt8DisablesWsStageOptions) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+ auto model = build_whisper_decoder_model();
+ model->set_rt_info("int8", "nncf", "weight_compression", "mode");
+
+ ASSERT_NO_THROW(compiled = create_compiled_model(model,
+ {{"NPUW_WHISPER", "YES"},
+ {"NPUW_WHISPER_EOS_TOKEN", "42"},
+ {"NPUW_FOLD", "YES"},
+ {"NPUW_FOLD_ONLY", "YES"},
+ {"NPUW_FUNCALL_FOR_ALL", "YES"},
+ {"NPUW_CWAI", "YES"},
+ {"NPUW_WEIGHTS_BANK", "whisper-shared"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ const auto& prefill = require_call(recorder, "_prefill");
+ const auto& generate = require_call_containing(recorder, "_kv");
+ for (const auto* call : {&prefill, &generate}) {
+ expect_missing_prop(call->props, "NPUW_FOLD");
+ expect_missing_prop(call->props, "NPUW_FOLD_ONLY");
+ expect_missing_prop(call->props, "NPUW_FUNCALL_FOR_ALL");
+ expect_missing_prop(call->props, "NPUW_CWAI");
+ expect_prop(call->props, "NPUW_WEIGHTS_BANK", "whisper-shared");
+ }
+}
+
TEST_F(LLMCompiledModelFactoryOptionsTest, WhisperPreparationAddsKvCacheInputsAndPresentOutputs) {
auto model = build_whisper_decoder_model();
ov::pass::StatefulToStateless().run_on_model(model);
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp
index 4a08ca2e6e5..e95dfdbb909 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/optimize_value_tensors_test.cpp
@@ -7,12 +7,16 @@
#include <gtest/gtest.h>
#include "llm_pass_test_fixture.hpp"
+#include "openvino/op/broadcast.hpp"
+#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/parameter.hpp"
+#include "openvino/op/reshape.hpp"
#include "openvino/op/scaled_dot_product_attention.hpp"
#include "openvino/op/softmax.hpp"
#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
namespace {
@@ -163,4 +167,102 @@ TEST_F(OptimizeValueTensorsPassTest, DirectValuePatternsDoNotDependOnNodeNames)
EXPECT_EQ(prefill_transpose->get_output_partial_shape(0), ov::PartialShape({1, 2, 4, 3}));
}
+TEST_F(OptimizeValueTensorsPassTest, SharedDirectGenerateValueIsTransposedOnce) {
+ auto value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 3, 4});
+ auto scores_0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 5, 3});
+ auto scores_1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 6, 3});
+ auto softmax_0 = std::make_shared<ov::op::v8::Softmax>(scores_0);
+ auto softmax_1 = std::make_shared<ov::op::v8::Softmax>(scores_1);
+ auto matmul_0 = std::make_shared<ov::op::v0::MatMul>(softmax_0, value);
+ auto matmul_1 = std::make_shared<ov::op::v0::MatMul>(softmax_1, value);
+ auto model = std::make_shared<ov::Model>(ov::OutputVector{matmul_0, matmul_1},
+ ov::ParameterVector{value, scores_0, scores_1});
+
+ ASSERT_NO_THROW(ov::npuw::util::OptimizeValueTensors(false, true).run_on_model(model));
+ EXPECT_TRUE(matmul_0->get_transpose_b());
+ EXPECT_TRUE(matmul_1->get_transpose_b());
+ EXPECT_EQ(value->get_partial_shape(), ov::PartialShape({1, 2, 4, 3}));
+}
+
+TEST_F(OptimizeValueTensorsPassTest, SharedConcatGenerateValueIsTransposedOnce) {
+ auto value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 3, 4});
+ ov::OutputVector results;
+ ov::ParameterVector parameters{value};
+ std::vector<std::shared_ptr<ov::op::v0::Concat>> concats;
+ std::vector<std::shared_ptr<ov::op::v0::MatMul>> matmuls;
+
+ for (std::size_t query_length : {5u, 6u}) {
+ auto new_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, 2, 4});
+ auto order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(new_value, order);
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{value, transpose}, 2);
+ auto scores =
+ std::make_shared<ov::op::v0::Parameter>(ov::element::f32,
+ ov::PartialShape{1, 2, static_cast<int64_t>(query_length), 4});
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(scores);
+ auto matmul = std::make_shared<ov::op::v0::MatMul>(softmax, concat);
+
+ results.push_back(matmul);
+ parameters.push_back(new_value);
+ parameters.push_back(scores);
+ concats.push_back(concat);
+ matmuls.push_back(matmul);
+ }
+
+ auto model = std::make_shared<ov::Model>(results, parameters);
+ ASSERT_NO_THROW(ov::npuw::util::OptimizeValueTensors(false).run_on_model(model));
+ EXPECT_EQ(value->get_partial_shape(), ov::PartialShape({1, 2, 4, 3}));
+ for (std::size_t index = 0; index < matmuls.size(); ++index) {
+ EXPECT_EQ(concats[index]->get_axis(), 3u);
+ EXPECT_TRUE(matmuls[index]->get_transpose_b());
+ }
+}
+
+TEST_F(OptimizeValueTensorsPassTest, SharedGqaConcatGenerateValueIsTransposedOnce) {
+ auto value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, 3, 4});
+ ov::OutputVector results;
+ ov::ParameterVector parameters{value};
+ std::vector<std::shared_ptr<ov::op::v0::Concat>> concats;
+ std::vector<std::shared_ptr<ov::op::v3::Broadcast>> broadcasts;
+ std::vector<std::shared_ptr<ov::op::v1::Reshape>> reshapes;
+ std::vector<std::shared_ptr<ov::op::v0::MatMul>> matmuls;
+
+ for (std::size_t query_length : {5u, 6u}) {
+ auto new_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 1, 4});
+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 1, 3});
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(new_value, transpose_order);
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{value, transpose}, 2);
+ auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(concat, unsqueeze_axes);
+ auto broadcast_shape = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {1, 1, 2, 5, 4});
+ auto broadcast =
+ std::make_shared<ov::op::v3::Broadcast>(unsqueeze, broadcast_shape, ov::op::BroadcastType::BIDIRECTIONAL);
+ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {1, 2, 5, 4});
+ auto reshape = std::make_shared<ov::op::v1::Reshape>(broadcast, reshape_shape, false);
+ auto scores =
+ std::make_shared<ov::op::v0::Parameter>(ov::element::f32,
+ ov::PartialShape{1, 2, static_cast<int64_t>(query_length), 5});
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(scores);
+ auto matmul = std::make_shared<ov::op::v0::MatMul>(softmax, reshape);
+
+ results.push_back(matmul);
+ parameters.push_back(new_value);
+ parameters.push_back(scores);
+ concats.push_back(concat);
+ broadcasts.push_back(broadcast);
+ reshapes.push_back(reshape);
+ matmuls.push_back(matmul);
+ }
+
+ auto model = std::make_shared<ov::Model>(results, parameters);
+ ASSERT_NO_THROW(ov::npuw::util::OptimizeValueTensors(false).run_on_model(model));
+ EXPECT_EQ(value->get_partial_shape(), ov::PartialShape({1, 1, 4, 3}));
+ for (std::size_t index = 0; index < matmuls.size(); ++index) {
+ EXPECT_EQ(concats[index]->get_axis(), 3u);
+ EXPECT_EQ(broadcasts[index]->get_output_partial_shape(0), ov::PartialShape({1, 1, 2, 4, 5}));
+ EXPECT_EQ(reshapes[index]->get_output_partial_shape(0), ov::PartialShape({1, 2, 4, 5}));
+ EXPECT_TRUE(matmuls[index]->get_transpose_b());
+ }
+}
+
} // namespace