Skip to content

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

[CPU][ARM] Allow AArch64 JIT reorder and relax stride checks for f16 paths (#33258)

Nesterov Alexander85c4cce2150e
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 996e060ef0c..8f39e9b9f5c 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
@@ -33,6 +33,79 @@
namespace ov::Extensions::Cpu::XARCH {
+#if defined(OPENVINO_ARCH_ARM64)
+namespace detail {
+inline void zero_out_dst(void* a_dst, ov::element::Type dst_precision, size_t total_size) {
+ if (total_size == 0) {
+ return;
+ }
+ if (dst_precision == ov::element::f32) {
+ memset(static_cast<float*>(a_dst), 0, sizeof(float) * total_size);
+ } else {
+ memset(static_cast<ov::float16*>(a_dst), 0, sizeof(ov::float16) * total_size);
+ }
+}
+
+inline bool handle_empty_len(size_t len, void* a_dst, ov::element::Type dst_precision, size_t total_size) {
+ if (len != 0) {
+ return false;
+ }
+ zero_out_dst(a_dst, dst_precision, total_size);
+ return true;
+}
+
+template <typename T>
+inline float to_float(T value) {
+ return static_cast<float>(value);
+}
+
+template <>
+inline float to_float<float>(float value) {
+ return value;
+}
+
+template <typename T>
+inline bool handle_inf_logits(const T* a,
+ void* a_dst,
+ ov::element::Type dst_precision,
+ size_t len,
+ size_t total_size,
+ const float* sink) {
+ size_t inf_count = 0;
+ if (sink != nullptr && std::isinf(*sink) && *sink > 0.0F) {
+ inf_count++;
+ }
+ for (size_t i = 0; i < len; i++) {
+ const float aval = to_float(a[i]);
+ if (std::isinf(aval) && aval > 0.0F) {
+ inf_count++;
+ }
+ }
+ const float inv = inf_count ? (1.0F / static_cast<float>(inf_count)) : 0.0F;
+ if (dst_precision == ov::element::f32) {
+ auto* dst = static_cast<float*>(a_dst);
+ for (size_t i = 0; i < len; i++) {
+ const float aval = to_float(a[i]);
+ dst[i] = (inf_count && std::isinf(aval) && aval > 0.0F) ? inv : 0.0F;
+ }
+ if (total_size > len) {
+ memset(dst + len, 0, sizeof(float) * (total_size - len));
+ }
+ } else {
+ auto* dst = static_cast<ov::float16*>(a_dst);
+ for (size_t i = 0; i < len; i++) {
+ const float aval = to_float(a[i]);
+ dst[i] = (inf_count && std::isinf(aval) && aval > 0.0F) ? ov::float16(inv) : ov::float16(0.0F);
+ }
+ if (total_size > len) {
+ memset(dst + len, 0, sizeof(ov::float16) * (total_size - len));
+ }
+ }
+ return true;
+}
+} // namespace detail
+#endif
+
#if defined(HAVE_AVX2)
inline void exp_ps_avx2(__m256& src) {
# define REPEAT8(x) x, x, x, x, x, x, x, x
@@ -56,7 +129,7 @@ inline void exp_ps_avx2(__m256& src) {
__m256 exp_log2ef = _mm256_loadu_ps(reinterpret_cast<const float*>(c_e)); // log2(e)
__m256 half = _mm256_loadu_ps(reinterpret_cast<const float*>(c_half)); // 0.5f
__m256 ln2f = _mm256_loadu_ps(reinterpret_cast<const float*>(c_ln2)); // ln(2)
- __m256 one = _mm256_loadu_ps(reinterpret_cast<const float*>(c_1)); // 1.0f
+ __m256 one = _mm256_loadu_ps(reinterpret_cast<const float*>(c_1)); // 1.0F
__m256i exponent_bias = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(c_bias)); // 127
__m256 exp_pol1 = _mm256_loadu_ps(reinterpret_cast<const float*>(c_p1)); // p1 = 0.999999701f
__m256 exp_pol2 = _mm256_loadu_ps(reinterpret_cast<const float*>(c_p2)); // p2 = 0.499991506f
@@ -487,7 +560,7 @@ inline void scale_add2_reduce_max(float* a,
if (has_sparse_mask) {
size_t mask_idx = i / sparse_block_size;
uint8_t mask_val = sparse_mask[mask_idx];
- float32x4_t v_mask_block = vdupq_n_f32(mask_val ? 0.f : -FLT_MAX);
+ float32x4_t v_mask_block = vdupq_n_f32(mask_val ? 0.0F : -FLT_MAX);
v_a = vaddq_f32(v_a, v_mask_block);
}
@@ -497,9 +570,9 @@ inline void scale_add2_reduce_max(float* a,
uint32x4_t v_maski32_low = vmovl_u16(vget_low_u16(v_maski16));
uint32x4_t v_maski32_high = vmovl_u16(vget_high_u16(v_maski16));
uint32x4_t v_maski32[2] = {v_maski32_low, v_maski32_high};
- for (int j = 0; j < 2; ++j) {
- uint32x4_t kmask = vceqq_u32(v_maski32[j], v_zeroi32); // ==0
- v_a = vbslq_f32(kmask, v_nfltmax, v_a); // mask => -FLT_MAX
+ for (const auto& mask_vec : v_maski32) {
+ uint32x4_t kmask = vceqq_u32(mask_vec, v_zeroi32); // ==0
+ v_a = vbslq_f32(kmask, v_nfltmax, v_a); // mask => -FLT_MAX
}
}
@@ -523,7 +596,7 @@ inline void scale_add2_reduce_max(float* a,
if (has_sparse_mask) {
size_t mask_idx = i / sparse_block_size;
uint8_t mask_val = sparse_mask[mask_idx];
- a[i] += (mask_val ? 0.f : -FLT_MAX);
+ a[i] += (mask_val ? 0.0F : -FLT_MAX);
}
if (has_causal_mask) {
@@ -585,7 +658,7 @@ inline void scale_add2_reduce_max(ov::float16* a,
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);
+ 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));
@@ -716,7 +789,7 @@ static inline void exp_ps_avx512(__m512& src) {
__m512 exp_log2ef = _mm512_loadu_ps(reinterpret_cast<const float*>(c_e)); // log2(e)
__m512 half = _mm512_loadu_ps(reinterpret_cast<const float*>(c_half)); // 0.5f
__m512 ln2f = _mm512_loadu_ps(reinterpret_cast<const float*>(c_ln2)); // ln(2)
- __m512 one = _mm512_loadu_ps(reinterpret_cast<const float*>(c_1)); // 1.0f
+ __m512 one = _mm512_loadu_ps(reinterpret_cast<const float*>(c_1)); // 1.0F
__m512i exponent_bias = _mm512_loadu_si512(c_bias); // 127
__m512 exp_pol1 = _mm512_loadu_ps(reinterpret_cast<const float*>(c_p1)); // p1 = 0.999999701f
__m512 exp_pol2 = _mm512_loadu_ps(reinterpret_cast<const float*>(c_p2)); // p2 = 0.499991506f
@@ -782,7 +855,7 @@ inline void exp_reduce_sum(float* a, const float max, const size_t size, float&
#if defined(HAVE_AVX512F)
__m512 v_a;
auto v_max = _mm512_set1_ps(max);
- auto v_sum = _mm512_set1_ps(0.0f);
+ auto v_sum = _mm512_set1_ps(0.0F);
while (i + vec_len_f32_avx512 <= size) {
v_a = _mm512_loadu_ps(a + i);
v_a = _mm512_sub_ps(v_a, v_max);
@@ -806,7 +879,7 @@ inline void exp_reduce_sum(float* a, const float max, const size_t size, float&
#elif defined(HAVE_AVX2)
__m256 v_a;
auto v_max = _mm256_set1_ps(max);
- auto v_sum = _mm256_set1_ps(0.0f);
+ auto v_sum = _mm256_set1_ps(0.0F);
while (i + vec_len_f32_avx2 <= size) {
v_a = _mm256_loadu_ps(a + i);
v_a = _mm256_sub_ps(v_a, v_max);
@@ -833,7 +906,7 @@ inline void exp_reduce_sum(float* a, const float max, const size_t size, float&
# if defined(HAVE_SVE)
svfloat32_t v_a;
svfloat32_t v_max = svdup_n_f32(max);
- svfloat32_t v_sum = svdup_n_f32(0.0f);
+ svfloat32_t v_sum = svdup_n_f32(0.0F);
size_t inc = vec_len_f32_sve();
svbool_t pg = svptrue_b32();
@@ -853,7 +926,7 @@ inline void exp_reduce_sum(float* a, const float max, const size_t size, float&
# else
float32x4_t v_a;
float32x4_t v_max = vdupq_n_f32(max);
- float32x4_t v_sum = vdupq_n_f32(0.0f);
+ float32x4_t v_sum = vdupq_n_f32(0.0F);
while (i + vec_len_f32_neon <= size) {
v_a = vld1q_f32(a + i);
@@ -877,7 +950,7 @@ inline void exp_reduce_sum_f32(ov::float16* a, const ov::float16 max, const size
size_t i = 0;
# if defined(HAVE_SVE)
svfloat32_t v_max = svdup_n_f32(static_cast<float>(max));
- svfloat32_t v_sum = svdup_n_f32(0.0f);
+ svfloat32_t v_sum = svdup_n_f32(0.0F);
svbool_t pg_f32 = svptrue_b32();
svbool_t pg_f16 = svptrue_b16();
@@ -906,7 +979,7 @@ inline void exp_reduce_sum_f32(ov::float16* a, const ov::float16 max, const size
# else
float32x4_t v_a;
float32x4_t v_max = vdupq_n_f32(static_cast<float>(max));
- float32x4_t v_sum = vdupq_n_f32(0.0f);
+ float32x4_t v_sum = vdupq_n_f32(0.0F);
// Process 4 FP32 elements at a time
for (; i + vec_len_f32_neon <= size; i += vec_len_f32_neon) {
@@ -929,7 +1002,7 @@ inline void exp_reduce_sum_f32(ov::float16* a, const ov::float16 max, const size
# endif
// Handle remaining elements
for (; i < size; ++i) {
- float val = exp(static_cast<float>(a[i] - max));
+ const float val = std::exp(static_cast<float>(a[i] - max));
a[i] = static_cast<ov::float16>(val);
total_sum += val;
}
@@ -944,7 +1017,7 @@ inline void exp_reduce_sum(ov::float16* a, const ov::float16 max, const size_t s
# if defined(HAVE_SVE)
svfloat16_t v_a;
svfloat16_t v_max = svdup_n_f16(max);
- svfloat16_t v_sum = svdup_n_f16(0.0f);
+ svfloat16_t v_sum = svdup_n_f16(0.0F);
svbool_t pg = svptrue_b16();
size_t inc = vec_len_f16_sve();
@@ -965,7 +1038,7 @@ inline void exp_reduce_sum(ov::float16* a, const ov::float16 max, const size_t s
const size_t vec_len_f16_neon = 8;
float16x8_t v_a;
float16x8_t v_max = vdupq_n_f16(max);
- float16x8_t v_sum = vdupq_n_f16(0.0f);
+ float16x8_t v_sum = vdupq_n_f16(0.0F);
for (; i + vec_len_f16_neon <= size; i += vec_len_f16_neon) {
v_a = vld1q_f16(reinterpret_cast<const float16_t*>(a + i));
@@ -1056,7 +1129,7 @@ inline void multiply_scalar(float* a, float* a_dst, const float val, const size_
}
template <typename T, typename = std::enable_if_t<ov::intel_cpu::any_of_v<T, ov::bfloat16, ov::float16>>>
-inline void multiply_scalar(float* a, T* a_dst, const float val, const size_t size) {
+inline void multiply_scalar(const float* a, T* a_dst, const float val, const size_t size) {
size_t i = 0;
#if defined(HAVE_AVX512F)
auto v_scale = _mm512_set1_ps(val);
@@ -1099,7 +1172,7 @@ inline void multiply_scalar(ov::float16* a, float* a_dst, const ov::float16 val,
}
for (; i < size; ++i) {
- float a_f32 = static_cast<float>(a[i]);
+ const auto a_f32 = static_cast<float>(a[i]);
a_dst[i] = a_f32 * static_cast<float>(val);
}
}
@@ -1208,6 +1281,11 @@ inline void attn_softmax_kernel<float>(float* a,
float&,
const uint8_t*,
size_t);
+#if defined(OPENVINO_ARCH_ARM64)
+ if (detail::handle_empty_len(len, a_dst, dst_precision, total_size)) {
+ return;
+ }
+#endif
using func_f16_type = void (*)(float*,
float,
const float*,
@@ -1308,17 +1386,23 @@ inline void attn_softmax_kernel<float>(float* a,
sparse_block_size);
}
- float sum = 0.0f;
+ float sum = 0.0F;
if (sink != nullptr) {
max = max > (*sink) ? max : (*sink);
}
+#if defined(OPENVINO_ARCH_ARM64)
+ if (std::isinf(max) && max > 0.0F) {
+ detail::handle_inf_logits(a, a_dst, dst_precision, len, total_size, sink);
+ return;
+ }
+#endif
// exp sum
exp_reduce_sum(a, max, len, sum);
if (sink != nullptr) {
sum += std::exp(*sink - max);
}
// divide sum
- float scalar = 1.0f / sum;
+ float scalar = 1.0F / sum;
if (dst_precision == ov::element::f32) {
multiply_scalar(a, reinterpret_cast<float*>(a_dst), scalar, len);
// apply causual mask to final result instead of attn_score
@@ -1412,6 +1496,11 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
scale_add2_reduce_max<true, true, false>,
scale_add2_reduce_max<true, true, true>};
int dispatch = (alibi ? 0b100 : 0) | (attn_mask ? 0b010 : 0) | (causal_mask ? 0b001 : 0);
+# if defined(OPENVINO_ARCH_ARM64)
+ if (detail::handle_empty_len(len, a_dst, dst_precision, total_size)) {
+ return;
+ }
+# endif
ov::float16 max = std::numeric_limits<ov::float16>::lowest();
if (attn_mask_prec == ov::element::f16) {
funcs_fp16[dispatch](a,
@@ -1447,17 +1536,24 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
max);
}
- ov::float16 sum = 0.0f;
+ ov::float16 sum = 0.0F;
+# if defined(OPENVINO_ARCH_ARM64)
+ const float max_f = static_cast<float>(max);
+ if (std::isinf(max_f) && max_f > 0.0F) {
+ detail::handle_inf_logits(a, a_dst, dst_precision, len, total_size, sink);
+ return;
+ }
+# endif
if (dst_precision == ov::element::f32) {
exp_reduce_sum_f32(a, max, len, sum);
- ov::float16 scalar = 1.0f / sum;
+ ov::float16 scalar = 1.0F / sum;
multiply_scalar(a, static_cast<float*>(a_dst), scalar, len);
// apply causual mask to final result instead of attn_score
if (total_size > len)
memset(static_cast<float*>(a_dst) + len, 0, sizeof(float) * (total_size - len));
} else {
exp_reduce_sum_f32(a, max, len, sum);
- ov::float16 scalar = 1.0f / sum;
+ ov::float16 scalar = 1.0F / sum;
multiply_scalar_f32(a, static_cast<ov::float16*>(a_dst), scalar, len);
// apply causual mask to final result instead of attn_score
if (total_size > len)
diff --git a/src/plugins/intel_cpu/src/nodes/reorder.cpp b/src/plugins/intel_cpu/src/nodes/reorder.cpp
index 52d0183b646..77c0e9a1a53 100644
--- a/src/plugins/intel_cpu/src/nodes/reorder.cpp
+++ b/src/plugins/intel_cpu/src/nodes/reorder.cpp
@@ -16,7 +16,6 @@
#include <oneapi/dnnl/dnnl_common.hpp>
#include <shape_inference/shape_inference_pass_through.hpp>
#include <string>
-#include <utility>
#include <vector>
#include "cache/multi_cache.h"
@@ -24,16 +23,31 @@
#include "cpu_memory.h"
#include "cpu_types.h"
#include "graph_context.h"
-#include "memory_desc/cpu_blocked_memory_desc.h"
+#if defined(OPENVINO_ARCH_ARM)
+# include "memory_desc/cpu_blocked_memory_desc.h"
+#endif
+#if defined(OV_CPU_WITH_ACL)
+# include "memory_desc/blocked_memory_desc.h"
+#endif
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "node.h"
#include "nodes/common/cpu_convert.h"
#include "nodes/common/cpu_memcpy.h"
#include "nodes/common/reorder_prim.h"
-#include "nodes/executors/executor.hpp"
-#include "nodes/executors/transpose.hpp"
-#include "nodes/executors/transpose_list.hpp"
+#if defined(OPENVINO_ARCH_ARM)
+# include "nodes/executors/transpose.hpp"
+# include "nodes/executors/transpose_list.hpp"
+#endif
+#if defined(OV_CPU_WITH_ACL)
+# include <arm_compute/core/CoreTypes.h>
+# include <arm_compute/core/TensorInfo.h>
+# include <arm_compute/core/TensorShape.h>
+# include <arm_compute/runtime/NEON/functions/NECopy.h>
+# include <arm_compute/runtime/Tensor.h>
+
+# include "nodes/executors/acl/acl_utils.hpp"
+#endif
#include "nodes/node_config.h"
#include "onednn/iml_type_mapper.h"
#include "openvino/core/except.hpp"
@@ -156,6 +170,7 @@ void Reorder::executeDynamicImpl(const dnnl::stream& strm) {
execute(strm);
}
+#if defined(OPENVINO_ARCH_ARM)
void Reorder::prepareReorderAsTranspose(const MemoryDescPtr& parentDesc, const MemoryDescPtr& childDesc) {
auto getOrderAndBlockedDims = [](const MemoryDesc& lhs,
const MemoryDesc& rhs) -> std::pair<std::vector<size_t>, std::vector<size_t>> {
@@ -204,6 +219,47 @@ void Reorder::prepareReorderAsTranspose(const MemoryDescPtr& parentDesc, const M
transposeExecutor = factory->makeExecutor(transposeParams, {parentDesc}, {transposedDesc}, attr);
getSelectedPrimitiveDescriptor()->setImplementationType(transposeExecutor->implType());
}
+#endif
+
+#if defined(OV_CPU_WITH_ACL)
+bool Reorder::prepareAclCopy(const MemoryDescPtr& parentDesc, const MemoryDescPtr& childDesc) {
+ if (!parentDesc->isCompatible(*childDesc)) {
+ return false;
+ }
+
+ const auto aclPrecision = precisionToAclDataType(parentDesc->getPrecision());
+ if (aclPrecision == arm_compute::DataType::UNKNOWN) {
+ return false;
+ }
+
+ const auto* parentBlocked = parentDesc->as<BlockedMemoryDesc>();
+ if (parentBlocked == nullptr) {
+ return false;
+ }
+
+ const auto elemCount = parentBlocked->getPaddedElementsCount();
+ if (elemCount == 0) {
+ return false;
+ }
+
+ const arm_compute::TensorInfo tensorInfo(arm_compute::TensorShape(elemCount), 1, aclPrecision);
+ const auto status = arm_compute::NECopy::validate(&tensorInfo, &tensorInfo);
+ if (!status) {
+ DEBUG_LOG("NECopy validation failed: ", status.error_description());
+ return false;
+ }
+
+ aclSrcTensor.allocator()->init(tensorInfo);
+ aclDstTensor.allocator()->init(tensorInfo);
+ aclCopy = std::make_unique<arm_compute::NECopy>();
+ configureThreadSafe([&] {
+ aclCopy->configure(&aclSrcTensor, &aclDstTensor);
+ });
+ useAclCopy = true;
+ getSelectedPrimitiveDescriptor()->setImplementationType(impl_desc_type::acl);
+ return true;
+}
+#endif
void Reorder::prepareParams() {
if (isOptimized) {
@@ -229,9 +285,7 @@ void Reorder::prepareParams() {
const auto& parentDesc = srcMemPtr->getDescPtr();
const auto& childDesc = dstMemPtr->getDescPtr();
-#if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
- // @todo current oneDNN v3.2 lacks optimized jit implementation for fp16 reorders.
- // Use transpose executor as a temporary WA.
+#if defined(OPENVINO_ARCH_ARM)
if (all_of(ov::element::f16, parentDesc->getPrecision(), childDesc->getPrecision()) &&
((parentDesc->hasLayoutType(LayoutType::ncsp) && childDesc->hasLayoutType(LayoutType::nspc)) ||
(parentDesc->hasLayoutType(LayoutType::nspc) && childDesc->hasLayoutType(LayoutType::ncsp))) &&
@@ -241,6 +295,12 @@ void Reorder::prepareParams() {
}
#endif
+#if defined(OV_CPU_WITH_ACL)
+ if (prepareAclCopy(parentDesc, childDesc)) {
+ return;
+ }
+#endif
+
if ((isNspc2NcspCase || isNcsp2NspcCase) && isSupportedDesc(*childDesc) && isSupportedDesc(*parentDesc)) {
const auto& inDims = srcMemPtr->getStaticDims();
// Check that child strides are consistent with parent dims if the child is inplace.
@@ -419,7 +479,7 @@ void Reorder::optimizedNspc2Ncsp() {
}
void Reorder::execute(const dnnl::stream& strm) {
-#if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
+#if defined(OPENVINO_ARCH_ARM)
if (transposeExecutor) {
auto dstMemPtr = getDstMemoryAtPort(0);
auto srcMemPtr = getSrcMemoryAtPort(0);
@@ -428,6 +488,19 @@ void Reorder::execute(const dnnl::stream& strm) {
}
#endif
+#if defined(OV_CPU_WITH_ACL)
+ if (useAclCopy) {
+ auto dstMemPtr = getDstMemoryAtPort(0);
+ auto srcMemPtr = getSrcMemoryAtPort(0);
+ aclSrcTensor.allocator()->import_memory(srcMemPtr->getData());
+ aclDstTensor.allocator()->import_memory(dstMemPtr->getData());
+ aclCopy->run();
+ aclSrcTensor.allocator()->free();
+ aclDstTensor.allocator()->free();
+ return;
+ }
+#endif
+
if (isOptimized) {
DEBUG_LOG("#",
getExecIndex(),
diff --git a/src/plugins/intel_cpu/src/nodes/reorder.h b/src/plugins/intel_cpu/src/nodes/reorder.h
index d5f15121116..ddfc6f83404 100644
--- a/src/plugins/intel_cpu/src/nodes/reorder.h
+++ b/src/plugins/intel_cpu/src/nodes/reorder.h
@@ -17,10 +17,17 @@
#include "graph_context.h"
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/dnnl_memory_desc.h"
-#include "nodes/executors/transpose.hpp"
#include "onednn/iml_type_mapper.h"
#include "openvino/core/node.hpp"
-
+#include "thread_pool_imp.hpp"
+
+#if defined(OPENVINO_ARCH_ARM)
+# include "nodes/executors/transpose.hpp"
+#endif
+#if defined(OV_CPU_WITH_ACL)
+# include <arm_compute/runtime/NEON/functions/NECopy.h>
+# include <arm_compute/runtime/Tensor.h>
+#endif
namespace ov::intel_cpu::node {
class Reorder : public Node {
@@ -94,8 +101,18 @@ private:
void optimizedNcsp2Nspc();
void createReorderPrimitive(const DnnlMemoryDescPtr& srcDesc, const DnnlMemoryDescPtr& dstDesc);
+#if defined(OPENVINO_ARCH_ARM)
void prepareReorderAsTranspose(const MemoryDescPtr& parentDesc, const MemoryDescPtr& childDesc);
TransposeExecutorPtr transposeExecutor;
+#endif
+
+#if defined(OV_CPU_WITH_ACL)
+ bool prepareAclCopy(const MemoryDescPtr& parentDesc, const MemoryDescPtr& childDesc);
+ std::unique_ptr<arm_compute::NECopy> aclCopy;
+ arm_compute::Tensor aclSrcTensor;
+ arm_compute::Tensor aclDstTensor;
+ bool useAclCopy = false;
+#endif
};
} // namespace ov::intel_cpu::node
diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
index 57a1a08a94b..fe2361daa16 100644
--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
@@ -10,6 +10,7 @@
#include <common/utils.hpp>
#include <cstddef>
#include <cstdint>
+#include <cstdlib>
#include <cstring>
#include <memory>
#include <oneapi/dnnl/dnnl.hpp>
@@ -39,9 +40,11 @@
#include "utils/general_utils.h"
#include "utils/plain_tensor.hpp"
-#ifdef OPENVINO_ARCH_X86_64
+#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_X86)
# include "openvino/core/type/bfloat16.hpp"
# include "openvino/core/type/float16.hpp"
+#elif defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
+# include "openvino/core/type/float16.hpp"
#endif
#ifdef OV_CPU_WITH_MLAS
@@ -123,10 +126,33 @@ struct MHAKernel {
}
void softmax(float* a, int len, const float* sink) {
+#if defined(OPENVINO_ARCH_ARM64)
+ if (len == 0) {
+ return;
+ }
+#endif
float max = *std::max_element(a, a + len);
if (sink != nullptr) {
max = max > (*sink) ? max : (*sink);
}
+#if defined(OPENVINO_ARCH_ARM64)
+ if (std::isinf(max) && max > 0.0F) {
+ size_t inf_count = 0;
+ if (sink != nullptr && std::isinf(*sink) && *sink > 0.0F) {
+ inf_count++;
+ }
+ for (int i = 0; i < len; i++) {
+ if (std::isinf(a[i]) && a[i] > 0.0F) {
+ inf_count++;
+ }
+ }
+ const float inv = inf_count ? (1.0F / static_cast<float>(inf_count)) : 0.0F;
+ for (int i = 0; i < len; i++) {
+ a[i] = (inf_count && std::isinf(a[i]) && a[i] > 0.0F) ? inv : 0.0F;
+ }
+ return;
+ }
+#endif
float sum = 0.0F;
for (int i = 0; i < len; i++) {
a[i] = exp(a[i] - max);
@@ -469,22 +495,27 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
cmask_stride = causal_mask.stride(2);
}
}
+ auto row_ptr = [](auto* base, size_t stride, size_t m) {
+ return base ? base + m * stride : nullptr;
+ };
+ auto sink_ptr = [&](size_t m) {
+ return sink_input ? &sink_input.at<float>({b, h, m, 0}, true) : nullptr;
+ };
for (size_t m = m_start; m < m_end; m++) {
// apply attention mask & sofmax
auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
auto* score = weight_score.ptr<float>(ithr, 0, m - m_start);
- float* sink = nullptr;
- if (sink_input) {
- sink = &sink_input.at<float>({b, h, m, 0}, true);
- }
- uint8_t* attn_mask_row = attn_mask_ptr ? attn_mask_ptr + m * attn_mask_stride : nullptr;
+ auto* sink = sink_ptr(m);
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ auto* alibi_row = row_ptr(alibi_ptr, alibi_stride, m);
attn_softmax(reinterpret_cast<void*>(score),
reinterpret_cast<T*>(score),
d_scale,
- reinterpret_cast<void*>(alibi_ptr + m * alibi_stride),
+ reinterpret_cast<void*>(alibi_row),
attn_mask_row,
- cmask_ptr + m * cmask_stride,
+ cmask_row,
select_nfltmax_at_0,
ncausal,
kv_len,
@@ -652,6 +683,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ACL, T> {
auto q_ptr = &query.at<T>({b, h, m_start, 0});
auto k_ptr = &present_key.at<T>({b, h / h_each_group_len, 0, 0});
auto v_ptr = &present_value.at<T>({b, h / h_each_group_len, 0, 0});
+ const bool b_transpose = (k_stride_s == 1);
T* alibi_ptr = nullptr;
auto alibi_stride = 0;
@@ -679,13 +711,212 @@ struct MHAKernel<ScaledDotProductAttention::KT_ACL, T> {
}
}
+# if defined(OPENVINO_ARCH_ARM64)
+ auto run_f32_path = [&]() {
+ std::vector<float> q_f32(m_cnt * head_size);
+ std::vector<float> k_f32(kv_len * head_size);
+ std::vector<float> v_f32(kv_len * head_size_v);
+
+ const size_t q_stride_m = query.stride(2);
+ const size_t q_stride_s = query.stride(3);
+ for (size_t m = 0; m < m_cnt; m++) {
+ const T* q_row = q_ptr + m * q_stride_m;
+ for (size_t s = 0; s < head_size; s++) {
+ q_f32[m * head_size + s] = static_cast<float>(q_row[s * q_stride_s]);
+ }
+ }
+
+ const size_t k_stride_m = present_key.stride(2);
+ const size_t k_stride_s = present_key.stride(3);
+ for (size_t n = 0; n < kv_len; n++) {
+ const T* k_row = k_ptr + n * k_stride_m;
+ for (size_t s = 0; s < head_size; s++) {
+ k_f32[n * head_size + s] = static_cast<float>(k_row[s * k_stride_s]);
+ }
+ }
+
+ const size_t v_stride_m = present_value.stride(2);
+ const size_t v_stride_s = present_value.stride(3);
+ for (size_t n = 0; n < kv_len; n++) {
+ const T* v_row = v_ptr + n * v_stride_m;
+ for (size_t s = 0; s < head_size_v; s++) {
+ v_f32[n * head_size_v + s] = static_cast<float>(v_row[s * v_stride_s]);
+ }
+ }
+
+ const bool b_transpose_f32 = true;
+ GemmKernel qk_gemm(m_cnt, head_size, kv_len, b_transpose_f32, ov::element::f32);
+ arm_compute::Tensor qkTensor;
+ arm_compute::TensorInfo qkInfo;
+ arm_compute::Strides qStrides({sizeof(float), head_size * sizeof(float)});
+ arm_compute::Strides kStrides({sizeof(float), head_size * sizeof(float)});
+ qk_gemm.executeGemm(reinterpret_cast<void*>(q_f32.data()),
+ reinterpret_cast<void*>(k_f32.data()),
+ qkInfo,
+ qkTensor,
+ qStrides,
+ kStrides);
+
+ auto* qk = reinterpret_cast<float*>(qkTensor.buffer());
+ const size_t qk_row_stride = qkInfo.strides_in_bytes()[1] / sizeof(float);
+ bool qk_has_nan = false;
+ for (size_t m = 0; m < m_cnt && !qk_has_nan; m++) {
+ const float* row = qk + m * qk_row_stride;
+ for (size_t n = 0; n < kv_len; n++) {
+ if (!std::isfinite(row[n])) {
+ qk_has_nan = true;
+ break;
+ }
+ }
+ }
+
+ std::vector<float> qk_f32;
+ float* qk_ptr = qk;
+ size_t qk_ptr_stride = qk_row_stride;
+ if (qk_has_nan) {
+ qk_f32.assign(m_cnt * kv_len, 0.0F);
+ for (size_t m = 0; m < m_cnt; m++) {
+ float* dst_row = qk_f32.data() + m * kv_len;
+ const float* q_row = q_f32.data() + m * head_size;
+ for (size_t n = 0; n < kv_len; n++) {
+ const float* k_row = k_f32.data() + n * head_size;
+ float sum = 0.0F;
+ for (size_t s = 0; s < head_size; s++) {
+ sum += q_row[s] * k_row[s];
+ }
+ dst_row[n] = sum;
+ }
+ }
+ qk_ptr = qk_f32.data();
+ qk_ptr_stride = kv_len;
+ }
+
+ float* alibi_ptr_f32 = nullptr;
+ size_t alibi_stride_f32 = 0;
+ const auto alibi_prec = alibi_mask ? alibi_mask.get_precision() : ov::element::f32;
+ const bool alibi_needs_convert = alibi_mask && (alibi_prec != ov::element::f32);
+ std::vector<float> alibi_row;
+ if (alibi_mask && !alibi_needs_convert) {
+ alibi_ptr_f32 = &alibi_mask.at<float>({b, h, 0, 0}, true);
+ if (alibi_mask.size(2) > 1) {
+ alibi_stride_f32 = alibi_mask.stride(2);
+ }
+ } else if (alibi_needs_convert) {
+ alibi_row.resize(kv_len);
+ }
+ auto row_ptr = [](auto* base, size_t stride, size_t m) {
+ return base ? base + m * stride : nullptr;
+ };
+ auto sink_ptr = [&](size_t m) {
+ return sink_input ? &sink_input.at<float>({b, h, m, 0}, true) : nullptr;
+ };
+
+ for (size_t m = m_start; m < m_end; m++) {
+ auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ float* alibi_row_ptr = nullptr;
+ if (alibi_ptr_f32) {
+ alibi_row_ptr = alibi_ptr_f32 + m * alibi_stride_f32;
+ } else if (alibi_needs_convert) {
+ const size_t b_idx = alibi_mask.size(0) > 1 ? b : 0;
+ const size_t h_idx = alibi_mask.size(1) > 1 ? h : 0;
+ const size_t m_idx = alibi_mask.size(2) > 1 ? m : 0;
+ const void* alibi_src = alibi_mask.ptr_v(b_idx, h_idx, m_idx, 0);
+ cpu_convert(alibi_src, alibi_row.data(), alibi_prec, ov::element::f32, kv_len);
+ alibi_row_ptr = alibi_row.data();
+ }
+ auto* sink = sink_ptr(m);
+ auto* qk_row = qk_ptr + (m - m_start) * qk_ptr_stride;
+ attn_softmax(reinterpret_cast<void*>(qk_row),
+ qk_row,
+ d_scale,
+ reinterpret_cast<void*>(alibi_row_ptr),
+ attn_mask_row,
+ cmask_row,
+ select_nfltmax_at_0,
+ ncausal,
+ kv_len,
+ ov::element::f32,
+ attn_mask_precision,
+ ov::element::f32,
+ sink);
+ }
+
+ bool out_has_nan = false;
+ std::vector<float> out_f32_fallback;
+ if (!qk_has_nan) {
+ GemmKernel out_gemm(m_cnt, kv_len, head_size_v, false, ov::element::f32);
+ arm_compute::TensorInfo outInfo;
+ arm_compute::Tensor outTensor;
+ arm_compute::Strides vStrides({sizeof(float), head_size_v * sizeof(float)});
+ out_gemm.executeGemm(qkTensor.buffer(),
+ reinterpret_cast<void*>(v_f32.data()),
+ outInfo,
+ outTensor,
+ qkInfo.strides_in_bytes(),
+ vStrides);
+
+ auto* out_f32 = reinterpret_cast<float*>(outTensor.buffer());
+ const size_t out_row_stride = outInfo.strides_in_bytes()[1] / sizeof(float);
+ for (size_t m = 0; m < m_cnt && !out_has_nan; m++) {
+ const float* row = out_f32 + m * out_row_stride;
+ for (size_t s = 0; s < head_size_v; s++) {
+ if (!std::isfinite(row[s])) {
+ out_has_nan = true;
+ break;
+ }
+ }
+ }
+
+ if (!out_has_nan) {
+ const int out_col_dim = has_out_transpose ? 2 : 3;
+ const size_t out_stride_s = output_emb.stride(out_col_dim);
+ for (size_t m = 0; m < m_cnt; m++) {
+ auto* out_dst = has_out_transpose ? &output_emb.at<T>({b, m_start + m, h * head_size_v})
+ : &output_emb.at<T>({b, h, m_start + m});
+ const float* src_row = out_f32 + m * out_row_stride;
+ for (size_t s = 0; s < head_size_v; s++) {
+ out_dst[s * out_stride_s] = static_cast<T>(src_row[s]);
+ }
+ }
+ }
+ outTensor.allocator()->free();
+ }
+
+ if (qk_has_nan || out_has_nan) {
+ out_f32_fallback.assign(m_cnt * head_size_v, 0.0F);
+ for (size_t m = 0; m < m_cnt; m++) {
+ const float* qk_row = qk_ptr + m * qk_ptr_stride;
+ float* out_row = out_f32_fallback.data() + m * head_size_v;
+ for (size_t s = 0; s < head_size_v; s++) {
+ float sum = 0.0F;
+ for (size_t n = 0; n < kv_len; n++) {
+ sum += qk_row[n] * v_f32[n * head_size_v + s];
+ }
+ out_row[s] = sum;
+ }
+ }
+
+ const int out_col_dim = has_out_transpose ? 2 : 3;
+ const size_t out_stride_s = output_emb.stride(out_col_dim);
+ for (size_t m = 0; m < m_cnt; m++) {
+ auto* out_dst = has_out_transpose ? &output_emb.at<T>({b, m_start + m, h * head_size_v})
+ : &output_emb.at<T>({b, h, m_start + m});
+ const float* src_row = out_f32_fallback.data() + m * head_size_v;
+ for (size_t s = 0; s < head_size_v; s++) {
+ out_dst[s * out_stride_s] = static_cast<T>(src_row[s]);
+ }
+ }
+ }
+ qkTensor.allocator()->free();
+ };
+
+# endif
+
arm_compute::Tensor qkTensor;
arm_compute::TensorInfo qkInfo;
- bool b_transpose = false;
- if (k_stride_s == 1) {
- b_transpose = true;
- }
GemmKernel qk_gemm(m_cnt, head_size, kv_len, b_transpose, precision);
arm_compute::Strides qStrides({query.stride_bytes(3), query.stride_bytes(2)});
@@ -698,31 +929,146 @@ struct MHAKernel<ScaledDotProductAttention::KT_ACL, T> {
kStrides);
auto qk = reinterpret_cast<T*>(qkTensor.buffer());
-
+# if defined(OPENVINO_ARCH_ARM64)
+ const size_t qk_row_stride = qkInfo.strides_in_bytes()[1] / sizeof(T);
+ std::vector<float> qk_row_f32;
+ if constexpr (std::is_same_v<T, ov::float16>) {
+ bool qk_has_nonfinite = false;
+ for (size_t m_check = 0; m_check < m_cnt && !qk_has_nonfinite; m_check++) {
+ const T* row = qk + m_check * qk_row_stride;
+ for (size_t n = 0; n < kv_len; n++) {
+ const auto v = static_cast<float>(row[n]);
+ if (!std::isfinite(v)) {
+ qk_has_nonfinite = true;
+ break;
+ }
+ }
+ }
+ if (qk_has_nonfinite) {
+ qkTensor.allocator()->free();
+ run_f32_path();
+ return;
+ }
+ qk_row_f32.resize(kv_len);
+ }
+# endif
+ auto row_ptr = [](auto* base, size_t stride, size_t m) {
+ return base ? base + m * stride : nullptr;
+ };
+ auto sink_ptr = [&](size_t m) {
+ return sink_input ? &sink_input.at<float>({b, h, m, 0}, true) : nullptr;
+ };
+# if defined(OPENVINO_ARCH_ARM64)
+ if constexpr (std::is_same_v<T, ov::float16>) {
+ float* alibi_ptr_f32 = nullptr;
+ size_t alibi_stride_f32 = 0;
+ const auto alibi_prec = alibi_mask ? alibi_mask.get_precision() : ov::element::f32;
+ const bool alibi_needs_convert = alibi_mask && (alibi_prec != ov::element::f32);
+ std::vector<float> alibi_row;
+ if (alibi_mask && !alibi_needs_convert) {
+ alibi_ptr_f32 = &alibi_mask.at<float>({b, h, 0, 0}, true);
+ if (alibi_mask.size(2) > 1) {
+ alibi_stride_f32 = alibi_mask.stride(2);
+ }
+ } else if (alibi_needs_convert) {
+ alibi_row.resize(kv_len);
+ }
+ for (size_t m = m_start; m < m_end; m++) {
+ // apply attention mask & sofmax
+ auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ float* alibi_row_ptr = nullptr;
+ if (alibi_ptr_f32) {
+ alibi_row_ptr = alibi_ptr_f32 + m * alibi_stride_f32;
+ } else if (alibi_needs_convert) {
+ const size_t b_idx = alibi_mask.size(0) > 1 ? b : 0;
+ const size_t h_idx = alibi_mask.size(1) > 1 ? h : 0;
+ const size_t m_idx = alibi_mask.size(2) > 1 ? m : 0;
+ const void* alibi_src = alibi_mask.ptr_v(b_idx, h_idx, m_idx, 0);
+ cpu_convert(alibi_src, alibi_row.data(), alibi_prec, ov::element::f32, kv_len);
+ alibi_row_ptr = alibi_row.data();
+ }
+ auto* sink = sink_ptr(m);
+ T* qk_row = qk + (m - m_start) * qk_row_stride;
+ for (size_t n = 0; n < kv_len; n++) {
+ qk_row_f32[n] = static_cast<float>(qk_row[n]);
+ }
+ attn_softmax(reinterpret_cast<void*>(qk_row_f32.data()),
+ qk_row_f32.data(),
+ d_scale,
+ reinterpret_cast<void*>(alibi_row_ptr),
+ attn_mask_row,
+ cmask_row,
+ select_nfltmax_at_0,
+ ncausal,
+ kv_len,
+ ov::element::f32,
+ attn_mask_precision,
+ ov::element::f32,
+ sink);
+ for (size_t n = 0; n < kv_len; n++) {
+ qk_row[n] = static_cast<T>(qk_row_f32[n]);
+ }
+ }
+ } else {
+ for (size_t m = m_start; m < m_end; m++) {
+ // apply attention mask & sofmax
+ auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ auto* alibi_row = row_ptr(alibi_ptr, alibi_stride, m);
+ auto* sink = sink_ptr(m);
+ attn_softmax(reinterpret_cast<void*>(qk + (m - m_start) * qk_row_stride),
+ qk + (m - m_start) * qk_row_stride,
+ d_scale,
+ reinterpret_cast<void*>(alibi_row),
+ attn_mask_row,
+ cmask_row,
+ select_nfltmax_at_0,
+ ncausal,
+ kv_len,
+ precision,
+ attn_mask_precision,
+ precision,
+ sink);
+ }
+ }
+# else
for (size_t m = m_start; m < m_end; m++) {
// apply attention mask & sofmax
auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
- uint8_t* attn_mask_row = attn_mask_ptr ? attn_mask_ptr + m * attn_mask_stride : nullptr;
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ auto* alibi_row = row_ptr(alibi_ptr, alibi_stride, m);
+ auto* sink = sink_ptr(m);
attn_softmax(reinterpret_cast<void*>(qk + (m - m_start) * kv_len),
qk + (m - m_start) * kv_len,
d_scale,
- reinterpret_cast<void*>(alibi_ptr + m * alibi_stride),
+ reinterpret_cast<void*>(alibi_row),
attn_mask_row,
- cmask_ptr + m * cmask_stride,
+ cmask_row,
select_nfltmax_at_0,
ncausal,
kv_len,
precision,
attn_mask_precision,
precision,
- nullptr);
+ sink);
}
+# endif
arm_compute::TensorInfo outInfo;
arm_compute::Tensor outTensor;
auto out = has_out_transpose ? &output_emb.at<T>({b, m_start, h * head_size_v})
: &output_emb.at<T>({b, h, m_start});
+# if defined(OPENVINO_ARCH_ARM64)
+ const int row_dim = has_out_transpose ? 1 : 2;
+ const int col_dim = has_out_transpose ? 2 : 3;
+ auto strides = arm_compute::Strides({output_emb.stride_bytes(col_dim), output_emb.stride_bytes(row_dim)});
+# else
auto strides = arm_compute::Strides({output_emb.stride_bytes(1), output_emb.stride_bytes(2)});
+# endif
GemmKernel out_gemm(m_cnt, kv_len, head_size_v, false, precision);
arm_compute::Strides vStrides({present_value.stride_bytes(3), present_value.stride_bytes(2)});
@@ -880,19 +1226,25 @@ struct MHAKernel<ScaledDotProductAttention::KT_MLAS, float> {
1);
}
+ auto row_ptr = [](auto* base, size_t stride, size_t m) {
+ return base ? base + m * stride : nullptr;
+ };
+ auto sink_ptr = [&](size_t m) {
+ return sink_input ? &sink_input.at<float>({b, h, m, 0}, true) : nullptr;
+ };
for (size_t m = m_start; m < m_end; m++) {
// apply attention mask & sofmax
auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
- float* sink = nullptr;
- if (sink_input) {
- sink = &sink_input.at<float>({b, h, m, 0}, true);
- }
+ auto* sink = sink_ptr(m);
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ auto* alibi_row = row_ptr(alibi_ptr, alibi_stride, m);
attn_softmax(reinterpret_cast<void*>(qk + (m - m_start) * qk_m_stride),
qk + (m - m_start) * qk_m_stride,
d_scale,
- reinterpret_cast<void*>(alibi_ptr + m * alibi_stride),
- attn_mask_ptr + m * attn_mask_stride,
- cmask_ptr + m * cmask_stride,
+ reinterpret_cast<void*>(alibi_row),
+ attn_mask_row,
+ cmask_row,
select_nfltmax_at_0,
ncausal,
kv_len,
@@ -1039,6 +1391,7 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
PlainTensor v_input; // f32[B, H|1, L1, S] / [B, H|1, L0+L1, S]
PlainTensor beam_table; // i32[B, max_kvLen]
PlainTensor attn_mask;
+ PlainTensor alibi_mask;
PlainTensor output_emb(output);
PlainTensor sink_input;
float scale_input = 0.0F;
@@ -1087,6 +1440,9 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
scale_input = *inputs[4]->getDataAs<float>();
}
}
+ if (input_num > 6) {
+ alibi_mask.reset(inputs[6]);
+ }
// q: [B, H, L1, S]
const auto& permute_axes = config.config.permute_axes;
@@ -1160,7 +1516,7 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
q_input,
k_input,
v_input,
- {},
+ alibi_mask,
use_attn_mask ? attn_mask : PlainTensor(),
output_emb,
has_out_transpose,
@@ -1176,7 +1532,7 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
kernel_single_token(q_input,
present_key,
present_value,
- {},
+ alibi_mask,
use_attn_mask ? attn_mask : PlainTensor(),
output_emb,
beam_table,
@@ -1187,6 +1543,192 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
v_scale_zp,
sink_input);
}
+
+#if defined(OPENVINO_ARCH_ARM64)
+ if (output_emb.get_precision() == ov::element::f16) {
+ bool has_nan = false;
+ if (output_emb.m_rank == 4) {
+ const auto Bn = output_emb.size(0);
+ const auto Hn = output_emb.size(1);
+ const auto Ln = output_emb.size(2);
+ const auto Sn = output_emb.size(3);
+ for (size_t b = 0; b < Bn && !has_nan; b++) {
+ for (size_t h = 0; h < Hn && !has_nan; h++) {
+ for (size_t l = 0; l < Ln && !has_nan; l++) {
+ for (size_t s = 0; s < Sn; s++) {
+ const float v = static_cast<float>(output_emb.at<ov::float16>({b, h, l, s}));
+ if (!std::isfinite(v)) {
+ has_nan = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+ } else if (output_emb.m_rank == 3) {
+ const auto Bn = output_emb.size(0);
+ const auto Ln = output_emb.size(1);
+ const auto HSn = output_emb.size(2);
+ for (size_t b = 0; b < Bn && !has_nan; b++) {
+ for (size_t l = 0; l < Ln && !has_nan; l++) {
+ for (size_t s = 0; s < HSn; s++) {
+ const float v = static_cast<float>(output_emb.at<ov::float16>({b, l, s}));
+ if (!std::isfinite(v)) {
+ has_nan = true;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if (has_nan) {
+ const auto& causal_mask = kernel.causal_mask;
+ const bool select_nfltmax_at_0 = kernel.select_nfltmax_at_0;
+ const size_t Bn = q_input.size(0);
+ const size_t Hn = q_input.size(1);
+ const size_t q_len = q_input.size(2);
+ const size_t head_size = q_input.size(3);
+ const size_t kv_len = present_key.size(2);
+ const size_t head_size_v = present_value.size(3);
+ const size_t h_group_num = present_key.size(1);
+ const size_t h_each_group_len = h_group_num ? (Hn / h_group_num) : 1;
+
+ float d_scale = scale_input;
+ if (d_scale == 0.0F) {
+ d_scale = 1.0F / static_cast<float>(sqrt(head_size));
+ }
+
+ const auto attn_mask_prec = use_attn_mask ? attn_mask.get_precision() : ov::element::f32;
+ const auto alibi_prec = alibi_mask ? alibi_mask.get_precision() : ov::element::f32;
+ const bool alibi_needs_convert = alibi_mask && (alibi_prec != ov::element::f32);
+
+ for (size_t b = 0; b < Bn; b++) {
+ for (size_t h = 0; h < Hn; h++) {
+ const size_t hk = h_group_num ? (h / h_each_group_len) : h;
+ std::vector<float> k_f32(kv_len * head_size);
+ std::vector<float> v_f32(kv_len * head_size_v);
+
+ const size_t k_stride_m = present_key.stride(2);
+ const size_t k_stride_s = present_key.stride(3);
+ const ov::float16* k_ptr = &present_key.at<ov::float16>({b, hk, 0, 0});
+ for (size_t n = 0; n < kv_len; n++) {
+ const ov::float16* k_row = k_ptr + n * k_stride_m;
+ for (size_t s = 0; s < head_size; s++) {
+ k_f32[n * head_size + s] = static_cast<float>(k_row[s * k_stride_s]);
+ }
+ }
+
+ const size_t v_stride_m = present_value.stride(2);
+ const size_t v_stride_s = present_value.stride(3);
+ const ov::float16* v_ptr = &present_value.at<ov::float16>({b, hk, 0, 0});
+ for (size_t n = 0; n < kv_len; n++) {
+ const ov::float16* v_row = v_ptr + n * v_stride_m;
+ for (size_t s = 0; s < head_size_v; s++) {
+ v_f32[n * head_size_v + s] = static_cast<float>(v_row[s * v_stride_s]);
+ }
+ }
+
+ uint8_t* attn_mask_ptr = nullptr;
+ size_t attn_mask_stride = 0;
+ if (use_attn_mask) {
+ const size_t mask_head = attn_mask.size(1) > 1 ? h : 0;
+ attn_mask_ptr = static_cast<uint8_t*>(attn_mask.ptr_v(b, mask_head, 0, 0));
+ if (attn_mask.size(2) > 1) {
+ attn_mask_stride = attn_mask.stride_bytes(2);
+ }
+ }
+ uint8_t* cmask_ptr = nullptr;
+ size_t cmask_stride = 0;
+ if (causal_mask) {
+ cmask_ptr = &causal_mask.template at<uint8_t>({b, h, 0, 0}, true);
+ if (causal_mask.size(2) > 1) {
+ cmask_stride = causal_mask.stride(2);
+ }
+ }
+ float* alibi_ptr_f32 = nullptr;
+ size_t alibi_stride_f32 = 0;
+ std::vector<float> alibi_row;
+ if (alibi_mask && !alibi_needs_convert) {
+ alibi_ptr_f32 = &alibi_mask.at<float>({b, h, 0, 0}, true);
+ if (alibi_mask.size(2) > 1) {
+ alibi_stride_f32 = alibi_mask.stride(2);
+ }
+ } else if (alibi_needs_convert) {
+ alibi_row.resize(kv_len);
+ }
+ auto row_ptr = [](auto* base, size_t stride, size_t m) {
+ return base ? base + m * stride : nullptr;
+ };
+ auto sink_ptr = [&](size_t m) {
+ return sink_input ? &sink_input.at<float>({b, h, m, 0}, true) : nullptr;
+ };
+
+ for (size_t m = 0; m < q_len; m++) {
+ std::vector<float> q_row(head_size);
+ const size_t q_stride_s = q_input.stride(3);
+ const ov::float16* q_ptr = &q_input.at<ov::float16>({b, h, m, 0});
+ for (size_t s = 0; s < head_size; s++) {
+ q_row[s] = static_cast<float>(q_ptr[s * q_stride_s]);
+ }
+
+ std::vector<float> scores(kv_len);
+ for (size_t n = 0; n < kv_len; n++) {
+ const float* k_row = k_f32.data() + n * head_size;
+ float sum = 0.0F;
+ for (size_t s = 0; s < head_size; s++) {
+ sum += q_row[s] * k_row[s];
+ }
+ scores[n] = sum;
+ }
+
+ auto ncausal = auto_causal ? (kv_len - q_len + m + 1) : kv_len;
+ auto* attn_mask_row = row_ptr(attn_mask_ptr, attn_mask_stride, m);
+ auto* cmask_row = row_ptr(cmask_ptr, cmask_stride, m);
+ float* alibi_row_ptr = nullptr;
+ if (alibi_ptr_f32) {
+ alibi_row_ptr = alibi_ptr_f32 + m * alibi_stride_f32;
+ } else if (alibi_needs_convert) {
+ const size_t b_idx = alibi_mask.size(0) > 1 ? b : 0;
+ const size_t h_idx = alibi_mask.size(1) > 1 ? h : 0;
+ const size_t m_idx = alibi_mask.size(2) > 1 ? m : 0;
+ const void* alibi_src = alibi_mask.ptr_v(b_idx, h_idx, m_idx, 0);
+ cpu_convert(alibi_src, alibi_row.data(), alibi_prec, ov::element::f32, kv_len);
+ alibi_row_ptr = alibi_row.data();
+ }
+ auto* sink = sink_ptr(m);
+ ov::Extensions::Cpu::XARCH::attn_softmax(scores.data(),
+ scores.data(),
+ d_scale,
+ reinterpret_cast<void*>(alibi_row_ptr),
+ attn_mask_row,
+ cmask_row,
+ select_nfltmax_at_0,
+ ncausal,
+ kv_len,
+ ov::element::f32,
+ attn_mask_prec,
+ ov::element::f32,
+ sink);
+
+ for (size_t s = 0; s < head_size_v; s++) {
+ float sum = 0.0F;
+ for (size_t n = 0; n < kv_len; n++) {
+ sum += scores[n] * v_f32[n * head_size_v + s];
+ }
+ if (has_out_transpose) {
+ output_emb.at<ov::float16>({b, m, h * head_size_v + s}) =
+ static_cast<ov::float16>(sum);
+ } else {
+ output_emb.at<ov::float16>({b, h, m, s}) = static_cast<ov::float16>(sum);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+#endif
}
};
@@ -1266,6 +1808,13 @@ void ScaledDotProductAttention::initSupportedPrimitiveDescriptors() {
creatorsMap.at(LayoutType::ncsp)->createSharedDesc(ov::element::f32, getInputShapeAtPort(nextPortIdx)));
nextPortIdx++;
}
+ // alibi
+ if (orginSDPInputNumber > 6) {
+ config.inConfs[nextPortIdx].setMemDesc(
+ creatorsMap.at(LayoutType::ncsp)
+ ->createSharedDesc(getOriginalInputPrecisionAtPort(nextPortIdx), getInputShapeAtPort(nextPortIdx)));
+ nextPortIdx++;
+ }
if (m_config.config.fuse_concat) {
// beam_idx
diff --git a/src/plugins/intel_cpu/thirdparty/ACLConfig.cmake b/src/plugins/intel_cpu/thirdparty/ACLConfig.cmake
index 33225b770f1..1ac029ecaa1 100644
--- a/src/plugins/intel_cpu/thirdparty/ACLConfig.cmake
+++ b/src/plugins/intel_cpu/thirdparty/ACLConfig.cmake
@@ -81,6 +81,8 @@ elseif(ENABLE_ARM_COMPUTE_CMAKE)
set(ARM_COMPUTE_GRAPH_ENABLED OFF CACHE BOOL "" FORCE)
set(ARM_COMPUTE_ENABLE_OPENMP OFF CACHE BOOL "" FORCE)
set(ARM_COMPUTE_ENABLE_CPPTHREADS OFF CACHE BOOL "" FORCE)
+ # Disable clang-tidy for ACL to avoid incompatible .clang-tidy settings.
+ set(ARM_COMPUTE_CLANG_TIDY "" CACHE STRING "" FORCE)
# SVE is not supported on Darwin
if(APPLE)
@@ -499,4 +501,3 @@ elseif(NOT TARGET arm_compute::arm_compute)
set(ACL_INCLUDE_DIR "${ARM_COMPUTE_SOURCE_DIR}")
set(ENV{ACL_ROOT_DIR} "${ARM_COMPUTE_SOURCE_DIR}")
endif()
-
diff --git a/src/plugins/intel_cpu/thirdparty/onednn b/src/plugins/intel_cpu/thirdparty/onednn
index 33bfbecd3f2..c6b79c1207b 160000
--- a/src/plugins/intel_cpu/thirdparty/onednn
+++ b/src/plugins/intel_cpu/thirdparty/onednn
@@ -1 +1 @@
-Subproject commit 33bfbecd3f249154d2b36547b98b4bd7967f22a9
+Subproject commit c6b79c1207bd5f20b9395536dab1d71a47cfcb1d