Skip to content

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

[CPU] add checking on attention_mask of SDPA (#30133)

Yuan Hua1cb056ecfe1
diff --git a/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp b/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
index c7a1630c7f2..29daf2596a9 100644
--- a/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
@@ -18,31 +18,14 @@ public:
IShapeInfer::Result infer(const std::vector<std::reference_wrapper<const VectorDims>>& input_shapes,
[[maybe_unused]] const std::unordered_map<size_t, MemoryPtr>& data_dependency) override {
- OPENVINO_ASSERT(input_shapes.size() >= 3,
- "SDPAShapeInfer: expected at least 3 inputs, got ",
- input_shapes.size());
+ const auto inputs_size = input_shapes.size();
+ OPENVINO_ASSERT(inputs_size >= 3, "SDPAShapeInfer: expected at least 3 inputs, got ", inputs_size);
const auto& query_dims = input_shapes.front().get();
VectorDims present_v_dims = input_shapes.back().get();
- const auto& beam_idx_dims = input_shapes[input_shapes.size() - 3].get();
- const auto& permute_axes = m_config.permute_axes;
-
- if (permute_axes.empty()) {
- // [B, H, L, S]
- present_v_dims[0] = beam_idx_dims[0];
- present_v_dims[2] += query_dims[2];
- // normal and fast path
- if (present_v_dims[3] == query_dims[3]) {
- return {{query_dims, present_v_dims, present_v_dims}, ShapeInferStatus::success};
- }
-
- // diff kv feature size
- auto output_dims = query_dims;
- output_dims[3] = present_v_dims[3];
- auto present_k_dims = present_v_dims;
- present_k_dims[3] = query_dims[3];
- return {{output_dims, present_k_dims, present_v_dims}, ShapeInferStatus::success};
- }
-
+ const auto& beam_idx_dims = input_shapes[inputs_size - 3].get();
+ const auto& permute_axes_origin = m_config.permute_axes;
+ static const std::vector<size_t> direct_axis_order = {0, 1, 2, 3};
+ const auto& permute_axes = (permute_axes_origin.empty()) ? direct_axis_order : permute_axes_origin;
// permute_axes[0,1,2,3] gives axis indices of B,H,L,S for query & present_kv
const size_t batch_index = permute_axes[0];
const size_t length_index = permute_axes[2];
@@ -54,6 +37,46 @@ public:
for (size_t i = 0; i < n_dims; i++) {
output_dims[i] = query_dims[permute_axes[i]];
}
+ if (inputs_size == 7 && !m_config.is_causal) {
+ const auto& attn_mask_dims = input_shapes[3].get();
+ bool attn_mask_ok = true;
+ auto attn_mask_dims_size = attn_mask_dims.size();
+ auto weight_dims = output_dims;
+ auto weight_dims_size = weight_dims.size();
+ if (attn_mask_dims_size >= 2 && attn_mask_dims_size <= weight_dims_size) {
+ auto check_broadcast = [](const size_t& target, const size_t& to) -> bool {
+ return (target != to && target != 1) ? false : true;
+ };
+ weight_dims[3] = present_v_dims[length_index];
+ auto offset = weight_dims_size - attn_mask_dims_size;
+ for (int i = attn_mask_dims_size - 1; i >= 0; i--) {
+ attn_mask_ok = attn_mask_ok && check_broadcast(attn_mask_dims[i], weight_dims[i + offset]);
+ }
+ } else {
+ attn_mask_ok = false;
+ }
+ if (!attn_mask_ok) {
+ const auto& cur_k_dims = input_shapes[1].get();
+ const auto& cur_v_dims = input_shapes[2].get();
+ const auto& cache_k_dims = input_shapes[5].get();
+ const auto& cache_v_dims = input_shapes[6].get();
+ OPENVINO_THROW("attention_mask do not match q and k,",
+ " query_dims:",
+ ov::intel_cpu::vec2str(query_dims),
+ " cur_k_dims:",
+ ov::intel_cpu::vec2str(cur_k_dims),
+ " cur_v_dims:",
+ ov::intel_cpu::vec2str(cur_v_dims),
+ " attn_mask_dims:",
+ ov::intel_cpu::vec2str(attn_mask_dims),
+ " beam_idx_dims:",
+ ov::intel_cpu::vec2str(beam_idx_dims),
+ " cache_k_dims:",
+ ov::intel_cpu::vec2str(cache_k_dims),
+ " cache_v_dims:",
+ ov::intel_cpu::vec2str(cache_v_dims));
+ }
+ }
// normal and fast path
if (present_v_dims[3] == query_dims[3]) {
diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/scaled_attn.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/scaled_attn.cpp
index 4e95595289e..d9f511efa98 100644
--- a/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/custom_shape_infer/scaled_attn.cpp
@@ -18,7 +18,8 @@ using namespace testing;
using SDPATestParams = std::tuple<unit_test::ShapeVector, // Input shapes
std::vector<size_t>, // permute_axes
- unit_test::ShapeVector // Expected output shapes
+ unit_test::ShapeVector, // Expected output shapes
+ bool // causal
>;
class SDPACpuShapeInferenceTest
@@ -29,17 +30,19 @@ public:
unit_test::ShapeVector tmp_input_shapes;
std::vector<size_t> tmp_permute_axes;
unit_test::ShapeVector tmp_exp_shape;
- std::tie(tmp_input_shapes, tmp_permute_axes, tmp_exp_shape) = obj.param;
+ bool tmp_causal = false;
+ std::tie(tmp_input_shapes, tmp_permute_axes, tmp_exp_shape, tmp_causal) = obj.param;
std::ostringstream result;
result << "IS" << ov::test::utils::vec2str(tmp_input_shapes) << "_";
result << "permute_axes" << ov::test::utils::vec2str(tmp_permute_axes) << "_";
result << "exp_shape" << ov::test::utils::vec2str(tmp_exp_shape);
+ result << "causal_" << unit_test::boolToString(tmp_causal);
return result.str();
}
protected:
void SetUp() override {
- std::tie(input_shapes, permute_axes, output_shapes) = GetParam();
+ std::tie(input_shapes, permute_axes, output_shapes, causal) = GetParam();
args.clear();
for (const auto& ishape : input_shapes) {
@@ -48,10 +51,12 @@ protected:
}
OutputVector args;
std::vector<size_t> permute_axes;
+ bool causal = false;
};
TEST_P(SDPACpuShapeInferenceTest, shape_inference) {
ov::intel_cpu::ScaledDotProductAttentionWithKVCache::Config config;
+ config.is_causal = causal;
config.permute_axes = permute_axes;
const auto op = make_op(args, config);
unit_test::cpu_test_shape_infer(op.get(), input_shapes, output_shapes);
@@ -69,7 +74,8 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{1, 32, 0, 128},
{1, 32, 0, 128}},
std::vector<size_t>{},
- unit_test::ShapeVector{{1, 32, 14, 128}, {1, 32, 14, 128}, {1, 32, 14, 128}}),
+ unit_test::ShapeVector{{1, 32, 14, 128}, {1, 32, 14, 128}, {1, 32, 14, 128}},
+ false),
make_tuple(unit_test::ShapeVector{{1, 32, 1, 128},
{1, 32, 1, 128},
{1, 32, 1, 128},
@@ -78,7 +84,8 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{1, 32, 15, 128},
{1, 32, 15, 128}},
std::vector<size_t>{},
- unit_test::ShapeVector{{1, 32, 1, 128}, {1, 32, 16, 128}, {1, 32, 16, 128}}),
+ unit_test::ShapeVector{{1, 32, 1, 128}, {1, 32, 16, 128}, {1, 32, 16, 128}},
+ false),
// chatglm
make_tuple(unit_test::ShapeVector{{1, 1, 32, 128},
{1, 1, 2, 128},
@@ -88,7 +95,8 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{7, 1, 2, 128},
{7, 1, 2, 128}},
std::vector<size_t>{1, 2, 0, 3},
- unit_test::ShapeVector{{1, 32, 1, 128}, {8, 1, 2, 128}, {8, 1, 2, 128}}),
+ unit_test::ShapeVector{{1, 32, 1, 128}, {8, 1, 2, 128}, {8, 1, 2, 128}},
+ false),
make_tuple(unit_test::ShapeVector{{7, 1, 32, 128},
{7, 1, 2, 128},
{7, 1, 2, 128},
@@ -97,7 +105,8 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{0, 1, 2, 128},
{0, 1, 2, 128}},
std::vector<size_t>{1, 2, 0, 3},
- unit_test::ShapeVector{{1, 32, 7, 128}, {7, 1, 2, 128}, {7, 1, 2, 128}}),
+ unit_test::ShapeVector{{1, 32, 7, 128}, {7, 1, 2, 128}, {7, 1, 2, 128}},
+ false),
// qwen
make_tuple(unit_test::ShapeVector{{1, 1, 32, 128},
{1, 1, 32, 128},
@@ -107,8 +116,8 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{1, 4, 32, 128},
{1, 4, 32, 128}},
std::vector<size_t>{0, 2, 1, 3},
- unit_test::ShapeVector{{1, 32, 1, 128}, {1, 5, 32, 128}, {1, 5, 32, 128}}),
-
+ unit_test::ShapeVector{{1, 32, 1, 128}, {1, 5, 32, 128}, {1, 5, 32, 128}},
+ false),
make_tuple(unit_test::ShapeVector{{1, 4, 32, 128},
{1, 4, 32, 128},
{1, 4, 32, 128},
@@ -117,9 +126,135 @@ INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
{1, 0, 32, 128},
{1, 0, 32, 128}},
std::vector<size_t>{0, 2, 1, 3},
- unit_test::ShapeVector{{1, 32, 4, 128}, {1, 4, 32, 128}, {1, 4, 32, 128}})),
+ unit_test::ShapeVector{{1, 32, 4, 128}, {1, 4, 32, 128}, {1, 4, 32, 128}},
+ false)),
SDPACpuShapeInferenceTest::getTestCaseName);
+using SDPACpuShapeInferenceThrowExceptionTest = SDPACpuShapeInferenceTest;
+TEST_P(SDPACpuShapeInferenceThrowExceptionTest, wrong_attention_mask) {
+ ov::intel_cpu::ScaledDotProductAttentionWithKVCache::Config config;
+ config.permute_axes = permute_axes;
+ config.is_causal = causal;
+ const auto op = make_op(args, config);
+ std::ostringstream os;
+ os << "attention_mask do not match q and k,";
+ auto set_input_shape_str = [&os](std::string name, const StaticShape& input_shape) {
+ os << name;
+ os << "(";
+ for (size_t i = 0; i < input_shape.size(); i++) {
+ os << input_shape[i];
+ if (i < input_shape.size() - 1) {
+ os << ".";
+ }
+ }
+ os << ")";
+ };
+ set_input_shape_str(" query_dims:", input_shapes[0]);
+ set_input_shape_str(" cur_k_dims:", input_shapes[1]);
+ set_input_shape_str(" cur_v_dims:", input_shapes[2]);
+ set_input_shape_str(" attn_mask_dims:", input_shapes[3]);
+ set_input_shape_str(" beam_idx_dims:", input_shapes[4]);
+ set_input_shape_str(" cache_k_dims:", input_shapes[5]);
+ set_input_shape_str(" cache_v_dims:", input_shapes[6]);
+ OV_EXPECT_THROW(unit_test::cpu_test_shape_infer(op.get(), input_shapes, output_shapes),
+ ov::Exception,
+ HasSubstr(os.str()));
+}
+
+auto wrongAttnmaskParams = []() -> std::vector<SDPATestParams> {
+ unit_test::ShapeVector attn_mask_vec = {{1, 1, 47, 47}, {47}, {1}, {9, 47, 94}, {3, 1, 47, 94}, {1, 1, 1, 1, 1}};
+ auto tuple = std::make_tuple(
+ unit_test::ShapeVector{{1, 16, 47, 56},
+ {1, 8, 47, 56},
+ {1, 8, 47, 56},
+ {1, 1, 47, 94},
+ {1},
+ {1, 8, 47, 56},
+ {1, 8, 47, 56}},
+ std::vector<size_t> {},
+ unit_test::ShapeVector{{1, 16, 47, 56}, {1, 8, 94, 56}, {1, 8, 94, 56}},
+ false);
+ std::vector<SDPATestParams> params;
+ auto createParams = [&attn_mask_vec, &tuple, &params]() {
+ for (auto& item : attn_mask_vec) {
+ auto& input_shapes = std::get<0>(tuple);
+ input_shapes[3] = item;
+ params.push_back(tuple);
+ }
+ };
+ createParams();
+ attn_mask_vec = {{1, 1, 1, 6}, {5}, {2, 1, 1}, {3, 1, 1, 6}, {2, 5}};
+ tuple = make_tuple(unit_test::ShapeVector{{3, 1, 32, 128},
+ {3, 1, 32, 128},
+ {3, 1, 32, 128},
+ {3, 1, 1, 5},
+ {3},
+ {3, 4, 32, 128},
+ {3, 4, 32, 128}},
+ std::vector<size_t> {0, 2, 1, 3},
+ unit_test::ShapeVector{{3, 32, 1, 128}, {3, 5, 32, 128}, {3, 5, 32, 128}},
+ false);
+ createParams();
+ return params;
+};
+
+INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
+ SDPACpuShapeInferenceThrowExceptionTest,
+ ValuesIn(wrongAttnmaskParams()),
+ SDPACpuShapeInferenceThrowExceptionTest::getTestCaseName);
+
+using SDPACpuShapeInferenceCorrectAttnMaskTest = SDPACpuShapeInferenceTest;
+TEST_P(SDPACpuShapeInferenceCorrectAttnMaskTest, shape_inference) {
+ ov::intel_cpu::ScaledDotProductAttentionWithKVCache::Config config;
+ config.is_causal = causal;
+ config.permute_axes = permute_axes;
+ const auto op = make_op(args, config);
+ unit_test::cpu_test_shape_infer(op.get(), input_shapes, output_shapes);
+}
+
+auto correctAttnmaskParams = []() -> std::vector<SDPATestParams> {
+ unit_test::ShapeVector attn_mask_vec = {{1, 1, 47, 94}, {47, 94}, {1, 94}, {1, 47, 94}, {47, 1}, {1, 1}, {1, 1, 1, 1}};
+ auto tuple = std::make_tuple(unit_test::ShapeVector{{1, 16, 47, 56},
+ {1, 8, 47, 56},
+ {1, 8, 47, 56},
+ {1, 1, 47, 94},
+ {1},
+ {1, 8, 47, 56},
+ {1, 8, 47, 56}},
+ std::vector<size_t> {},
+ unit_test::ShapeVector{{1, 16, 47, 56}, {1, 8, 94, 56}, {1, 8, 94, 56}},
+ false);
+ std::vector<SDPATestParams> params;
+ auto createParams = [&attn_mask_vec, &tuple, &params]() {
+ for (auto& item : attn_mask_vec) {
+ auto& input_shapes = std::get<0>(tuple);
+ input_shapes[3] = item;
+ auto& causal = std::get<3>(tuple);
+ causal = false;
+ params.push_back(tuple);
+ causal = true;
+ params.push_back(tuple);
+ }
+ };
+ createParams();
+ attn_mask_vec = {{1, 1, 1, 5}, {1, 5}, {1, 1}, {1, 1, 5}, {3, 1, 1, 5}, {1, 1, 1, 1}};
+ tuple = make_tuple(unit_test::ShapeVector{{3, 1, 32, 128},
+ {3, 1, 32, 128},
+ {3, 1, 32, 128},
+ {3, 1, 1, 5},
+ {3},
+ {3, 4, 32, 128},
+ {3, 4, 32, 128}},
+ std::vector<size_t> {0, 2, 1, 3},
+ unit_test::ShapeVector{{3, 32, 1, 128}, {3, 5, 32, 128}, {3, 5, 32, 128}},
+ false);
+ createParams();
+ return params;
+};
+INSTANTIATE_TEST_SUITE_P(CpuShapeInfer,
+ SDPACpuShapeInferenceCorrectAttnMaskTest,
+ ValuesIn(correctAttnmaskParams()),
+ SDPACpuShapeInferenceCorrectAttnMaskTest::getTestCaseName);
} // namespace cpu_shape_infer
} // namespace unit_test
} // namespace intel_cpu