Skip to content

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

[TRANSFORMATIONS] Move convert_pagedattn_inputs to paged_attention dir (#35471)

Mikhail Ryzhov2699faec78c4
diff --git a/src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp b/src/common/transformations/include/transformations/paged_attention/convert_pagedattn_inputs.hpp
similarity index 98%
rename from src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp
rename to src/common/transformations/include/transformations/paged_attention/convert_pagedattn_inputs.hpp
index 9e7d945cf87..971ad7b4300 100644
--- a/src/common/transformations/include/transformations/common_optimizations/convert_pagedattn_inputs.hpp
+++ b/src/common/transformations/include/transformations/paged_attention/convert_pagedattn_inputs.hpp
@@ -52,4 +52,4 @@ private:
};
} // namespace pass
-} // namespace ov
+} // namespace ov
\ No newline at end of file
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
deleted file mode 100644
index 403ae25362b..00000000000
--- a/src/common/transformations/src/transformations/common_optimizations/convert_pagedattn_inputs.cpp
+++ /dev/null
@@ -1,184 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
-
-#include <cstdint>
-#include <memory>
-
-#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/pass/pattern/op/wrap_type.hpp"
-#include "openvino/util/log.hpp"
-#include "transformations/utils/utils.hpp"
-
-using namespace ov::pass;
-
-namespace v0 = ov::op::v0;
-
-namespace ov::pass {
-
-ConvertPagedAttnInputs::ConvertPagedAttnInputs(const KVCacheConfig& config,
- UpdateShapeFunc func,
- UpdatePrecisionFunc update_precision_func)
- : m_config(config),
- m_update_shape_func(std::move(func)),
- m_update_precision_func(std::move(update_precision_func)) {
- MATCHER_SCOPE(ConvertPagedAttnInputs);
-
- auto Q = pattern::any_input(pattern::has_static_rank());
- auto K = pattern::any_input(pattern::has_static_rank());
- auto V = pattern::any_input(pattern::has_static_rank());
- auto key_cache_0 = pattern::wrap_type<v0::Parameter>({});
- auto value_cache_0 = pattern::wrap_type<v0::Parameter>({});
- auto past_lens = pattern::any_input(pattern::has_static_rank());
- auto subsequence_begins = pattern::any_input(pattern::has_static_rank());
- auto block_indices = pattern::any_input(pattern::has_static_rank());
- auto block_indices_begins = pattern::any_input(pattern::has_static_rank());
- auto scale = pattern::any_input(pattern::has_static_rank());
- auto sliding_window = pattern::any_input(pattern::has_static_rank());
- auto alibi_slopes = pattern::any_input(pattern::has_static_rank());
- auto max_context_len = pattern::any_input(pattern::has_static_rank());
- auto score_aggregation_window = pattern::any_input(pattern::has_static_rank());
- auto rotated_block_indices = pattern::any_input(pattern::has_static_rank());
- auto rotation_deltas = pattern::any_input(pattern::has_static_rank());
- auto rotation_trig_lut = pattern::any_input(pattern::has_static_rank());
- auto xattention_threshold = pattern::any_input(pattern::has_static_rank());
- auto xattention_block_size = pattern::any_input(pattern::has_static_rank());
- auto xattention_stride = pattern::any_input(pattern::has_static_rank());
- auto sinks = pattern::any_input(pattern::has_static_rank());
- auto adaptive_rkv_start_size = pattern::any_input(pattern::has_static_rank());
- auto adaptive_rkv_evictable_sizes = pattern::any_input(pattern::has_static_rank());
- auto adaptive_rkv_diversity_block_set_indices = pattern::any_input(pattern::has_static_rank());
- auto adaptive_rkv_diversity_block_set_indices_begins = pattern::any_input(pattern::has_static_rank());
- auto token_type_ids = pattern::any_input(pattern::has_static_rank());
-
- auto qq_bias = pattern::any_input(pattern::has_static_rank());
- auto qq_bias_begins = pattern::any_input(pattern::has_static_rank());
- auto result = pattern::wrap_type<ov::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,
- score_aggregation_window,
- rotated_block_indices,
- rotation_deltas,
- rotation_trig_lut,
- xattention_threshold,
- xattention_block_size,
- xattention_stride,
- sinks,
- adaptive_rkv_start_size,
- adaptive_rkv_evictable_sizes,
- adaptive_rkv_diversity_block_set_indices,
- adaptive_rkv_diversity_block_set_indices_begins,
- token_type_ids,
- qq_bias,
- qq_bias_begins});
- ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](pattern::Matcher& m) {
- const auto pa_op = m.get_match_root();
- auto key_cache = ov::as_type_ptr<v0::Parameter>(pa_op->get_input_node_shared_ptr(3));
- auto value_cache = ov::as_type_ptr<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) {
- ov::Dimension::value_type _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;
- // Update head_size and block_size by precision and quantizing channel mode
- m_update_shape_func(precision, bychannel, group_num, _head_size, _block_size);
-
- 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);
- bool status = false;
- 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("v_head_size")) {
- 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);
- status = true;
- } else {
- 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");
- status = false;
- }
-
- if (m_update_precision_func) {
- m_update_precision_func(key_cache_precision);
- m_update_precision_func(value_cache_precision);
- key_cache->set_element_type(key_cache_precision);
- value_cache->set_element_type(value_cache_precision);
- }
-
- key_cache->validate_and_infer_types();
- value_cache->validate_and_infer_types();
- return status;
- };
-
- auto m = std::make_shared<pattern::Matcher>(result, matcher_name);
- this->register_matcher(m, callback);
-}
-
-void ConvertPagedAttnInputs::setKVCacheConfig(const KVCacheConfig& config) {
- m_config = config;
-}
-
-const ConvertPagedAttnInputs::KVCacheConfig& ConvertPagedAttnInputs::getKVCacheConfig() const {
- return m_config;
-}
-
-} // namespace ov::pass
diff --git a/src/common/transformations/src/transformations/paged_attention/convert_pagedattn_inputs.cpp b/src/common/transformations/src/transformations/paged_attention/convert_pagedattn_inputs.cpp
new file mode 100644
index 00000000000..84fa60b0268
--- /dev/null
+++ b/src/common/transformations/src/transformations/paged_attention/convert_pagedattn_inputs.cpp
@@ -0,0 +1,136 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/paged_attention/convert_pagedattn_inputs.hpp"
+
+#include <cstdint>
+#include <memory>
+
+#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/pass/pattern/op/wrap_type.hpp"
+#include "openvino/util/log.hpp"
+#include "transformations/utils/utils.hpp"
+
+using namespace ov::pass;
+
+namespace v0 = ov::op::v0;
+
+namespace ov::pass {
+
+ConvertPagedAttnInputs::ConvertPagedAttnInputs(const KVCacheConfig& config,
+ UpdateShapeFunc func,
+ UpdatePrecisionFunc update_precision_func)
+ : m_config(config),
+ m_update_shape_func(std::move(func)),
+ m_update_precision_func(std::move(update_precision_func)) {
+ MATCHER_SCOPE(ConvertPagedAttnInputs);
+
+ auto result = pattern::wrap_type<ov::op::PagedAttentionExtension>();
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](pattern::Matcher& m) {
+ const auto root = m.get_match_root();
+ bool status = false;
+
+ if (const auto pa_op = ov::as_type_ptr<ov::op::PagedAttentionExtension>(root)) {
+ auto key_cache = ov::as_type_ptr<v0::Parameter>(pa_op->get_input_node_shared_ptr(3));
+ auto value_cache = ov::as_type_ptr<v0::Parameter>(pa_op->get_input_node_shared_ptr(4));
+ if (!key_cache || !value_cache) {
+ return false;
+ }
+ 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) {
+ ov::Dimension::value_type _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;
+ // Update head_size and block_size by precision and quantizing channel mode
+ m_update_shape_func(precision, bychannel, group_num, _head_size, _block_size);
+
+ 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("v_head_size")) {
+ 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);
+ status = true;
+ } else {
+ OPENVINO_DEBUG("PagedAttn ",
+ pa_op->get_friendly_name(),
+ " doesn't have rtinfo for num_k_heads/k_head_size/num_v_heads/v_head_size");
+ status = false;
+ }
+
+ if (m_update_precision_func) {
+ m_update_precision_func(key_cache_precision);
+ m_update_precision_func(value_cache_precision);
+ key_cache->set_element_type(key_cache_precision);
+ value_cache->set_element_type(value_cache_precision);
+ }
+
+ key_cache->validate_and_infer_types();
+ value_cache->validate_and_infer_types();
+ }
+
+ return status;
+ };
+
+ auto m = std::make_shared<pattern::Matcher>(result, matcher_name);
+ this->register_matcher(m, callback);
+}
+
+void ConvertPagedAttnInputs::setKVCacheConfig(const KVCacheConfig& config) {
+ m_config = config;
+}
+
+const ConvertPagedAttnInputs::KVCacheConfig& ConvertPagedAttnInputs::getKVCacheConfig() const {
+ return m_config;
+}
+
+} // namespace ov::pass
diff --git a/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp b/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp
index 0aa648aab3f..0a056e2cac2 100644
--- a/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp
+++ b/src/common/transformations/tests/common_optimizations/convert_pagedattn_inputs.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "transformations/common_optimizations/convert_pagedattn_inputs.hpp"
+#include "transformations/paged_attention/convert_pagedattn_inputs.hpp"
#include <gtest/gtest.h>
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index 7b2ee3251ea..9ec13b6a962 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -59,7 +59,6 @@
#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_gated_delta_net.hpp"
@@ -126,6 +125,7 @@
#include "transformations/op_conversions/softsign_decomposition.hpp"
#include "transformations/op_conversions/unique_decomposition.hpp"
#include "transformations/opset_conversions/convert_opset2_to_opset1.hpp"
+#include "transformations/paged_attention/convert_pagedattn_inputs.hpp"
#include "transformations/rt_info/keep_const_precision.hpp"
#include "transformations/smart_reshape/matmul_sr.hpp"
#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index dd72028ce26..a661bc13a17 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -111,7 +111,6 @@
#include "transformations/common_optimizations/broadcast_elementwise_fusion.hpp"
#include "transformations/common_optimizations/broadcast_transition.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/fuse_rotary_positional_embeddings.hpp"
#include "transformations/common_optimizations/fuse_gated_delta_net.hpp"
@@ -195,6 +194,7 @@
#include "transformations/op_conversions/softplus_decomposition.hpp"
#include "transformations/opset_conversions/convert_opset2_to_opset1.hpp"
#include "transformations/opset_conversions/convert_opset3_to_opset2.hpp"
+#include "transformations/paged_attention/convert_pagedattn_inputs.hpp"
#include "transformations/resolve_names_collisions.hpp"
#include "transformations/rt_info/dequantization_node.hpp"
#include "transformations/rt_info/fused_names_attribute.hpp"