Skip to content

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

[NPUW] Align DynamicQuantize group sizes with KV-cache reduction axes (#37374)

Ding Zhang98d61c81552a
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/kv_cache_compressed.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/kv_cache_compressed.cpp
index 9bd57d0d04f..8fea041173b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/kv_cache_compressed.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/kv_cache_compressed.cpp
@@ -464,6 +464,12 @@ void ov::npuw::run_kv_cache_dynamic_quantization_passes(const std::shared_ptr<ov
auto rank = dq_input.get_partial_shape().size();
std::vector<uint64_t> shape_group_size(rank, 1);
+ constexpr auto wholeDim = std::numeric_limits<uint64_t>::max();
+ constexpr size_t keyReductionAxis = 3;
+ constexpr size_t valueReductionAxis = 2;
+ const auto reductionAxis = is_key ? keyReductionAxis : valueReductionAxis;
+ OPENVINO_ASSERT(reductionAxis < rank, "KV cache DynamicQuantize input rank must be at least 4, got {0}", rank);
+ shape_group_size[reductionAxis] = wholeDim;
ov::op::internal::DynamicQuantize::Attributes dq_config;
dq_config.quantization_dt = cfg.quantization_dt;
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/kv_cache_compression_pass_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/kv_cache_compression_pass_test.cpp
index db390969039..5b67e1fb5aa 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/kv_cache_compression_pass_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/kv_cache_compression_pass_test.cpp
@@ -704,6 +704,17 @@ TEST_P(KVCacheMultiStepDecodeTest, TransformedModelIoTypesMatchQuantConfig) {
<< "Result '" << name << "' has unexpected type";
};
+ auto expect_result_shape = [&](const std::string& name, const Shape& expected) {
+ const auto it = std::find_if(xform_model->get_results().begin(),
+ xform_model->get_results().end(),
+ [&](const auto& res) {
+ return res->get_friendly_name() == name;
+ });
+ ASSERT_NE(it, xform_model->get_results().end()) << "Missing result: " << name;
+ EXPECT_EQ((*it)->get_input_shape(0), expected)
+ << "Result '" << name << "' has unexpected shape";
+ };
+
auto expect_result_absent = [&](const std::string& name) {
const auto it = std::find_if(xform_model->get_results().begin(),
xform_model->get_results().end(),
@@ -734,6 +745,12 @@ TEST_P(KVCacheMultiStepDecodeTest, TransformedModelIoTypesMatchQuantConfig) {
expect_result_type("DynamicQuantize/0/present/key/scale", element::f32);
expect_result_type("DynamicQuantize/0/present/value/scale", element::f32);
+ // KV-cache DynamicQuantize reduces key over axis 3 and value over axis 2.
+ // The resulting scale shapes verify the corresponding WHOLE_DIM group-size
+ // entries {1, 1, 1, UINT64_MAX} and {1, 1, UINT64_MAX, 1}.
+ expect_result_shape("DynamicQuantize/0/present/key/scale", Shape{1, 1, WINDOW + 1, 1});
+ expect_result_shape("DynamicQuantize/0/present/value/scale", Shape{1, 1, 1, WINDOW + 1});
+
if (key_asym) {
expect_result_type("DynamicQuantize/0/present/key/zp", p.key_dt);
} else {