Skip to content

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

[CPU]Check beam index during init (#29999)

Zhang Yi1a0099b6a684
diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
index 49f376f1106..96c6f49cc47 100644
--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
@@ -1778,9 +1778,15 @@ void ScaledDotProductAttention::updateBeamTable(const MemoryPtr& mem_beam_idx, s
// beam order is like [0, 1, 2,...]
bool no_reorder = true;
for (size_t i = 0; i < B; i++) {
- if (beam_idx.ptr<int32_t>()[i] != static_cast<int32_t>(i)) {
+ const auto index = beam_idx.ptr<int32_t>()[i];
+ CPU_NODE_ASSERT(index >= 0 && index < static_cast<int32_t>(B),
+ "beam_idx ",
+ index,
+ " is outside of the allowed interval [0, ",
+ B,
+ ")");
+ if (index != static_cast<int32_t>(i)) {
no_reorder = false;
- break;
}
}
if (!no_reorder && m_key_quant_param.isByChannel) {
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
index 704c787d641..92457f65cb8 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
@@ -216,7 +216,7 @@ public:
value += stride;
}
}
- void generate(int idx, const std::vector<ov::Shape>& targetInputStaticShapes) {
+ virtual void generate(int idx, const std::vector<ov::Shape>& targetInputStaticShapes) {
inputs.clear();
auto create_input = [this](std::shared_ptr<ov::op::v0::Parameter> param, ov::Shape shape, float val) {
if (param->get_element_type() == ov::element::i32) {
@@ -521,6 +521,71 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTransposeTestSetState,
::testing::Values(0)),
ConcatSDPTransposeTest::getTestCaseName);
+class ConcatSDPTransposeTestWrongBeamIdx : public ConcatSDPTransposeTest {
+public:
+ void generate(int idx, const std::vector<ov::Shape>& targetInputStaticShapes) override {
+ inputs.clear();
+ auto create_input = [this](std::shared_ptr<ov::op::v0::Parameter> param, ov::Shape shape, float val) {
+ if (param->get_element_type() == ov::element::i32) {
+ ov::Tensor t{ov::element::i32, shape};
+ auto size = shape[0];
+ auto* p = static_cast<int*>(t.data());
+ auto start = static_cast<int>(val);
+
+ for (size_t i = 0; i < size; i++) {
+ p[i] = (start + i) % size + 1024;
+ }
+
+ inputs.insert({param, t});
+ } else if (param->get_element_type() == ov::element::f32) {
+ ov::Tensor t{ov::element::f32, shape};
+ strided_iota(static_cast<float*>(t.data()), t.get_size(), val, 0.1f);
+ inputs.insert({param, t});
+ } else if (param->get_element_type() == ov::element::f16) {
+ ov::Tensor t{ov::element::f16, shape};
+ strided_iota(static_cast<ov::float16*>(t.data()), t.get_size(), val, 0.1f);
+ inputs.insert({param, t});
+ } else {
+ ASSERT_TRUE(param->get_element_type() == ov::element::bf16);
+ ov::Tensor t{ov::element::bf16, shape};
+ strided_iota(static_cast<ov::bfloat16*>(t.data()), t.get_size(), val, 0.1f);
+ inputs.insert({param, t});
+ }
+ };
+ // q, k, v, pastkv
+ create_input(function->get_parameters()[0], targetInputStaticShapes[0], idx + 1.0f);
+ create_input(function->get_parameters()[1], targetInputStaticShapes[0], idx + 2.0f);
+ create_input(function->get_parameters()[2], targetInputStaticShapes[0], idx + 3.0f);
+ create_input(function->get_parameters()[3], targetInputStaticShapes[1], idx + 4.0f);
+ create_input(function->get_parameters()[4], ov::Shape{targetInputStaticShapes[0][0]}, idx + 0.0f);
+ }
+};
+
+TEST_P(ConcatSDPTransposeTestWrongBeamIdx, CompareWithRefs) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ ElementType inType;
+ InputShapeAndTransposeOrder inputShapeAndOrders;
+ bool hasShapeOf;
+ bool quantKeyByChannel;
+ size_t groupSize;
+ std::tie(inType, inputShapeAndOrders, hasShapeOf, quantKeyByChannel, groupSize) = this->GetParam();
+
+ // skip bf16 test on avx512 platform
+ if (inType == ElementType::bf16 && !ov::with_cpu_x86_bfloat16())
+ GTEST_SKIP();
+
+ ASSERT_THROW(run_test(function), ov::Exception);
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTransposeTestWrongBeamIdx,
+ ConcatSDPTransposeTestWrongBeamIdx,
+ ::testing::Combine(::testing::Values(ElementType::f32, ElementType::bf16, ElementType::f16),
+ ::testing::ValuesIn(inputShapeAndReordersSetState),
+ ::testing::Values(false),
+ ::testing::Values(false),
+ ::testing::Values(0)),
+ ConcatSDPTransposeTest::getTestCaseName);
+
} // namespace
} // namespace test
-} // namespace ov
+} // namespace ov
\ No newline at end of file