Skip to content

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

[CPU]Support "dynamic" KV cache precision for PA (#29018)

Zhang Yib194730c10c1
diff --git a/src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp b/src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp
new file mode 100644
index 00000000000..8d7d217eb38
--- /dev/null
+++ b/src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp
@@ -0,0 +1,48 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/core/type/element_type.hpp"
+#include "openvino/pass/graph_rewrite.hpp"
+#include "transformations_visibility.hpp"
+
+namespace ov {
+namespace pass {
+class TRANSFORMATIONS_API ConvertPagedAttnInputs;
+
+/**
+ * @ingroup ov_transformation_common_api
+ * @brief Set precision and shape of KV cache in PagedAttn op based runtime options
+ */
+
+class ConvertPagedAttnInputs : public ov::pass::MatcherPass {
+public:
+ struct KVCacheConfig {
+ ov::element::Type keyCachePrecision;
+ ov::element::Type valueCachePrecision;
+ ov::element::Type inferencePrecision;
+ size_t keyCacheBlockSize = 32;
+ size_t valueCacheBlockSize = 32;
+ size_t keyCacheGroupSize = 0;
+ size_t valueCacheGroupSize = 0;
+ bool keyCacheQuantBychannel = false;
+ bool valueCacheQuantBychannel = false;
+ std::vector<size_t> keyCacheDimOrder = {0, 1, 2, 3};
+ std::vector<size_t> valueCacheDimOrder = {0, 1, 2, 3};
+ };
+
+ OPENVINO_MATCHER_PASS_RTTI("ConvertPagedAttnInputs");
+ ConvertPagedAttnInputs(const KVCacheConfig& config);
+
+ void setKVCacheConfig(const KVCacheConfig& config);
+
+ const KVCacheConfig& getKVCacheConfig() const;
+
+private:
+ KVCacheConfig m_config;
+};
+
+} // namespace pass
+} // namespace ov
diff --git a/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp b/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp
new file mode 100644
index 00000000000..5f797fb6b28
--- /dev/null
+++ b/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp
@@ -0,0 +1,156 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
+
+#include <cstdint>
+#include <memory>
+#include <transformations/utils/gen_pattern.hpp>
+
+#include "itt.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/paged_attention.hpp"
+#include "openvino/util/log.hpp"
+#include "transformations/utils/utils.hpp"
+using namespace ov::gen_pattern;
+
+ov::pass::ConvertPagedAttnInputs::ConvertPagedAttnInputs(const KVCacheConfig& config) : m_config(config) {
+ MATCHER_SCOPE(ConvertPagedAttnInputs);
+
+ auto Q = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto K = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto V = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto key_cache_0 = makePattern<ov::op::v0::Parameter>({});
+ auto value_cache_0 = makePattern<ov::op::v0::Parameter>({});
+ auto past_lens = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto subsequence_begins = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto block_indices = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto block_indices_begins = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto scale = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto sliding_window = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto alibi_slopes = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto max_context_len = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto rotated_block_indices = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto rotation_deltas = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+ auto rotation_trig_lut = ov::pass::pattern::any_input(ov::pass::pattern::has_static_rank());
+
+ auto pa_1 = makePattern<op::PagedAttentionExtension>({Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ scale,
+ sliding_window,
+ alibi_slopes,
+ max_context_len});
+
+ auto pa_2 = makePattern<op::PagedAttentionExtension>({Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ scale,
+ sliding_window,
+ alibi_slopes,
+ max_context_len,
+ rotated_block_indices,
+ rotation_deltas,
+ rotation_trig_lut});
+ auto result = pa_1 | pa_2;
+ ov::matcher_pass_callback callback = [=](ov::pass::pattern::Matcher& m) {
+ const auto pa_op = m.get_match_root();
+ auto key_cache = ov::as_type_ptr<ov::op::v0::Parameter>(pa_op->get_input_node_shared_ptr(3));
+ auto value_cache = ov::as_type_ptr<ov::op::v0::Parameter>(pa_op->get_input_node_shared_ptr(4));
+ auto format_cache_precision = [](ov::element::Type cache_precision, ov::element::Type infer_precision) {
+ return cache_precision == ov::element::f16 && infer_precision == ov::element::bf16 ? infer_precision
+ : cache_precision;
+ };
+ auto init_cache_shape = [&](const size_t head_nums,
+ const size_t head_size,
+ const size_t block_size,
+ const ov::element::Type precision,
+ const size_t group_size,
+ const bool bychannel,
+ const std::vector<size_t>& orders) {
+ size_t _block_size = block_size;
+ ov::Dimension::value_type _head_nums = head_nums;
+ ov::Dimension::value_type _head_size = head_size;
+ ov::Dimension::value_type _group_size = group_size;
+ _group_size = _group_size ? _group_size : _head_size;
+ if (!bychannel) {
+ if (_head_size % _group_size != 0) {
+ OPENVINO_THROW("cache head_size ", head_size, "cannot be divided by group_size ", group_size);
+ }
+ }
+ size_t group_num = _head_size / _group_size;
+ if (precision == ov::element::u8) {
+ if (bychannel) {
+ _block_size += 2 * sizeof(float);
+ } else {
+ _head_size += sizeof(float) * 2 * group_num;
+ }
+ } else if (precision == ov::element::u4) {
+ _head_size += sizeof(float) * 2 * group_num * 2;
+ }
+ auto block_shape = ov::PartialShape::dynamic(4);
+
+ block_shape[orders[0]] = -1;
+ block_shape[orders[1]] = _head_nums;
+ block_shape[orders[2]] = _block_size;
+ block_shape[orders[3]] = _head_size;
+
+ return block_shape;
+ };
+ auto key_cache_precision = format_cache_precision(m_config.keyCachePrecision, m_config.inferencePrecision);
+ auto value_cache_precision = format_cache_precision(m_config.valueCachePrecision, m_config.inferencePrecision);
+ key_cache->set_element_type(key_cache_precision);
+ value_cache->set_element_type(value_cache_precision);
+ if (!pa_op->get_rt_info().count("num_k_heads") || !pa_op->get_rt_info().count("k_head_size") ||
+ !pa_op->get_rt_info().count("num_v_heads") || !pa_op->get_rt_info().count("num_v_heads")) {
+ OPENVINO_DEBUG("PagedAttn ",
+ pa_op->get_friendly_name(),
+ " doesn't have rtinfo for num_k_heads/k_head_size/num_v_heads/num_v_heads");
+ return false;
+ }
+ const auto key_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_k_heads"].as<size_t>(),
+ pa_op->get_rt_info()["k_head_size"].as<size_t>(),
+ m_config.keyCacheBlockSize,
+ key_cache_precision,
+ m_config.keyCacheGroupSize,
+ m_config.keyCacheQuantBychannel,
+ m_config.keyCacheDimOrder);
+ const auto value_cache_shape = init_cache_shape(pa_op->get_rt_info()["num_v_heads"].as<size_t>(),
+ pa_op->get_rt_info()["v_head_size"].as<size_t>(),
+ m_config.valueCacheBlockSize,
+ value_cache_precision,
+ m_config.valueCacheGroupSize,
+ m_config.valueCacheQuantBychannel,
+ m_config.valueCacheDimOrder);
+
+ key_cache->set_partial_shape(key_cache_shape);
+ value_cache->set_partial_shape(value_cache_shape);
+ return true;
+ };
+
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(result, matcher_name);
+ this->register_matcher(m, callback);
+}
+
+void ov::pass::ConvertPagedAttnInputs::setKVCacheConfig(const KVCacheConfig& config) {
+ m_config = config;
+}
+
+const ov::pass::ConvertPagedAttnInputs::KVCacheConfig& ov::pass::ConvertPagedAttnInputs::getKVCacheConfig() const {
+ return m_config;
+}
diff --git a/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp b/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp
new file mode 100644
index 00000000000..6b0e7bcd481
--- /dev/null
+++ b/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp
@@ -0,0 +1,280 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/op/paged_attention.hpp"
+#include "openvino/runtime/properties.hpp"
+#include "transformations/utils/gen_pattern.hpp"
+
+using namespace ov::test;
+using namespace ov;
+using namespace ov::op;
+using namespace ov::gen_pattern;
+using ConvertPagedAttnInputsParams = std::tuple<std::vector<ov::element::Type>, // cache_precision
+ std::vector<size_t>, // cache_group_size
+ std::vector<size_t>, // block_size
+ ov::element::Type, // infer_precsion
+ bool, // quant_key_by_channel
+ bool, // accuracy_mode
+ bool // is_ir_kv_cache_f16
+ >;
+namespace {
+class ConvertPagedAttnInputsTest : public TransformationTestsF,
+ public testing::WithParamInterface<ConvertPagedAttnInputsParams> {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<ConvertPagedAttnInputsParams>& obj) {
+ std::vector<ov::element::Type> cachePrecision;
+ std::vector<size_t> cacheGroupSize;
+ std::vector<size_t> blcokSize;
+ bool quantKeybychannel;
+ bool isAccuracyMode;
+ bool isIRKVCacheF16;
+ ov::element::Type inferPrec;
+ std::tie(cachePrecision,
+ cacheGroupSize,
+ blcokSize,
+ inferPrec,
+ quantKeybychannel,
+ isAccuracyMode,
+ isIRKVCacheF16) = obj.param;
+ std::ostringstream result;
+ result << "KeyPrc=" << cachePrecision[0] << "_";
+ result << "ValuePrc=" << cachePrecision[1] << "_";
+ result << "KeyBlockSize=" << blcokSize[0] << "_";
+ result << "ValueBlockSize=" << blcokSize[1] << "_";
+ result << "InferPrec=" << inferPrec << "_";
+ result << "KeyGS=" << cacheGroupSize[0] << "_";
+ result << "ValueGS=" << cacheGroupSize[1] << "_";
+ result << "KeyByChannel=" << quantKeybychannel << "_";
+ result << "isAccuracyMode=" << isAccuracyMode << "_";
+ result << "isIRKVCacheF16=" << isIRKVCacheF16;
+ return result.str();
+ }
+
+public:
+ size_t keyHeadSize;
+ size_t valueHeadSize;
+ size_t numKeyHeads;
+ size_t numValueHeads;
+ ov::element::Type inferPrec;
+ ov::element::Type keyCachePrecision;
+ ov::element::Type valueCachePrecision;
+ std::vector<size_t> blockSize;
+ size_t keyCacheGroupSize;
+ size_t valueCacheGroupSize;
+ bool quantKeybychannel;
+ bool isAccuracyMode;
+ bool isIRKVCacheF16;
+};
+
+TEST_P(ConvertPagedAttnInputsTest, checkPrecisionAndShape) {
+ std::vector<ov::element::Type> cachePrecision;
+ std::vector<size_t> cacheGroupSize;
+ std::tie(cachePrecision, cacheGroupSize, blockSize, inferPrec, quantKeybychannel, isAccuracyMode, isIRKVCacheF16) =
+ this->GetParam();
+ keyCachePrecision = cachePrecision[0];
+ valueCachePrecision = cachePrecision[1];
+ keyCacheGroupSize = cacheGroupSize[0];
+ valueCacheGroupSize = cacheGroupSize[1];
+ numKeyHeads = 2;
+ keyHeadSize = 32;
+
+ numValueHeads = 2;
+ valueHeadSize = 64;
+ {
+ auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
+ auto K = std::make_shared<v0::Parameter>(
+ ov::element::f32,
+ PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
+ auto V = std::make_shared<v0::Parameter>(
+ ov::element::f32,
+ PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
+ auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
+ auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto key_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
+ auto value_cache_0 = std::make_shared<v0::Parameter>(ov::element::dynamic, PartialShape::dynamic(4));
+ auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
+ auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
+ auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
+
+ auto pa = std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ scale,
+ sliding_window,
+ alibi_slopes,
+ max_context_len});
+ pa->get_rt_info()["num_k_heads"] = numKeyHeads;
+ pa->get_rt_info()["k_head_size"] = keyHeadSize;
+ pa->get_rt_info()["num_v_heads"] = numValueHeads;
+ pa->get_rt_info()["v_head_size"] = valueHeadSize;
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{pa},
+ ov::ParameterVector{Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ max_context_len});
+ if (isIRKVCacheF16) {
+ model->set_rt_info("f16", "runtime_options", ov::hint::kv_cache_precision.name());
+ }
+ }
+
+ {
+ auto getCachePrec = [&](ov::element::Type tensorPrec) {
+ tensorPrec =
+ tensorPrec == ov::element::f16 && inferPrec == ov::element::bf16 ? ov::element::bf16 : tensorPrec;
+ if (isAccuracyMode)
+ tensorPrec = ov::element::f32;
+ return tensorPrec;
+ };
+ auto getCacheShape = [&](ov::element::Type cachePrec,
+ size_t headNums,
+ size_t headSize,
+ size_t groupSize,
+ size_t blockSize,
+ bool quantBychannel) {
+ auto targeShape = PartialShape::dynamic(4);
+ targeShape[1] = headNums;
+ groupSize = groupSize ? groupSize : headSize;
+ const size_t paramSize = 2 * sizeof(float) * 8 / cachePrec.bitwidth();
+ if (!cachePrec.is_integral()) {
+ targeShape[2] = blockSize;
+ targeShape[3] = headSize;
+ } else if (quantBychannel) {
+ targeShape[2] = paramSize + blockSize;
+ targeShape[3] = headSize;
+ } else {
+ size_t groupNum = headSize / groupSize;
+ targeShape[2] = blockSize;
+ targeShape[3] = headSize + paramSize * groupNum;
+ }
+ return targeShape;
+ };
+
+ keyCachePrecision = getCachePrec(keyCachePrecision);
+ valueCachePrecision = getCachePrec(valueCachePrecision);
+ auto Q = std::make_shared<v0::Parameter>(ov::element::f32, PartialShape{-1, 4 * 32});
+ auto K = std::make_shared<v0::Parameter>(
+ ov::element::f32,
+ PartialShape{-1, static_cast<ov::Dimension::value_type>(numKeyHeads * keyHeadSize)});
+ auto V = std::make_shared<v0::Parameter>(
+ ov::element::f32,
+ PartialShape{-1, static_cast<ov::Dimension::value_type>(numValueHeads * valueHeadSize)});
+ auto max_context_len = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{});
+ auto block_indices_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto block_indices = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto subsequence_begins = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto past_lens = std::make_shared<v0::Parameter>(ov::element::i32, PartialShape{DYN});
+ auto key_cache_0 = std::make_shared<v0::Parameter>(keyCachePrecision,
+ getCacheShape(keyCachePrecision,
+ numKeyHeads,
+ keyHeadSize,
+ keyCacheGroupSize,
+ blockSize[0],
+ quantKeybychannel));
+ auto value_cache_0 = std::make_shared<v0::Parameter>(
+ valueCachePrecision,
+ getCacheShape(valueCachePrecision, numKeyHeads, valueHeadSize, valueCacheGroupSize, blockSize[1], false));
+ auto scale = std::make_shared<v0::Constant>(element::f32, Shape{}, 0.5f);
+ auto sliding_window = std::make_shared<v0::Constant>(element::i32, Shape{}, 0);
+ auto alibi_slopes = std::make_shared<v0::Constant>(element::f32, Shape{0});
+
+ auto pa = std::make_shared<op::PagedAttentionExtension>(OutputVector{Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ scale,
+ sliding_window,
+ alibi_slopes,
+ max_context_len});
+ pa->get_rt_info()["num_k_heads"] = numKeyHeads;
+ pa->get_rt_info()["k_head_size"] = keyHeadSize;
+ pa->get_rt_info()["num_v_heads"] = numValueHeads;
+ pa->get_rt_info()["v_head_size"] = valueHeadSize;
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{pa},
+ ov::ParameterVector{Q,
+ K,
+ V,
+ key_cache_0,
+ value_cache_0,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ max_context_len});
+ }
+ ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
+ cacheConfig.keyCacheBlockSize = blockSize[0];
+ cacheConfig.valueCacheBlockSize = blockSize[1];
+ if (isAccuracyMode) {
+ cacheConfig.inferencePrecision = ov::element::f32;
+ cacheConfig.keyCachePrecision = ov::element::f32;
+ cacheConfig.valueCachePrecision = ov::element::f32;
+ } else {
+ cacheConfig.keyCachePrecision = keyCachePrecision;
+ cacheConfig.valueCachePrecision = valueCachePrecision;
+ cacheConfig.keyCacheGroupSize = keyCacheGroupSize;
+ cacheConfig.valueCacheGroupSize = valueCacheGroupSize;
+ }
+
+ manager.register_pass<ov::pass::ConvertPagedAttnInputs>(cacheConfig);
+ comparator.disable(FunctionsComparator::ACCURACY);
+ comparator.disable(FunctionsComparator::RUNTIME_KEYS);
+ disable_result_friendly_names_check();
+ disable_rt_info_check();
+}
+
+std::vector<std::vector<ov::element::Type>> get_cache_prec() {
+ return {
+ {ov::element::f32, ov::element::f32},
+ {ov::element::f16, ov::element::f16},
+ {ov::element::u8, ov::element::u8},
+ {ov::element::u8, ov::element::u4},
+ };
+}
+
+// group size
+const std::vector<std::vector<size_t>> cache_gs = {{32, 16}, {0, 0}};
+
+// block size
+const std::vector<std::vector<size_t>> cache_bs = {{32, 16}};
+
+INSTANTIATE_TEST_SUITE_P(smoke_ConvertPagedAttnInputsTest,
+ ConvertPagedAttnInputsTest,
+ ::testing::Combine(::testing::ValuesIn(get_cache_prec()),
+ ::testing::ValuesIn(cache_gs),
+ ::testing::ValuesIn(cache_bs),
+ ::testing::Values(ov::element::f32, ov::element::bf16),
+ ::testing::Values(false),
+ ::testing::Values(true, false),
+ ::testing::Values(true, false)),
+ ConvertPagedAttnInputsTest::getTestCaseName);
+
+} // namespace
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
index dbb896b37fe..fd10472e740 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
@@ -2330,8 +2330,8 @@ struct AttentionExecutor : public PagedAttentionExecutor {
void concat_pastkv(const PlainTensor& k,
const PlainTensor& v,
- const PlainTensor& k_cache,
- const PlainTensor& v_cache,
+ PlainTensor& k_cache,
+ PlainTensor& v_cache,
const PlainTensor& past_lens,
const PlainTensor& subsequence_begins,
const PlainTensor& block_indices,
@@ -2370,8 +2370,17 @@ struct AttentionExecutor : public PagedAttentionExecutor {
auto SV = v_cache.m_dims[3];
auto Hk = k_cache.m_dims[1]; // shape: [block, H, 32, S]
parallel_for2d(Hk, zero_tokens, [&](size_t h, size_t l) {
- std::memset(k_cache.ptr_v(block_number, h, block_offset + l, 0), 0, S * k_cache.m_element_size);
- std::memset(v_cache.ptr_v(block_number, h, block_offset + l, 0), 0, SV * v_cache.m_element_size);
+ auto set_zero =
+ [](PlainTensor& cache, size_t block_number, size_t h, size_t l, size_t hidden_dims) {
+ auto sub_byte_multiplier = get_sub_byte_multiplier(cache.get_precision());
+ size_t cache_stride =
+ (block_number * cache.stride(0) + h * cache.stride(1) + l * cache.stride(2)) *
+ cache.get_precision().size() / sub_byte_multiplier;
+ auto cache_ptr = cache.m_ptr.get() + cache_stride;
+ std::memset(cache_ptr, 0, hidden_dims * cache.m_element_size / sub_byte_multiplier);
+ };
+ set_zero(k_cache, block_number, h, block_offset + l, S);
+ set_zero(v_cache, block_number, h, block_offset + l, SV);
});
}
}
diff --git a/src/plugins/intel_cpu/src/plugin.cpp b/src/plugins/intel_cpu/src/plugin.cpp
index b93dd62d695..8817e4a0054 100644
--- a/src/plugins/intel_cpu/src/plugin.cpp
+++ b/src/plugins/intel_cpu/src/plugin.cpp
@@ -218,25 +218,14 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
// verification of supported input
for (const auto& ii : model->inputs()) {
auto input_precision = ii.get_element_type();
- static const std::set<ov::element::Type_t> supported_precisions = {ov::element::Type_t::u4,
- ov::element::Type_t::i4,
- ov::element::Type_t::u8,
- ov::element::Type_t::i8,
- ov::element::Type_t::f8e4m3,
- ov::element::Type_t::f8e5m2,
- ov::element::Type_t::u16,
- ov::element::Type_t::i16,
- ov::element::Type_t::u32,
- ov::element::Type_t::i32,
- ov::element::Type_t::u64,
- ov::element::Type_t::i64,
- ov::element::Type_t::bf16,
- ov::element::Type_t::f16,
- ov::element::Type_t::f32,
- ov::element::Type_t::f64,
- ov::element::Type_t::boolean,
- ov::element::Type_t::string,
- ov::element::Type_t::nf4};
+ static const std::set<ov::element::Type_t> supported_precisions = {
+ ov::element::Type_t::u4, ov::element::Type_t::i4, ov::element::Type_t::u8,
+ ov::element::Type_t::i8, ov::element::Type_t::f8e4m3, ov::element::Type_t::f8e5m2,
+ ov::element::Type_t::u16, ov::element::Type_t::i16, ov::element::Type_t::u32,
+ ov::element::Type_t::i32, ov::element::Type_t::u64, ov::element::Type_t::i64,
+ ov::element::Type_t::bf16, ov::element::Type_t::f16, ov::element::Type_t::f32,
+ ov::element::Type_t::f64, ov::element::Type_t::boolean, ov::element::Type_t::string,
+ ov::element::Type_t::nf4, ov::element::Type_t::dynamic};
if (!supported_precisions.count(input_precision)) {
OPENVINO_THROW_NOT_IMPLEMENTED("CPU plugin: Input image format ",
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index f512640513b..1c16f3f52c2 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -27,6 +27,7 @@
#include "transformations/common_optimizations/add_fake_quantize_fusion.hpp"
#include "transformations/common_optimizations/augru_cell_fusion.hpp"
#include "transformations/common_optimizations/common_optimizations.hpp"
+#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
#include "transformations/common_optimizations/convert_quantize_dequantize.hpp"
#include "transformations/common_optimizations/fq_mul_fusion.hpp"
#include "transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp"
@@ -446,6 +447,20 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
CPU_REGISTER_PASS_COMMON(manager, ov::pass::AUGRUCellFusion);
CPU_REGISTER_PASS_COMMON(manager, SDPASubgraphFusion);
+ ov::pass::ConvertPagedAttnInputs::KVCacheConfig cacheConfig;
+ cacheConfig.keyCachePrecision = config.keyCachePrecision;
+ cacheConfig.valueCachePrecision = config.valueCachePrecision;
+ cacheConfig.inferencePrecision = config.inferencePrecision;
+ cacheConfig.keyCacheGroupSize = config.keyCacheGroupSize;
+ cacheConfig.valueCacheGroupSize = config.valueCacheGroupSize;
+ cacheConfig.keyCacheBlockSize = 32;
+ cacheConfig.valueCacheBlockSize = 32;
+ // TODO enable quant_by_channel when available.
+ cacheConfig.keyCacheQuantBychannel = false;
+ cacheConfig.valueCacheQuantBychannel = false;
+ cacheConfig.keyCacheDimOrder = {0, 1, 2, 3};
+ cacheConfig.valueCacheDimOrder = {0, 1, 2, 3};
+ CPU_REGISTER_PASS_COMMON(manager, ov::pass::ConvertPagedAttnInputs, cacheConfig);
CPU_REGISTER_PASS_COMMON(manager, ov::pass::CommonOptimizations);
CPU_REGISTER_PASS_X64(manager, ov::pass::KeepConstPrecision, decompression_precisions, false, true);
CPU_SET_CALLBACK_X64(