Skip to content

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

PagedAttention op, validate refactoring (#30446)

Ivan Tikhonov2d471143ac2a
diff --git a/src/core/include/openvino/core/type/element_type.hpp b/src/core/include/openvino/core/type/element_type.hpp
index 4a1fe8ff3b3..a255cfbd372 100644
--- a/src/core/include/openvino/core/type/element_type.hpp
+++ b/src/core/include/openvino/core/type/element_type.hpp
@@ -97,7 +97,6 @@ public:
std::string get_type_name() const;
friend OPENVINO_API std::ostream& operator<<(std::ostream&, const Type&);
- OPENVINO_DEPRECATED("This function is deprecated and will be removed in 2026.0.")
static std::vector<const Type*> get_known_types();
/// \brief Checks whether this element type is merge-compatible with `t`.
diff --git a/src/core/src/op/paged_attention.cpp b/src/core/src/op/paged_attention.cpp
index 11d3769dec6..72821cc3937 100644
--- a/src/core/src/op/paged_attention.cpp
+++ b/src/core/src/op/paged_attention.cpp
@@ -6,8 +6,68 @@
#include "dimension_util.hpp"
#include "itt.hpp"
+#include "openvino/core/validation_util.hpp"
#include "openvino/op/op.hpp"
+namespace {
+
+// Validates input rank and type for a node input.
+// We consider that dynamic rank/type are always valid case.
+// Empty {} means any rank/type
+inline void input_check(const ov::Node* node,
+ size_t idx,
+ const std::string_view input_name,
+ std::initializer_list<ov::Rank>&& allowed_ranks,
+ const std::vector<ov::element::Type>& allowed_types) {
+ using namespace ov;
+ using namespace ov::util;
+ using namespace ov::element;
+
+ const auto& rank = node->get_input_partial_shape(idx).rank();
+ const auto& tp = node->get_input_element_type(idx);
+
+ auto rank_check = [&](const Rank& rank) {
+ return rank.is_dynamic() || empty(allowed_ranks) || is_rank_compatible_any_of(rank.get_length(), allowed_ranks);
+ };
+
+ auto type_check = [&](const Type& type) {
+ auto it = std::find(allowed_types.begin(), allowed_types.end(), tp);
+ return type.is_dynamic() || allowed_types.empty() || it != allowed_types.end();
+ };
+
+ NODE_VALIDATION_CHECK(node,
+ rank_check(rank),
+ "Rank of `",
+ input_name,
+ "` input should be in [dynamic, ",
+ join(allowed_ranks),
+ "] list, but it is ",
+ rank,
+ ".");
+
+ NODE_VALIDATION_CHECK(node,
+ type_check(tp),
+ "Element type of `",
+ input_name,
+ "` input should be in [dynamic, ",
+ join(allowed_types),
+ "] list, but it is ",
+ tp,
+ ".");
+}
+
+std::vector<ov::element::Type> get_real_types() {
+ std::vector<ov::element::Type> real_types;
+ for (const auto& type : ov::element::Type::get_known_types()) {
+ if (type->is_real()) {
+ real_types.push_back(*type);
+ }
+ }
+ return real_types;
+}
+
+} // namespace
+
namespace ov {
namespace op {
@@ -23,165 +83,25 @@ void PagedAttentionExtension::validate_and_infer_types() {
"PagedAttensionExtension expects 13 or 16 inputs, but it has ",
get_input_size());
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(0).rank().is_dynamic() || get_input_partial_shape(0).rank().get_length() == 2,
- "Rank of `query` input should be 2, but it is ",
- get_input_partial_shape(0).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(1).rank().is_dynamic() || get_input_partial_shape(1).rank().get_length() == 2,
- "Rank of `key` input should be 2, but it is ",
- get_input_partial_shape(1).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(2).rank().is_dynamic() || get_input_partial_shape(2).rank().get_length() == 2,
- "Rank of `value` input should be 2, but it is ",
- get_input_partial_shape(2).rank().get_length(),
- ".");
-
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(3).rank().is_dynamic() || get_input_partial_shape(3).rank().get_length() >= 2,
- "Rank of `key_cache` input should be at least 2, but it is ",
- get_input_partial_shape(3).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(4).rank().is_dynamic() || get_input_partial_shape(4).rank().get_length() >= 2,
- "Rank of `value_cache` input should be at least 2, but it is ",
- get_input_partial_shape(4).rank().get_length(),
- ".");
-
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(5).rank().is_dynamic() || get_input_partial_shape(5).rank().get_length() == 1,
- "Rank of `past_lens` input should be 1, but it is ",
- get_input_partial_shape(5).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(5).is_dynamic() || get_input_element_type(5) == element::i32,
- "Element type of `past_lens` input should be i32, but it is ",
- get_input_element_type(5),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(6).rank().is_dynamic() || get_input_partial_shape(6).rank().get_length() == 1,
- "Rank of `subsequence_begins` input should be 1, but it is ",
- get_input_partial_shape(6).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(6).is_dynamic() || get_input_element_type(6) == element::i32,
- "Element type of `subsequence_begins` input should be i32, but it is ",
- get_input_element_type(6),
- ".");
-
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(7).rank().is_dynamic() || get_input_partial_shape(7).rank().get_length() == 1,
- "Rank of `block_indices` input should be 1, but it is ",
- get_input_partial_shape(7).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(7).is_dynamic() || get_input_element_type(7) == element::i32,
- "Element type of `block_indices` input should be i32, but it is ",
- get_input_element_type(7),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(8).rank().is_dynamic() || get_input_partial_shape(8).rank().get_length() == 1,
- "Rank of `block_indices_begins` input should be 1, but it is ",
- get_input_partial_shape(8).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(8).is_dynamic() || get_input_element_type(8) == element::i32,
- "Element type of `block_indices_begins` input should be i32, but it is ",
- get_input_element_type(8),
- ".");
-
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(9).rank().is_dynamic() || get_input_partial_shape(9).rank().get_length() == 0,
- "Input `scale` should be a scalar but it has rank ",
- get_input_partial_shape(9).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(9).is_dynamic() || get_input_element_type(9).is_real(),
- "Element type of `scale` input should be a floating type, but it is ",
- get_input_element_type(9),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(10).rank().is_dynamic() || get_input_partial_shape(10).rank().get_length() == 0,
- "Input `sliding_window` should be a scalar but it has rank ",
- get_input_partial_shape(10).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(10).is_dynamic() || get_input_element_type(10) == element::i32,
- "Element type of `sliding_window` input should be i32, but it is ",
- get_input_element_type(10),
- ".");
-
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(11).rank().is_dynamic() || get_input_partial_shape(11).rank().get_length() == 1,
- "Rank of `alibi_slopes` input should be 1, but it is ",
- get_input_partial_shape(11).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(11).is_dynamic() || get_input_element_type(11).is_real(),
- "Element type of `alibi_slopes` input should be a floating type, but it is ",
- get_input_element_type(11),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(12).rank().is_dynamic() || get_input_partial_shape(12).rank().get_length() == 0,
- "Input `max_context_len` should be a scalar but it has rank ",
- get_input_partial_shape(12).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(12).is_dynamic() || get_input_element_type(12) == element::i32,
- "Element type of `max_context_len` input should be i32, but it is ",
- get_input_element_type(12),
- ".");
+ // format: Node*, input_idx, name, {rank_list}, {type_list}
+ input_check(this, 0, "query", {2}, {});
+ input_check(this, 1, "key", {2}, {});
+ input_check(this, 2, "value", {2}, {});
+ input_check(this, 3, "key_cache", {2, 3, 4, 5}, {});
+ input_check(this, 4, "value_cache", {2, 3, 4, 5}, {});
+ input_check(this, 5, "past_lens", {1}, {element::i32});
+ input_check(this, 6, "subsequence_begins", {1}, {element::i32});
+ input_check(this, 7, "block_indices", {1}, {element::i32});
+ input_check(this, 8, "block_indices_begins", {1}, {element::i32});
+ input_check(this, 9, "scale", {0}, get_real_types());
+ input_check(this, 10, "sliding_window", {0}, {element::i32});
+ input_check(this, 11, "alibi_slopes", {1}, get_real_types());
+ input_check(this, 12, "max_context_len", {0}, {element::i32});
if (get_input_size() == 16) {
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(13).rank().is_dynamic() || get_input_partial_shape(13).rank().get_length() == 1,
- "Input `rotated_block_indices` should either have rank 1 or be omitted, but it has rank ",
- get_input_partial_shape(13).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(13).is_dynamic() || get_input_element_type(13) == element::i32,
- "Element type of `rotated_block_indices` input should be i32, but it is ",
- get_input_element_type(13),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(14).rank().is_dynamic() || get_input_partial_shape(14).rank().get_length() == 2,
- "Input `rotation_deltas` should either have rank 2 or be omitted, but it has rank ",
- get_input_partial_shape(14).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(14).is_dynamic() || get_input_element_type(14) == element::i32,
- "Element type of `rotation_deltas` input should be i32, but it is ",
- get_input_element_type(14),
- ".");
- NODE_VALIDATION_CHECK(
- this,
- get_input_partial_shape(15).rank().is_dynamic() || get_input_partial_shape(15).rank().get_length() == 2,
- "Input `rotation_trig_lut` should either have rank 2 or be omitted, but it has rank ",
- get_input_partial_shape(15).rank().get_length(),
- ".");
- NODE_VALIDATION_CHECK(this,
- get_input_element_type(15).is_dynamic() || get_input_element_type(15) == element::f32 ||
- get_input_element_type(15) == element::f16,
- "Element type of `rotation_trig_lut` input should be f32 or f16, but it is ",
- get_input_element_type(15),
- ".");
+ input_check(this, 13, "rotated_block_indices", {1}, {element::i32});
+ input_check(this, 14, "rotation_deltas", {2}, {element::i32});
+ input_check(this, 15, "rotation_trig_lut", {2}, {element::f16, element::f32});
}
// value head_size may be not same with key
diff --git a/src/core/tests/type_prop/paged_attention.cpp b/src/core/tests/type_prop/paged_attention.cpp
index d37f6eb34e5..1f6525dcbc7 100644
--- a/src/core/tests/type_prop/paged_attention.cpp
+++ b/src/core/tests/type_prop/paged_attention.cpp
@@ -126,5 +126,98 @@ TEST(type_prop, paged_attention_static_16_inputs_eviction_per_token) {
EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{3, 4}));
}
+TEST(type_prop, paged_attention_dynamic_ranks_and_types) {
+ using namespace ov::op;
+ const auto dyn = PartialShape::dynamic();
+
+ const auto query = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto key = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto value = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto key_cache = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto value_cache = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto past_lens = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto subsequence_begins = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto block_indices = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto block_indices_begins = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto scale = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto sliding_window = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto alibi_slopes = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+ const auto max_context_len = std::make_shared<v0::Parameter>(element::dynamic, dyn);
+
+ ov::OutputVector args = {query,
+ key,
+ value,
+ key_cache,
+ value_cache,
+ past_lens,
+ subsequence_begins,
+ block_indices,
+ block_indices_begins,
+ scale,
+ sliding_window,
+ alibi_slopes,
+ max_context_len};
+
+ EXPECT_NO_THROW(std::ignore = std::make_shared<op::PagedAttentionExtension>(args));
+}
+
+TEST(type_prop, paged_attention_invalid_rank_query) {
+ auto query = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3});
+ auto key = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3, 4});
+ auto value = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3, 4});
+ auto dummy = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3, 4});
+ auto scalar = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});
+ ov::OutputVector args =
+ {query, key, value, dummy, dummy, scalar, scalar, scalar, scalar, dummy, scalar, dummy, scalar};
+
+ EXPECT_THROW(std::ignore = std::make_shared<op::PagedAttentionExtension>(args), ov::NodeValidationFailure);
+}
+
+TEST(type_prop, paged_attention_invalid_type_scale) {
+ auto scale = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});
+ auto dummy2D = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3, 4});
+ auto dummy1D = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{3});
+ auto dummyScalar = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});
+
+ ov::OutputVector args = {dummy2D,
+ dummy2D,
+ dummy2D,
+ dummy2D,
+ dummy2D,
+ dummy1D,
+ dummy1D,
+ dummy1D,
+ dummy1D,
+ scale,
+ dummyScalar,
+ dummy2D,
+ dummyScalar};
+
+ EXPECT_THROW(std::ignore = std::make_shared<op::PagedAttentionExtension>(args), ov::NodeValidationFailure);
+}
+
+TEST(type_prop, paged_attention_invalid_rank_key_cache) {
+ auto key_cache = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1});
+ auto dummy = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{3, 4});
+ auto dummy1D = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{3});
+ auto dummyScalar = std::make_shared<op::v0::Parameter>(element::i32, PartialShape{});
+
+ ov::OutputVector args = {dummy,
+ dummy,
+ dummy,
+ key_cache,
+ dummy,
+ dummy1D,
+ dummy1D,
+ dummy1D,
+ dummy1D,
+ dummyScalar,
+ dummyScalar,
+ dummy,
+ dummyScalar};
+
+ EXPECT_THROW(std::ignore = std::make_shared<op::PagedAttentionExtension>(args), ov::NodeValidationFailure);
+}
+
} // namespace testing
} // namespace ov