Skip to content

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

[ Aarch64 ] Paged Attention FP16 precision enablement. (#29219)

ashwins9901cc068392eae
diff --git a/cmake/developer_package/compile_flags/functions.cmake b/cmake/developer_package/compile_flags/functions.cmake
index 072ddcd7969..a49ae7e2f3e 100644
--- a/cmake/developer_package/compile_flags/functions.cmake
+++ b/cmake/developer_package/compile_flags/functions.cmake
@@ -283,7 +283,7 @@ macro(ov_arm_sve_optimization_flags flags)
endif()
# Check for compiler SVE support
- ov_check_compiler_supports_sve("-march=armv8-a+sve")
+ ov_check_compiler_supports_sve("-march=armv8-a+sve+fp16")
if(OV_COMPILER_IS_INTEL_LLVM)
message(WARNING "Unsupported CXX compiler ${CMAKE_CXX_COMPILER_ID}")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
@@ -305,7 +305,7 @@ macro(ov_arm_sve_optimization_flags flags)
# Add flag for SVE if supported
if(CXX_SVE_FOUND)
- list(APPEND ${flags} -march=armv8-a+sve)
+ list(APPEND ${flags} -march=armv8-a+sve+fp16)
endif()
if(NOT CMAKE_CL_64)
list(APPEND ${flags} -ftree-vectorize)
diff --git a/cmake/developer_package/features.cmake b/cmake/developer_package/features.cmake
index cfa048f49ab..1aaf316248b 100644
--- a/cmake/developer_package/features.cmake
+++ b/cmake/developer_package/features.cmake
@@ -56,7 +56,7 @@ ov_dependent_option (ENABLE_AVX512F "Enable AVX512 optimizations" ON "X86_64 OR
ov_dependent_option (ENABLE_NEON_FP16 "Enable ARM FP16 optimizations" ON "AARCH64" OFF)
-ov_dependent_option (ENABLE_SVE "Enable SVE optimizations" ON "AARCH64" OFF)
+ov_dependent_option (ENABLE_SVE "Enable SVE optimizations" ON "AARCH64 AND NOT APPLE" OFF)
# Type of build, we add this as an explicit option to default it to ON
get_property(BUILD_SHARED_LIBS_DEFAULT GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
@@ -115,7 +115,7 @@ if(ENABLE_AVX512F)
endif()
if(ENABLE_SVE)
- ov_check_compiler_supports_sve("-march=armv8-a+sve")
+ ov_check_compiler_supports_sve("-march=armv8-a+sve+fp16")
if(NOT CXX_HAS_SVE)
set(ENABLE_SVE OFF CACHE BOOL "Enables ARM64 SVE support" FORCE)
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/aarch64/sve_utils.hpp b/src/plugins/intel_cpu/src/nodes/kernels/aarch64/sve_utils.hpp
new file mode 100644
index 00000000000..f343432555c
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/kernels/aarch64/sve_utils.hpp
@@ -0,0 +1,85 @@
+// Copyright (C) 2024 FUJITSU LIMITED
+// SPDX-License-Identifier: Apache-2.0
+//
+#include <arm_sve.h>
+
+#include "openvino/core/type/float16.hpp"
+
+namespace ov::intel_cpu::sve_utils {
+
+template <typename T, typename... Args>
+constexpr bool one_of(T val, Args... args) {
+ return ((val == args) || ...);
+}
+
+template <size_t T_SIZE>
+svbool_t sve_predicate() {
+ static_assert(one_of(T_SIZE, 8, 16, 32, 64), "Unexpected parameter size");
+ if constexpr (8 == T_SIZE) {
+ return svptrue_b8();
+ } else if (16 == T_SIZE) {
+ return svptrue_b16();
+ } else if (32 == T_SIZE) {
+ return svptrue_b32();
+ } else if (64 == T_SIZE) {
+ return svptrue_b64();
+ }
+}
+
+template <typename T_TYPE, size_t T_SIZE>
+svbool_t sve_predicate(T_TYPE lower, T_TYPE higher) {
+ static_assert(one_of(T_SIZE, 8, 16, 32, 64), "Unexpected parameter size");
+ if constexpr (8 == T_SIZE) {
+ return svwhilelt_b8(lower, higher);
+ } else if (16 == T_SIZE) {
+ return svwhilelt_b16(lower, higher);
+ } else if (32 == T_SIZE) {
+ return svwhilelt_b32(lower, higher);
+ } else if (64 == T_SIZE) {
+ return svwhilelt_b64(lower, higher);
+ }
+}
+
+template <size_t T_SIZE>
+size_t sve_vlen() {
+ static_assert(one_of(T_SIZE, 8, 16, 32, 64), "Unexpected parameter size");
+ if constexpr (8 == T_SIZE) {
+ return svcntb();
+ } else if (16 == T_SIZE) {
+ return svcnth();
+ } else if (32 == T_SIZE) {
+ return svcntw();
+ } else if (64 == T_SIZE) {
+ return svcntd();
+ }
+}
+
+template <typename TA, typename TB>
+static void cvt_copy(TA* dst, TB* src, size_t n) {
+ size_t i = 0;
+ if constexpr (std::is_same<TA, TB>::value) {
+ auto pg_dst = sve_predicate<sizeof(TA)>();
+ auto vlen = sve_vlen<sizeof(TA)>();
+ for (; i + vlen <= n; i += vlen) {
+ auto vb = svld1(pg_dst, src + i);
+ svst1(pg_dst, dst + i, vb);
+ }
+ auto pgt = sve_predicate<TA, sizeof(TA)>(i, n);
+ auto vb = svld1(pg_dst, src + i);
+ svst1(pg_dst, dst + i, vb);
+ return;
+ } else if constexpr (std::is_same<TA, float>::value && std::is_same<TB, ov::float16>::value) {
+ auto src_ptr = reinterpret_cast<float16_t*>(src);
+ auto pg_vl2 = svwhilelt_b16(svcnth() / 2, svcnth());
+ auto vlen = svcnth() / 2;
+ auto pg_dst = svptrue_b32();
+ for (; i + vlen <= n; i += vlen) {
+ auto load_src = svld1_f16(pg_vl2, src_ptr + i);
+ auto src_interleave = svzip1_f16(load_src, load_src);
+ auto cvt_dst = svcvt_f32_f16_z(pg_dst, src_interleave);
+ svst1(pg_dst, dst + i, cvt_dst);
+ }
+ }
+}
+
+} // namespace ov::intel_cpu::sve_utils
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/kai/kleidi_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/kai/kleidi_kernel.hpp
new file mode 100644
index 00000000000..b1a82d5e3af
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/kernels/kai/kleidi_kernel.hpp
@@ -0,0 +1,104 @@
+// Copyright (C) 2025 FUJITSU LIMITED
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <arm_neon.h>
+#include <kai/ukernels/matmul/matmul_clamp_f16_f16_f16p/kai_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla.h>
+#include <kai/ukernels/matmul/matmul_clamp_f16_f16_f16p/kai_matmul_clamp_f16_f16_f16p_interface.h>
+#include <kai/ukernels/matmul/pack/kai_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon.h>
+
+#include <limits>
+#include <openvino/core/type/element_type.hpp>
+
+namespace ov::intel_cpu {
+
+class KleidiGemm {
+public:
+ KleidiGemm(size_t M, size_t N, size_t K, size_t lda, size_t ldb, size_t ldc);
+ void executeGemm(const void* a, const void* b, void* c);
+ void packB(const float16_t* inp, const float16_t* bias, float16_t* packed_out);
+ const size_t get_packed_rhs_size() const;
+
+private:
+ static constexpr kai_matmul_clamp_f16_f16_f16p_ukernel ukernel{
+ kai_get_m_step_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_n_step_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_nr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_kr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_sr_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_lhs_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_rhs_packed_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_dst_offset_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_get_dst_size_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla,
+ kai_run_matmul_clamp_f16_f16_f16p16x1biasf16_6x16x8_neon_mla};
+ size_t M, N, K;
+ size_t lda, ldb, ldc;
+ size_t nr, kr, sr;
+ size_t packedRHSsize;
+};
+
+KleidiGemm::KleidiGemm(size_t _M, size_t _N, size_t _K, size_t _lda, size_t _ldb, size_t _ldc)
+ : M(_M),
+ N(_N),
+ K(_K),
+ lda(_lda),
+ ldb(_ldb),
+ ldc(_ldc),
+ nr(ukernel.get_nr()),
+ kr(ukernel.get_kr()),
+ sr(ukernel.get_sr()),
+ packedRHSsize(kai_get_rhs_packed_size_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon(_N, _K)){};
+
+const size_t KleidiGemm::get_packed_rhs_size() const {
+ return packedRHSsize;
+}
+
+void KleidiGemm::packB(const float16_t* inp, const float16_t* bias, float16_t* packed_out) {
+ // Packing only needs to be performed once if the contents of the bias and RHS matrices are expected to be constant.
+ kai_run_rhs_pack_kxn_f16p16x1biasf16_f16_f16_neon(1,
+ N,
+ K,
+ nr,
+ kr,
+ sr, // Packing arguments
+ ldb * sizeof(float16_t), // RHS stride
+ inp, // RHS
+ bias, // Bias
+ NULL, // Scale
+ packed_out, // RHS packed
+ 0,
+ NULL);
+}
+
+void KleidiGemm::executeGemm(const void* a, const void* b, void* c) {
+ const size_t m_step = ukernel.get_m_step();
+ const size_t n_step = ukernel.get_n_step();
+ for (size_t i_m_step = 0; i_m_step < M; i_m_step += m_step) {
+ for (size_t i_n_step = 0; i_n_step < N; i_n_step += n_step) {
+ const uint8_t* lhs_ptr =
+ static_cast<const uint8_t*>(a) + (ukernel.get_lhs_packed_offset(i_m_step, lda * sizeof(float16_t)));
+ const uint8_t* rhs_ptr = static_cast<const uint8_t*>(b) + (ukernel.get_rhs_packed_offset(i_n_step, K));
+ uint8_t* dst_ptr =
+ static_cast<uint8_t*>(c) + (ukernel.get_dst_offset(i_m_step, i_n_step, ldc * sizeof(float16_t)));
+ const size_t actual_m = std::min(M - i_m_step, m_step);
+ const size_t actual_n = std::min(N - i_n_step, n_step);
+
+ ukernel.run_matmul(actual_m,
+ actual_n,
+ K, // Dimensions
+ lhs_ptr, // LHS
+ lda * sizeof(float16_t), // LHS stride
+ rhs_ptr, // RHS packed
+ dst_ptr, // DST
+ ldc * sizeof(float16_t), // DST stride (row)
+ sizeof(float16_t), // DST stride (col)
+ -std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max() // Min and max for the clamp operation
+ );
+ }
+ }
+}
+
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
index fb9ef05fb66..4a6930766c2 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa.cpp
@@ -30,6 +30,8 @@
#elif defined(OPENVINO_ARCH_ARM64) && defined(HAVE_SVE)
# include "arm_sve.h"
# include "nodes/kernels/aarch64/brgemm_kernel.hpp"
+# include "nodes/kernels/aarch64/sve_utils.hpp"
+# include "nodes/kernels/kai/kleidi_kernel.hpp"
#endif
namespace ov::Extensions::Cpu::XARCH {
@@ -37,7 +39,7 @@ namespace ov::Extensions::Cpu::XARCH {
using namespace ov;
using namespace ov::intel_cpu;
-// currently depends on brgemm which only support x64
+// currently depends on brgemm which only support x64 or ARM SVE
#if defined(OPENVINO_ARCH_X86_64) || (defined(OPENVINO_ARCH_ARM64) && defined(HAVE_SVE))
# if defined(HAVE_AVX2) || defined(HAVE_AVX512F)
@@ -321,6 +323,41 @@ static void attn_acc_value_block_by_dim(float* out,
weight++;
}
return;
+# elif defined(HAVE_SVE)
+ auto sve_pg = svptrue_b32();
+ size_t j = 0;
+ for (; j < block_size; ++j) {
+ dst_offset = 0;
+ src_offset = 0;
+ while (dst_offset < S) {
+ uint8_t* v_ptr = v + src_offset;
+ uint8_t* v_data_ptr = v_ptr + params_offset;
+ auto v_f0 = reinterpret_cast<float*>(v_ptr);
+ svfloat32_t attn_w_vec0 = svdup_n_f32(weight[0] * v_f0[0]);
+ svfloat32_t zp0 = svdup_n_f32(v_f0[1]);
+ size_t i = 0;
+ for (; i + svcntw() <= group_size; i += svcntw()) {
+ auto v_out = svld1_f32(sve_pg, out + dst_offset + i);
+ svuint32_t reg1 = svld1ub_u32(sve_pg, v_data_ptr + i);
+ svfloat32_t reg2 = svcvt_f32_u32_z(sve_pg, reg1);
+ svfloat32_t v0 = svsub_f32_z(sve_pg, reg2, zp0);
+ v_out = svmla_f32_x(sve_pg, v_out, attn_w_vec0, v0);
+ svst1_f32(sve_pg, out + dst_offset + i, v_out);
+ }
+ auto sve_pgt = svwhilelt_b32(i, group_size);
+ auto v_out = svld1_f32(sve_pgt, out + dst_offset + i);
+ svuint32_t reg1 = svld1ub_u32(sve_pgt, v_data_ptr + i);
+ svfloat32_t reg2 = svcvt_f32_u32_z(sve_pgt, reg1);
+ svfloat32_t v0 = svsub_f32_z(sve_pgt, reg2, zp0);
+ v_out = svmla_f32_x(sve_pgt, v_out, attn_w_vec0, v0);
+ svst1_f32(sve_pgt, out + dst_offset + i, v_out);
+ dst_offset += group_size;
+ src_offset += group_size + params_offset;
+ }
+ v += src_stride;
+ weight++;
+ }
+ return;
# endif
for (size_t j = 0; j < block_size; j++) {
dst_offset = 0;
@@ -1337,6 +1374,45 @@ static void dot_product_block_quantized_by_dims(TA* a,
*c++ = sum;
}
return;
+# elif defined(HAVE_SVE)
+ auto scratch = svdup_f16_x(svptrue_b16(), 0);
+ size_t j = 0;
+ for (; j < block_size; j++) {
+ src_offset = 0;
+ dst_offset = 0;
+ float sum = 0;
+ while (dst_offset < n) {
+ auto vsum = svdup_n_f32(0.0f);
+ auto b0 = reinterpret_cast<float*>(b + src_offset);
+ auto v_zp = svdup_n_f32(b0[1]);
+ size_t i = 0;
+ uint8_t* b_data_ptr = b + src_offset + params_offset;
+ auto vlen = svcntw();
+ svbool_t pg_a, pg_f16;
+ for (; i <= group_size; i += vlen) {
+ svfloat32_t va;
+ pg_a = svwhilelt_b32(i, group_size);
+ pg_f16 = svand_z(svptrue_b16(), svwhilelt_b16(svcnth() / 2, svcnth()), svwhilelt_b16(i, group_size));
+ if constexpr (std::is_same<TA, ov::float16>::value) {
+ auto load_src = svld1_f16(pg_f16, reinterpret_cast<float16_t*>(a + dst_offset + i));
+ auto src_interleave = svzip1_f16(load_src, scratch);
+ va = svcvt_f32_f16_z(pg_a, src_interleave);
+ } else {
+ va = svld1(pg_a, a + dst_offset + i);
+ }
+ auto r1 = svcvt_f32_u32_z(pg_a, svld1ub_u32(pg_a, b_data_ptr + i));
+ auto vb = svsub_f32_z(pg_a, r1, v_zp);
+ vsum = svmla_f32_m(pg_a, vsum, va, vb);
+ }
+ float group_sum = svaddv_f32(svptrue_b32(), vsum);
+ sum += group_sum * b0[0];
+ dst_offset += group_size;
+ src_offset += group_size + params_offset;
+ }
+ b += src_stride;
+ *c++ = sum;
+ }
+ return;
# endif
for (size_t j = 0; j < block_size; j++) {
float sum = 0;
@@ -2101,6 +2177,7 @@ struct MHAHelper {
bool _quant_key_bychannel = false;
bool _quant_value_bychannel = false;
size_t _new_score_stride = 0;
+ bool AarchF16 = false;
PlainTensor _weight; // [nthr, H, 32, rnd_up(kv_len, block_size)], shared by first and second loop along bh
PlainTensor _output; // [nthr, 32, H, S], shared by first and second loop along bh
@@ -2181,6 +2258,9 @@ struct MHAHelper {
_sliding_window = sliding_window;
_d_scale = d_scale;
+# if defined(OPENVINO_ARCH_ARM64)
+ AarchF16 = one_of(precision_of<DATA_TYPE>::value, ov::element::f16);
+# endif
auto prev_score_stride = _new_score_stride;
auto want_score_stride = rnd_up(kv_len, _block_size);
_new_score_stride = std::max(prev_score_stride, want_score_stride);
@@ -2192,7 +2272,7 @@ struct MHAHelper {
}
// TODO: kernel supports stride
- if (_qk_gemm.empty() || prev_score_stride < _new_score_stride) {
+ if (!AarchF16 && (_qk_gemm.empty() || prev_score_stride < _new_score_stride)) {
_qk_gemm.resize(_block_size);
_wv_gemm.resize(_block_size);
_wv_gemm_acc.resize(_block_size);
@@ -2272,7 +2352,12 @@ struct MHAHelper {
void init_reorder_buffers(size_t batch, size_t kv_len_in_blocks) {
_qk_scratch_b.resize<DATA_TYPE>({batch, kv_len_in_blocks, Hk, _block_size * S});
- _wv_scratch_b.resize<DATA_TYPE>({batch, kv_len_in_blocks, Hk, _block_size * rnd_up(SV, _block_size)});
+ if (AarchF16) {
+ // It is required to keep kv_cache continuous in mem, as kleidi do not support accumulation
+ _wv_scratch_b.resize<DATA_TYPE>({batch, Hk, kv_len_in_blocks, _block_size * rnd_up(SV, _block_size)});
+ } else {
+ _wv_scratch_b.resize<DATA_TYPE>({batch, kv_len_in_blocks, Hk, _block_size * rnd_up(SV, _block_size)});
+ }
}
void init_score_buffers(const PlainTensor& past_lens, const PlainTensor& subsequence_begins) {
@@ -2440,6 +2525,140 @@ struct MHAHelper {
}
}
}
+# if defined(OPENVINO_ARCH_ARM64)
+ // compute one block(such as 32 tokens) of query in M dimension: softmax(q_block*k')*v
+ // all tensors such as query... have no batch dimension because batch dimension is varying
+ // query: [H, L, S]
+ // present_value: [block_number, H, 32, S]
+ // output_emb: [L, H * S]
+ // qk_scratch_b: [rnd_up(kv_len, block_size), Hk, scratch_b_size]
+ // wv_scratch_b: [rnd_up(kv_len, block_size), Hk, scratch_b_size]
+ void exec_kernel_multiple_kai(const PlainTensor& query,
+ const PlainTensor& present_value,
+ const PlainTensor& output_emb,
+ const PlainTensor& qk_scratch_b,
+ const PlainTensor& wv_scratch_b,
+ const int32_t* block_table,
+ size_t ithr,
+ size_t q_blk,
+ size_t hq_beg,
+ size_t hq_end,
+ size_t hk,
+ size_t q_len,
+ size_t cur_kv_len,
+ const PlainTensor& alibi_slopes,
+ float* score_output) {
+ auto q_start = q_blk * _block_size;
+ auto q_end = std::min(q_start + _block_size, q_len);
+ auto q_cnt = q_end - q_start;
+ constexpr bool q_is_xf16 = one_of(precision_of<DATA_TYPE>::value, ov::element::bf16, ov::element::f16);
+ auto cur_kv_len_blocks = div_up(cur_kv_len, _block_size);
+ auto _score_stride = _weight.stride_bytes(2) / 2;
+ PlainTensor bias_wv, bias_qk;
+ bias_wv.resize<float16_t>({SV});
+ bias_qk.resize<float16_t>({_block_size});
+ memset(bias_wv.ptr<float16_t>(0), 0, sizeof(DATA_TYPE) * SV);
+ memset(bias_qk.ptr<float16_t>(0), 0, sizeof(DATA_TYPE) * _block_size);
+ for (size_t h = hq_beg; h < hq_end; h++) {
+ auto* q_ptr = query.ptr<DATA_TYPE>(h, q_start, 0);
+ float* c_ptr = _weight.ptr<float>(ithr, h - hq_beg, 0, 0);
+ // for each query block, loop through all key block
+ // for blocks:
+ // 1 0 0 0 ...
+ // 1 1 0 0 ...
+ // 1 1 1 0 ...
+ // just computing the positions of 1 should be enough
+ for (size_t k_blk = 0; k_blk < cur_kv_len_blocks; k_blk++) {
+ auto* k_ptr = qk_scratch_b.ptr<DATA_TYPE>(k_blk, hk);
+ auto* qk_out_ptr =
+ c_ptr +
+ (precision_of<DATA_TYPE>::value == ov::element::Type_t::f32 ? _block_size : _block_size / 2) *
+ k_blk;
+
+ KleidiGemm qkKernel(q_cnt, _block_size, S, H * S, _block_size, _score_stride);
+ PlainTensor packedB_k;
+ packedB_k.resize<float16_t>({qkKernel.get_packed_rhs_size()});
+ qkKernel.packB(reinterpret_cast<float16_t*>(k_ptr),
+ bias_qk.ptr<float16_t>(0),
+ packedB_k.ptr<float16_t>(0));
+ qkKernel.executeGemm(q_ptr, packedB_k.ptr<float16_t>(0), qk_out_ptr);
+ }
+
+ for (size_t m = q_start; m < q_end; m++) {
+ // apply softmax in f32 precision
+ auto ncausal = (cur_kv_len - q_cnt + (m - q_start) + 1);
+ auto soft_in = _weight.ptr<float>(ithr, h - hq_beg, m - q_start);
+ auto score = _weight.ptr<float>(ithr, h - hq_beg, m - q_start);
+ PlainTensor f32_cvt;
+ if (q_is_xf16) {
+ f32_cvt.resize<float>({size_t{rnd_up(cur_kv_len, _block_size)}});
+ sve_utils::cvt_copy(f32_cvt.ptr<float>(0),
+ reinterpret_cast<DATA_TYPE*>(score),
+ rnd_up(cur_kv_len, _block_size));
+ soft_in = f32_cvt.ptr<float>(0);
+ }
+ if (_sliding_window) {
+ size_t start_idx = 0;
+ auto new_causal = ncausal;
+ float* alibi_lookup = nullptr;
+ if (ncausal > _sliding_window) {
+ start_idx = ncausal - static_cast<size_t>(_sliding_window);
+ new_causal = _sliding_window;
+ }
+ attn_softmax_kernel<float>(soft_in + start_idx,
+ reinterpret_cast<DATA_TYPE*>(score) + start_idx,
+ _d_scale,
+ alibi_lookup,
+ nullptr,
+ nullptr,
+ false,
+ new_causal,
+ rnd_up(cur_kv_len, _block_size) - start_idx,
+ precision_of<DATA_TYPE>::value,
+ precision_of<DATA_TYPE>::value);
+
+ memset(score, 0, sizeof(DATA_TYPE) * start_idx);
+ } else {
+ // alibi may available when _sliding_window is false
+ float* alibi_lookup = nullptr;
+ float alibi_slope = 0.f;
+ if (alibi_slopes) {
+ alibi_slope = alibi_slopes.ptr<float>()[h];
+ alibi_lookup = _alibi_lookup.ptr<float>() + _alibi_lookup.m_dims[0] - ncausal;
+ }
+ attn_softmax_kernel<float>(soft_in,
+ reinterpret_cast<DATA_TYPE*>(score),
+ _d_scale,
+ alibi_lookup,
+ nullptr,
+ nullptr,
+ false,
+ ncausal,
+ rnd_up(cur_kv_len, _block_size),
+ precision_of<DATA_TYPE>::value,
+ precision_of<DATA_TYPE>::value,
+ alibi_slope);
+ }
+ if (score_output) {
+ sve_utils::cvt_copy(score_output + h * rnd_up(cur_kv_len, 16),
+ reinterpret_cast<DATA_TYPE*>(score),
+ cur_kv_len);
+ }
+ }
+
+ // reuse float buffer, need to use float to compute offset
+ auto* w_ptr = reinterpret_cast<DATA_TYPE*>(_weight.ptr<float>(ithr, h - hq_beg, 0, 0));
+ DATA_TYPE* out_ptr = output_emb.ptr<DATA_TYPE>(q_start, h * SV);
+ DATA_TYPE* v_ptr;
+ v_ptr = wv_scratch_b.ptr<DATA_TYPE>(hk, 0);
+ PlainTensor packedB;
+ KleidiGemm wvKernel(q_cnt, SV, _block_size * cur_kv_len_blocks, _score_stride, SV, H * SV);
+ packedB.resize<float16_t>({wvKernel.get_packed_rhs_size()});
+ wvKernel.packB(reinterpret_cast<float16_t*>(v_ptr), bias_wv.ptr<float16_t>(0), packedB.ptr<float16_t>(0));
+ wvKernel.executeGemm(reinterpret_cast<float16_t*>(w_ptr), packedB.ptr<float16_t>(0), out_ptr);
+ }
+ }
+# endif
// compute one token, loop along batch and head dimensions
// all tensors such as query... have no batch dimension because batch dimension is varying
@@ -2938,6 +3157,15 @@ struct MHA {
if (q_is_xf16) {
auto* v_ptr =
v_cache.ptr<typename element_type_traits<VALUE_PREC>::value_type, VALUE_PREC>(block_number, hk);
+# if defined(OPENVINO_ARCH_ARM64)
+ dequant<DATA_TYPE, VALUE_PREC>(
+ _helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, hk, kv_block),
+ v_ptr,
+ _helper._block_size,
+ _helper.SV,
+ _helper._value_group_size,
+ _helper._quant_value_bychannel);
+# else
pack_32NxK<DATA_TYPE, VALUE_PREC>(
_helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, kv_block, hk),
v_ptr,
@@ -2948,6 +3176,7 @@ struct MHA {
_helper.SV,
_helper._value_group_size,
_helper._quant_value_bychannel);
+# endif
} else {
// need to decompress
if (!q_cache_is_same) {
@@ -3033,6 +3262,45 @@ struct MHA {
PlainTensor sub_query;
sub_query.resize({q_len, _helper.H, _helper.S}, q.ptr<DATA_TYPE>(batch_in_token));
sub_query = sub_query.permute({1, 0, 2});
+# if defined(OPENVINO_ARCH_ARM64)
+ if constexpr (q_is_xf16) {
+ _helper.exec_kernel_multiple_kai(
+ sub_query,
+ v_cache,
+ output_emb.slice(0, batch_in_token, batch_in_token + q_len)
+ .reshape({q_len, _helper.H * _helper.SV}),
+ _helper._qk_scratch_b.slice(0, batch_in_reorder, batch_in_reorder),
+ _helper._wv_scratch_b.slice(0, batch_in_reorder, batch_in_reorder),
+ block_indices.ptr<int32_t>() + block_indices_begins.ptr<int32_t>()[batch_in_seq],
+ ithr,
+ q_blk,
+ hq_beg,
+ hq_end,
+ hk,
+ q_len,
+ cur_kv_len,
+ alibi_slopes,
+ score_output);
+ } else {
+ _helper.exec_kernel_multiple(
+ sub_query,
+ v_cache,
+ output_emb.slice(0, batch_in_token, batch_in_token + q_len)
+ .reshape({q_len, _helper.H * _helper.SV}),
+ _helper._qk_scratch_b.slice(0, batch_in_reorder, batch_in_reorder),
+ _helper._wv_scratch_b.slice(0, batch_in_reorder, batch_in_reorder),
+ block_indices.ptr<int32_t>() + block_indices_begins.ptr<int32_t>()[batch_in_seq],
+ ithr,
+ q_blk,
+ hq_beg,
+ hq_end,
+ hk,
+ q_len,
+ cur_kv_len,
+ alibi_slopes,
+ score_output);
+ }
+# else
_helper.exec_kernel_multiple(
sub_query,
v_cache,
@@ -3050,6 +3318,7 @@ struct MHA {
cur_kv_len,
alibi_slopes,
score_output);
+# endif
}
});
if (output_score) {
@@ -3659,6 +3928,17 @@ std::shared_ptr<PagedAttentionExecutor> make_pa_executor(ov::element::Type data_
OPENVINO_THROW("make_pa_executor: key_cache_type and value_cache_type of u8 is only support");
}
}
+ if (data_type == ov::element::f16) {
+ if (key_cache_type == ov::element::u8 && value_cache_type == ov::element::u8) {
+ executor = std::make_shared<AttentionExecutor<ov::float16, ov::element::u8, ov::element::u8>>(
+ key_group_size,
+ value_group_size,
+ quant_key_bychannel,
+ quant_value_bychannel);
+ } else {
+ OPENVINO_THROW("make_pa_executor: key_cache_type and value_cache_type of u8 is only support");
+ }
+ }
#else
OPENVINO_THROW("make_pa_executor: only support x64 platform or ARM with SVE support");
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/transpose_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/transpose_kernel.hpp
index 74f59d0e768..11ae62cc4f8 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/transpose_kernel.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/transpose_kernel.hpp
@@ -613,6 +613,102 @@ inline void transpose_16xK_kernel(TDST* dst, TSRC* src, size_t K, size_t dst_str
}
}
+inline void transpose_8x16_kernel(float16_t* dst, float16_t* src, size_t dst_stride, size_t src_stride) {
+ // load 8 rows of 16 elements
+ // a -> a0 a1 a2 a3 a4 a5 a6 a7 a8 ...
+ // b -> b0 b1 b2 b3 b4 b5 b6 b7 b8 ...
+ // c -> c0 c1 c2 c3 c4 c5 c6 c7 c8 ...
+ // d -> d0 d1 d2 d3 d4 d5 d6 d7 d8 ...
+ // ...
+ auto a = svld1_f16(svptrue_b16(), src);
+ auto b = svld1_f16(svptrue_b16(), src + 1 * src_stride);
+ auto c = svld1_f16(svptrue_b16(), src + 2 * src_stride);
+ auto d = svld1_f16(svptrue_b16(), src + 3 * src_stride);
+ auto e = svld1_f16(svptrue_b16(), src + 4 * src_stride);
+ auto f = svld1_f16(svptrue_b16(), src + 5 * src_stride);
+ auto g = svld1_f16(svptrue_b16(), src + 6 * src_stride);
+ auto h = svld1_f16(svptrue_b16(), src + 7 * src_stride);
+
+ // a -> a0 b0 | a2 b2 | a4 b4 | a6 b6 | a8 b8 ...
+ // b -> a1 b1 | a3 b3 | a5 b5 | a7 b7 | a9 b9 ...
+ // c -> c0 d0 | c2 d2 | c4 d6 | c6 d8 | c8 d8 ...
+ // d -> c1 d1 | c3 d3 | c5 d5 | c7 d7 | c9 d9 ...
+ // ...
+
+ auto ta = svtrn1_f16(a, b);
+ auto tb = svtrn2_f16(a, b);
+ auto tc = svtrn1_f16(c, d);
+ auto td = svtrn2_f16(c, d);
+ auto te = svtrn1_f16(e, f);
+ auto tf = svtrn2_f16(e, f);
+ auto tg = svtrn1_f16(g, h);
+ auto th = svtrn2_f16(g, h);
+
+ // a -> a0 b0 c0 d0 | a4 b4 c4 d6 | a8 b8 ...
+ // b -> a2 b2 c2 d2 | a6 b6 c6 d8 | c8 d8 ...
+ // c -> a1 b1 c1 d1 | a5 b5 c5 d5 | a9 b9 ...
+ // d -> a3 b3 c3 d3 | a7 b7 c7 d7 | c9 d9 ...
+ // ...
+
+ auto a1 = svtrn1_f32(svreinterpret_f32_f16(ta), svreinterpret_f32_f16(tc));
+ auto b1 = svtrn2_f32(svreinterpret_f32_f16(ta), svreinterpret_f32_f16(tc));
+ auto c1 = svtrn1_f32(svreinterpret_f32_f16(tb), svreinterpret_f32_f16(td));
+ auto d1 = svtrn2_f32(svreinterpret_f32_f16(tb), svreinterpret_f32_f16(td));
+ auto e1 = svtrn1_f32(svreinterpret_f32_f16(te), svreinterpret_f32_f16(tg));
+ auto f1 = svtrn2_f32(svreinterpret_f32_f16(te), svreinterpret_f32_f16(tg));
+ auto g1 = svtrn1_f32(svreinterpret_f32_f16(tf), svreinterpret_f32_f16(th));
+ auto h1 = svtrn2_f32(svreinterpret_f32_f16(tf), svreinterpret_f32_f16(th));
+
+ auto a2 = svtrn1_f64(svreinterpret_f64_f32(a1), svreinterpret_f64_f32(e1));
+ auto b2 = svtrn2_f64(svreinterpret_f64_f32(a1), svreinterpret_f64_f32(e1));
+ auto c2 = svtrn1_f64(svreinterpret_f64_f32(b1), svreinterpret_f64_f32(f1));
+ auto d2 = svtrn2_f64(svreinterpret_f64_f32(b1), svreinterpret_f64_f32(f1));
+ auto e2 = svtrn1_f64(svreinterpret_f64_f32(c1), svreinterpret_f64_f32(g1));
+ auto f2 = svtrn2_f64(svreinterpret_f64_f32(c1), svreinterpret_f64_f32(g1));
+ auto g2 = svtrn1_f64(svreinterpret_f64_f32(d1), svreinterpret_f64_f32(h1));
+ auto h2 = svtrn2_f64(svreinterpret_f64_f32(d1), svreinterpret_f64_f32(h1));
+
+ auto pgh = svnot_b_z(svptrue_b64(), svptrue_pat_b64(SV_VL2));
+ auto pgl = svptrue_pat_b64(SV_VL2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst), a2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 1), e2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 2), c2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 3), g2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 4), b2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 5), f2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 6), d2);
+ svst1_f64(pgl, reinterpret_cast<float64_t*>(dst + dst_stride * 7), h2);
+
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 8) - 2, a2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 9) - 2, e2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 10) - 2, c2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 11) - 2, g2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 12) - 2, b2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 13) - 2, f2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 14) - 2, d2);
+ svst1_f64(pgh, reinterpret_cast<float64_t*>(dst + dst_stride * 15) - 2, h2);
+}
+
+inline void transpose_16x16_kernel(ov::float16* dst, ov::float16* src, size_t dst_stride, size_t src_stride) {
+ // check for SVE256
+ if (svcnth() == 16) {
+ transpose_8x16_kernel(reinterpret_cast<float16_t*>(dst),
+ reinterpret_cast<float16_t*>(src),
+ dst_stride,
+ src_stride);
+ transpose_8x16_kernel(reinterpret_cast<float16_t*>(dst + 8),
+ reinterpret_cast<float16_t*>(src + 128),
+ dst_stride,
+ src_stride);
+ } else {
+ for (size_t i = 0; i < 16; i++) {
+ for (size_t j = 0; j < 16; j++) {
+ dst[i * dst_stride + j] = (src[i + j * src_stride]);
+ }
+ }
+ }
+}
+
inline void transpose_8x8_kernel(float* src, size_t ld_src, float* dst, size_t ld_dst) {
// load from src to registers
// a: a0 a1 a2 a3 a4 a5 a6 a7
@@ -696,8 +792,8 @@ inline void transpose_8x8_kernel(float* src, size_t ld_src, float* dst, size_t l
svst1_f32(svptrue_b8(), &dst[6 * ld_dst], tf);
svst1_f32(svptrue_b8(), &dst[7 * ld_dst], th);
}
-template <>
-inline void transpose_16x16_kernel<float, float>(float* dst, float* src, size_t dst_stride, size_t src_stride) {
+
+inline void transpose_16x16_kernel(float* dst, float* src, size_t dst_stride, size_t src_stride) {
transpose_8x8_kernel(src, src_stride, dst, dst_stride);
transpose_8x8_kernel(src + 8, src_stride, dst + 8 * dst_stride, dst_stride);
transpose_8x8_kernel(src + 8 * src_stride, src_stride, dst + 8, dst_stride);
diff --git a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
index a22d81ebcff..0f561139ee3 100644
--- a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
@@ -283,6 +283,14 @@ bool PagedAttention::isSupportedOperation(const std::shared_ptr<const ov::Node>&
ov::element::Type PagedAttention::getRuntimePrecision() const {
auto rtPrecision = getOriginalInputPrecisionAtPort(0);
+#if defined(OPENVINO_ARCH_ARM64)
+ if (rtPrecision == ov::element::f16) {
+ rtPrecision = ov::element::f16;
+ } else {
+ rtPrecision = ov::element::f32;
+ }
+ return rtPrecision;
+#endif
// bf16 should be enabled only when platform supports
if (rtPrecision == ov::element::bf16 && ov::with_cpu_x86_bfloat16()) {
rtPrecision = ov::element::bf16;