Skip to content

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

[CPU] attn supports f16 (#26487)

Xiuchuan Zhai9486b7dac93b
diff --git a/src/core/dev_api/openvino/op/paged_attention.hpp b/src/core/dev_api/openvino/op/paged_attention.hpp
index e5995e0b869..0c1c396cbef 100644
--- a/src/core/dev_api/openvino/op/paged_attention.hpp
+++ b/src/core/dev_api/openvino/op/paged_attention.hpp
@@ -17,6 +17,11 @@ public:
PagedAttentionExtension(const ov::OutputVector& args);
void validate_and_infer_types() override;
std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override;
+
+ void set_out_type(int index, const ov::element::Type& output_type);
+
+protected:
+ std::vector<ov::element::Type> m_output_type = {ov::element::undefined, ov::element::undefined};
};
} // namespace op
diff --git a/src/core/src/op/paged_attention.cpp b/src/core/src/op/paged_attention.cpp
index e3771bcbf92..261b0ce1c47 100644
--- a/src/core/src/op/paged_attention.cpp
+++ b/src/core/src/op/paged_attention.cpp
@@ -146,13 +146,27 @@ void PagedAttentionExtension::validate_and_infer_types() {
get_input_element_type(12),
".");
- set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));
- set_output_type(1, get_input_element_type(0), {Dimension::dynamic()});
+ if (m_output_type[0] == ov::element::undefined) {
+ set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));
+ } else {
+ set_output_type(0, m_output_type[0], get_input_partial_shape(0));
+ }
+
+ if (m_output_type[1] == ov::element::undefined) {
+ set_output_type(1, get_input_element_type(0), {Dimension::dynamic()});
+ } else {
+ set_output_type(1, m_output_type[1], {Dimension::dynamic()});
+ }
}
std::shared_ptr<ov::Node> PagedAttentionExtension::clone_with_new_inputs(const ov::OutputVector& new_args) const {
return std::make_shared<PagedAttentionExtension>(new_args);
}
+void PagedAttentionExtension::set_out_type(int index, const ov::element::Type& output_type) {
+ OPENVINO_ASSERT(index < 2, "Output index should be 0 or 1, but got " + std::to_string(index));
+ m_output_type[index] = output_type;
+}
+
} // namespace op
} // namespace ov
diff --git a/src/plugins/intel_cpu/src/graph.cpp b/src/plugins/intel_cpu/src/graph.cpp
index a17b8d28e17..dc0f953efe7 100644
--- a/src/plugins/intel_cpu/src/graph.cpp
+++ b/src/plugins/intel_cpu/src/graph.cpp
@@ -194,8 +194,8 @@ void Graph::Replicate(const std::shared_ptr<const ov::Model> &model,
const auto port = unusedOutput.get_index();
const auto nodeName = std::string("stub_") + std::to_string(unusedOutput.get_index()) + "_" + parentNode->getName();
const NodePtr outNode = std::make_shared<node::Input>(parentNode->outputShapes[port],
- parentNode->getOriginalOutputPrecisionAtPort(port),
- nodeName, "Result", m_context);
+ parentNode->getOriginalOutputPrecisionAtPort(port),
+ nodeName, "Result", m_context);
CreateEdge(parentNode, outNode, port, 0);
AddNode(outNode);
}
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp
index a9998e88402..d95f973fa9f 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_quant.cpp
@@ -180,7 +180,6 @@ static void attn_quant_mt(const ov::intel_cpu::PlainTensor& k_src,
// For compatibility, all input_kvs are permuted to BHLS
size_t B = k_src.m_dims[0], H = k_src.m_dims[1], L1 = k_src.m_dims[2], S = k_src.m_dims[3];
// Internal LBHS layout has strides[L] > strides[B]
- assert(k_src.m_strides[2] > k_src.m_strides[0]);
parallel_for3d(L1, B, H, [&](size_t m, size_t b, size_t h) {
auto p_k = k_scale_zp.ptr<float>(m, b, h);
auto p_v = v_scale_zp.ptr<float>(m, b, h);
@@ -238,6 +237,8 @@ void attn_quantkv(const ov::intel_cpu::PlainTensor& k_src,
attn_quant_mt<float, uint8_t>(k_src, v_src, k_dst, v_dst, k_scale_zp, v_scale_zp);
} else if (k_src.get_precision() == ov::element::bf16 && k_dst.get_precision() == ov::element::u8) {
attn_quant_mt<ov::bfloat16, uint8_t>(k_src, v_src, k_dst, v_dst, k_scale_zp, v_scale_zp);
+ } else if (k_src.get_precision() == ov::element::f16 && k_dst.get_precision() == ov::element::u8) {
+ attn_quant_mt<ov::float16, uint8_t>(k_src, v_src, k_dst, v_dst, k_scale_zp, v_scale_zp);
} else {
OPENVINO_THROW("unsupport src type: ", k_src.get_precision(), ", dst type: ", k_dst.get_precision(), " in attn_quantkv");
}
@@ -252,6 +253,8 @@ void paged_attn_quantkv(const ov::intel_cpu::PlainTensor& k_src,
paged_attn_quant_mt<float, uint8_t>(k_src, v_src, k_dst, v_dst, slot_mapping);
} else if (k_src.get_precision() == ov::element::bf16 && k_dst.get_precision() == ov::element::u8) {
paged_attn_quant_mt<ov::bfloat16, uint8_t>(k_src, v_src, k_dst, v_dst, slot_mapping);
+ } else if (k_src.get_precision() == ov::element::f16 && k_dst.get_precision() == ov::element::u8) {
+ paged_attn_quant_mt<ov::float16, uint8_t>(k_src, v_src, k_dst, v_dst, slot_mapping);
} else {
OPENVINO_THROW("unsupport src type: ", k_src.get_precision(), ", dst type: ", k_dst.get_precision(), " in paged_attn_quantkv");
}
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 3341f6f6082..2956c8a6a6b 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
@@ -37,15 +37,22 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
return _mm512_castsi512_ps(_mm512_slli_epi32(y, 16));
}
+ // load addr to __m512 reg
+ inline __m512 mm512_uni_loadu_ps(const float* a) {
+ return _mm512_loadu_ps(a);
+ }
+
inline __m512 mm512_uni_loadu_ps(const ov::bfloat16* a) {
auto vec_bf16 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a));
return cvt_bf16_to_fp32(vec_bf16);
}
- inline __m512 mm512_uni_loadu_ps(const float* a) {
- return _mm512_loadu_ps(a);
+ inline __m512 mm512_uni_loadu_ps(const ov::float16* a) {
+ auto vec_f16 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a));
+ return _mm512_cvtph_ps(vec_f16);
}
+ // load addr to __m512 reg
inline __m512 mm512_uni_loadu_tail_ps(const float* a, size_t count) {
__mmask16 mask = (1 << count) - 1;
return _mm512_maskz_loadu_ps(mask, a);
@@ -57,6 +64,13 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
return cvt_bf16_to_fp32(bf16_vec);
}
+ inline __m512 mm512_uni_loadu_tail_ps(const ov::float16* a, size_t count) {
+ auto mask = (1 << count) - 1;
+ auto f16_vec = _mm256_maskz_loadu_epi16(mask, a);
+ return _mm512_cvtph_ps(f16_vec);
+ }
+
+ // store __m512 reg to addr
inline void mm512_uni_storeu_ps(float* a, __m512 v) {
_mm512_storeu_ps(a, v);
}
@@ -72,6 +86,13 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
x = _mm512_mask_blend_epi32(mask, nan, x); // Check NaN before converting back to bf16
_mm256_storeu_si256(reinterpret_cast<__m256i *>(addr), _mm512_cvtepi32_epi16(x));
}
+
+ inline void mm512_uni_storeu_ps(ov::float16* addr, __m512 v) {
+ __m256i vec_f16 = _mm512_cvtps_ph(v, 0);
+ _mm256_storeu_si256(reinterpret_cast<__m256i *>(addr), vec_f16);
+ }
+
+ // store __m512 reg to addr
inline void mm512_uni_mask_storeu_ps(ov::bfloat16 *addr, __mmask16 mask_addr, __m512 xps) {
__m512i xpi32 = _mm512_castps_si512(xps);
__m512i nan = _mm512_set1_epi32(0xffff);
@@ -85,18 +106,29 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
_mm512_mask_cvtepi32_storeu_epi16(addr, mask_addr, x);
}
- inline __m512 mm512_uni_loadu_ps(ov::float16* a) {
- auto vec_f16 = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a));
- return _mm512_cvtph_ps(vec_f16);
+ inline void mm512_uni_storeu_tail_ps(float *addr, __m512 v, size_t count) {
+ __mmask16 mask_addr = (1 << count) - 1;
+ _mm512_mask_storeu_ps(addr, mask_addr, v);
}
- inline __m512 mm512_uni_loadu_tail_ps(const ov::float16* a, size_t count) {
- auto mask = (1 << count) - 1;
- auto f16_vec = _mm256_maskz_loadu_epi16(mask, a);
- return _mm512_cvtph_ps(f16_vec);
+
+ inline void mm512_uni_storeu_tail_ps(ov::bfloat16 *addr, __m512 v, size_t count) {
+ __mmask16 mask_addr = (1 << count) - 1;
+ __m512i xpi32 = _mm512_castps_si512(v);
+ __m512i nan = _mm512_set1_epi32(0xffff);
+ auto mask = _mm512_cmp_ps_mask(v, v, _CMP_ORD_Q);
+ __m512i ones = _mm512_set1_epi32(0x1);
+ __m512i vec_bias = _mm512_set1_epi32(0x7fff);
+ auto x = _mm512_and_si512(_mm512_srli_epi32(xpi32, 16), ones); // LSB = x[16]
+ x = _mm512_add_epi32(x, vec_bias); // rounding_bias = 0x7fff + LSB
+ x = _mm512_srli_epi32(_mm512_add_epi32(x, xpi32), 16); // x = (x + rounding_bias) >> 16;
+ x = _mm512_mask_blend_epi32(mask, nan, x); // Check NaN before converting back to bf16
+ _mm512_mask_cvtepi32_storeu_epi16(addr, mask_addr, x);
}
- inline void mm512_uni_storeu_ps(ov::float16* addr, __m512 v) {
+
+ inline void mm512_uni_storeu_tail_ps(ov::float16 *addr, __m512 v, size_t count) {
+ __mmask16 mask_addr = (1 << count) - 1;
__m256i vec_f16 = _mm512_cvtps_ph(v, 0);
- _mm256_storeu_si256(reinterpret_cast<__m256i *>(addr), vec_f16);
+ _mm256_mask_storeu_epi16(reinterpret_cast<__m256i *>(addr), mask_addr, vec_f16);
}
#endif
@@ -115,12 +147,11 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
};
return _mm256_loadu_si256(&mask[N7]);
}
+
+ // load addr to __m256 reg
inline __m256 mm256_uni_loadu_ps(const float* a) {
return _mm256_loadu_ps(a);
}
- inline void mm256_uni_storeu_ps(float* a, __m256 v) {
- _mm256_storeu_ps(a, v);
- }
inline __m256 mm256_uni_loadu_ps(const ov::bfloat16* a) {
auto vec_bf16 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(a));
@@ -128,6 +159,13 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
return o;
}
+ inline __m256 mm256_uni_loadu_ps(const ov::float16* a) {
+ auto vec_f16 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(a));
+ auto o = _mm256_cvtph_ps(vec_f16);
+ return o;
+ }
+
+ // load addr tail to __m256 reg
inline __m256 mm256_uni_loadu_tail_ps(const float* a, const size_t count) {
auto mask = get_mask(count);
return _mm256_maskload_ps(a, mask);
@@ -140,6 +178,17 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
return mm256_uni_loadu_ps(tmp_values);
}
+ inline __m256 mm256_uni_loadu_tail_ps(const ov::float16* a, const size_t count) {
+ ov::float16 tmp_values[8] = {0};
+ std::memcpy(tmp_values, a, count * sizeof(ov::float16));
+ return mm256_uni_loadu_ps(tmp_values);
+ }
+
+ // store __m256 reg to addr
+ inline void mm256_uni_storeu_ps(float* a, __m256 v) {
+ _mm256_storeu_ps(a, v);
+ }
+
inline void mm256_uni_storeu_ps(ov::bfloat16 *addr, __m256 xps) {
__m256i xpi32 = _mm256_castps_si256(xps);
__m256i nan = _mm256_set1_epi32(0xffff);
@@ -156,21 +205,17 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
_mm_storeu_si128(reinterpret_cast<__m128i *>(addr), bf16_o);
}
- inline __m256 mm256_uni_loadu_ps(ov::float16* a) {
- auto vec_f16 = _mm_loadu_si128(reinterpret_cast<__m128i*>(a));
- auto o = _mm256_cvtph_ps(vec_f16);
- return o;
- }
- inline __m256 mm256_uni_loadu_tail_ps(const ov::float16* a, const size_t count) {
- ov::float16 tmp_values[8] = {0};
- std::memcpy(tmp_values, a, count * sizeof(ov::float16));
- return mm256_uni_loadu_ps(tmp_values);
- }
inline void mm256_uni_storeu_ps(ov::float16* a, __m256 v) {
__m128i vec_f16 = _mm256_cvtps_ph(v, 0);
_mm_storeu_si128(reinterpret_cast<__m128i *>(a), vec_f16);
}
+ // store __m256 to addr
+ inline void mm256_uni_storeu_tail_ps(float *addr, __m256 v, size_t count) {
+ const auto mask = get_mask(count);
+ return _mm256_maskstore_ps(addr, mask, v);
+ }
+
inline void hsum(__m256& x) {
__m256 y; // x: 0 1 2 3 4 5 6 7
y = _mm256_permute_ps(x, 0x39); // y: 1 2 3 0 5 6 7 4
@@ -292,4 +337,4 @@ static constexpr size_t vec_len_f16_neon = vec_len_neon / sizeof(ov::float16);
} // namespace XARCH
} // namespace Cpu
} // namespace Extensions
-} // namespace ov
\ No newline at end of file
+} // namespace ov
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 1fe7b811b92..971aa6bb58c 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
@@ -14,6 +14,7 @@
#endif
#include "openvino/core/type/bfloat16.hpp"
+#include "openvino/core/type/float16.hpp"
#include "openvino/core/parallel.hpp"
#include "executor_pa.hpp"
#include "executor_pa_common.hpp"
@@ -619,7 +620,8 @@ void transpose_16NxK(TDST* dst, TSRC* src, TDST* tmp, size_t N, size_t K, size_t
}
#if defined(HAVE_AVX512F)
-static void transpose_16NxK(ov::bfloat16* dst, ov::bfloat16* src, ov::bfloat16* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+static void transpose_16NxK(T* dst, T* src, T* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
// will treat as uint32_t transpose
auto s = reinterpret_cast<uint32_t*>(src);
auto d = reinterpret_cast<uint32_t*>(dst);
@@ -669,8 +671,8 @@ void dequant(TDST* dst, uint8_t* src, size_t N, size_t K) {
}
#if defined(HAVE_AVX512F)
-// pack bf16/u8 to bf16
-static void pack_32x32_kernel(ov::bfloat16* dst, ov::bfloat16* src, size_t dst_stride, size_t src_stride) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+static void pack_32x32_kernel(T* dst, T* src, size_t dst_stride, size_t src_stride) {
static const uint64_t idx[8] = {0, 4, 1, 5, 2, 6, 3, 7};
auto midx = _mm512_loadu_si512(idx);
for (size_t i = 0; i < 16; i++) {
@@ -687,7 +689,8 @@ static void pack_32x32_kernel(ov::bfloat16* dst, ov::bfloat16* src, size_t dst_s
}
}
-static void pack_32x16_kernel(ov::bfloat16* dst, ov::bfloat16* src, size_t dst_stride, size_t src_stride) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+static void pack_32x16_kernel(T* dst, T* src, size_t dst_stride, size_t src_stride) {
static const uint64_t idx[8] = {0, 4, 1, 5, 2, 6, 3, 7};
auto midx = _mm512_loadu_si512(idx);
for (size_t i = 0; i < 16; i++) {
@@ -704,7 +707,8 @@ static void pack_32x16_kernel(ov::bfloat16* dst, ov::bfloat16* src, size_t dst_s
}
}
-static void pack_32Nx16K(ov::bfloat16* dst, ov::bfloat16* src, ov::bfloat16* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+static void pack_32Nx16K(T* dst, T* src, T* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
for (size_t n = 0; n < N; n += 32) {
size_t k = 0;
for (; k + 32 <= K; k += 32) {
@@ -718,7 +722,8 @@ static void pack_32Nx16K(ov::bfloat16* dst, ov::bfloat16* src, ov::bfloat16* tmp
}
}
-static void pack_32Nx16K(ov::bfloat16* dst, uint8_t* src, ov::bfloat16* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+static void pack_32Nx16K(T* dst, uint8_t* src, T* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
// The layout for per token per head:
// |scale(f32)|zeropoint(f32)|quantized feature(u8,idx_1)|quantized feature(u8,idx_2)|...|quantized feature(u8,idx_S)|
// The quantized feature will start from 8bytes=sizeof(float)+sizeof(float)
@@ -730,7 +735,7 @@ static void pack_32Nx16K(ov::bfloat16* dst, uint8_t* src, ov::bfloat16* tmp, siz
s += src_stride + 2 * sizeof(float);
t += src_stride;
}
- pack_32Nx16K(dst, tmp, reinterpret_cast<ov::bfloat16*>(0), N, K, dst_stride, src_stride);
+ pack_32Nx16K(dst, tmp, reinterpret_cast<T*>(0), N, K, dst_stride, src_stride);
}
#endif
@@ -769,7 +774,7 @@ struct MHAHelper {
std::vector<std::shared_ptr<BrgemmKernel>> _wv_gemm_acc;
// second token
std::shared_ptr<JitMatMulVecAMX> _gemv;
- bool _fastpath_valid = false;
+ ov::element::Type _fastpath_valid_prec = ov::element::undefined;
// second token for bhl loop
PlainTensor _weight_bhl;
PlainTensor _output_bhl;
@@ -851,11 +856,20 @@ struct MHAHelper {
_qk_scratch_a.resize<DATA_TYPE>({_nthr, _qk_gemm[_block_size - 1]->get_scratch_a_size() / sizeof(DATA_TYPE)});
_wv_scratch_a.resize<DATA_TYPE>({_nthr, _wv_gemm[_block_size - 1]->get_scratch_a_size() / sizeof(DATA_TYPE)});
- _fastpath_valid = dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::amx_bf16) &&
- (S % 32 == 0) && (block_size % 16 == 0) && (S <= 32 * 6) && precision_of<KVCACHE_TYPE>::value == ov::element::bf16;
- // aligned to cache line (64bytes=16*sizeof(float)) to avoid false sharing
- if (_fastpath_valid && !_gemv)
- _gemv = std::make_shared<JitMatMulVecAMX>(static_cast<int>(S), static_cast<int>(block_size));
+ if ((S % 32 == 0) && (block_size % 16 == 0) && (S <= 32 * 6)) {
+ if (dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::amx_bf16) &&
+ precision_of<DATA_TYPE>::value == ov::element::bf16 &&
+ precision_of<KVCACHE_TYPE>::value == ov::element::bf16) {
+ _fastpath_valid_prec = ov::element::bf16;
+ } else if (dnnl::impl::cpu::x64::mayiuse(dnnl::impl::cpu::x64::amx_fp16) &&
+ precision_of<DATA_TYPE>::value == ov::element::f16 &&
+ precision_of<KVCACHE_TYPE>::value == ov::element::f16) {
+ _fastpath_valid_prec = ov::element::f16;
+ }
+ }
+ if (one_of(_fastpath_valid_prec, ov::element::bf16, ov::element::f16) && !_gemv) {
+ _gemv = std::make_shared<JitMatMulVecAMX>(static_cast<int>(S), static_cast<int>(block_size), _fastpath_valid_prec);
+ }
}
if (init_alibi_lookup && (!_alibi_lookup || _alibi_lookup.m_dims[0] < kv_len)) {
@@ -903,7 +917,7 @@ struct MHAHelper {
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_bf16 = precision_of<DATA_TYPE>::value == ov::element::bf16;
+ constexpr bool q_is_xf16 = one_of(precision_of<DATA_TYPE>::value, ov::element::bf16, ov::element::f16);
constexpr bool q_cache_is_same = precision_of<DATA_TYPE>::value == precision_of<KVCACHE_TYPE>::value;
auto cur_kv_len_blocks = div_up(cur_kv_len, _block_size);
for (size_t h = hk * _h_each_group_len; h < (hk + 1) * _h_each_group_len; h++) {
@@ -978,12 +992,12 @@ struct MHAHelper {
// reuse float buffer, need to use float to compute offset
auto* w_ptr = reinterpret_cast<DATA_TYPE*>(_weight.ptr<float>(ithr, h, 0, 0));
- float* fp32_out_ptr = q_is_bf16 ? _output.ptr<float>(ithr, 0, h, 0) : output_emb.ptr<float>(q_start, h * _S);
+ float* fp32_out_ptr = q_is_xf16 ? _output.ptr<float>(ithr, 0, h, 0) : output_emb.ptr<float>(q_start, h * _S);
// for each weight block, loop through all value block
for (size_t v_blk = 0; v_blk < cur_kv_len_blocks; v_blk++) {
DATA_TYPE* v_ptr;
- if (q_is_bf16 || !q_cache_is_same) {
+ if (q_is_xf16 || !q_cache_is_same) {
v_ptr = wv_scratch_b.ptr<DATA_TYPE>(v_blk, hk);
} else {
v_ptr = present_value.ptr<DATA_TYPE>(block_table[v_blk], hk);
@@ -1004,11 +1018,11 @@ struct MHAHelper {
_wv_scratch_a ? _wv_scratch_a.ptr<DATA_TYPE>(ithr, 0) : nullptr);
}
}
- if (q_is_bf16) {
+ if (q_is_xf16) {
attn_memcpy2d_kernel(_output.ptr<float>(ithr, 0, h, 0),
output_emb.ptr<DATA_TYPE>(q_start, h * _S),
ov::element::f32,
- ov::element::bf16,
+ precision_of<DATA_TYPE>::value,
_output.stride(1),
output_emb.stride(0),
_S,
@@ -1026,13 +1040,13 @@ struct MHAHelper {
// output: [nthr, 32, H, S]
void exec_kernel_one_bh(const PlainTensor& query, const PlainTensor& present_key, const PlainTensor& present_value, const PlainTensor& output_emb,
const int32_t* block_table, size_t ithr, size_t hk, size_t q_len, size_t cur_kv_len, const PlainTensor& alibi_slopes, float* score_output) {
- if (_fastpath_valid) {
+ if (one_of(_fastpath_valid_prec, ov::element::bf16, ov::element::f16)) {
_gemv->tile_config();
for (size_t pk = 0, i = 0; pk < cur_kv_len; pk += _block_size, i++) {
auto block_number = block_table[i];
for (size_t pq = 0; pq < q_len; pq++) {
for (size_t h = hk * _h_each_group_len; h < (hk + 1) * _h_each_group_len; h++) {
- (*_gemv)(query.ptr<ov::bfloat16>(h, pq), present_key.ptr<ov::bfloat16>(block_number, hk),
+ (*_gemv)(query.ptr<DATA_TYPE>(h, pq), present_key.ptr<KVCACHE_TYPE>(block_number, hk),
_weight.ptr<float>(ithr, h, pq) + pk);
}
}
@@ -1128,11 +1142,11 @@ struct MHAHelper {
auto pk = pk_in_blocks * _block_size;
if (pk < context_len) {
auto block_number = block_indices.ptr<int32_t>()[block_indices_begins.ptr<int32_t>()[b] + pk_in_blocks];
- if (_fastpath_valid) {
+ if (one_of(_fastpath_valid_prec, ov::element::bf16, ov::element::f16)) {
_gemv->tile_config();
for (size_t pq = 0; pq < q_len; pq++) {
for (size_t h = hk * _h_each_group_len; h < (hk + 1) * _h_each_group_len; h++) {
- (*_gemv)(query.ptr<ov::bfloat16>(b, h, pq), present_key.ptr<ov::bfloat16>(block_number, hk),
+ (*_gemv)(query.ptr<DATA_TYPE>(b, h, pq), present_key.ptr<KVCACHE_TYPE>(block_number, hk),
_weight_bhl.ptr<float>(b, h, pq) + pk);
}
}
@@ -1334,7 +1348,7 @@ struct MHA {
const PlainTensor& alibi_slopes) {
auto Hk = v_cache.m_dims[1];
- constexpr bool q_is_bf16 = precision_of<DATA_TYPE>::value == ov::element::bf16;
+ constexpr bool q_is_xf16 = one_of(precision_of<DATA_TYPE>::value, ov::element::bf16, ov::element::f16);
constexpr bool q_cache_is_same = precision_of<DATA_TYPE>::value == precision_of<KVCACHE_TYPE>::value;
auto attn_work_count = _workitems.attn_work_size();
auto reorder_work_count = _workitems.reorder_work_size();
@@ -1360,7 +1374,7 @@ struct MHA {
_helper._output.template ptr<DATA_TYPE>(ithr),
_helper._block_size,
_helper._S, _helper._block_size, _helper._S);
- if (q_is_bf16) {
+ if (q_is_xf16) {
pack_32Nx16K(_helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, kv_block, hk),
v_ptr,
_helper._output.template ptr<DATA_TYPE>(ithr),
@@ -1604,6 +1618,17 @@ std::shared_ptr<PagedAttentionExecutor> make_pa_executor(ov::element::Type data_
}
#else
OPENVINO_THROW("make_pa_executor: bf16 needs avx512+ hardware.");
+#endif
+ } else if (data_type == ov::element::f16) {
+#if defined(HAVE_AVX512F)
+ if (kvcache_type == ov::element::u8) {
+ executor = std::make_shared<AttentionExecutor<ov::float16, uint8_t>>();
+ } else {
+ OPENVINO_ASSERT(kvcache_type == ov::element::f16, "expect kvcache type f16, current: ", kvcache_type);
+ executor = std::make_shared<AttentionExecutor<ov::float16, ov::float16>>();
+ }
+#else
+ OPENVINO_THROW("make_pa_executor: f16 needs avx512+ hardware.");
#endif
} else if (data_type == ov::element::f32) {
if (kvcache_type == ov::element::u8) {
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.cpp
index 63a8a0f7d24..70723a577b0 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.cpp
@@ -10,6 +10,7 @@
#include <type_traits>
#include "openvino/core/type/bfloat16.hpp"
+#include "openvino/core/type/float16.hpp"
#include "openvino/core/parallel.hpp"
#include "executor_pa_common.hpp"
#include "utils/plain_tensor.hpp"
@@ -57,7 +58,8 @@ void TileConfiger::generate() {
ret();
}
-JitMatMulVecAMX::JitMatMulVecAMX(int head_size, int block_size) : jit_generator(jit_name()), m_head_size(head_size), m_block_size(block_size) {
+JitMatMulVecAMX::JitMatMulVecAMX(int head_size, int block_size, ov::element::Type amx_prec) :
+ jit_generator(jit_name()), m_head_size(head_size), m_block_size(block_size), m_amx_prec(amx_prec) {
create_kernel();
m_tile_cfg.reset(1,
0,
@@ -98,7 +100,11 @@ void JitMatMulVecAMX::generate() {
tilezero(tmmC);
for (int i = 0; i < num_B_tiles; i++) {
tileloadd(tmmA, ptr[reg_k_addr + reg_stride_A + i * 64]);
- tdpbf16ps(tmmC, tmmA, Xbyak::Tmm(tmmB0.getIdx() + i));
+ if (m_amx_prec == ov::element::bf16) {
+ tdpbf16ps(tmmC, tmmA, Xbyak::Tmm(tmmB0.getIdx() + i));
+ } else if (m_amx_prec == ov::element::f16) {
+ tdpfp16ps(tmmC, tmmA, Xbyak::Tmm(tmmB0.getIdx() + i));
+ }
}
tilestored(ptr[reg_dst_addr + reg_stride_BC + m * sizeof(float)], tmmC);
add(reg_k_addr, m_head_size * 2 * 16);
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.hpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.hpp
index 237860ec692..bc21457a328 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/executor_pa_common.hpp
@@ -69,9 +69,10 @@ public:
DECLARE_CPU_JIT_AUX_FUNCTIONS(JitMatMulVecAMX)
int m_head_size;
int m_block_size;
+ ov::element::Type m_amx_prec;
TileConfiger m_tile_configer;
TileConfig m_tile_cfg;
- JitMatMulVecAMX(int head_size, int block_size);
+ JitMatMulVecAMX(int head_size, int block_size, ov::element::Type amx_prec);
void tile_config() {
m_tile_configer(&m_tile_cfg);
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/mha_single_token.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/mha_single_token.cpp
index 3ce275d47e3..0670c744a6d 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/mha_single_token.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/mha_single_token.cpp
@@ -841,20 +841,20 @@ static void attn_reduce(ov::float16* dst, ov::float16* temp, size_t M, size_t S,
template <typename T, typename T2, typename T3>
static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
- const ov::intel_cpu::PlainTensor& present_key,
- const ov::intel_cpu::PlainTensor& present_value,
- const ov::intel_cpu::PlainTensor& alibi_mask,
- const ov::intel_cpu::PlainTensor& attention_mask,
- const ov::intel_cpu::PlainTensor& beams,
- ov::intel_cpu::PlainTensor& output_emb,
- ov::intel_cpu::PlainTensor& buf_attn_w,
- ov::intel_cpu::PlainTensor& buf_attn_score,
- bool has_out_transpose,
- bool auto_causal,
- float d_scale,
- const ov::intel_cpu::PlainTensor& past_k_scale_zp,
- const ov::intel_cpu::PlainTensor& past_v_scale_zp,
- ov::intel_cpu::PlainTensor& head_sum) {
+ const ov::intel_cpu::PlainTensor& present_key,
+ const ov::intel_cpu::PlainTensor& present_value,
+ const ov::intel_cpu::PlainTensor& alibi_mask,
+ const ov::intel_cpu::PlainTensor& attention_mask,
+ const ov::intel_cpu::PlainTensor& beams,
+ ov::intel_cpu::PlainTensor& output_emb,
+ ov::intel_cpu::PlainTensor& buf_attn_w,
+ ov::intel_cpu::PlainTensor& buf_attn_score,
+ bool has_out_transpose,
+ bool auto_causal,
+ float d_scale,
+ const ov::intel_cpu::PlainTensor& past_k_scale_zp,
+ const ov::intel_cpu::PlainTensor& past_v_scale_zp,
+ ov::intel_cpu::PlainTensor& head_sum) {
ov::intel_cpu::PlainTensor causal_mask;
bool select_nfltmax_at_0 = false;
auto B = query.size(0);
@@ -976,16 +976,16 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
attn_mask_ptr = reinterpret_cast<uint8_t*>(&attention_mask.at<T>({b, h, pq, 0}, true));
uint8_t* cmask_ptr = causal_mask ? &causal_mask.at<uint8_t>({b, h, pq, 0}, true) : nullptr;
attn_softmax_kernel<T3>(buf_attn_w.ptr<T3>(b, h, pq),
- buf_attn_w.ptr<T3>(b, h, pq),
- d_scale,
- alibi_ptr,
- attn_mask_ptr,
- cmask_ptr,
- select_nfltmax_at_0,
- ncausal,
- cur_kv_len,
- attn_mask_prec,
- precision);
+ buf_attn_w.ptr<T3>(b, h, pq),
+ d_scale,
+ alibi_ptr,
+ attn_mask_ptr,
+ cmask_ptr,
+ select_nfltmax_at_0,
+ ncausal,
+ cur_kv_len,
+ attn_mask_prec,
+ precision);
});
// attn_w * V
@@ -1054,11 +1054,11 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
for (size_t pq = 0; pq < q_len; pq++) {
for (size_t h = h_group * h_each_group_len; h < (h_group + 1) * h_each_group_len; h++) {
attn_acc_value(buf_attn_score.ptr<T3>(ithr, b, pq, h),
- buf_attn_w.ptr<T3>(b, h, pq)[pv],
- v,
- S,
- p + 0,
- p + 1);
+ buf_attn_w.ptr<T3>(b, h, pq)[pv],
+ v,
+ S,
+ p + 0,
+ p + 1);
}
}
parallel_it_step(pv, kv_len, b, B, h_group, h_group_num);
@@ -1093,86 +1093,36 @@ void mha_single_token(const ov::intel_cpu::PlainTensor& query,
if (query.get_precision() == ov::element::bf16) {
if (present_key.get_precision() == ov::element::u8) {
mha_single_token_kernel<ov::bfloat16, uint8_t, float>(query,
- present_key,
- present_value,
- alibi_mask,
- attention_mask,
- beams,
- output_emb,
- buf_attn_w,
- buf_attn_score,
- has_out_transpose,
- auto_causal,
- d_scale,
- past_k_scale_zp,
- past_v_scale_zp,
- head_sum);
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
} else {
mha_single_token_kernel<ov::bfloat16, ov::bfloat16, float>(query,
- present_key,
- present_value,
- alibi_mask,
- attention_mask,
- beams,
- output_emb,
- buf_attn_w,
- buf_attn_score,
- has_out_transpose,
- auto_causal,
- d_scale,
- past_k_scale_zp,
- past_v_scale_zp,
- head_sum);
- }
- } else if (query.get_precision() == ov::element::f32) {
- if (present_key.get_precision() == ov::element::u8) {
- mha_single_token_kernel<float, uint8_t, float>(query,
- present_key,
- present_value,
- alibi_mask,
- attention_mask,
- beams,
- output_emb,
- buf_attn_w,
- buf_attn_score,
- has_out_transpose,
- auto_causal,
- d_scale,
- past_k_scale_zp,
- past_v_scale_zp,
- head_sum);
- } else if (present_key.get_precision() == ov::element::f16) {
- mha_single_token_kernel<float, ov::float16, float>(query,
- present_key,
- present_value,
- alibi_mask,
- attention_mask,
- beams,
- output_emb,
- buf_attn_w,
- buf_attn_score,
- has_out_transpose,
- auto_causal,
- d_scale,
- past_k_scale_zp,
- past_v_scale_zp,
- head_sum);
- } else {
- mha_single_token_kernel<float, float, float>(query,
- present_key,
- present_value,
- alibi_mask,
- attention_mask,
- beams,
- output_emb,
- buf_attn_w,
- buf_attn_score,
- has_out_transpose,
- auto_causal,
- d_scale,
- past_k_scale_zp,
- past_v_scale_zp,
- head_sum);
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
}
} else if (query.get_precision() == ov::element::f16) {
#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
@@ -1196,8 +1146,90 @@ void mha_single_token(const ov::intel_cpu::PlainTensor& query,
OPENVINO_THROW("Unsupported precision: ", query.get_precision());
}
#else
- OPENVINO_THROW("Unsupported precision: ", query.get_precision());
+ if (present_key.get_precision() == ov::element::u8) {
+ mha_single_token_kernel<ov::float16, uint8_t, float>(query,
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
+ } else {
+ mha_single_token_kernel<ov::float16, ov::float16, float>(query,
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
+ }
#endif
+ } else if (query.get_precision() == ov::element::f32) {
+ if (present_key.get_precision() == ov::element::u8) {
+ mha_single_token_kernel<float, uint8_t, float>(query,
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
+ } else if (present_key.get_precision() == ov::element::f16) {
+ mha_single_token_kernel<float, ov::float16, float>(query,
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
+ } else {
+ mha_single_token_kernel<float, float, float>(query,
+ present_key,
+ present_value,
+ alibi_mask,
+ attention_mask,
+ beams,
+ output_emb,
+ buf_attn_w,
+ buf_attn_score,
+ has_out_transpose,
+ auto_causal,
+ d_scale,
+ past_k_scale_zp,
+ past_v_scale_zp,
+ head_sum);
+ }
} else {
OPENVINO_THROW("Unsupported precision: ", query.get_precision());
}
@@ -1205,4 +1237,4 @@ void mha_single_token(const ov::intel_cpu::PlainTensor& query,
} // namespace XARCH
} // namespace Cpu
} // namespace Extensions
-} // namespace ov
\ No newline at end of file
+} // namespace ov
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 bffe0ee3761..60c6a24ec5f 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
@@ -751,14 +751,14 @@ inline void multiply_scalar(float* a, float* a_dst, const float val, const size_
while (i + vec_len_f32_avx512 <= size) {
v_a = _mm512_loadu_ps(a + i);
v_a = _mm512_mul_ps(v_a, v_scale);
- _mm512_storeu_ps(a_dst + i, v_a);
+ mm512_uni_storeu_ps(a_dst + i, v_a);
i += vec_len_f32_avx512;
}
if (i < size) {
__mmask16 mask = (1 << (size - i)) - 1;
v_a = _mm512_maskz_loadu_ps(mask, a + i);
v_a = _mm512_mul_ps(v_a, v_scale);
- _mm512_mask_storeu_ps(a_dst + i, mask, v_a);
+ mm512_uni_storeu_tail_ps(a_dst + i, v_a, size - i);
i += (size - i);
}
@@ -768,14 +768,14 @@ inline void multiply_scalar(float* a, float* a_dst, const float val, const size_
while (i + vec_len_f32_avx2 <= size) {
v_a = _mm256_loadu_ps(a + i);
v_a = _mm256_mul_ps(v_a, v_scale);
- _mm256_storeu_ps(a_dst + i, v_a);
+ mm256_uni_storeu_ps(a_dst + i, v_a);
i += vec_len_f32_avx2;
}
if (i < size) {
auto mask = get_mask(size - i);
v_a = _mm256_maskload_ps(a + i, mask);
v_a = _mm256_mul_ps(v_a, v_scale);
- _mm256_maskstore_ps(a_dst + i, mask, v_a);
+ mm256_uni_storeu_tail_ps(a_dst + i, v_a, size - i);
i += (size - i);
}
@@ -793,11 +793,12 @@ inline void multiply_scalar(float* a, float* a_dst, const float val, const size_
}
}
-inline void multiply_scalar(float* a, ov::bfloat16* a_dst, const float val, const size_t size) {
+template<typename T, typename = typename std::enable_if<(std::is_same<T, ov::bfloat16>::value || std::is_same<T, ov::float16>::value), bool>::type>
+inline void multiply_scalar(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);
__m512 v_a = {0};
- size_t i = 0;
while (i + vec_len_f32_avx512 <= size) {
v_a = _mm512_loadu_ps(a + i);
v_a = _mm512_mul_ps(v_a, v_scale);
@@ -808,10 +809,12 @@ inline void multiply_scalar(float* a, ov::bfloat16* a_dst, const float val, cons
__mmask16 mask = (1 << (size - i)) - 1;
v_a = _mm512_maskz_loadu_ps(mask, a + i);
v_a = _mm512_mul_ps(v_a, v_scale);
- mm512_uni_mask_storeu_ps(a_dst + i, mask, v_a);
+ mm512_uni_storeu_tail_ps(a_dst + i, v_a, size - i);
+
+ i += (size - i);
}
#else
- for (size_t i = 0; i < size; i++) {
+ for (; i < size; i++) {
a_dst[i] = a[i] * val;
}
#endif
@@ -898,6 +901,7 @@ inline void attn_softmax_kernel<float>(float* a,
float alibi_slope) {
using func_fp32_type = void (*)(float*, float, const float*, const float*, const uint8_t*, bool, size_t, float, float&);
using func_bf16_type = void (*)(float*, float, const float*, const ov::bfloat16*, const uint8_t*, bool, size_t, float, float&);
+ using func_f16_type = void (*)(float*, float, const float*, const ov::float16*, const uint8_t*, bool, size_t, float, float&);
static constexpr func_fp32_type funcs_fp32[] = {
scale_add2_reduce_max<false, false, false>,
scale_add2_reduce_max<false, false, true>,
@@ -918,12 +922,24 @@ inline void attn_softmax_kernel<float>(float* a,
scale_add2_reduce_max<true, true, false>,
scale_add2_reduce_max<true, true, true>
};
+ static constexpr func_f16_type funcs_f16[] = {
+ scale_add2_reduce_max<false, false, false>,
+ scale_add2_reduce_max<false, false, true>,
+ scale_add2_reduce_max<false, true, false>,
+ scale_add2_reduce_max<false, true, true>,
+ scale_add2_reduce_max<true, false, false>,
+ scale_add2_reduce_max<true, false, true>,
+ 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);
float max = std::numeric_limits<float>::lowest();
if (attn_mask_prec == ov::element::f32) {
funcs_fp32[dispatch](a, scale, alibi, static_cast<const float*>(attn_mask), causal_mask, select_nfltmax_at_0, len, alibi_slope, max);
- } else {
+ } else if (attn_mask_prec == ov::element::bf16) {
funcs_bf16[dispatch](a, scale, alibi, static_cast<const ov::bfloat16*>(attn_mask), causal_mask, select_nfltmax_at_0, len, alibi_slope, max);
+ } else {
+ funcs_f16[dispatch](a, scale, alibi, static_cast<const ov::float16*>(attn_mask), causal_mask, select_nfltmax_at_0, len, alibi_slope, max);
}
float sum = 0.0f;
@@ -936,11 +952,16 @@ inline void attn_softmax_kernel<float>(float* a,
// 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 {
+ } else if (dst_precision == ov::element::bf16) {
multiply_scalar(a, static_cast<ov::bfloat16*>(a_dst), scalar, len);
// apply causual mask to final result instead of attn_score
if (total_size > len)
memset(static_cast<ov::bfloat16*>(a_dst) + len, 0, sizeof(ov::bfloat16) * (total_size - len));
+ } else {
+ multiply_scalar(a, static_cast<ov::float16*>(a_dst), scalar, len);
+ // apply causual mask to final result instead of attn_score
+ if (total_size > len)
+ memset(static_cast<ov::float16*>(a_dst) + len, 0, sizeof(ov::float16) * (total_size - len));
}
}
#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
@@ -1022,4 +1043,4 @@ inline void attn_softmax_kernel<ov::float16>(ov::float16* a,
} // namespace XARCH
} // namespace Cpu
} // namespace Extensions
-} // namespace ov
\ No newline at end of file
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.cpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.cpp
index e729fac66dd..2895a272b98 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.cpp
@@ -38,32 +38,54 @@ BrgemmKernel::BrgemmKernel(size_t M,
// blocking M
M_blk = matmulOptimalM;
M_tail = M % M_blk;
- brgVnniFactor = 4 / inType.size();
- if (inType != ov::element::bf16 && inType != ov::element::f32)
- THROW_ERROR("brgemm kernel only supports bf16, f32");
+ if (!one_of(inType, ov::element::bf16, ov::element::f16, ov::element::f32))
+ THROW_ERROR("brgemm kernel only supports f16, bf16, f32");
+ bool is_f32 = inType == ov::element::f32;
+
bool is_bf16 = inType == ov::element::bf16;
if (is_bf16 && !mayiuse(avx512_core_bf16))
THROW_ERROR("brgemm bf16 kernel could only be used above avx512_bf16");
- bool isAMXSupported = is_bf16 && mayiuse(avx512_core_amx);
+ bool is_f16 = inType == ov::element::f16;
+ if (is_f16 && !mayiuse(avx512_core_fp16))
+ THROW_ERROR("brgemm f16 kernel could only be used above avx512_f16");
+
+ srcType = weiType = inType;
+ // If isa is avx512_core_fp16, f16 is supported by upconverted to f32
+ is_avx_f16_only = inType == ov::element::f16 && mayiuse(avx512_core_fp16) && !mayiuse(avx512_core_amx_fp16);
+ if (is_avx_f16_only) {
+ srcType = ov::element::f32;
+ weiType = ov::element::f32;
+ }
+ brgVnniFactor = 4 / weiType.size();
+
+ /*
+ AVX AMX
+ fp32 Y N
+ bf16 Y Y
+ fp16 Y Y
+ */
+ bool isAMXSupported = (is_bf16 && mayiuse(avx512_core_amx)) || (is_f16 && mayiuse(avx512_core_amx_fp16));
+ bool isBrgWithAMX = isAMXSupported && !is_avx_f16_only;
+
size_t vlen;
if (mayiuse(avx512_core))
vlen = cpu_isa_traits<avx512_core>::vlen;
else
vlen = cpu_isa_traits<cpu_isa_t::avx2>::vlen;
// blocking N
- N_blk = is_bf16 ? 32 : std::max(N, vlen / inType.size());
+ N_blk = !is_f32 ? 32 : std::max(N, vlen / inType.size());
N_tail = N % N_blk;
// blocking K
- K_blk = isAMXSupported ? 32 : K;
+ K_blk = isBrgWithAMX ? 32 : K;
K_tail = K % K_blk;
- if (isAMXSupported && K_tail) {
+ if (isBrgWithAMX && K_tail) {
K_tail = rnd_up(K_tail, 2);
}
// copied K must be round up by vlen / inType.size(), otherwise copy B kernel may access wrong memory
- packedBSize = rnd_up(K, vlen / inType.size()) * rnd_up(N, N_blk) * inType.size();
+ packedBSize = rnd_up(K, vlen / weiType.size()) * rnd_up(N, N_blk) * weiType.size();
size_t brg0BaseIdx = std::numeric_limits<size_t>::max();
for (size_t m = 0; m < 2; m++) {
for (size_t k = 0; k < 2; k++) {
@@ -78,18 +100,18 @@ BrgemmKernel::BrgemmKernel(size_t M,
brgemmCtx.M = M_;
brgemmCtx.N = N_;
brgemmCtx.K = K_;
- brgemmCtx.LDA = k ? K_blk : lda;
- brgemmCtx.LDB = (is_bf16 || b_transposed) ? rnd_up(N, N_blk) : ldb; // bf16/b_transposed needs copy
+ brgemmCtx.LDA = k ? K_blk : (is_avx_f16_only ? K : lda); // f16 use f32 internally
+ brgemmCtx.LDB = (!is_f32 || b_transposed) ? rnd_up(N, N_blk) : ldb; // bf16/fp16/b_transposed needs copy
brgemmCtx.LDC = ldc;
- brgemmCtx.dt_in0 = static_cast<dnnl_data_type_t>(DnnlExtensionUtils::ElementTypeToDataType(inType));
- brgemmCtx.dt_in1 = static_cast<dnnl_data_type_t>(DnnlExtensionUtils::ElementTypeToDataType(inType));
+ brgemmCtx.dt_in0 = static_cast<dnnl_data_type_t>(DnnlExtensionUtils::ElementTypeToDataType(srcType));
+ brgemmCtx.dt_in1 = static_cast<dnnl_data_type_t>(DnnlExtensionUtils::ElementTypeToDataType(weiType));
brgemmCtx.beta = beta;
// don't create brgemm kernels for empty tiles
if (M_ != 0 && K_ != 0 && N_ != 0) {
if (brg0BaseIdx == std::numeric_limits<size_t>::max())
brg0BaseIdx = getBrgIdx(m, k, n);
- init_brgemm(brgemmCtx, brgKernels[getBrgIdx(m, k, n)], isAMXSupported);
+ init_brgemm(brgemmCtx, brgKernels[getBrgIdx(m, k, n)], isBrgWithAMX);
}
}
}
@@ -97,12 +119,19 @@ BrgemmKernel::BrgemmKernel(size_t M,
auto& brgemmCtx0 = brgCtxs[brg0BaseIdx];
- if (brgemmCtx0.is_with_amx && K_tail) {
- init_brgemm_copy_a(brgCopyAKernel, K, K_blk, K_tail, K_blk, brgemmCtx0.dt_in0, false, lda * inType.size());
- packedASize = M_blk * rnd_up(K, K_blk) * inType.size();
+ if ((brgemmCtx0.is_with_amx && K_tail) || is_avx_f16_only) {
+ init_brgemm_copy_a(brgCopyAKernel,
+ K,
+ K_blk,
+ K_tail,
+ is_avx_f16_only ? K : K_blk,
+ brgemmCtx0.dt_in0,
+ false,
+ lda * inType.size());
+ packedASize = M_blk * rnd_up(K, brgemmCtx0.LDA) * srcType.size();
}
- if (brgemmCtx0.is_with_amx || inType == ov::element::bf16 || b_transposed) {
+ if (brgemmCtx0.is_with_amx || !is_f32 || b_transposed) {
size_t b_stride = 0;
b_stride = ldb * inType.size();
// K should use the original K
@@ -136,10 +165,20 @@ void BrgemmKernel::init_brgemm(brgemmCtx& ctx,
const bool is_int8 =
one_of(ctx.dt_in0, data_type::u8, data_type::s8) && one_of(ctx.dt_in1, data_type::u8, data_type::s8);
cpu_isa_t isa;
- if (mayiuse(avx512_core)) {
- isa = use_amx ? isa_undef
- : ctx.dt_in0 == dnnl_data_type_t::dnnl_bf16 ? avx512_core_bf16
- : (is_int8 ? avx512_core_vnni : avx512_core);
+ if (use_amx) {
+ isa = isa_undef;
+ } else if (mayiuse(avx512_core)) {
+ if (ctx.dt_in0 == dnnl_data_type_t::dnnl_bf16 && mayiuse(avx512_core_bf16)) {
+ isa = avx512_core_bf16;
+ } else if (ctx.dt_in0 == dnnl_data_type_t::dnnl_f16 && mayiuse(avx512_core_fp16)) {
+ isa = avx512_core_fp16;
+ } else {
+ if (is_int8) {
+ isa = avx512_core_vnni;
+ } else {
+ isa = avx512_core;
+ }
+ }
} else {
isa = cpu_isa_t::avx2;
}
@@ -161,7 +200,7 @@ void BrgemmKernel::init_brgemm(brgemmCtx& ctx,
ctx.K,
nullptr);
if (status != dnnl_success) {
- THROW_ERROR("cannot be executed due to invalid brgconv params");
+ THROW_ERROR("cannot be executed due to invalid brgemm params");
}
if (use_amx && b_accumulate) {
@@ -193,6 +232,7 @@ void BrgemmKernel::init_brgemm(brgemmCtx& ctx,
}
brgKernel.reset(brgKernel_);
}
+
void BrgemmKernel::init_brgemm_copy_a(
std::unique_ptr<dnnl::impl::cpu::x64::matmul::jit_brgemm_matmul_copy_a_t>& brgCopyKernel,
size_t K,
@@ -214,13 +254,15 @@ void BrgemmKernel::init_brgemm_copy_a(
brgCopyKernelConf.s8s8_compensation_required = false;
brgCopyKernelConf.wei_zp_type = dnnl::impl::cpu::x64::none;
brgCopyKernelConf.src_zp_type = dnnl::impl::cpu::x64::none;
- brgCopyKernelConf.src_dt = dt_in0;
+ brgCopyKernelConf.src_dt = is_avx_f16_only ? dnnl_data_type_t::dnnl_f32 : dt_in0;
brgCopyKernelConf.copy_A_src_stride = copy_A_src_stride;
- brgCopyKernelConf.a_dt_sz = DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(dt_in0));
+ // copy_a_kernel assumes that in/out tensor has same data type except f16
+ // copy_a_kernel has special path for f16: assuming input(f16) -> output(f32)
+ brgCopyKernelConf.a_dt_sz = is_avx_f16_only ? sizeof(ov::float16) : DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(dt_in0));
// copied A has the same precision of original
- brgCopyKernelConf.tr_a_dt_sz = DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(dt_in0));
+ brgCopyKernelConf.tr_a_dt_sz = is_avx_f16_only ? sizeof(float) : DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(dt_in0));
brgCopyKernelConf.transposed_A = transpose;
- brgCopyKernelConf.isa = avx512_core_amx;
+ brgCopyKernelConf.isa = is_avx_f16_only ? avx512_core_fp16 : avx512_core_amx;
create_brgemm_matmul_copy_a(brgCopyKernel, &brgCopyKernelConf);
}
@@ -238,8 +280,8 @@ void BrgemmKernel::init_brgemm_copy_b(
bool transpose,
size_t copy_B_wei_stride) {
brgemm_matmul_conf_t brgCopyKernelConf;
- brgCopyKernelConf.src_dt = dt_in0;
- brgCopyKernelConf.wei_dt = dt_in1;
+ brgCopyKernelConf.src_dt = is_avx_f16_only ? dnnl_data_type_t::dnnl_f32 : dt_in0;
+ brgCopyKernelConf.wei_dt = is_avx_f16_only ? dnnl_data_type_t::dnnl_f32 : dt_in1;
brgCopyKernelConf.orig_wei_dt = dt_in1;
brgCopyKernelConf.wei_n_blk = N_blk;
brgCopyKernelConf.wei_tag = transpose ? dnnl_ba : dnnl_ab;
@@ -255,17 +297,23 @@ void BrgemmKernel::init_brgemm_copy_b(
brgCopyKernelConf.K_blk = K;
brgCopyKernelConf.K_tail = 0;
brgCopyKernelConf.N_chunk_elems = brgCopyKernelConf.N_blk;
- brgCopyKernelConf.b_dt_sz =
+ // f16 is computed by upconverting. in(f16) -> out(f32)
+ brgCopyKernelConf.b_dt_sz = is_avx_f16_only ? sizeof(ov::float16) :
DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(brgCopyKernelConf.src_dt));
- brgCopyKernelConf.tr_b_dt_sz =
+ brgCopyKernelConf.tr_b_dt_sz = is_avx_f16_only ? sizeof(float) :
DnnlExtensionUtils::sizeOfDataType(static_cast<dnnl::memory::data_type>(brgCopyKernelConf.src_dt));
brgCopyKernelConf.req_wei_vnni_downconvert = false;
if (is_with_amx) {
- brgCopyKernelConf.isa = avx512_core_amx;
+ brgCopyKernelConf.isa = dt_in0 == dnnl_data_type_t::dnnl_f16 ? avx512_core_amx_fp16 : avx512_core_amx;
brgCopyKernelConf.s8s8_compensation_required = false;
} else {
- brgCopyKernelConf.isa = dt_in0 == dnnl_data_type_t::dnnl_bf16 ? avx512_core_bf16 : avx512_core_vnni;
+ if (inType == ov::element::f16) {
+ brgCopyKernelConf.isa = mayiuse(avx512_core_fp16) ? avx512_core_fp16 : avx2_vnni_2;
+ } else {
+ brgCopyKernelConf.isa = dt_in0 == dnnl_data_type_t::dnnl_bf16 ? avx512_core_bf16 : avx512_core_vnni;
+ }
+ brgCopyKernelConf.s8s8_compensation_required = false;
}
brgCopyKernelConf.has_zero_point_a = false;
@@ -283,7 +331,7 @@ void BrgemmKernel::copy_buffer_b(void* b, void* scratch_b) {
for (size_t nb = 0; nb < div_up(N, N_blk); nb++) {
auto N_stride = b_transposed ? ldb : 1;
auto pCopyKernel0In = ptr_b + nb * N_blk * inType.size() * N_stride;
- auto pCopyKernel0Out = ptr_scartch_b + nb * N_blk * brgVnniFactor * inType.size();
+ auto pCopyKernel0Out = ptr_scartch_b + nb * N_blk * brgVnniFactor * weiType.size();
auto ctx = jit_brgemm_matmul_copy_b_t::ctx_t();
@@ -306,15 +354,13 @@ void BrgemmKernel::executeGemm(bool is_M_tail, void* a, void* b, void* c, void*
auto ptr_C = reinterpret_cast<uint8_t*>(c);
auto ptr_scartch_a = reinterpret_cast<uint8_t*>(scratch_a);
auto ptr_scartch_b = reinterpret_cast<uint8_t*>(b);
- uint8_t* ptr_a_tail = nullptr;
size_t brgIdx0 = getBrgIdx(0, 0, 0);
// The step for matrix A over main K dimension
size_t K0_step0 = brgCtxs[brgIdx0].K;
auto cur_M_blk = is_M_tail ? M_tail : M_blk;
if (brgCopyAKernel) {
- // only copy tailed data;
- size_t K_offset = K < K_blk ? 0 : K0_step0 * inType.size();
+ size_t K_offset = is_avx_f16_only ? 0 : (K < K_blk ? 0 : K0_step0 * srcType.size());
auto pCopyKernelIn = ptr_A + K_offset;
auto pCopyKernelOut = ptr_scartch_a;
@@ -331,8 +377,6 @@ void BrgemmKernel::executeGemm(bool is_M_tail, void* a, void* b, void* c, void*
ctx.current_K_blk = K % K_blk;
(*brgCopyAKernel)(&ctx);
-
- ptr_a_tail = pCopyKernelOut;
}
size_t count_N = 0;
for (size_t n = 0; n < 2; n++) {
@@ -341,17 +385,17 @@ void BrgemmKernel::executeGemm(bool is_M_tail, void* a, void* b, void* c, void*
size_t mIdx = is_M_tail ? 1 : 0;
auto& brgemmCtx = brgCtxs[getBrgIdx(mIdx, k, n)];
if (brgemmCtx.K != 0 && brgemmCtx.N != 0 && brgemmCtx.M != 0) {
- auto local_a_ptr = k > 0 ? ptr_a_tail : ptr_A;
- auto B_stride = (k * count_K + n * count_N * brgVnniFactor) * inType.size();
+ auto local_a_ptr = is_avx_f16_only ? ptr_scartch_a : (k > 0 ? ptr_scartch_a : ptr_A);
+ auto B_stride = (k * count_K + n * count_N * brgVnniFactor) * weiType.size();
auto weight_ptr = ptr_scartch_b + B_stride;
auto C_stride = n * count_N * ov::element::f32.size();
auto out_ptr = ptr_C + C_stride;
callBrgemm(brgemmCtx,
- brgKernels[getBrgIdx(mIdx, k, n)],
- local_a_ptr,
- weight_ptr,
- out_ptr,
- wsp);
+ brgKernels[getBrgIdx(mIdx, k, n)],
+ local_a_ptr,
+ weight_ptr,
+ out_ptr,
+ wsp);
// stride K, N if body kernel is executed.
if (k == 0) {
count_K = brgemmCtx.K * brgemmCtx.LDB;
@@ -373,17 +417,17 @@ void BrgemmKernel::executeGemm(void* a, void* b, void* c, void* wsp, void* scrat
for (size_t mb = 0; mb < div_up(M, M_blk); mb++) {
const bool is_M_tail = (M - mb * M_blk < M_blk);
- auto ptr_a = ptr_A + (mb * M_blk * lda) * inType.size();
+ auto ptr_a = ptr_A + (mb * M_blk * lda) * srcType.size();
auto ptr_c = ptr_C + (mb * M_blk * ldc) * ov::element::f32.size();
executeGemm(is_M_tail, ptr_a, scratch_b, wsp, ptr_c, scratch_a);
}
}
void BrgemmKernel::callBrgemm(brgemmCtx& ctx,
- std::unique_ptr<dnnl::impl::cpu::x64::brgemm_kernel_t>& brgKernel,
- const void* pin0,
- const void* pin1,
- void* pout,
- void* wsp) {
+ std::unique_ptr<dnnl::impl::cpu::x64::brgemm_kernel_t>& brgKernel,
+ const void* pin0,
+ const void* pin1,
+ void* pout,
+ void* wsp) {
if (ctx.is_with_amx)
amx_tile_configure(ctx.palette);
if (ctx.is_with_comp) {
@@ -398,4 +442,4 @@ void BrgemmKernel::callBrgemm(brgemmCtx& ctx,
}
} // namespace intel_cpu
-} // namespace ov
\ No newline at end of file
+} // namespace ov
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.hpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.hpp
index 513b484ab0b..38384f2acea 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.hpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/brgemm_kernel.hpp
@@ -59,6 +59,9 @@ private:
size_t packedBSize = 0;
size_t packedASize = 0;
ov::element::Type inType;
+ ov::element::Type weiType;
+ ov::element::Type srcType;
+ bool is_avx_f16_only = false;
bool b_accumulate = false;
static constexpr size_t MHA_BRGEMM_KERNELS_NUM = 8;
static constexpr size_t matmulOptimalM = 32;
diff --git a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
index 2272fa481d5..6bf7d3099a8 100644
--- a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
@@ -190,6 +190,8 @@ ov::element::Type PagedAttention::getRuntimePrecision() const {
// bf16 should be enabled only when platform supports
if (rtPrecision == ov::element::bf16 && ov::with_cpu_x86_bfloat16()) {
rtPrecision = ov::element::bf16;
+ } else if (rtPrecision == ov::element::f16 && ov::with_cpu_x86_avx512_core_fp16()) {
+ rtPrecision = ov::element::f16;
} else {
rtPrecision = ov::element::f32;
}
diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
index 016fa90398a..eecba2acff2 100644
--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
@@ -261,7 +261,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
void prepare_brgemm_prim(dnnl::stream strm, PlainTensor& query, PlainTensor& present_key, bool has_out_transpose) {
auto in_type = precision_of<T>::value;
- auto qkv_dt = in_type == ov::element::f32 ? dt::f32 : dt::bf16;
+ auto qkv_dt = DnnlExtensionUtils::ElementTypeToDataType(in_type);
auto B = query.size(0);
auto H = query.size(1);
auto q_len = query.size(2);
@@ -354,13 +354,13 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
size_t h_each_group_len = H / Hk;
const size_t m_block_size = qk_gemm_ptr->get_mblk_size();
auto m_blocks = (q_len + m_block_size - 1) / m_block_size;
- bool is_bf16 = precision_of<T>::value == ov::element::bf16;
+ bool is_xf16 = precision_of<T>::value == ov::element::bf16 || precision_of<T>::value == ov::element::f16;
// packed k, v
parallel_for2d(B, Hk, [&](size_t b, size_t h) {
T* k_ptr = &present_key.at<T>({b, h, 0, 0});
T* v_ptr = &present_value.at<T>({b, h, 0, 0});
qk_gemm_ptr->copy_buffer_b(k_ptr, &qk_scratch_b.at<T>({b, h, 0}));
- if (is_bf16)
+ if (is_xf16)
wv_gemm_ptr->copy_buffer_b(v_ptr, &wv_scratch_b.at<T>({b, h, 0}));
});
@@ -420,12 +420,12 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
}
auto* w_ptr = reinterpret_cast<T*>(weight_score.ptr<float>(ithr, h, 0, 0));
float* fp32_out_ptr;
- if (is_bf16) {
+ if (is_xf16) {
fp32_out_ptr = has_out_transpose ? &fp32_out.at<float>({b, m_start, h, 0}) : &fp32_out.at<float>({b, h, m_start, 0});
} else {
fp32_out_ptr = has_out_transpose ? &output_emb.at<float>({b, m_start, h * head_size}) : &output_emb.at<float>({b, h, m_start, 0});
}
- T* v_ptr = is_bf16 ? &wv_scratch_b.at<T>({b, h / h_each_group_len, 0})
+ T* v_ptr = is_xf16 ? &wv_scratch_b.at<T>({b, h / h_each_group_len, 0})
: &present_value.at<T>({b, h / h_each_group_len, 0, 0});
wv_gemm_ptr->executeGemm(m_cnt < m_block_size,
w_ptr,
@@ -433,12 +433,12 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
fp32_out_ptr,
wsp.data() + tid * wsp_size_per_thread,
wv_scratch_a ? &wv_scratch_a.at<T>({tid, 0}) : nullptr);
- if (is_bf16) {
+ if (is_xf16) {
if (has_out_transpose) {
attn_memcpy2d_kernel(&fp32_out.at<float>({b, m_start, h, 0}),
&output_emb.at<T>({b, m_start, h * head_size}),
ov::element::f32,
- ov::element::bf16,
+ precision_of<T>::value,
fp32_out.stride(1),
output_emb.stride(1),
head_size,
@@ -447,7 +447,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
attn_memcpy2d_kernel(&fp32_out.at<float>({b, h, m_start, 0}),
&output_emb.at<T>({b, h, m_start, 0}),
ov::element::f32,
- ov::element::bf16,
+ precision_of<T>::value,
0,
0,
m_cnt * head_size,
@@ -1068,28 +1068,35 @@ void ScaledDotProductAttention::createPrimitive() {
auto builder = [&](const ScaledDotProductAttentionKey& key) -> std::shared_ptr<Executor> {
std::shared_ptr<Executor> executor = nullptr;
- if (rtPrecision == ov::element::bf16) {
#ifdef OPENVINO_ARCH_X86_64
+ if (rtPrecision == ov::element::bf16) {
executor = std::make_shared<AttentionExecutor<KT_ONEDNN, ov::bfloat16>>(context);
-#endif
+ } else if (rtPrecision == ov::element::f16) {
+ if (with_cpu_x86_avx512_core_fp16()) {
+ executor = std::make_shared<AttentionExecutor<KT_ONEDNN, ov::float16>>(context);
+ } else {
+ executor = std::make_shared<AttentionExecutor<KT_REF, ov::float16>>(context);
+ }
} else {
-#if defined(OV_CPU_WITH_ACL)
- if (rtPrecision == ov::element::f16)
- executor = std::make_shared<AttentionExecutor<KT_ACL, ov::float16>>(context);
- else
- executor = std::make_shared<AttentionExecutor<KT_ACL, float>>(context);
-#elif defined(OV_CPU_WITH_MLAS)
+#ifdef OV_CPU_WITH_MLAS
executor = std::make_shared<AttentionExecutor<KT_MLAS, float>>(context);
-#elif defined(OPENVINO_ARCH_X86_64)
+#else
if (with_cpu_x86_avx512_core()) {
executor = std::make_shared<AttentionExecutor<KT_ONEDNN, float>>(context);
} else {
executor = std::make_shared<AttentionExecutor<KT_REF, float>>(context);
}
-#else
- executor = std::make_shared<AttentionExecutor<KT_REF, float>>(context);
#endif
}
+#elif defined(OV_CPU_WITH_ACL)
+ if (rtPrecision == ov::element::f16) {
+ executor = std::make_shared<AttentionExecutor<KT_ACL, ov::float16>>(context);
+ } else {
+ executor = std::make_shared<AttentionExecutor<KT_ACL, float>>(context);
+ }
+#else
+ executor = std::make_shared<AttentionExecutor<KT_REF, float>>(context);
+#endif
return executor;
};
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index abf1ad8f283..0e683482a97 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -238,6 +238,17 @@ bool Transformations::fuse_type_to_fq(const std::shared_ptr<ov::Node>& node, con
return true;
}
+bool Transformations::fuse_type_to_pa(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions) {
+ auto pa = ov::as_type_ptr<ov::op::PagedAttentionExtension>(node);
+ if (!pa)
+ return false;
+ // PagedAttentionExtension's 2nd output type should be kept f32.
+ // The reason is that the pagedattention node in CPU plugin hardcodes 2nd output type as f32.
+ // So, set f32 to the 2nd output type, which can avoid extra data type conversion during transformation.
+ pa->set_out_type(1, ov::element::f32);
+ return true;
+}
+
bool Transformations::fuse_type_to_convert(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions) {
auto convert = ov::as_type_ptr<ov::opset10::Convert>(node);
if (!convert)
@@ -391,7 +402,7 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
#if defined(OPENVINO_ARCH_ARM) || defined(OPENVINO_ARCH_ARM64)
type_to_fuse_map fuse_map = {{ov::opset1::FakeQuantize::get_type_info_static(), fuse_type_to_fq}};
#else
- type_to_fuse_map fuse_map = {};
+ type_to_fuse_map fuse_map = {{ov::op::PagedAttentionExtension::get_type_info_static(), fuse_type_to_pa}};
#endif
const bool keep_precision_sensitive_in_fp32 = true;
CPU_REGISTER_PASS_COMMON(manager,
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.h b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.h
index 0b6a437f667..33c26ab8aea 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.h
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.h
@@ -48,6 +48,7 @@ private:
static bool fuse_type_to_convert(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions);
static bool fuse_type_to_fq(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions);
+ static bool fuse_type_to_pa(const std::shared_ptr<ov::Node>& node, const precisions_map& precisions);
};
} // namespace intel_cpu
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.cpp
index eb6fdc2a6bf..8a9212f8998 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
@@ -37,6 +37,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
ConcatSDPTest,
::testing::Combine(::testing::Values(ElementType::f16),
::testing::ValuesIn(inputShapes),
+ ::testing::Values(false),
::testing::Values(true, false)),
ConcatSDPTest::getTestCaseName);
} // namespace
diff --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.cpp
index f4abaa03b7c..f5a7bfacfac 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
@@ -28,8 +28,9 @@ namespace test {
std::string ConcatSDPTest::getTestCaseName(const testing::TestParamInfo<ConcatSDPTestParams>& obj) {
ElementType inType;
std::vector<InputShape> inputShapes;
- bool hasShapeof;
- std::tie(inType, inputShapes, hasShapeof) = obj.param;
+ bool forceKVU8;
+ bool hasShapeOf;
+ std::tie(inType, inputShapes, forceKVU8, hasShapeOf) = obj.param;
std::ostringstream result;
result << "IS=";
for (const auto& shape : inputShapes) {
@@ -46,21 +47,24 @@ std::string ConcatSDPTest::getTestCaseName(const testing::TestParamInfo<ConcatSD
result << ")_";
}
result << "Prc=" << inType << "_";
- result << "HasShapeOf=" << hasShapeof;
+ result << "ForceKVU8=" << forceKVU8 << "_";
+ result << "HasShapeOf=" << hasShapeOf;
return result.str();
}
void ConcatSDPTest::SetUp() {
ElementType inType;
std::vector<InputShape> inputShapes;
- std::tie(inType, inputShapes, hasShapeOf) = this->GetParam();
+ std::tie(inType, inputShapes, m_forceKVU8, m_hasShapeOf) = this->GetParam();
targetDevice = ov::test::utils::DEVICE_CPU;
rel_threshold = 1e-2f;
- if (inType == ElementType::bf16) {
- configuration.insert({"ENFORCE_BF16", "YES"});
- rel_threshold = 0.01f;
- } else if (inType == ElementType::f16) {
+ if (inType == ElementType::bf16 || inType == ElementType::f16) {
configuration.insert({"INFERENCE_PRECISION_HINT", ov::element::Type(inType).get_type_name()});
+ rel_threshold = 0.01f;
+ }
+
+ if (m_forceKVU8) {
+ configuration["KV_CACHE_PRECISION"] = "u8";
}
init_input_shapes(inputShapes);
ov::ParameterVector inputParams;
@@ -92,7 +96,7 @@ void ConcatSDPTest::SetUp() {
// |
// ShapeOf...
// The transformation 'SimplifyGatherShapeOf' will move ShapeOf to be the child of ReadValue
- if (hasShapeOf) {
+ if (m_hasShapeOf) {
shapeof_k = std::make_shared<ov::op::v0::ShapeOf>(gatherK);
shapeof_v = std::make_shared<ov::op::v0::ShapeOf>(gatherV);
}
@@ -107,20 +111,20 @@ void ConcatSDPTest::SetUp() {
pastv_assign->set_friendly_name("pastv_w");
ResultVector results{std::make_shared<ov::op::v0::Result>(add)};
- if (hasShapeOf) {
+ if (m_hasShapeOf) {
results.push_back(std::make_shared<ov::op::v0::Result>(shapeof_k));
results.push_back(std::make_shared<ov::op::v0::Result>(shapeof_v));
}
SinkVector sinks{pastk_assign, pastv_assign};
function = std::make_shared<ov::Model>(results, sinks, inputParams, "ConcatSDP");
targetDevice = ov::test::utils::DEVICE_CPU;
-
functionRefs = function->clone();
pass::Manager manager;
// decompose ScaledDotProductAttention
manager.register_pass<ov::pass::ScaledDotProductAttentionDecomposition>();
manager.run_passes(functionRefs);
}
+
void ConcatSDPTest::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
std::vector<ov::Shape> shapes(4);
shapes[0] = targetInputStaticShapes[0];
@@ -129,6 +133,7 @@ void ConcatSDPTest::generate_inputs(const std::vector<ov::Shape>& targetInputSta
shapes[3] = targetInputStaticShapes[1];
SubgraphBaseTest::generate_inputs(shapes);
}
+
template<typename IT, typename T>
void strided_iota(IT first, size_t n, T value, T stride) {
for (size_t i = 0; i < n; i++) {
@@ -136,6 +141,7 @@ void strided_iota(IT first, size_t n, T value, T stride) {
value += stride;
}
}
+
void ConcatSDPTest::generate(int idx, const std::vector<ov::Shape>& targetInputStaticShapes) {
inputs.clear();
auto create_input = [this] (std::shared_ptr<op::v0::Parameter> param, ov::Shape shape, float val) {
@@ -169,16 +175,19 @@ void ConcatSDPTest::generate(int idx, const std::vector<ov::Shape>& targetInputS
create_input(function->get_parameters()[3], targetInputStaticShapes[1], idx + 4.0f);
create_input(function->get_parameters()[4], ov::Shape{targetInputStaticShapes[0][0]}, idx + 0.0f);
}
+
void ConcatSDPTest::prepare() {
compile_model();
inferRequest = compiledModel.create_infer_request();
ASSERT_TRUE(inferRequest);
}
+
void ConcatSDPTest::reset() {
for (auto&& state : inferRequest.query_state()) {
state.reset();
}
}
+
std::vector<ov::Tensor> ConcatSDPTest::run_test(std::shared_ptr<ov::Model> model) {
function = model;
prepare();
@@ -201,6 +210,12 @@ std::vector<ov::Tensor> ConcatSDPTest::run_test(std::shared_ptr<ov::Model> model
}
TEST_P(ConcatSDPTest, CompareWithRefs) {
SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ ElementType inType;
+ std::vector<InputShape> inputShapes;
+ bool forceKVU8;
+ bool hasShapeOf;
+ std::tie(inType, inputShapes, forceKVU8, hasShapeOf) = this->GetParam();
+
auto actualOutputs = run_test(function);
if (!hasShapeOf) {
CheckNumberOfNodesWithType(compiledModel, "ScaledDotProductAttention", 1);
@@ -216,9 +231,14 @@ TEST_P(ConcatSDPTest, CompareWithRefs) {
}
}
}
+
+ // the range of our result will exceed f16 max value and there may be 'inf'. In softmax, there is a step:
+ // v - max(v), if v is inf, the result of 'v-max(v)' will be nan
+ // use f32 as reference
if (inType == ElementType::f16) {
configuration["INFERENCE_PRECISION_HINT"] = "f32";
}
+
auto expectedOutputs = run_test(functionRefs);
CheckNumberOfNodesWithType(compiledModel, "ScaledDotProductAttention", 0);
for (size_t i = 0; i < actualOutputs.size(); i++) {
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp
index 56fad11f53e..ac59e48f496 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/concat_sdp.hpp
@@ -34,7 +34,7 @@ namespace test {
template<typename IT, typename T>
void strided_iota(IT first, size_t n, T value, T stride);
-typedef std::tuple<ElementType, std::vector<InputShape>, bool> ConcatSDPTestParams;
+typedef std::tuple<ElementType, std::vector<InputShape>, bool, bool> ConcatSDPTestParams;
class ConcatSDPTest :
public testing::WithParamInterface<ConcatSDPTestParams>,
@@ -46,7 +46,8 @@ public:
void prepare();
void reset();
std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model);
- bool hasShapeOf;
+ bool m_forceKVU8;
+ bool m_hasShapeOf;
protected:
void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
void SetUp() override;
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_multiple_query_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_multiple_query_sdp.cpp
index bc73de76999..d05e7840562 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_multiple_query_sdp.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_multiple_query_sdp.cpp
@@ -18,6 +18,7 @@ namespace test {
using InputShapeAndTransposeOrder = std::pair<std::vector<InputShape>, std::vector<size_t>>;
using ConcatMultiQuerySDPParams = std::tuple<ElementType,
InputShapeAndTransposeOrder,
+ bool, // force kvcache int8
bool // has ShapeOf
>;
// Subgraph:
@@ -52,8 +53,10 @@ public:
static std::string getTestCaseName(const testing::TestParamInfo<ConcatMultiQuerySDPParams>& obj) {
ElementType qkvType;
InputShapeAndTransposeOrder inputShapeAndOrders;
- bool hasShapeof;
- std::tie(qkvType, inputShapeAndOrders, hasShapeof) = obj.param;
+ bool forceKVU8;
+ bool hasShapeOf;
+ std::tie(qkvType, inputShapeAndOrders, forceKVU8, hasShapeOf) = obj.param;
+ ElementType kvCacheType = forceKVU8 ? ov::element::Type_t::u8 : qkvType;
std::ostringstream result;
std::vector<InputShape>& inputShapes = inputShapeAndOrders.first;
std::vector<size_t>& transposeOrder = inputShapeAndOrders.second;
@@ -71,8 +74,9 @@ public:
}
result << ")_";
}
- result << "Prc=" << qkvType << "_";
- result << "HasShapeOf=" << hasShapeof << "_";
+ result << "qDataType=" << qkvType << "_";
+ result << "kvDataType=" << kvCacheType << "_";
+ result << "HasShapeOf=" << hasShapeOf << "_";
result << "TransposeOrder=";
result << "(";
for (const auto& itr : transposeOrder) {
@@ -85,18 +89,21 @@ public:
void SetUp() override {
InputShapeAndTransposeOrder inputShapeAndOrders;
+ bool forceKVU8;
bool hasShapeOf;
ElementType qkvType;
- std::tie(qkvType, inputShapeAndOrders, hasShapeOf) = this->GetParam();
+ std::tie(qkvType, inputShapeAndOrders, forceKVU8, hasShapeOf) = this->GetParam();
std::vector<InputShape>& inputShapes = inputShapeAndOrders.first;
std::vector<size_t>& transposeOrder = inputShapeAndOrders.second;
targetDevice = ov::test::utils::DEVICE_CPU;
rel_threshold = 1e-2f;
configuration[ov::hint::inference_precision.name()] = ov::element::f32;
- if (qkvType == ElementType::bf16) {
- configuration[ov::hint::inference_precision.name()] = ov::element::bf16;
+ if (qkvType == ElementType::bf16 || qkvType == ElementType::f16) {
+ configuration[ov::hint::inference_precision.name()] = ov::element::Type(qkvType).get_type_name();
rel_threshold = 0.01f;
}
+ if (forceKVU8)
+ configuration["KV_CACHE_PRECISION"] = "u8";
init_input_shapes(inputShapes);
ov::ParameterVector inputParams;
// q,k,v
@@ -229,6 +236,10 @@ public:
ov::Tensor t{ov::element::f32, shape};
strided_iota(static_cast<float*>(t.data()), t.get_size(), val, 0.1f);
inputs.insert({param, t});
+ } else if (param->get_element_type() == element::f16) {
+ ov::Tensor t{ov::element::f16, shape};
+ strided_iota(static_cast<ov::float16*>(t.data()), t.get_size(), val, 0.1f);
+ inputs.insert({param, t});
} else {
ov::Tensor t{ov::element::bf16, shape};
strided_iota(static_cast<ov::bfloat16*>(t.data()), t.get_size(), val, 0.1f);
@@ -269,6 +280,10 @@ public:
outputs.push_back(copy);
}
auto states = inferRequest.query_state();
+ // k, v may be in any order
+ std::sort(states.begin(), states.end(), [] (VariableState& a, VariableState& b) {
+ return a.get_name() > b.get_name();
+ });
for (std::string name : {"pastk", "pastv"}) {
auto itr = std::find_if(states.begin(), states.end(), [&](const ov::VariableState& state) {
return name == state.get_name();
@@ -290,17 +305,20 @@ public:
TEST_P(ConcatMultiQuerySDPTest, CompareWithRefs) {
SKIP_IF_CURRENT_TEST_IS_DISABLED();
InputShapeAndTransposeOrder inputShapeAndOrders;
+ bool forceKVU8;
bool hasShapeOf;
ElementType qkvType;
- std::tie(qkvType, inputShapeAndOrders, hasShapeOf) = this->GetParam();
- if (qkvType == ElementType::bf16 && !ov::with_cpu_x86_bfloat16())
- GTEST_SKIP();
+ std::tie(qkvType, inputShapeAndOrders, forceKVU8, hasShapeOf) = this->GetParam();
auto actualOutputs = run_test(function);
CheckNumberOfNodesWithType(compiledModel, "ScaledDotProductAttention", 1);
CheckNumberOfNodesWithType(compiledModel, "Concatenation", 0);
CheckNumberOfNodesWithType(compiledModel, "Reorder", 0);
CheckNumberOfNodesWithType(compiledModel, "Transpose", 1);
CheckNumberOfNodesWithType(compiledModel, "Gather", 0);
+ // use f32 as reference
+ if (qkvType == ElementType::f16) {
+ configuration["INFERENCE_PRECISION_HINT"] = "f32";
+ }
auto expectedOutputs = run_test(functionRefs);
CheckNumberOfNodesWithType(compiledModel, "ScaledDotProductAttention", 0);
for (size_t i = 0; i < actualOutputs.size(); i++) {
@@ -384,8 +402,9 @@ const std::vector<InputShapeAndTransposeOrder> inputShapeAndReorders = {{
INSTANTIATE_TEST_SUITE_P(smoke_ConcatMultiQuerySDPTest,
ConcatMultiQuerySDPTest,
- ::testing::Combine(::testing::Values(ElementType::f32, ElementType::bf16),
+ ::testing::Combine(::testing::Values(ElementType::f32, ElementType::bf16, ElementType::f16),
::testing::ValuesIn(inputShapeAndReorders),
+ ::testing::Values(true, false),
::testing::Values(true, false)),
ConcatMultiQuerySDPTest::getTestCaseName);
} // namespace
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp
index f9971a7fe9c..57927434524 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_sdp.cpp
@@ -37,8 +37,10 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
ConcatSDPTest,
::testing::Combine(::testing::Values(ElementType::f32),
::testing::ValuesIn(inputShapes),
+ ::testing::Values(true, false),
::testing::Values(true, false)),
ConcatSDPTest::getTestCaseName);
+
} // namespace
} // namespace test
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
index 839370d3a97..65bc379c78b 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/common/concat_transpose_sdp_transpose.cpp
@@ -253,6 +253,10 @@ public:
outputs.push_back(copy);
}
auto states = inferRequest.query_state();
+ // k, v may be in any order
+ std::sort(states.begin(), states.end(), [] (VariableState& a, VariableState& b) {
+ return a.get_name() > b.get_name();
+ });
for (std::string name : {"pastk", "pastv"}) {
auto itr = std::find_if(states.begin(), states.end(), [&](const ov::VariableState& state) {
return name == state.get_name();
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp
new file mode 100644
index 00000000000..93c99048fec
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/concat_sdp.cpp
@@ -0,0 +1,47 @@
+// Copyright (C) 2023 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/opsets/opset13.hpp"
+#include "openvino/pass/manager.hpp"
+#include "transformations/op_conversions/scaled_dot_product_attention_decomposition.hpp"
+
+#include "custom/subgraph_tests/src/classes/concat_sdp.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "utils/cpu_test_utils.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov {
+namespace test {
+
+namespace {
+const std::vector<std::vector<InputShape>> inputShapes = {
+ // greedy search
+ {
+ // B, H, L1, S
+ {{1, 8, -1, 64}, {{1, 8, 10, 64}, {1, 8, 1, 64}, {1, 8, 1, 64}, {1, 8, 20, 64}, {1, 8, 1, 64}}},
+ // B, H, L0, S
+ {{1, 8, -1, 64}, {{1, 8, 0, 64}, {1, 8, 10, 64}, {1, 8, 11, 64}, {1, 8, 12, 64}, {1, 8, 32, 64}}},
+ },
+ // beam search
+ {
+ // B, H, L1, S
+ {{-1, 8, -1, 64}, {{4, 8, 10, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}, {4, 8, 1, 64}}},
+ // B, H, L0, S
+ {{-1, 8, -1, 64}, {{4, 8, 0, 64}, {4, 8, 10, 64}, {4, 8, 11, 64}, {4, 8, 12, 64}, {4, 8, 13, 64}}},
+ },
+};
+
+INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
+ ConcatSDPTest,
+ ::testing::Combine(::testing::Values(ElementType::bf16, ElementType::f16),
+ ::testing::ValuesIn(inputShapes),
+ ::testing::Values(true, false),
+ ::testing::Values(true, false)),
+ ConcatSDPTest::getTestCaseName);
+
+} // namespace
+
+} // namespace test
+} // namespace ov
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 2a8f49b5dcf..e7c006ab974 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
@@ -358,6 +358,8 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(smoke_VariableState/OVInferRequestVariableStateTest.*)");
// Issue: 141705
retVector.emplace_back(R"(.*smoke_arm_Deconv_2D_Planar_FP16/DeconvolutionLayerCPUTest.*INFERENCE_PRECISION_HINT=f16.*)");
+ // Issue: 154882
+ retVector.emplace_back(R"(.*ConcatMultiQuerySDPTest.*f16.*)");
#endif
#if defined(OPENVINO_ARCH_ARM)
@@ -529,6 +531,8 @@ std::vector<std::string> disabledTestPatterns() {
if (!ov::with_cpu_x86_avx512_core_fp16()) {
// Skip fp16 tests for paltforms that don't support fp16 precision
retVector.emplace_back(R"(.*INFERENCE_PRECISION_HINT=(F|f)16.*)");
+ retVector.emplace_back(R"(.*ConcatMultiQuerySDPTest.*f16.*)");
+ retVector.emplace_back(R"(.*ConcatSDPTest.*f16.*)");
}
#elif defined(OPENVINO_ARCH_ARM64) || defined(OPENVINO_ARCH_ARM)
if (!ov::intel_cpu::hasHardwareSupport(ov::element::f16)) {
@@ -560,6 +564,7 @@ std::vector<std::string> disabledTestPatterns() {
retVector.emplace_back(R"(.*smoke_Snippets_EnforcePrecision_bf16.*)");
retVector.emplace_back(R"(.*smoke_Snippets_MHAWOTransposeEnforceBF16.*)");
retVector.emplace_back(R"(.*smoke_Snippets_MHAEnforceBF16.*)");
+ retVector.emplace_back(R"(.*ConcatSDPTest.*bf16.*)");
}
// [150842] Need to support dynamic K dimension of BF16|INT8 MatMul on AMX systems
if (ov::with_cpu_x86_avx512_core_amx()) {
diff --git a/src/plugins/intel_cpu/tests/unit/brgemm_executor_test.cpp b/src/plugins/intel_cpu/tests/unit/brgemm_executor_test.cpp
index 35a29f97452..9ae58561d4d 100644
--- a/src/plugins/intel_cpu/tests/unit/brgemm_executor_test.cpp
+++ b/src/plugins/intel_cpu/tests/unit/brgemm_executor_test.cpp
@@ -28,18 +28,19 @@ void run_test(ov::element::Type rtPrec) {
size_t K = 33;
ov::intel_cpu::BrgemmKernel gemm(M, N, K, K, N, N, false, rtPrec);
size_t nthr = 8;
- bool is_bf16 = (rtPrec == ov::element::bf16);
+ bool is_f32 = (rtPrec == ov::element::f32);
std::vector<T> a_data(M * K, (1.0f/33));
std::vector<T> b_data(K * N, 4.0f);
std::vector<float> c_data(nthr * M * N, 0.0f);
std::vector<size_t> wsp(nthr * 4 * 1024, 0.0f);
- std::vector<T> b_scracth(gemm.get_scratch_b_size(), 0.0f);
- std::vector<T> a_scracth(gemm.get_scratch_a_size(), 0.0f);
- if (is_bf16)
- gemm.copy_buffer_b(b_data.data(), b_scracth.data());
+ std::vector<uint8_t> a_scratch(gemm.get_scratch_a_size(), 0.0f);
+ std::vector<uint8_t> b_scratch(gemm.get_scratch_b_size(), 0.0f);
+ if (!is_f32) {
+ gemm.copy_buffer_b(b_data.data(), b_scratch.data());
+ }
auto m_block_size = gemm.get_mblk_size();
auto m_blocks = (M + gemm.get_mblk_size() - 1) / m_block_size;
- T* b_ptr = is_bf16 ? b_scracth.data() : b_data.data();
+ void* b_ptr = !is_f32 ? static_cast<void*>(b_scratch.data()) : static_cast<void*>(b_data.data());
ov::parallel_for2d(nthr, m_blocks, [&](size_t i, size_t m_blk) {
auto m_start = m_blk * m_block_size;
auto m_end = std::min(m_start + m_block_size, M);
@@ -49,7 +50,7 @@ void run_test(ov::element::Type rtPrec) {
b_ptr,
c_data.data() + i * M * N + m_start * N,
wsp.data() + i * 4 * 1024,
- a_scracth.data());
+ a_scratch.data());
});
ov::parallel_for(nthr, [&](size_t i){
for (size_t m = 0; m < M; m++) {
@@ -73,9 +74,13 @@ TEST_P(BrgemmKernelTest, simpleGemmTest) {
GTEST_SKIP();
if (rtPrec == ov::element::f32 && !ov::with_cpu_x86_avx512_core())
GTEST_SKIP();
+ if (rtPrec == ov::element::f16 && !ov::with_cpu_x86_avx512_core_fp16())
+ GTEST_SKIP();
if (rtPrec == ov::element::bf16) {
run_test<ov::bfloat16>(rtPrec);
+ } else if (rtPrec == ov::element::f16) {
+ run_test<ov::float16>(rtPrec);
} else {
run_test<float>(rtPrec);
}
@@ -83,6 +88,6 @@ TEST_P(BrgemmKernelTest, simpleGemmTest) {
INSTANTIATE_TEST_SUITE_P(BrgemmKernelUnitTest,
BrgemmKernelTest,
- ::testing::Values(ov::element::f32, ov::element::bf16),
+ ::testing::Values(ov::element::f32, ov::element::bf16, ov::element::f16),
BrgemmKernelTest::getTestCaseName);
} // namespace brgemmUnitTest