OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[CPU][ARM] Fix NaN in f16 softmax for all-negative score rows (#39262)
Yasharth Singh514aa33964c3
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.hppindex efc6f2596bc..cca05eb6006 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@@ -634,7 +634,8 @@ 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();+ // std::numeric_limits is not specialized for float16_t (__fp16), so its lowest() would return 0+ const auto min_f16 = static_cast<float16_t>(std::numeric_limits<ov::float16>::lowest());# if defined(HAVE_SVE)svfloat16_t v_max = svdup_n_f16(min_f16);svfloat16_t v_scale = svdup_n_f16(static_cast<float16_t>(scale));diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/concat_sdp.cppindex b6a392a796d..58b4c4a3e36 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/concat_sdp.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/concat_sdp.cpp@@ -41,6 +41,42 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,::testing::Values<int64_t>(8)),ConcatSDPTest::getTestCaseName);+// Shifts q and k apart so that every attention score is far below zero. The f16 softmax must still pick the+// row max from the scores; seeding it with 0 made exp() underflow and the attention output NaN.+class ConcatSDPNegativeScoresTest : public ConcatSDPTest {+protected:+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {+ ConcatSDPTest::generate_inputs(targetInputStaticShapes);+ const auto& params = function->get_parameters();+ shift(inputs.at(params[0]), 2.0F); // q+ shift(inputs.at(params[1]), -2.0F); // k+ shift(inputs.at(params[3]), -2.0F); // past k+ }++private:+ static void shift(ov::Tensor& tensor, float delta) {+ auto* data = tensor.data<ov::float16>();+ for (size_t i = 0; i < tensor.get_size(); ++i) {+ data[i] = ov::float16(static_cast<float>(data[i]) + delta);+ }+ }+};++TEST_P(ConcatSDPNegativeScoresTest, CompareWithRefs) {+ SKIP_IF_CURRENT_TEST_IS_DISABLED();+ run();+}++INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPNegativeScoresTest,+ ConcatSDPNegativeScoresTest,+ ::testing::Combine(::testing::Values(ElementType::f16),+ ::testing::Values(inputShapes[0]),+ ::testing::Values(cfg_none, cfg_u8_sym),+ ::testing::Values(false),+ ::testing::Values<int64_t>(8),+ ::testing::Values<int64_t>(8, 2)),+ ConcatSDPTest::getTestCaseName);+} // namespace} // namespace test} // namespace ovdiff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cppindex 0105df5d81f..9a9237da4a1 100644--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.cpp@@ -80,7 +80,7 @@ void ConcatSDPTest::SetUp() {if (inType == ElementType::bf16 && !ov::with_cpu_x86_bfloat16()) {GTEST_SKIP() << "Host has no native bf16 support.";}- if (inType == ElementType::f16 && !ov::with_cpu_x86_avx512_core_fp16()) {+ if (inType == ElementType::f16 && !ov::with_cpu_x86_avx512_core_fp16() && !ov::with_cpu_neon_fp16()) {GTEST_SKIP() << "Host has no native f16 support.";}diff --git a/src/plugins/intel_cpu/tests/unit/softmax_kernel_fp16_test.cpp b/src/plugins/intel_cpu/tests/unit/softmax_kernel_fp16_test.cppnew file mode 100644index 00000000000..a9d19ab3fcb--- /dev/null+++ b/src/plugins/intel_cpu/tests/unit/softmax_kernel_fp16_test.cpp@@ -0,0 +1,46 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <algorithm>+#include <cstdint>+#include <vector>++#include "gtest/gtest.h"+#include "nodes/kernels/scaled_attn/softmax.hpp"+#include "openvino/core/type/element_type.hpp"+#include "openvino/core/type/float16.hpp"++namespace {++// Masked positions must be filled with the lowest f16 value. std::numeric_limits<float16_t>::lowest() is 0+// because the __fp16 type is not specialized, which gave masked positions more weight than the real scores.+TEST(SoftmaxKernelFp16Test, CausalMaskedPositionsGetZeroWeight) {+#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)+ std::vector<ov::float16> scores(16, ov::float16(-1.0F));+ std::vector<ov::float16> weights(scores.size());+ std::vector<uint8_t> causal_mask(scores.size(), 0);+ std::fill(causal_mask.begin() + 8, causal_mask.end(), 1);++ ov::Extensions::Cpu::XARCH::attn_softmax(scores.data(),+ weights.data(),+ 1.0F,+ nullptr,+ nullptr,+ causal_mask.data(),+ false,+ scores.size(),+ scores.size(),+ ov::element::f16,+ ov::element::f16,+ ov::element::f16,+ nullptr);+ for (size_t i = 0; i < weights.size(); ++i) {+ EXPECT_NEAR(weights[i], causal_mask[i] ? 0.0F : 1.0F / 8, 1e-3F) << "at position " << i;+ }+#else+ GTEST_SKIP() << "The f16 softmax kernel is not built for this target.";+#endif+}++} // namespacediff --git a/src/plugins/intel_cpu/tests/unit/sources.cmake b/src/plugins/intel_cpu/tests/unit/sources.cmakeindex d632009cb20..bb6e7312c85 100644--- a/src/plugins/intel_cpu/tests/unit/sources.cmake+++ b/src/plugins/intel_cpu/tests/unit/sources.cmake@@ -216,6 +216,7 @@ set(CPU_UNIT_TESTS_ARM_SRCS)set(CPU_UNIT_TESTS_AARCH64_SRCS+ ${CMAKE_CURRENT_LIST_DIR}/softmax_kernel_fp16_test.cpp${CMAKE_CURRENT_LIST_DIR}/snippets_transformations/aarch64/snipptes_mark_skipped.cpp${CMAKE_CURRENT_LIST_DIR}/snippets_transformations/aarch64/lowered/adjust_gemm_copy_b_loop_ports.cpp${CMAKE_CURRENT_LIST_DIR}/snippets_transformations/aarch64/lowered/gemm_blocking.cpp