Skip to content

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

[CPU] Support different Key/Value head size and not multiples 16 of head size length for SDPA/PA (#26945)

Luo Cheng2f62be0a7ba1
diff --git a/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp b/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp
index c259e9387d9..28e7cd90019 100644
--- a/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp
+++ b/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp
@@ -383,12 +383,18 @@ ov::pass::StateManagementPattern::StateManagementPattern(ParameterVector& kv_par
auto paged_attention = std::make_shared<ov::op::PagedAttentionExtension>(pa_arguments);
+ // The output shape of PagedAttention will be converted to [batch, 1, head_num, head_size_v], the head_size_v
+ // may be different from head_size_q/head_size_k. The head_size_v could be got from the shape of value input
+ auto hidden_dim_v = std::make_shared<v8::Gather>(std::make_shared<v3::ShapeOf>(v_target_layout),
+ v0::Constant::create(element::i64, Shape{}, {-1}),
+ v0::Constant::create(element::i64, Shape{}, {0}));
+
auto pa_shape = std::make_shared<v0::Concat>(
OutputVector{
v0::Constant::create(element::i64, Shape{1}, {0}),
v0::Constant::create(element::i64, Shape{1}, {1}),
v0::Constant::create(element::i64, Shape{1}, {-1}),
- std::make_shared<v0::Unsqueeze>(hidden_dim, v0::Constant::create(element::i64, Shape{}, {0})),
+ std::make_shared<v0::Unsqueeze>(hidden_dim_v, v0::Constant::create(element::i64, Shape{}, {0})),
},
0);
auto pa_reshape = std::make_shared<v1::Reshape>(paged_attention->output(0), pa_shape, true);
diff --git a/src/core/src/op/paged_attention.cpp b/src/core/src/op/paged_attention.cpp
index 261b0ce1c47..cdcb66e86ee 100644
--- a/src/core/src/op/paged_attention.cpp
+++ b/src/core/src/op/paged_attention.cpp
@@ -4,6 +4,7 @@
#include "openvino/op/paged_attention.hpp"
+#include "dimension_util.hpp"
#include "itt.hpp"
#include "openvino/op/op.hpp"
@@ -146,10 +147,33 @@ void PagedAttentionExtension::validate_and_infer_types() {
get_input_element_type(12),
".");
+ // value head_size may be not same with key
+ auto out_ps = get_input_partial_shape(0);
+ const auto& key_ps = get_input_partial_shape(1);
+ const auto& value_ps = get_input_partial_shape(2);
+ if (out_ps.rank().is_static()) {
+ if (key_ps.rank().is_static() && value_ps.rank().is_static() && key_ps[1].is_static()) {
+ // The dim of out_ps[1] should be `num_heads * v_head_size`, it can be got from:
+ // because:
+ // q: query_ps[1] = num_heads * head_size
+ // k: key_ps[1] = num_kv_heads * head_size
+ // v: value_ps[1] = num_kv_heads * v_head_size
+ // therefore:
+ // q * v / k = (num_heads * head_size) * (num_kv_heads * v_head_size) /
+ // (num_kv_heads * head_size) = num_heads * v_head_size
+ out_ps[1] = out_ps[1] * value_ps[1] / key_ps[1].get_length();
+ NODE_VALIDATION_CHECK(this,
+ !ov::util::dim::is_empty(out_ps[1]),
+ "The last dimension of output should not be empty.");
+ } else {
+ out_ps[1] = Dimension::dynamic();
+ }
+ }
+
if (m_output_type[0] == ov::element::undefined) {
- set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));
+ set_output_type(0, get_input_element_type(0), out_ps);
} else {
- set_output_type(0, m_output_type[0], get_input_partial_shape(0));
+ set_output_type(0, m_output_type[0], out_ps);
}
if (m_output_type[1] == ov::element::undefined) {
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_memcpy.cpp b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_memcpy.cpp
index 21d8fbbe6e2..755330bd850 100644
--- a/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_memcpy.cpp
+++ b/src/plugins/intel_cpu/src/nodes/kernels/scaled_attn/attn_memcpy.cpp
@@ -51,16 +51,14 @@ void attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
const ov::intel_cpu::PlainTensor& past_k_output,
const ov::intel_cpu::PlainTensor& past_v_output) {
// For compatibility, all input_kvs are permuted to BHLS
- size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3];
- // Internal LBHS layout has strides[L] > strides[B]
- assert(past_k_output.m_strides[2] >= past_k_output.m_strides[0]);
+ size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3], SV = v_input.m_dims[3];
parallel_for3d(L1, B, H, [&](size_t m, size_t b, size_t h) {
attn_copy(past_k_output.ptr<T2>(b, h, m, 0),
k_input.ptr<T>(b, h, m, 0),
S);
attn_copy(past_v_output.ptr<T2>(b, h, m, 0),
v_input.ptr<T>(b, h, m, 0),
- S);
+ SV);
});
}
@@ -69,16 +67,14 @@ static void attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
const ov::intel_cpu::PlainTensor& past_k_output,
const ov::intel_cpu::PlainTensor& past_v_output) {
// For compatibility, all input_kvs are permuted to BHLS
- size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3];
- // Internal LBHS layout has strides[L] > strides[B]
- assert(past_k_output.m_strides[2] >= past_k_output.m_strides[0]);
+ size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3], SV = v_input.m_dims[3];
parallel_for3d(L1, B, H, [&](size_t m, size_t b, size_t h) {
std::memcpy(past_k_output.ptr_v(b, h, m, 0),
k_input.ptr_v(b, h, m, 0),
S * k_input.m_element_size);
std::memcpy(past_v_output.ptr_v(b, h, m, 0),
v_input.ptr_v(b, h, m, 0),
- S * v_input.m_element_size);
+ SV * v_input.m_element_size);
});
}
@@ -88,7 +84,7 @@ static void paged_attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
const ov::intel_cpu::PlainTensor& past_k_output,
const ov::intel_cpu::PlainTensor& past_v_output,
const ov::intel_cpu::PlainTensor& slot_mapping) {
- size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3];
+ size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3], SV = v_input.m_dims[3];
size_t block_size = past_k_output.m_dims[2];
parallel_for3d(B, L1, H, [&](size_t b, size_t m, size_t h) {
auto slot = slot_mapping.ptr<int32_t>(b)[m];
@@ -100,7 +96,7 @@ static void paged_attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
S);
attn_copy(past_v_output.ptr<T2>(block_number, h, block_offset, 0),
v_input.ptr<T>(b, h, m, 0),
- S);
+ SV);
});
}
@@ -109,7 +105,7 @@ static void paged_attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
const ov::intel_cpu::PlainTensor& past_k_output,
const ov::intel_cpu::PlainTensor& past_v_output,
const ov::intel_cpu::PlainTensor& slot_mapping) {
- size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3];
+ size_t B = k_input.m_dims[0], H = k_input.m_dims[1], L1 = k_input.m_dims[2], S = k_input.m_dims[3], SV = v_input.m_dims[3];
size_t block_size = past_k_output.m_dims[2];
parallel_for3d(B, L1, H, [&](size_t b, size_t m, size_t h) {
auto slot = slot_mapping.ptr<int32_t>(b)[m];
@@ -121,7 +117,7 @@ static void paged_attn_memcpy_kernel(const ov::intel_cpu::PlainTensor& k_input,
S * k_input.m_element_size);
std::memcpy(past_v_output.ptr_v(block_number, h, block_offset, 0),
v_input.ptr_v(b, h, m, 0),
- S * v_input.m_element_size);
+ SV * v_input.m_element_size);
});
}
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 d95f973fa9f..66772bda03d 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
@@ -178,8 +178,7 @@ static void attn_quant_mt(const ov::intel_cpu::PlainTensor& k_src,
const ov::intel_cpu::PlainTensor& k_scale_zp,
const ov::intel_cpu::PlainTensor& v_scale_zp) {
// 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]
+ 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], SV = v_src.m_dims[3];
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);
@@ -190,7 +189,7 @@ static void attn_quant_mt(const ov::intel_cpu::PlainTensor& k_src,
p_k[1]);
quant_u8(v_src.ptr<T>(b, h, m),
v_dst.ptr<T2>(b, h, m),
- S,
+ SV,
p_v[0],
p_v[1]);
});
@@ -202,7 +201,7 @@ static void paged_attn_quant_mt(const ov::intel_cpu::PlainTensor& k_src,
const ov::intel_cpu::PlainTensor& k_dst,
const ov::intel_cpu::PlainTensor& v_dst,
const ov::intel_cpu::PlainTensor& slot_mapping) {
- 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];
+ 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], SV = v_src.m_dims[3];
size_t block_size = k_dst.m_dims[2];
parallel_for3d(B, L1, H, [&](size_t b, size_t m, size_t h) {
auto slot = slot_mapping.ptr<int32_t>(b)[m];
@@ -221,7 +220,7 @@ static void paged_attn_quant_mt(const ov::intel_cpu::PlainTensor& k_src,
p_k[1]);
quant_u8(v_src.ptr<T>(b, h, m),
v_dst.ptr<T2>(block_number, h, block_offset) + sizeof(float) + sizeof(float),
- S,
+ SV,
p_v[0],
p_v[1]);
});
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 971aa6bb58c..bef34881ca4 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
@@ -708,14 +708,38 @@ static void pack_32x16_kernel(T* dst, T* src, size_t dst_stride, size_t src_stri
}
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) {
+static void pack_32xK_kernel(T* dst, T* src, size_t dst_stride, size_t src_stride, size_t K) {
+ static const uint64_t idx[8] = {0, 4, 1, 5, 2, 6, 3, 7};
+ auto midx = _mm512_loadu_si512(idx);
+ __mmask16 mask = (1 << K) - 1;
+ for (size_t i = 0; i < K; i++) {
+ auto x = _mm256_maskz_loadu_epi16(mask, src); // [a1 a2 a3 a4] total 256-bits in 4 64bits unit
+ auto y = _mm256_maskz_loadu_epi16(mask, src + src_stride); // [b1 b2 b3 b4] total 256-bits
+ auto a = _mm512_castsi256_si512(x);
+ auto b = _mm512_castsi256_si512(y);
+ a = _mm512_permutexvar_epi64(midx, a); // [a1 x | a2 x | a3 x | a4 x]
+ b = _mm512_permutexvar_epi64(midx, b); // [b1 x | b2 x | b3 x | b4 x]
+ auto B0 = _mm512_unpacklo_epi16(a, b);
+ _mm512_mask_storeu_epi32(dst, mask, B0);
+ src += 2 * src_stride;
+ dst += 2 * dst_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_32NxK(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) {
pack_32x32_kernel(dst + k * 2, src + k, dst_stride, src_stride);
}
- if (k < K)
+ if (k + 16 <= K) {
pack_32x16_kernel(dst + k * 2, src + k, dst_stride, src_stride);
+ k += 16;
+ }
+ if (k < K) {
+ pack_32xK_kernel(dst + k * 2, src + k, dst_stride, src_stride, K - k);
+ }
dst += 32 * dst_stride;
src += 32 * src_stride;
@@ -723,7 +747,7 @@ static void pack_32Nx16K(T* dst, T* src, T* tmp, size_t N, size_t K, size_t dst_
}
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) {
+static void pack_32NxK(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)
@@ -735,14 +759,14 @@ static void pack_32Nx16K(T* dst, uint8_t* src, T* tmp, size_t N, size_t K, size_
s += src_stride + 2 * sizeof(float);
t += src_stride;
}
- pack_32Nx16K(dst, tmp, reinterpret_cast<T*>(0), N, K, dst_stride, src_stride);
+ pack_32NxK(dst, tmp, reinterpret_cast<T*>(0), N, K, dst_stride, src_stride);
}
#endif
template<typename T>
-static void pack_32Nx16K(float* dst, T* src, float* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
+static void pack_32NxK(float* dst, T* src, float* tmp, size_t N, size_t K, size_t dst_stride, size_t src_stride) {
// never called
- OPENVINO_THROW("pack_32Nx16K: should not be called.");
+ OPENVINO_THROW("pack_32NxK: should not be called.");
}
template <typename DATA_TYPE, typename KVCACHE_TYPE>
@@ -750,6 +774,7 @@ struct MHAHelper {
// initialize once
size_t _H;
size_t _S;
+ size_t _SV;
size_t _Hk;
size_t _h_each_group_len;
size_t _block_size;
@@ -785,7 +810,7 @@ struct MHAHelper {
_weight.resize<float>({size_t{1}, size_t{1}, size_t{1}, size_t{1}});
}
- void init(size_t H, size_t S, size_t Hk, size_t h_each_group_len, size_t block_size, size_t sliding_window,
+ void init(size_t H, size_t S, size_t SV, size_t Hk, size_t h_each_group_len, size_t block_size, size_t sliding_window,
float d_scale, size_t kv_len, bool init_alibi_lookup) {
// query shape: [B, H, L, S]
// present_key shape: [block, H, 32, S]
@@ -799,6 +824,7 @@ struct MHAHelper {
auto in_type = precision_of<DATA_TYPE>::value;
_H = H;
_S = S;
+ _SV = SV;
_Hk = Hk;
_h_each_group_len = h_each_group_len;
_block_size = block_size;
@@ -811,7 +837,7 @@ struct MHAHelper {
auto new_score_stride = std::max(prev_score_stride, want_score_stride);
// resize temporary buffers, weight.size(3) will be aligned to block_size
_weight.resize<float>({static_cast<size_t>(_nthr), H, _block_size, new_score_stride});
- _output.resize<float>({static_cast<size_t>(_nthr), _block_size, H, S});
+ _output.resize<float>({static_cast<size_t>(_nthr), _block_size, H, SV});
// TODO: kernel supports stride
if (_qk_gemm.empty() || prev_score_stride < new_score_stride) {
@@ -828,20 +854,20 @@ struct MHAHelper {
false,
in_type);
_wv_gemm[i] = std::make_shared<BrgemmKernel>(i + 1,
- _S,
+ _SV,
_block_size,
// if it's bf16, the stride needs double due to reuse float buffer
(in_type == ov::element::Type_t::f32 ? 1 : 2) * _weight.stride(2),
- _S,
+ _SV,
_output.stride(1),
false,
in_type);
_wv_gemm_acc[i] = std::make_shared<BrgemmKernel>(i + 1,
- _S,
+ _SV,
_block_size,
// if it's bf16, the stride needs double due to reuse float buffer
(in_type == ov::element::Type_t::f32 ? 1 : 2) * _weight.stride(2),
- _S,
+ _SV,
_output.stride(1),
false,
in_type,
@@ -881,7 +907,7 @@ 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(_S, _block_size)});
+ _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) {
@@ -992,7 +1018,7 @@ 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_xf16 ? _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 * _SV);
// for each weight block, loop through all value block
for (size_t v_blk = 0; v_blk < cur_kv_len_blocks; v_blk++) {
@@ -1020,12 +1046,12 @@ struct MHAHelper {
}
if (q_is_xf16) {
attn_memcpy2d_kernel(_output.ptr<float>(ithr, 0, h, 0),
- output_emb.ptr<DATA_TYPE>(q_start, h * _S),
+ output_emb.ptr<DATA_TYPE>(q_start, h * _SV),
ov::element::f32,
precision_of<DATA_TYPE>::value,
_output.stride(1),
output_emb.stride(0),
- _S,
+ _SV,
q_cnt);
}
}
@@ -1091,7 +1117,7 @@ struct MHAHelper {
}
}
- memset(_output.ptr<float>(ithr), 0, q_len * _H * _S * sizeof(float));
+ memset(_output.ptr<float>(ithr), 0, q_len * _H * _SV * sizeof(float));
for (size_t pv = 0, i = 0; pv < cur_kv_len; pv += _block_size, i++) {
auto block_number = block_table[i];
auto* v = present_value.ptr<KVCACHE_TYPE>(block_number, hk);
@@ -1100,7 +1126,7 @@ struct MHAHelper {
attn_acc_value_block(_output.ptr<float>(ithr, pq, h),
_weight.ptr<float>(ithr, h, pq) + pv,
v,
- _S,
+ _SV,
std::min(_block_size, cur_kv_len - pv));
}
}
@@ -1108,7 +1134,7 @@ struct MHAHelper {
// convert to dst
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++)
- cvt_copy(output_emb.ptr<DATA_TYPE>(pq, h * _S), _output.ptr<float>(ithr, pq, h), _S);
+ cvt_copy(output_emb.ptr<DATA_TYPE>(pq, h * _SV), _output.ptr<float>(ithr, pq, h), _SV);
}
// compute one token, loop along batch, head dimensions and kv_len, it's special for very long kv_len with small batch tokens.
@@ -1197,7 +1223,7 @@ struct MHAHelper {
}
// attn_w * V
- _output_bhl.resize<float>({static_cast<size_t>(_nthr), B, q_len, _H, _S});
+ _output_bhl.resize<float>({static_cast<size_t>(_nthr), B, q_len, _H, _SV});
// m_attn_w {B, H, q_len, kv_len}
parallel_nt_static(_nthr, [&](const size_t ithr, const size_t nthr) {
memset(_output_bhl.ptr<float>(ithr, 0, 0, 0, 0), 0, _output_bhl.stride(0) * sizeof(float));
@@ -1216,7 +1242,7 @@ struct MHAHelper {
attn_acc_value_block(_output_bhl.ptr<float>(ithr, b, pq, h),
_weight_bhl.ptr<float>(b, h, pq) + pv,
v,
- _S,
+ _SV,
std::min(_block_size, context_len - pv));
}
}
@@ -1226,8 +1252,8 @@ struct MHAHelper {
parallel_for3d(B, _H, q_len, [&](size_t b, size_t h, size_t pq) {
auto* temp = _output_bhl.ptr<float>(0, b, pq, h);
size_t temp_stride = _output_bhl.stride(0);
- auto* dst = output_emb.ptr<DATA_TYPE>(b, pq, h * _S);
- attn_reduce(dst, temp, _nthr, _S, temp_stride);
+ auto* dst = output_emb.ptr<DATA_TYPE>(b, pq, h * _SV);
+ attn_reduce(dst, temp, _nthr, _SV, temp_stride);
});
}
};
@@ -1375,17 +1401,17 @@ struct MHA {
_helper._block_size,
_helper._S, _helper._block_size, _helper._S);
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),
- _helper._block_size,
- _helper._S,
- rnd_up(_helper._S, _helper._block_size),
- _helper._S);
+ pack_32NxK(_helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, kv_block, hk),
+ v_ptr,
+ _helper._output.template ptr<DATA_TYPE>(ithr),
+ _helper._block_size,
+ _helper._SV,
+ rnd_up(_helper._SV, _helper._block_size),
+ _helper._SV);
} else {
// need to decompress
if (!q_cache_is_same) {
- dequant(_helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, kv_block, hk), v_ptr, _helper._block_size, _helper._S);
+ dequant(_helper._wv_scratch_b.template ptr<DATA_TYPE>(batch_in_reorder, kv_block, hk), v_ptr, _helper._block_size, _helper._SV);
}
}
});
@@ -1429,7 +1455,7 @@ struct MHA {
sub_query = sub_query.permute({1, 0, 2});
_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._S}),
+ 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],
@@ -1518,7 +1544,8 @@ struct AttentionExecutor : public PagedAttentionExecutor {
// The layout for per token per head for u8 kv cache:
// |scale(f32)|zeropoint(f32)|quantized feature(u8,idx_1)|quantized feature(u8,idx_2)|...|quantized feature(u8,idx_S)|
// The actual size needs to deduct scale and zeropoint.
- auto S = v_cache.size(3) - (k_cache.m_dt == ov::element::Type_t::u8 ? sizeof(float) * 2 : 0);
+ auto S = k_cache.size(3) - (k_cache.m_dt == ov::element::Type_t::u8 ? sizeof(float) * 2 : 0);
+ auto SV = v_cache.size(3) - (k_cache.m_dt == ov::element::Type_t::u8 ? sizeof(float) * 2 : 0);
auto block_size = k_cache.size(2);
auto H = q.size(1) / S;
auto h_each_group_len = 1;
@@ -1529,16 +1556,16 @@ struct AttentionExecutor : public PagedAttentionExecutor {
q.assert_dims({B_token, H * S});
k.assert_dims({B_token, Hk * S});
- v.assert_dims({B_token, Hk * S});
+ v.assert_dims({B_token, Hk * SV});
q = q.reshape({B_token, H, 1, S});
k = k.reshape({B_token, Hk, 1, S});
- v = v.reshape({B_token, Hk, 1, S});
+ v = v.reshape({B_token, Hk, 1, SV});
if (k_cache.m_dt == ov::element::Type_t::u8) {
k_cache.assert_dims({0, Hk, block_size, S + sizeof(float) * 2}, true);
- v_cache.assert_dims({k_cache.m_dims[0], Hk, block_size, S + sizeof(float) * 2});
+ v_cache.assert_dims({k_cache.m_dims[0], Hk, block_size, SV + sizeof(float) * 2});
} else {
k_cache.assert_dims({0, Hk, block_size, S}, true);
- v_cache.assert_dims({k_cache.m_dims[0], Hk, block_size, S});
+ v_cache.assert_dims({k_cache.m_dims[0], Hk, block_size, SV});
}
past_lens.assert_dims({B_seq});
subsequence_begins.assert_dims({B_seq + 1});
@@ -1549,14 +1576,13 @@ struct AttentionExecutor : public PagedAttentionExecutor {
if (alibi_slopes) {
alibi_slopes.assert_dims({H});
}
- output_emb.assert_dims({B_token, H * S});
- output_emb = output_emb.reshape({B_token, 1, H * S});
+ output_emb.assert_dims({B_token, H * SV});
+ output_emb = output_emb.reshape({B_token, 1, H * SV});
// TODO: enable block_size to be multiple of 32
OPENVINO_ASSERT(block_size == 32, "CPU: block size must be 32, current: ", block_size);
- OPENVINO_ASSERT(S % 16 == 0, "CPU: head size must be multiple of 16, current: ", S);
- _helper.init(H, S, Hk, h_each_group_len, block_size, sliding_window, scale, max_context_len, alibi_slopes);
+ _helper.init(H, S, SV, Hk, h_each_group_len, block_size, sliding_window, scale, max_context_len, alibi_slopes);
}
void concat_pastkv(const PlainTensor& k, const PlainTensor& v, const PlainTensor& k_cache, const PlainTensor& v_cache,
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 0670c744a6d..1543c168403 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
@@ -861,6 +861,7 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
auto H = query.size(1);
auto q_len = query.size(2);
auto S = query.size(3);
+ auto SV = present_value.size(3);
auto h_group_num = present_value.size(1);
auto precision = ov::element::f32;
if (std::is_same<T3, ov::float16>::value) {
@@ -991,10 +992,10 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
// attn_w * V
// Fast Path if there are enough works for each thread
if (B >= static_cast<size_t>(nthr)) {
- buf_attn_score.resize<T3>({static_cast<size_t>(nthr), q_len, h_each_group_len, S});
+ buf_attn_score.resize<T3>({static_cast<size_t>(nthr), q_len, h_each_group_len, SV});
parallel_for2d(B, h_group_num, [&](size_t b, size_t h_group) {
auto ithr = parallel_get_thread_num();
- memset(buf_attn_score.ptr<T3>(ithr), 0, q_len * h_each_group_len * S * sizeof(T3));
+ memset(buf_attn_score.ptr<T3>(ithr), 0, q_len * h_each_group_len * SV * sizeof(T3));
for (size_t pv = 0; pv < kv_len; pv++) {
auto b_kv = beams ? beams.ptr<int32_t>(b)[pv] : b;
auto* v = present_value.ptr<T2>(b_kv, h_group, pv);
@@ -1004,7 +1005,7 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
attn_acc_value(buf_attn_score.ptr<T3>(ithr, pq, group_idx),
buf_attn_w.ptr<T3>(b, h, pq)[pv],
v,
- S,
+ SV,
p + 0,
p + 1);
}
@@ -1014,15 +1015,15 @@ 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, group_idx = 0; h < (h_group + 1) * h_each_group_len;
h++, group_idx++) {
- auto* dst = has_out_transpose ? output_emb.ptr<T>(b, pq, h * S) : output_emb.ptr<T>(b, h, pq);
- cvt_copy(dst, buf_attn_score.ptr<T3>(ithr, pq, group_idx), S);
+ auto* dst = has_out_transpose ? output_emb.ptr<T>(b, pq, h * SV) : output_emb.ptr<T>(b, h, pq);
+ cvt_copy(dst, buf_attn_score.ptr<T3>(ithr, pq, group_idx), SV);
}
}
});
return;
}
- buf_attn_score.resize<T3>({static_cast<size_t>(nthr), B, q_len, H, S});
+ buf_attn_score.resize<T3>({static_cast<size_t>(nthr), B, q_len, H, SV});
// buf_attn_w {B, H, q_len, kv_len}
parallel_nt_static(nthr, [&](const size_t ithr, const size_t nthr) {
size_t start{0}, end{0};
@@ -1041,7 +1042,7 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
attn_acc_value(buf_attn_score.ptr<T3>(ithr, b, 0, h_group),
buf_attn_w.ptr<T3>(b, h_group, 0, pv)[0],
v,
- S,
+ SV,
p + 0,
p + 1);
parallel_it_step(pv, kv_len, b, B, h_group, h_group_num);
@@ -1056,7 +1057,7 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
attn_acc_value(buf_attn_score.ptr<T3>(ithr, b, pq, h),
buf_attn_w.ptr<T3>(b, h, pq)[pv],
v,
- S,
+ SV,
p + 0,
p + 1);
}
@@ -1070,8 +1071,8 @@ static void mha_single_token_kernel(const ov::intel_cpu::PlainTensor& query,
parallel_for3d(B, H, q_len, [&](size_t b, size_t h, size_t pq) {
auto* temp = buf_attn_score.ptr<T3>(0, b, pq, h);
size_t temp_stride = buf_attn_score.stride(0);
- auto* dst = has_out_transpose ? output_emb.ptr<T>(b, pq, h * S) : output_emb.ptr<T>(b, h, pq);
- attn_reduce(dst, temp, nthr, S, temp_stride);
+ auto* dst = has_out_transpose ? output_emb.ptr<T>(b, pq, h * SV) : output_emb.ptr<T>(b, h, pq);
+ attn_reduce(dst, temp, nthr, SV, temp_stride);
});
}
diff --git a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
index 6bf7d3099a8..b9666388490 100644
--- a/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/paged_attn.cpp
@@ -152,18 +152,32 @@ void PagedAttention::execute(dnnl::stream strm) {
inputs[i] = getSrcMemoryAtPort(i);
}
- const auto& queryDims = inputs[0]->getStaticDims();
+ auto outDims = inputs[0]->getStaticDims();
+ const auto& keyDims = inputs[1]->getStaticDims();
+ const auto& valueDims = inputs[2]->getStaticDims();
+ // value head_size may be not same with key
+ if (keyDims[1] != valueDims[1]) {
+ // The outDims[1] should be `num_heads * v_head_size`, it can be got from:
+ // because:
+ // q: query_ps[1] = num_heads * head_size
+ // k: key_ps[1] = num_kv_heads * head_size
+ // v: value_ps[1] = num_kv_heads * v_head_size
+ // therefore:
+ // q * v / k = (num_heads * head_size) * (num_kv_heads * v_head_size) /
+ // (num_kv_heads * head_size) = num_heads * v_head_size
+ outDims[1] = outDims[1] * valueDims[1] / keyDims[1];
+ }
if (m_hasScore) {
size_t len = 0;
const auto& pastLensDims = inputs[5]->getStaticDims();
auto pastLens = inputs[5]->getDataAs<const int32_t>();
for (size_t i = 0; i < pastLensDims[0]; i++)
len += pastLens[i];
- len += queryDims[0];
+ len += outDims[0];
VectorDims scoreDims{len};
- redefineOutputMemory({queryDims, scoreDims});
+ redefineOutputMemory({outDims, scoreDims});
} else {
- redefineOutputMemory(0, queryDims);
+ redefineOutputMemory(0, outDims);
}
outputs[0] = getDstMemoryAtPort(0);
diff --git a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
index eecba2acff2..e70a3932b11 100644
--- a/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/scaled_attn.cpp
@@ -135,6 +135,7 @@ struct MHAKernel {
auto H = query.size(1);
auto q_len = query.size(2);
auto head_size = query.size(3);
+ auto head_size_v = present_value.size(3);
auto kv_len = present_key.size(2);
auto Hk = present_key.size(1);
size_t h_each_group_len = H / Hk;
@@ -145,7 +146,7 @@ struct MHAKernel {
parallel_for2d(B, H, [&](size_t b, size_t h) {
std::vector<float> attn_score(kv_len);
- std::vector<float> word_vec(head_size, 0.0f);
+ std::vector<float> word_vec(head_size_v, 0.0f);
for (size_t m = 0; m < q_len; m++) {
// dot-product to get attention scores
@@ -185,14 +186,14 @@ struct MHAKernel {
softmax(&attn_score[0], ncausal);
// linearly combine value
- word_vec.assign(head_size, 0.0f);
+ word_vec.assign(head_size_v, 0.0f);
for (size_t n = 0; n < ncausal; n++) {
auto* v = &present_value.at<T>({b, h / h_each_group_len, n, 0}, true);
- accumulate(word_vec.data(), v, head_size, attn_score[n]);
+ accumulate(word_vec.data(), v, head_size_v, attn_score[n]);
}
// output [B, L1, H*head_size]
- auto* out = has_out_transpose ? &output_emb.at<T>({b, m, h * head_size}) : &output_emb.at<T>({b, h, m});
+ auto* out = has_out_transpose ? &output_emb.at<T>({b, m, h * head_size_v}) : &output_emb.at<T>({b, h, m});
std::copy(word_vec.begin(), word_vec.end(), out);
}
});
@@ -259,13 +260,14 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
return dnnl_dims;
}
- void prepare_brgemm_prim(dnnl::stream strm, PlainTensor& query, PlainTensor& present_key, bool has_out_transpose) {
+ void prepare_brgemm_prim(dnnl::stream strm, PlainTensor& query, PlainTensor& present_key, PlainTensor& present_value, bool has_out_transpose) {
auto in_type = precision_of<T>::value;
auto qkv_dt = DnnlExtensionUtils::ElementTypeToDataType(in_type);
auto B = query.size(0);
auto H = query.size(1);
auto q_len = query.size(2);
auto head_size = query.size(3);
+ auto head_size_v = present_value.size(3);
auto kv_len = present_key.size(2);
auto Hk = present_key.size(1);
brgemmKey qk_key = {q_len, kv_len, head_size, query.stride(2), present_key.stride(2), kv_len, true, in_type};
@@ -289,19 +291,19 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
qk_gemm_ptr = qk_result.first;
if (has_out_transpose)
- out_md = dnnl::memory::desc(make_dnnl_dims({B, q_len, H, head_size}), qkv_dt, tag::abcd);
+ out_md = dnnl::memory::desc(make_dnnl_dims({B, q_len, H, head_size_v}), qkv_dt, tag::abcd);
else
- out_md = dnnl::memory::desc(make_dnnl_dims({B, H, q_len, head_size}), qkv_dt, tag::abcd);
+ out_md = dnnl::memory::desc(make_dnnl_dims({B, H, q_len, head_size_v}), qkv_dt, tag::abcd);
size_t ldc_index = 2;
if (has_out_transpose) {
ldc_index = 1;
}
brgemmKey wv_key = {q_len,
- head_size,
+ head_size_v,
kv_len,
kv_len * (in_type == ov::element::Type_t::f32 ? 1 : 2),
- present_key.stride(2),
+ present_value.stride(2),
static_cast<size_t>(out_md.get_strides()[ldc_index]),
false,
in_type};
@@ -329,9 +331,9 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
const size_t m_block_size = qk_gemm_ptr->get_mblk_size();
weight_score.resize<float>({static_cast<size_t>(parallel_get_max_threads()), H, m_block_size, kv_len});
if (has_out_transpose) {
- fp32_out.resize<float>({B, q_len, H, head_size});
+ fp32_out.resize<float>({B, q_len, H, head_size_v});
} else {
- fp32_out.resize<float>({B, H, q_len, head_size});
+ fp32_out.resize<float>({B, H, q_len, head_size_v});
}
return;
}
@@ -348,7 +350,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
const auto B = query.size(0);
const auto H = query.size(1);
const auto q_len = query.size(2);
- const auto head_size = query.size(3);
+ const auto head_size_v = present_value.size(3);
const auto Hk = present_key.size(1);
const auto kv_len = present_key.size(2);
size_t h_each_group_len = H / Hk;
@@ -423,7 +425,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
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});
+ fp32_out_ptr = has_out_transpose ? &output_emb.at<float>({b, m_start, h * head_size_v}) : &output_emb.at<float>({b, h, m_start, 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});
@@ -436,12 +438,12 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
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}),
+ &output_emb.at<T>({b, m_start, h * head_size_v}),
ov::element::f32,
precision_of<T>::value,
fp32_out.stride(1),
output_emb.stride(1),
- head_size,
+ head_size_v,
m_cnt);
} else {
attn_memcpy2d_kernel(&fp32_out.at<float>({b, h, m_start, 0}),
@@ -450,7 +452,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
precision_of<T>::value,
0,
0,
- m_cnt * head_size,
+ m_cnt * head_size_v,
1);
}
}
@@ -485,7 +487,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ONEDNN, T> {
if (d_scale == 0.0f)
d_scale = 1.0f / sqrt(head_size);
- prepare_brgemm_prim(strm, query, present_key, has_out_transpose);
+ prepare_brgemm_prim(strm, query, present_key, present_value, has_out_transpose);
execute_brgemm(query,
present_key,
present_value,
@@ -540,6 +542,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_ACL, T> {
auto H = query.size(1);
auto q_len = query.size(2);
auto head_size = query.size(3);
+ auto head_size_v = present_value.size(3);
auto kv_len = present_key.size(2);
auto h_group_num = present_key.size(1);
size_t h_each_group_len = H / h_group_num;
@@ -620,9 +623,9 @@ struct MHAKernel<ScaledDotProductAttention::KT_ACL, T> {
arm_compute::TensorInfo outInfo;
arm_compute::Tensor outTensor;
- auto out = has_out_transpose ? &output_emb.at<T>({b, m_start, h * head_size}) : &output_emb.at<T>({b, h, m_start});
+ auto out = has_out_transpose ? &output_emb.at<T>({b, m_start, h * head_size_v}) : &output_emb.at<T>({b, h, m_start});
auto strides = arm_compute::Strides({output_emb.stride_bytes(1), output_emb.stride_bytes(2)});
- GemmKernel out_gemm(m_cnt, kv_len, head_size, false, precision);
+ GemmKernel out_gemm(m_cnt, kv_len, head_size_v, false, precision);
arm_compute::Strides vStrides({present_value.stride_bytes(3), present_value.stride_bytes(2)});
out_gemm.executeGemm(qkTensor.buffer(),
@@ -685,6 +688,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_MLAS, float> {
auto H = query.size(1);
auto q_len = query.size(2);
auto head_size = query.size(3);
+ auto head_size_v = present_value.size(3);
auto kv_len = present_key.size(2);
auto h_group_num = present_key.size(1);
size_t h_each_group_len = H / h_group_num;
@@ -786,7 +790,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_MLAS, float> {
mlas_sgemm("N",
"N",
m_cnt,
- head_size,
+ head_size_v,
kv_len,
1.0f,
qk,
@@ -794,7 +798,7 @@ struct MHAKernel<ScaledDotProductAttention::KT_MLAS, float> {
v_ptr,
present_value.stride(2),
0.f,
- has_out_transpose ? &output_emb.at<float>({b, m_start, h * head_size}) : &output_emb.at<float>({b, h, m_start}),
+ has_out_transpose ? &output_emb.at<float>({b, m_start, h * head_size_v}) : &output_emb.at<float>({b, h, m_start}),
has_out_transpose ? output_emb.stride(1) : output_emb.stride(2),
1);
});
@@ -875,7 +879,7 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
PlainTensor attn_mask;
PlainTensor output_emb(output);
float scale_input = 0.0f;
- size_t B, L1, L0, S;
+ size_t B, L1, L0, S, SV;
q_input.reset(inputs[0]);
k_input.reset(inputs[1]);
@@ -911,18 +915,19 @@ struct ScaledDotProductAttention::AttentionExecutor : public ScaledDotProductAtt
B = q_input.size(0);
L1 = q_input.size(2);
S = q_input.size(3);
+ SV = v_input.size(3);
L0 = present_key.size(2) - L1;
auto Hk = k_input.size(1);
if (fuse_concat) {
k_input.assert_dims({B, Hk, L1, S});
- v_input.assert_dims({B, Hk, L1, S});
+ v_input.assert_dims({B, Hk, L1, SV});
} else {
k_input.assert_dims({B, Hk, L0 + L1, S});
- v_input.assert_dims({B, Hk, L0 + L1, S});
+ v_input.assert_dims({B, Hk, L0 + L1, SV});
}
present_key.assert_dims({B, Hk, L0 + L1, S});
- present_value.assert_dims({B, Hk, L0 + L1, S});
+ present_value.assert_dims({B, Hk, L0 + L1, SV});
if (beam_table)
beam_table.assert_dims({B, L0 + L1});
@@ -1222,6 +1227,7 @@ void ScaledDotProductAttention::resetBeamTablePastkv(const MemoryPtr& mem_cur_k,
auto H = cur_k.size(1);
auto L1 = cur_k.size(2);
auto S = cur_k.size(3);
+ auto SV = cur_v.size(3);
auto reverse = [&order] (const std::vector<size_t>& cur) {
std::vector<size_t> result(cur.size());
for (size_t i = 0; i < cur.size(); i++) {
@@ -1244,12 +1250,17 @@ void ScaledDotProductAttention::resetBeamTablePastkv(const MemoryPtr& mem_cur_k,
// BHLS is the stated input shape of SDPA, however internally we use LBHS for KV-cache storage.
// real_order is used to permute the original shape to LBHS
std::vector<size_t> shape = reverse({B, H, (L0 + L1) * 2, S});
- auto mem_desc = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
- Shape(shape),
- permute_axes(shape, real_order),
- real_order);
- auto new_internal_mem_k = std::make_shared<Memory>(getEngine(), mem_desc);
- auto new_internal_mem_v = std::make_shared<Memory>(getEngine(), mem_desc);
+ auto mem_desc_k = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(shape),
+ permute_axes(shape, real_order),
+ real_order);
+ auto new_internal_mem_k = std::make_shared<Memory>(getEngine(), mem_desc_k);
+ shape = reverse({B, H, (L0 + L1) * 2, SV});
+ auto mem_desc_v = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(shape),
+ permute_axes(shape, real_order),
+ real_order);
+ auto new_internal_mem_v = std::make_shared<Memory>(getEngine(), mem_desc_v);
PlainTensor new_pastk, new_pastv, old_past_k, old_past_v;
new_pastk.reset(new_internal_mem_k);
@@ -1271,7 +1282,7 @@ void ScaledDotProductAttention::resetBeamTablePastkv(const MemoryPtr& mem_cur_k,
S * old_past_k.m_element_size);
memcpy(&new_pastv.at<char>({b, h, m}),
&old_past_v.at<char>({b_kv, h, m}),
- S * old_past_v.m_element_size);
+ SV * old_past_v.m_element_size);
});
}
if (kvcache_precision == ov::element::u8) {
@@ -1301,16 +1312,26 @@ void ScaledDotProductAttention::resetBeamTablePastkv(const MemoryPtr& mem_cur_k,
std::vector<size_t> new_shape = reverse({B, H, (L0 + L1), S});
// Get the shape of physical layout using real order
- auto strides = mem_desc->getStrides();
- mem_desc = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
- Shape(new_shape),
- permute_axes(new_shape, real_order),
- real_order,
- 0,
- VectorDims{},
- strides);
- new_internal_mem_k->redefineDesc(mem_desc);
- new_internal_mem_v->redefineDesc(mem_desc);
+ auto strides = mem_desc_k->getStrides();
+ mem_desc_k = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(new_shape),
+ permute_axes(new_shape, real_order),
+ real_order,
+ 0,
+ VectorDims{},
+ mem_desc_k->getStrides());
+ new_internal_mem_k->redefineDesc(mem_desc_k);
+ new_shape = reverse({B, H, (L0 + L1), SV});
+ // Get the shape of physical layout using real order
+ strides = mem_desc_v->getStrides();
+ mem_desc_v = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(new_shape),
+ permute_axes(new_shape, real_order),
+ real_order,
+ 0,
+ VectorDims{},
+ strides);
+ new_internal_mem_v->redefineDesc(mem_desc_v);
if (kvcache_precision == ov::element::u8) {
// past_k's shape is BHLS, internal layout LBHS
// scale_zp's shape is LBHS, internal layout LBHS
@@ -1324,7 +1345,7 @@ void ScaledDotProductAttention::resetBeamTablePastkv(const MemoryPtr& mem_cur_k,
m_k_state->assign_internal_state(new_internal_mem_k);
m_v_state->assign_internal_state(new_internal_mem_v);
m_k_state->assign_internal_state_max_size(B * H * (L0 + L1) * 2 * S);
- m_v_state->assign_internal_state_max_size(B * H * (L0 + L1) * 2 * S);
+ m_v_state->assign_internal_state_max_size(B * H * (L0 + L1) * 2 * SV);
}
// 3. create beam table
{
@@ -1534,6 +1555,7 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M
auto H = cur_k.size(1);
auto L1 = cur_k.size(2);
auto S = cur_k.size(3);
+ auto SV = cur_v.size(3);
auto reverse = [&order] (const std::vector<size_t>& cur) {
std::vector<size_t> result(cur.size());
for (size_t i = 0; i < cur.size(); i++) {
@@ -1558,13 +1580,15 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M
// new_shape is the shape used by the original model which maybe different from BHLS, reverse here is to permute BHLS to original model shape.
// BHLS is the stated input shape of SDPA, however internally we use LBHS for KV-cache storage.
// real_order is used to permute the original shape to LBHS
- std::vector<size_t> new_shape = reverse({B, H, (L0 + L1) * 2, S});
- auto real_shape = permute_axes(new_shape, real_order);
- auto mem_desc =
- std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision, Shape(new_shape), real_shape, real_order);
+ auto new_memory = [&] (size_t new_S) {
+ std::vector<size_t> new_shape = reverse({B, H, (L0 + L1) * 2, new_S});
+ auto real_shape = permute_axes(new_shape, real_order);
+ auto mem_desc = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision, Shape(new_shape), real_shape, real_order);
+ return std::make_shared<Memory>(getEngine(), mem_desc);
+ };
- auto new_internal_mem_k = std::make_shared<Memory>(getEngine(), mem_desc);
- auto new_internal_mem_v = std::make_shared<Memory>(getEngine(), mem_desc);
+ auto new_internal_mem_k = new_memory(S);
+ auto new_internal_mem_v = new_memory(SV);
PlainTensor new_pastk, new_pastv;
new_pastk.reset(new_internal_mem_k);
@@ -1585,7 +1609,7 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M
m_k_state->assign_internal_state(new_internal_mem_k);
m_v_state->assign_internal_state(new_internal_mem_v);
m_k_state->assign_internal_state_max_size(2 * (L0 + L1) * B * H * S);
- m_v_state->assign_internal_state_max_size(2 * (L0 + L1) * B * H * S);
+ m_v_state->assign_internal_state_max_size(2 * (L0 + L1) * B * H * SV);
if (kvcache_precision == ov::element::u8) {
auto& old_scale_zp_k = m_k_state->get_scale_zp();
auto& old_scale_zp_v = m_v_state->get_scale_zp();
@@ -1610,21 +1634,23 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M
// new_shape is the shape used by the original model which maybe different from BHLS, reverse here is to permute BHLS to original model shape.
// BHLS is the stated input shape of SDPA, however internally we use LBHS for KV-cache storage.
// real_order is used to permute the original shape to LBHS
- std::vector<size_t> new_shape = reverse({B, H, (L0 + L1), S});
- VectorDims strides(new_shape.size(), 1);
- auto real_shape = permute_axes(new_shape, real_order);
- for (size_t i = 2; i <= real_shape.size(); i++) {
- strides[real_shape.size() - i] = strides[real_shape.size() - (i-1)] * real_shape[real_shape.size() - (i-1)];
- }
- auto mem_desc = std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
- Shape(new_shape),
- real_shape,
- real_order,
- 0,
- VectorDims{},
- strides);
- internal_mem_k->redefineDesc(mem_desc);
- internal_mem_v->redefineDesc(mem_desc);
+ auto reset_desc = [&] (size_t new_S) {
+ std::vector<size_t> new_shape = reverse({B, H, (L0 + L1), new_S});
+ VectorDims strides(new_shape.size(), 1);
+ auto real_shape = permute_axes(new_shape, real_order);
+ for (size_t i = 2; i <= real_shape.size(); i++) {
+ strides[real_shape.size() - i] = strides[real_shape.size() - (i-1)] * real_shape[real_shape.size() - (i-1)];
+ }
+ return std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(new_shape),
+ real_shape,
+ real_order,
+ 0,
+ VectorDims{},
+ strides);
+ };
+ internal_mem_k->redefineDesc(reset_desc(S));
+ internal_mem_v->redefineDesc(reset_desc(SV));
if (kvcache_precision == ov::element::u8) {
auto& old_scale_zp_k = m_k_state->get_scale_zp();
auto& old_scale_zp_v = m_v_state->get_scale_zp();
@@ -1640,18 +1666,19 @@ void ScaledDotProductAttention::updatePastkv(const MemoryPtr& mem_cur_k, const M
// new_shape is the shape used by the original model which maybe different from BHLS, reverse here is to permute BHLS to original model shape.
// BHLS is the stated input shape of SDPA, however internally we use LBHS for KV-cache storage.
// real_order is used to permute the original shape to LBHS
- std::vector<size_t> new_shape = reverse({B, H, (L0 + L1), S});
- auto real_shape = permute_axes(new_shape, real_order);
- auto mem_desc =
- std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
- Shape(new_shape),
- real_shape,
- real_order,
- 0,
- VectorDims{},
- internal_mem_k->getDescWithType<BlockedMemoryDesc>()->getStrides());
- internal_mem_k->redefineDesc(mem_desc);
- internal_mem_v->redefineDesc(mem_desc);
+ auto redefine_desc = [&] (MemoryPtr& mem, size_t new_S) {
+ std::vector<size_t> new_shape = reverse({B, H, (L0 + L1), new_S});
+ auto real_shape = permute_axes(new_shape, real_order);
+ return std::make_shared<CpuBlockedMemoryDesc>(kvcache_precision,
+ Shape(new_shape),
+ real_shape,
+ real_order,
+ 0,
+ VectorDims{},
+ mem->getDescWithType<BlockedMemoryDesc>()->getStrides());
+ };
+ internal_mem_k->redefineDesc(redefine_desc(internal_mem_k, S));
+ internal_mem_v->redefineDesc(redefine_desc(internal_mem_v, SV));
}
if (!past_k) {
diff --git a/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp b/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
index e00e5cae8aa..c2e8ebd9243 100644
--- a/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
+++ b/src/plugins/intel_cpu/src/shape_inference/custom/scaled_attn.cpp
@@ -20,29 +20,47 @@ public:
IShapeInfer::Result infer(const std::vector<std::reference_wrapper<const VectorDims>>& input_shapes,
const std::unordered_map<size_t, MemoryPtr>& data_dependency) override {
const auto& query_dims = input_shapes.front().get();
- VectorDims present_kv_dims = input_shapes.back().get();
+ VectorDims present_v_dims = input_shapes.back().get();
const auto& beam_idx_dims = input_shapes.end()[-3].get();
const auto& permute_axes = m_config.permute_axes;
if (permute_axes.empty()) {
// [B, H, L, S]
- present_kv_dims[0] = beam_idx_dims[0];
- present_kv_dims[2] += query_dims[2];
- return {{query_dims, present_kv_dims, present_kv_dims}, ShapeInferStatus::success};
+ present_v_dims[0] = beam_idx_dims[0];
+ present_v_dims[2] += query_dims[2];
+ // normal and fast path
+ if (present_v_dims[3] == query_dims[3])
+ return {{query_dims, present_v_dims, present_v_dims}, ShapeInferStatus::success};
+
+ // diff kv feature size
+ auto output_dims = query_dims;
+ output_dims[3] = present_v_dims[3];
+ auto present_k_dims = present_v_dims;
+ present_k_dims[3] = query_dims[3];
+ return {{output_dims, present_k_dims, present_v_dims}, ShapeInferStatus::success};
}
// permute_axes[0,1,2,3] gives axis indices of B,H,L,S for query & present_kv
const size_t batch_index = permute_axes[0];
const size_t length_index = permute_axes[2];
- present_kv_dims[batch_index] = beam_idx_dims[0];
- present_kv_dims[length_index] += query_dims[length_index];
+ present_v_dims[batch_index] = beam_idx_dims[0];
+ present_v_dims[length_index] += query_dims[length_index];
auto n_dims = query_dims.size();
VectorDims output_dims(n_dims);
for (size_t i = 0; i < n_dims; i++) {
output_dims[i] = query_dims[permute_axes[i]];
}
- return {{output_dims, present_kv_dims, present_kv_dims}, ShapeInferStatus::success};
+
+ // normal and fast path
+ if (present_v_dims[3] == query_dims[3])
+ return {{output_dims, present_v_dims, present_v_dims}, ShapeInferStatus::success};
+
+ // diff kv feature size
+ output_dims[3] = present_v_dims[3];
+ auto present_k_dims = present_v_dims;
+ present_k_dims[3] = query_dims[3];
+ return {{output_dims, present_k_dims, present_v_dims}, ShapeInferStatus::success};
}
port_mask_t get_port_mask() const override {
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/op/sdpa.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/op/sdpa.cpp
index 63b4520cf1b..4421499d102 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/op/sdpa.cpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/op/sdpa.cpp
@@ -27,35 +27,52 @@ void ov::intel_cpu::ScaledDotProductAttentionWithKVCache::validate_and_infer_typ
// [B, H, L1, S]
auto q_ps = get_input_partial_shape(0);
// [B, H, L0, S]
- auto past_kv_ps = get_input_partial_shape(input_num - 1);
+ auto past_k_ps = get_input_partial_shape(input_num - 2);
+ auto past_v_ps = get_input_partial_shape(input_num - 1);
// [present_kv_batch_size]
auto beam_idx_ps = get_input_partial_shape(input_num - 3);
auto output_logits = q_ps;
NODE_VALIDATION_CHECK(this, m_config.output_BLHxS == false);
+ NODE_VALIDATION_CHECK(this, q_ps.rank().is_static());
NODE_VALIDATION_CHECK(this, q_ps.size() >= 3);
// permute_axes from original to [B, H, L, S]
const auto& permute_axes = this->m_config.permute_axes;
- if (past_kv_ps.rank().is_static()) {
+ if (past_k_ps.rank().is_static() || past_v_ps.rank().is_static()) {
const size_t batch_index = permute_axes.empty() ? 0 : permute_axes[0];
const size_t length_index = permute_axes.empty() ? q_ps.size() - 2 : permute_axes[permute_axes.size() - 2];
const size_t head_num_index = permute_axes.empty() ? q_ps.size() - 3 : permute_axes[permute_axes.size() - 3];
- NODE_VALIDATION_CHECK(this, q_ps.size() == past_kv_ps.size());
+ if (past_k_ps.rank().is_static())
+ NODE_VALIDATION_CHECK(this, q_ps.size() == past_k_ps.size());
+ if (past_v_ps.rank().is_static())
+ NODE_VALIDATION_CHECK(this, q_ps.size() == past_v_ps.size());
for (size_t i = 0; i < q_ps.size(); i++) {
if (i == head_num_index) {
- if (q_ps[i].is_static() && past_kv_ps[i].is_static()) {
+ if (q_ps[i].is_static() && past_v_ps[i].is_static()) {
NODE_VALIDATION_CHECK(this,
- q_ps[i].get_length() % past_kv_ps[i].get_length() == 0,
+ q_ps[i].get_length() % past_v_ps[i].get_length() == 0,
"shape not compatiable at index ",
i);
}
+ if (past_k_ps[i].is_static() && past_v_ps[i].is_static()) {
+ NODE_VALIDATION_CHECK(this,
+ past_k_ps[i].get_length() == past_v_ps[i].get_length(),
+ "kv shape not compatiable at index ",
+ i);
+ }
} else {
continue;
}
}
// batch_size can be dynamically changed by gather logic
- past_kv_ps[batch_index] = beam_idx_ps[0];
- past_kv_ps[length_index] += q_ps[length_index];
+ if (past_k_ps.rank().is_static()) {
+ past_k_ps[batch_index] = beam_idx_ps[0];
+ past_k_ps[length_index] += q_ps[length_index];
+ }
+ if (past_v_ps.rank().is_static()) {
+ past_v_ps[batch_index] = beam_idx_ps[0];
+ past_v_ps[length_index] += q_ps[length_index];
+ }
}
if (!permute_axes.empty()) {
if (q_ps.rank().is_static()) {
@@ -65,9 +82,11 @@ void ov::intel_cpu::ScaledDotProductAttentionWithKVCache::validate_and_infer_typ
}
}
}
+ if (output_logits.rank().is_static() && past_v_ps.rank().is_static())
+ output_logits[output_logits.size() - 1] = past_v_ps[output_logits.size() - 1];
set_output_type(0, get_input_element_type(0), output_logits);
- set_output_type(1, get_input_element_type(input_num - 1), past_kv_ps);
- set_output_type(2, get_input_element_type(input_num - 1), past_kv_ps);
+ set_output_type(1, get_input_element_type(input_num - 1), past_k_ps);
+ set_output_type(2, get_input_element_type(input_num - 1), past_v_ps);
}
bool ov::intel_cpu::ScaledDotProductAttentionWithKVCache::visit_attributes(ov::AttributeVisitor& visitor) {
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 8a9212f8998..f049a16a764 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
@@ -38,6 +38,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
::testing::Combine(::testing::Values(ElementType::f16),
::testing::ValuesIn(inputShapes),
::testing::Values(false),
+ ::testing::Values(true, 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 f5a7bfacfac..83fc0a63554 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
@@ -30,7 +30,8 @@ std::string ConcatSDPTest::getTestCaseName(const testing::TestParamInfo<ConcatSD
std::vector<InputShape> inputShapes;
bool forceKVU8;
bool hasShapeOf;
- std::tie(inType, inputShapes, forceKVU8, hasShapeOf) = obj.param;
+ bool isDiffKVHeadSize;
+ std::tie(inType, inputShapes, forceKVU8, hasShapeOf, isDiffKVHeadSize) = obj.param;
std::ostringstream result;
result << "IS=";
for (const auto& shape : inputShapes) {
@@ -48,14 +49,15 @@ std::string ConcatSDPTest::getTestCaseName(const testing::TestParamInfo<ConcatSD
}
result << "Prc=" << inType << "_";
result << "ForceKVU8=" << forceKVU8 << "_";
- result << "HasShapeOf=" << hasShapeOf;
+ result << "HasShapeOf=" << hasShapeOf << "_";
+ result << "IsDiffKVHeadSize=" << isDiffKVHeadSize;
return result.str();
}
void ConcatSDPTest::SetUp() {
ElementType inType;
std::vector<InputShape> inputShapes;
- std::tie(inType, inputShapes, m_forceKVU8, m_hasShapeOf) = this->GetParam();
+ std::tie(inType, inputShapes, m_forceKVU8, m_hasShapeOf, m_isDiffKVHeadSize) = this->GetParam();
targetDevice = ov::test::utils::DEVICE_CPU;
rel_threshold = 1e-2f;
if (inType == ElementType::bf16 || inType == ElementType::f16) {
@@ -71,7 +73,11 @@ void ConcatSDPTest::SetUp() {
// q,k,v
inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, inputDynamicShapes[0]));
inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, inputDynamicShapes[0]));
- inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, inputDynamicShapes[0]));
+ auto v_ps = inputDynamicShapes[0];
+ if (m_isDiffKVHeadSize) {
+ v_ps[3] += m_diffKVHeadSize;
+ }
+ inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, v_ps));
inputParams[0]->set_friendly_name("q");
inputParams[1]->set_friendly_name("k");
inputParams[2]->set_friendly_name("v");
@@ -81,9 +87,15 @@ void ConcatSDPTest::SetUp() {
ov::op::util::VariableInfo{inputDynamicShapes[1], inType, "pastk"});
auto pastk = std::make_shared<ov::op::v6::ReadValue>(inputParams[3], var_k);
pastk->set_friendly_name("pastk_r");
+ // pastv init_cost
+ auto v_init_ps = inputDynamicShapes[1];
+ if (m_isDiffKVHeadSize) {
+ v_init_ps[3] += m_diffKVHeadSize;
+ }
+ inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, v_init_ps));
auto var_v = std::make_shared<ov::op::util::Variable>(
- ov::op::util::VariableInfo{inputDynamicShapes[1], inType, "pastv"});
- auto pastv = std::make_shared<ov::op::v6::ReadValue>(inputParams[3], var_v);
+ ov::op::util::VariableInfo{v_init_ps, inType, "pastv"});
+ auto pastv = std::make_shared<ov::op::v6::ReadValue>(inputParams[4], var_v);
pastv->set_friendly_name("pastv_r");
auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ElementType::i32, ov::PartialShape{-1});
beam_idx->set_friendly_name("beam_idx");
@@ -125,15 +137,6 @@ void ConcatSDPTest::SetUp() {
manager.run_passes(functionRefs);
}
-void ConcatSDPTest::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
- std::vector<ov::Shape> shapes(4);
- shapes[0] = targetInputStaticShapes[0];
- shapes[1] = targetInputStaticShapes[0];
- shapes[2] = targetInputStaticShapes[0];
- 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++) {
@@ -163,17 +166,26 @@ void ConcatSDPTest::generate(int idx, const std::vector<ov::Shape>& targetInputS
strided_iota(static_cast<ov::float16 *>(t.data()), t.get_size(), val, 0.0f);
inputs.insert({param, t});
} else {
+ ASSERT_TRUE(param->get_element_type() == element::bf16);
ov::Tensor t{ov::element::bf16, shape};
strided_iota(static_cast<ov::bfloat16*>(t.data()), t.get_size(), val, 0.1f);
inputs.insert({param, t});
}
};
// q, k, v, pastkv
+ auto v_shape = targetInputStaticShapes[0];
+ auto v_init_shape = targetInputStaticShapes[1];
+ if (m_isDiffKVHeadSize) {
+ v_shape[3] += m_diffKVHeadSize;
+ v_init_shape[3] += m_diffKVHeadSize;
+ }
+
create_input(function->get_parameters()[0], targetInputStaticShapes[0], idx + 1.0f);
create_input(function->get_parameters()[1], targetInputStaticShapes[0], idx + 2.0f);
- create_input(function->get_parameters()[2], targetInputStaticShapes[0], idx + 3.0f);
+ create_input(function->get_parameters()[2], v_shape, idx + 3.0f);
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);
+ create_input(function->get_parameters()[4], v_init_shape, idx + 4.0f);
+ create_input(function->get_parameters()[5], ov::Shape{targetInputStaticShapes[0][0]}, idx + 0.0f);
}
void ConcatSDPTest::prepare() {
@@ -214,7 +226,8 @@ TEST_P(ConcatSDPTest, CompareWithRefs) {
std::vector<InputShape> inputShapes;
bool forceKVU8;
bool hasShapeOf;
- std::tie(inType, inputShapes, forceKVU8, hasShapeOf) = this->GetParam();
+ bool isDiffKVHeadSize;
+ std::tie(inType, inputShapes, forceKVU8, hasShapeOf, isDiffKVHeadSize) = this->GetParam();
auto actualOutputs = run_test(function);
if (!hasShapeOf) {
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 ac59e48f496..83e1814f18b 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, bool> ConcatSDPTestParams;
+typedef std::tuple<ElementType, std::vector<InputShape>, bool, bool, bool> ConcatSDPTestParams;
class ConcatSDPTest :
public testing::WithParamInterface<ConcatSDPTestParams>,
@@ -48,9 +48,11 @@ public:
std::vector<ov::Tensor> run_test(std::shared_ptr<ov::Model> model);
bool m_forceKVU8;
bool m_hasShapeOf;
+ bool m_isDiffKVHeadSize;
protected:
- void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
void SetUp() override;
+
+ static constexpr size_t m_diffKVHeadSize = 16;
};
} // namespace test
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 57927434524..6761acf8b5d 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
@@ -38,6 +38,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
::testing::Combine(::testing::Values(ElementType::f32),
::testing::ValuesIn(inputShapes),
::testing::Values(true, false),
+ ::testing::Values(true, false),
::testing::Values(true, false)),
ConcatSDPTest::getTestCaseName);
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
index 93c99048fec..29667e2ffa3 100644
--- 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
@@ -38,6 +38,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_ConcatSDPTest,
::testing::Combine(::testing::Values(ElementType::bf16, ElementType::f16),
::testing::ValuesIn(inputShapes),
::testing::Values(true, false),
+ ::testing::Values(true, false),
::testing::Values(true, false)),
ConcatSDPTest::getTestCaseName);