Skip to content

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

[CPU][ARM] Fixes in SDPA kernels (#33325)

Aleksandr Voroncf1c268b892a
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp
index 706fbbbc03d..18f02615a8b 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/common.hpp
@@ -406,13 +406,20 @@ inline svfloat32_t exp_ps_sve(svbool_t& pg, svfloat32_t& src) {
const auto zero = svdup_n_f32(0.F);
const auto min_input = svdup_n_f32(-86.64f); // Approximately ln(2^-125)
- const auto z = svmla_f32_z(pg, shift, src, inv_ln2);
+ auto x = svmin_f32_z(pg, src, max_input);
+ x = svmax_f32_z(pg, x, min_input);
+
+ const auto z = svmla_f32_z(pg, shift, x, inv_ln2);
auto n = svsub_f32_z(pg, z, shift);
- n = svsub_f32_z(pg, n, one);
- const auto scale = svreinterpret_f32_u32(svlsl_n_u32_z(pg, svreinterpret_u32_f32(z), 23)); // 2^n
- const auto r_hi = svmla_f32_z(pg, src, n, neg_ln2_hi);
+ const auto r_hi = svmla_f32_z(pg, x, n, neg_ln2_hi);
const auto r = svmla_f32_z(pg, r_hi, n, neg_ln2_lo);
+ n = svsub_f32_z(pg, n, one);
+
+ const auto n_int = svcvt_s32_f32_z(pg, n);
+ const auto exponent_bias = svdup_n_s32(127);
+ const auto n_int_bias = svadd_s32_z(pg, n_int, exponent_bias);
+ const auto scale = svreinterpret_f32_s32(svlsl_n_s32_z(pg, n_int_bias, 23)); // 2^(n-1)
const auto r2 = svmul_f32_z(pg, r, r);
const auto p1 = svmul_f32_z(pg, c1, r);
@@ -449,13 +456,20 @@ inline float32x4_t exp_ps_neon_f32(const float32x4_t& src) {
const auto zero = vdupq_n_f32(0.F);
const auto min_input = vdupq_n_f32(-86.64f); // Approximately ln(2^-125)
- const auto z = vmlaq_f32(shift, src, inv_ln2);
+ auto x = vminq_f32(src, max_input);
+ x = vmaxq_f32(x, min_input);
+
+ const auto z = vmlaq_f32(shift, x, inv_ln2);
auto n = z - shift;
- n = vsubq_f32(n, one);
- const auto scale = vreinterpretq_f32_u32(vreinterpretq_u32_f32(z) << 23); // 2^n
- const auto r_hi = vfmaq_f32(src, n, neg_ln2_hi);
+ const auto r_hi = vfmaq_f32(x, n, neg_ln2_hi);
const auto r = vfmaq_f32(r_hi, n, neg_ln2_lo);
+ n = vsubq_f32(n, one);
+
+ const auto n_int = vcvtq_s32_f32(n);
+ const auto exponent_bias = vdupq_n_s32(127);
+ const auto n_int_bias = vaddq_s32(n_int, exponent_bias);
+ const auto scale = vreinterpretq_f32_s32(vshlq_n_s32(n_int_bias, 23)); // 2^(n-1)
const auto r2 = r * r;
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/softmax_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/softmax_kernel.hpp
index 00fa0ce681e..996e060ef0c 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/softmax_kernel.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/softmax_kernel.hpp
@@ -554,12 +554,13 @@ inline void scale_add2_reduce_max(ov::float16* a,
float alibi_slope,
ov::float16& max) {
size_t i = 0;
+ constexpr float16_t min_f16 = std::numeric_limits<float16_t>::lowest();
# if defined(HAVE_SVE)
- svfloat16_t v_max = svdup_n_f16(static_cast<float16_t>(-FLT_MAX));
+ svfloat16_t v_max = svdup_n_f16(min_f16);
svfloat16_t v_scale = svdup_n_f16(static_cast<float16_t>(scale));
svfloat16_t v_a;
svuint16_t v_zeroi16 = svdup_n_u16(0);
- svfloat16_t v_nfltmax = svdup_n_f16(static_cast<float16_t>(-FLT_MAX));
+ svfloat16_t v_nfltmax = svdup_n_f16(min_f16);
svfloat16_t v_alibi_slope = svdup_n_f16(static_cast<float16_t>(alibi_slope));
svbool_t mask_xor = svptrue_b16();
@@ -569,15 +570,9 @@ inline void scale_add2_reduce_max(ov::float16* a,
svbool_t pg_f16 = svptrue_b16();
svbool_t pg_u8 = svptrue_b8();
svbool_t pg_u16 = svptrue_b16();
- size_t inc = vec_len_f16_sve();
+ size_t vec_len = svcnth();
- while (i < size) {
- if (size - i < vec_len_f16_sve()) {
- inc = size - i;
- pg_f16 = svwhilelt_b16(0, static_cast<int>(inc));
- pg_u8 = svwhilelt_b8(0, static_cast<int>(inc));
- pg_u16 = svwhilelt_b16(0, static_cast<int>(inc));
- }
+ for (; i + vec_len <= size; i += vec_len) {
v_a = svld1_f16(pg_f16, reinterpret_cast<const float16_t*>(a + i));
v_a = svmul_f16_z(pg_f16, v_a, v_scale);
@@ -587,8 +582,34 @@ inline void scale_add2_reduce_max(ov::float16* a,
}
if (has_attn_mask) {
- svfloat16_t v_mask = svld1_f16(pg_f16, reinterpret_cast<const float16_t*>(attn_mask + i));
- v_a = svadd_f16_z(pg_f16, v_a, v_mask);
+ static_assert(std::is_same_v<T, float> || std::is_same_v<T, ov::float16>,
+ "attn_mask must be float or float16 type.");
+ if constexpr (std::is_same_v<T, float>) {
+ svfloat16_t zero = svdup_n_f16(0.0f);
+ size_t inc_low = (vec_len + 1) / 2;
+ size_t inc_high = vec_len / 2;
+ svbool_t pg_f32_low = svwhilelt_b32(0, static_cast<int>(inc_low));
+ svbool_t pg_f32_high = svwhilelt_b32(0, static_cast<int>(inc_high));
+
+ svfloat16_t low_f16 = svzip1_f16(v_a, zero);
+ svfloat16_t high_f16 = svzip2_f16(v_a, zero);
+ svfloat32_t low_f32 = svcvt_f32_f16_x(pg_f32_low, low_f16);
+ svfloat32_t high_f32 = svcvt_f32_f16_x(pg_f32_high, high_f16);
+
+ svuint32_t idx_even = svindex_u32(0, 2);
+ svuint32_t idx_odd = svindex_u32(1, 2);
+ svfloat32_t mask_low = svld1_gather_u32index_f32(pg_f32_low, attn_mask + i, idx_even);
+ svfloat32_t mask_high = svld1_gather_u32index_f32(pg_f32_high, attn_mask + i, idx_odd);
+ low_f32 = svadd_f32_x(pg_f32_low, low_f32, mask_low);
+ high_f32 = svadd_f32_x(pg_f32_high, high_f32, mask_high);
+
+ svfloat16_t low_f16_out = svcvt_f16_f32_x(pg_f32_low, low_f32);
+ svfloat16_t high_f16_out = svcvt_f16_f32_x(pg_f32_high, high_f32);
+ v_a = svuzp1(low_f16_out, high_f16_out);
+ } else if constexpr (std::is_same_v<T, ov::float16>) {
+ svfloat16_t v_mask = svld1_f16(pg_f16, reinterpret_cast<const float16_t*>(attn_mask + i));
+ v_a = svadd_f16_z(pg_f16, v_a, v_mask);
+ }
}
if (has_causal_mask) {
@@ -601,15 +622,14 @@ inline void scale_add2_reduce_max(ov::float16* a,
v_max = svmax_f16_z(pg_f16, v_max, v_a);
svst1_f16(pg_f16, reinterpret_cast<float16_t*>(a + i), v_a);
- i += inc;
}
- max = svmaxv_f16(pg_f16, v_max);
+ max = svmaxv_f16(svptrue_b16(), v_max);
# elif defined(HAVE_NEON_FP16)
- float16x8_t v_max = vdupq_n_f16(static_cast<float16_t>(-FLT_MAX));
+ float16x8_t v_max = vdupq_n_f16(min_f16);
float16x8_t v_scale = vdupq_n_f16(static_cast<float16_t>(scale));
float16x8_t v_a;
uint16x8_t v_zeroi16 = vdupq_n_u16(0);
- float16x8_t v_nfltmax = vdupq_n_f16(static_cast<float16_t>(-FLT_MAX));
+ float16x8_t v_nfltmax = vdupq_n_f16(min_f16);
uint16x8_t mask_xor = vdupq_n_u16(select_nfltmax_at_0 ? 0xFFFF : 0);
float16x8_t v_alibi_slope = vdupq_n_f16(static_cast<float16_t>(alibi_slope));
@@ -624,7 +644,16 @@ inline void scale_add2_reduce_max(ov::float16* a,
}
if (has_attn_mask) {
- float16x8_t v_mask = vld1q_f16(reinterpret_cast<const float16_t*>(attn_mask + i));
+ static_assert(std::is_same_v<T, float> || std::is_same_v<T, ov::float16>,
+ "attn_mask must be float or float16 type.");
+ float16x8_t v_mask;
+ if constexpr (std::is_same_v<T, float>) {
+ float32x4_t m0 = vld1q_f32(attn_mask + i);
+ float32x4_t m1 = vld1q_f32(attn_mask + i + vec_len_f32_neon);
+ v_mask = vcombine_f16(vcvt_f16_f32(m0), vcvt_f16_f32(m1));
+ } else if constexpr (std::is_same_v<T, ov::float16>) {
+ v_mask = vld1q_f16(reinterpret_cast<const float16_t*>(attn_mask + i));
+ }
v_a = vaddq_f16(v_a, v_mask);
}
@@ -653,10 +682,10 @@ inline void scale_add2_reduce_max(ov::float16* a,
if (has_causal_mask) {
if (select_nfltmax_at_0) {
if (causal_mask[i] == 0)
- a[i] = -FLT_MAX;
+ a[i] = min_f16;
} else {
if (causal_mask[i] != 0)
- a[i] = -FLT_MAX;
+ a[i] = min_f16;
}
}
max = a[i] > max ? a[i] : max;
@@ -1336,6 +1365,7 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
size_t,
float,
ov::float16&);
+# if !defined(OPENVINO_ARCH_ARM64)
using func_bf16_type = void (*)(ov::float16*,
float,
const ov::float16*,
@@ -1345,6 +1375,7 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
size_t,
float,
ov::float16&);
+# endif
using func_fp16_type = void (*)(ov::float16*,
float,
const ov::float16*,
@@ -1362,6 +1393,7 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
scale_add2_reduce_max<true, false, true>,
scale_add2_reduce_max<true, true, false>,
scale_add2_reduce_max<true, true, true>};
+# if !defined(OPENVINO_ARCH_ARM64)
static constexpr func_bf16_type funcs_bf16[] = {scale_add2_reduce_max<false, false, false>,
scale_add2_reduce_max<false, false, true>,
scale_add2_reduce_max<false, true, false>,
@@ -1370,6 +1402,7 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
scale_add2_reduce_max<true, false, true>,
scale_add2_reduce_max<true, true, false>,
scale_add2_reduce_max<true, true, true>};
+# endif
static constexpr func_fp16_type funcs_fp16[] = {scale_add2_reduce_max<false, false, false>,
scale_add2_reduce_max<false, false, true>,
scale_add2_reduce_max<false, true, false>,
@@ -1380,31 +1413,33 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
scale_add2_reduce_max<true, true, true>};
int dispatch = (alibi ? 0b100 : 0) | (attn_mask ? 0b010 : 0) | (causal_mask ? 0b001 : 0);
ov::float16 max = std::numeric_limits<ov::float16>::lowest();
- if (attn_mask_prec == ov::element::f32) {
- funcs_fp32[dispatch](a,
+ if (attn_mask_prec == ov::element::f16) {
+ funcs_fp16[dispatch](a,
scale,
alibi,
- static_cast<const float*>(attn_mask),
+ static_cast<const ov::float16*>(attn_mask),
causal_mask,
select_nfltmax_at_0,
len,
alibi_slope,
max);
- } else if (attn_mask_prec == ov::element::f16) {
- funcs_fp16[dispatch](a,
+# if !defined(OPENVINO_ARCH_ARM64)
+ } else if (attn_mask_prec == ov::element::bf16) {
+ funcs_bf16[dispatch](a,
scale,
alibi,
- static_cast<const ov::float16*>(attn_mask),
+ static_cast<const ov::bfloat16*>(attn_mask),
causal_mask,
select_nfltmax_at_0,
len,
alibi_slope,
max);
+# endif
} else {
- funcs_bf16[dispatch](a,
+ funcs_fp32[dispatch](a,
scale,
alibi,
- static_cast<const ov::bfloat16*>(attn_mask),
+ static_cast<const float*>(attn_mask),
causal_mask,
select_nfltmax_at_0,
len,
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/stateful_sdpa_bool_mask.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/stateful_sdpa_bool_mask.cpp
index 85a9b132fb1..1b39e33870b 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/stateful_sdpa_bool_mask.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/stateful_sdpa_bool_mask.cpp
@@ -51,10 +51,10 @@ void StatefulSdpaBoolMaskTest::SetUp() {
abs_threshold = 0.02f;
selectedType = makeSelectedTypeStr(getPrimitiveType(), inferencePrecision);
- const InputShape q_shape{{-1, 8, -1, 64}, {{1, 8, 10, 64}}};
- const InputShape k_shape{{-1, 8, -1, 64}, {{1, 8, 10, 64}}};
- const InputShape v_shape{{-1, 8, -1, 64}, {{1, 8, 10, 64}}};
- const InputShape mask_shape{{1, 1, -1, -1}, {{1, 1, 10, 10}}};
+ const InputShape q_shape{{-1, 8, -1, 64}, {{1, 8, 16, 64}}};
+ const InputShape k_shape{{-1, 8, -1, 64}, {{1, 8, 16, 64}}};
+ const InputShape v_shape{{-1, 8, -1, 64}, {{1, 8, 16, 64}}};
+ const InputShape mask_shape{{1, 1, -1, -1}, {{1, 1, 16, 16}}};
const InputShape past_shape{{-1, 8, -1, 64}, {{1, 8, 0, 64}}};
const InputShape beam_shape{{-1}, {{1}}};
@@ -118,7 +118,7 @@ void StatefulSdpaBoolMaskTest::generate_inputs(const std::vector<ov::Shape>& tar
const auto& shape = targetInputStaticShapes[idx];
if (param->get_element_type() == ov::element::f32) {
ov::Tensor tensor{ov::element::f32, shape};
- utils::fill_data_random(static_cast<float*>(tensor.data()), tensor.get_size(), 2, -1, 10);
+ utils::fill_data_random(static_cast<float*>(tensor.data()), tensor.get_size(), 2, -1, 16);
inputs.insert({param, tensor});
} else if (param->get_element_type() == ov::element::boolean) {
ov::Tensor tensor{ov::element::boolean, shape};
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 110019c0728..a6416aff2e4 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -316,8 +316,6 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(.*smoke_AvgPoolV14_CPU_4D/AvgPoolingV14LayerCPUTest.CompareWithRefs.*)");
// Ticket: 168931
retVector.emplace_back(R"(.*smoke_Reduce_OneAxis_dynamic_CPU/ReduceCPULayerTest.CompareWithRefs.*)");
- // Ticket: 178089
- retVector.emplace_back(R"(.*smoke_ARM_StatefulSdpaBoolMask/StatefulSdpaBoolMaskTest.*)");
}
// invalid test: checks u8 precision for runtime graph, while it should be f32
retVector.emplace_back(R"(smoke_NegativeQuantizedMatMulMultiplyFusion.*)");