OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU][VL_SPDA]Extend vl_sdpa to support model with head_size with multiple of 8 (#32415)
Zhang Yi3fe2187fe0d7
diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/cm_sdpa_vlen.cm b/src/plugins/intel_gpu/src/graph/impls/cm/cm_sdpa_vlen.cmindex f06979e72dc..67ef4c54732 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/cm_sdpa_vlen.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/cm_sdpa_vlen.cm@@ -48,10 +48,10 @@ _GENX_MAIN_ void KERNEL_NAME(//# query [q_len, num_heads, S]//# key [kv_len, num_heads, S]//# value [kv_len, num_heads, S]-- constexpr uint K_SLM_SIZE = (4*kv_step * head_size * sizeof(half));- constexpr uint V_SLM_SIZE = (4*kv_step * head_size * sizeof(half));- constexpr uint Q_SLM_SIZE = 0;//(q_step * head_size * sizeof(half)) * local_size;+ constexpr int padded_head_size = (head_size + 16 - 1) / 16 *16;+ constexpr uint K_SLM_SIZE = (4*kv_step * padded_head_size * sizeof(half));+ constexpr uint V_SLM_SIZE = (4*kv_step * padded_head_size * sizeof(half));+ constexpr uint Q_SLM_SIZE = 0;//(q_step * padded_head_size * sizeof(half)) * local_size;cm_slm_init(K_SLM_SIZE + V_SLM_SIZE + Q_SLM_SIZE);diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hppindex 325cd25fc5b..c26abc9d397 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp@@ -176,6 +176,28 @@ CM_INLINE void cm_load_2d(matrix_ref<half, M, N> out, SurfaceIndex base, uint of}}+template <int M, int N, int num_elem>+CM_INLINE void cm_load_2d_with_tail(matrix_ref<uint, M, N> out, SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ auto row_data = out.row(i).format<uint>();+ row_data = 0;+ auto src = cm_load<uint, N>(base, offset + i * pitch);+ row_data.select<num_elem, 1>(0) = src.select<num_elem, 1>(0);+ }+}++template <int M, int N, int num_elem>+CM_INLINE void cm_load_2d_with_tail(matrix_ref<half, M, N> out, SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ auto row_data = out.row(i).format<uint>();+ row_data = 0;+ auto src = cm_load<uint, N/2>(base, offset + i * pitch);+ row_data.select<num_elem/2, 1>(0) = src.select<num_elem/2, 1>(0);+ }+}+template <int M, int N>CM_INLINE void cm_store_2d(matrix_ref<half, M, N> out, SurfaceIndex base, uint offset, uint pitch) {#pragma unrolldiff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_sdpa_common.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_sdpa_common.hppindex 402cacb2e77..b4160fe81a4 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_sdpa_common.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_sdpa_common.hpp@@ -283,15 +283,17 @@ void sdpa_kernel_lsc(constexpr uint o_pitch = (num_heads * head_size * sizeof(half));constexpr uint q_pitch = is_qkv_fused ? ((num_heads + num_kv_heads*2) * head_size * sizeof(half)) : o_pitch;constexpr uint kv_pitch = is_qkv_fused ? q_pitch : (num_kv_heads * head_size * sizeof(half));-+ // round up head_size to multiple of 16+ // block_2d_desc will automatically handle the tailing block+ constexpr int padded_head_size = (head_size + 16 - 1) / 16 * 16;vector<float, q_step> cur_max;vector<float, q_step> cur_sum;cur_max = -3e38f;cur_sum = 0;constexpr int num_P_tiles = REG_N / REG_M;- matrix<half, head_size/REG_K, REG_K*REG_N> rQ;- matrix <float, head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;+ matrix<half, padded_head_size/REG_K, REG_K*REG_N> rQ;+ matrix <float, padded_head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;auto q_tokens_left = q_len;static_assert(q_step == REG_N);@@ -303,7 +305,7 @@ void sdpa_kernel_lsc(if (q_tokens_left > 0) {lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_left - 1, head_size*sizeof(half) - 1, q_pitch - 1, 0, 0);#pragma unroll- for(int k = 0, ri = 0; k < head_size/2; k += REG_K/2, ri++) {+ for(int k = 0, ri = 0; k < padded_head_size/2; k += REG_K/2, ri++) {cm_load<lsc::Transpose>(rQ[ri].format<uint>(), b2dQ.set_block_x(k));rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);}@@ -325,7 +327,7 @@ void sdpa_kernel_lsc(if (wg_local_id < local_size/2) {vector<half, kv_step * REG_K> temp0;b2dK.set_block_y(kv_pos);- for(int k = REG_K*wg_local_id; k < head_size; k += REG_K*(local_size/2)) {+ for(int k = REG_K*wg_local_id; k < padded_head_size; k += REG_K*(local_size/2)) {cm_load<lsc::Normal>(temp0, b2dK.set_block_x(k));cm_slm_block_write(slm_K, slm_offset + k * kv_step * sizeof(half), temp0);}@@ -333,7 +335,7 @@ void sdpa_kernel_lsc(vector<half, REG_K*REG_N> temp2;b2dV.set_block_y(kv_pos);#pragma unroll- for(int k = REG_N*(wg_local_id-(local_size/2)); k < head_size; k += REG_N*(local_size/2)) {+ for(int k = REG_N*(wg_local_id-(local_size/2)); k < padded_head_size; k += REG_N*(local_size/2)) {cm_load<lsc::VNNI>(temp2, b2dV.set_block_x(k));cm_slm_block_write(slm_V, slm_offset + k * REG_K * sizeof(half), temp2);}@@ -410,7 +412,7 @@ void sdpa_kernel_lsc(lsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_left - 1, head_size*sizeof(half) - 1, o_pitch - 1, 0, 0);#pragma unroll- for(int k = 0, ri=0; k < head_size; k += REG_N, ri += num_P_tiles) {+ for(int k = 0, ri=0; k < padded_head_size; k += REG_N, ri += num_P_tiles) {#pragma unrollfor(int p = 0; p < num_P_tiles; p++) {auto cO = rO[ri + p].format<float, REG_M, REG_N>();@@ -440,6 +442,9 @@ void sdpa_kernel_lsc_prefetch(constexpr uint o_pitch = (num_heads * head_size * sizeof(half));constexpr uint q_pitch = is_qkv_fused ? ((num_heads + num_kv_heads*2) * head_size * sizeof(half)) : o_pitch;constexpr uint kv_pitch = is_qkv_fused ? q_pitch : (num_kv_heads * head_size * sizeof(half));+ // round up head_size to multiple of 16+ // block_2d_desc will automatically handle the tailing block+ constexpr int padded_head_size = (head_size + 16 - 1) / 16 * 16;vector<float, q_step> cur_max;vector<float, q_step> cur_sum;@@ -447,8 +452,8 @@ void sdpa_kernel_lsc_prefetch(cur_max = -3e38f;cur_sum = 0;constexpr int num_P_tiles = REG_N / REG_M;- matrix<half, head_size/REG_K, REG_K*REG_N> rQ;- matrix <float, head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;+ matrix<half, padded_head_size/REG_K, REG_K*REG_N> rQ;+ matrix <float, padded_head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;auto q_tokens_left = q_len;// - q_start;static_assert(q_step == REG_N);@@ -460,7 +465,7 @@ void sdpa_kernel_lsc_prefetch(if (q_tokens_left > 0) {lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_left - 1, head_size*sizeof(half) - 1, q_pitch - 1, 0, 0);#pragma unroll- for(int k = 0, ri = 0; k < head_size/2; k += REG_K/2, ri++) {+ for(int k = 0, ri = 0; k < padded_head_size/2; k += REG_K/2, ri++) {cm_load<lsc::Transpose>(rQ[ri].format<uint>(), b2dQ.set_block_x(k));rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);}@@ -499,7 +504,7 @@ void sdpa_kernel_lsc_prefetch(Kmat[k].format<int32_t>());#pragma unroll- for(int ri = 1; ri < head_size/REG_K; ri++) {+ for(int ri = 1; ri < padded_head_size/REG_K; ri++) {//cm_slm_block_read(slm_K, GENX_NONE, slm_offset + ri * Kmat.n_elems() * sizeof(half), Kmat.format<half>());cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(ri*REG_K));cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(ri*REG_K));@@ -538,7 +543,7 @@ void sdpa_kernel_lsc_prefetch(// ugemm_PV0(slm_V, P, rO, slm_offset);auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();#pragma unroll- for(int k = 0, ri = 0; k < head_size; k += REG_N, ri += num_P_tiles) {+ for(int k = 0, ri = 0; k < padded_head_size; k += REG_N, ri += num_P_tiles) {matrix<half, REG_K/2, REG_N*2> Vmat;cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(k));cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(k));@@ -555,7 +560,7 @@ void sdpa_kernel_lsc_prefetch(//ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();#pragma unroll- for(int k = 0, ri=0; k < head_size; k += REG_N, ri += num_P_tiles) {+ for(int k = 0, ri=0; k < padded_head_size; k += REG_N, ri += num_P_tiles) {matrix<half, REG_K/2, REG_N*2> Vmat;cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(k));@@ -590,7 +595,7 @@ void sdpa_kernel_lsc_prefetch(lsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_left - 1, head_size*sizeof(half) - 1, o_pitch - 1, 0, 0);#pragma unroll- for(int k = 0, ri=0; k < head_size; k += REG_N, ri += num_P_tiles) {+ for(int k = 0, ri=0; k < padded_head_size; k += REG_N, ri += num_P_tiles) {#pragma unrollfor(int p = 0; p < num_P_tiles; p++) {auto cO = rO[ri + p].format<float, REG_M, REG_N>();@@ -625,7 +630,7 @@ void sdpa_kernel(uint k_off,uint v_off,uint o_off) {-+ constexpr int padded_head_size = (head_size + 16 - 1) / 16 *16;constexpr uint o_pitch = (num_heads * head_size * sizeof(half));constexpr uint q_pitch = is_qkv_fused ? ((num_heads + num_kv_heads*2) * head_size * sizeof(half)) : o_pitch;constexpr uint kv_pitch = is_qkv_fused ? q_pitch : (num_kv_heads * head_size * sizeof(half));@@ -636,10 +641,11 @@ void sdpa_kernel(cur_max = -3e38f;cur_sum = 0;- matrix<half, head_size/REG_K, REG_K*REG_N> rQ;+ matrix<half, padded_head_size/REG_K, REG_K*REG_N> rQ;auto q_tokens_left = q_len;static_assert(q_step == REG_N);static_assert(kv_step == REG_K);+ static_assert((head_size % 8) == 0);if (q_tokens_left < 0) q_tokens_left = 0;if (q_tokens_left > q_step) q_tokens_left = q_step;@@ -655,21 +661,33 @@ void sdpa_kernel(rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);}} else {+ constexpr int num_full_blocks = head_size / REG_K;+ int i = 0;#pragma unroll- for(int k = 0, ri = 0; k < head_size/2; k += REG_K/2, ri++) {+ for(; i < num_full_blocks; i++) {matrix<uint, q_step, REG_K/2> QmatI32;- cm_load_2d(QmatI32, query, q_off + k * sizeof(uint), q_pitch);- Transpose2DMatrix(QmatI32, rQ[ri].format<uint, REG_K/2, q_step>());- rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);+ int k = i * REG_K;+ cm_load_2d(QmatI32, query, q_off + k * sizeof(uint) / 2, q_pitch);+ Transpose2DMatrix(QmatI32, rQ[i].format<uint, REG_K/2, q_step>());+ rQ[i].format<half>() = cm_mul<half>(rQ[i].format<half>(), (half)scale_factor);+ }++ // if with tail, load with head_size_tail+ // following code will be optimized out when head_size_tail = 0+ if constexpr (head_size % REG_K > 0) {+ int k = num_full_blocks * REG_K;+ matrix<uint, q_step, REG_K/2> QmatI32;+ cm_load_2d_with_tail<q_step, REG_K/2, (head_size % REG_K) / 2>(QmatI32, query, q_off + k * sizeof(half), q_pitch);+ Transpose2DMatrix(QmatI32, rQ[num_full_blocks].format<uint, REG_K/2, q_step>());+ rQ[num_full_blocks].format<half>() = cm_mul<half>(rQ[num_full_blocks].format<half>(), (half)scale_factor);}- }}constexpr int num_P_tiles = REG_N / REG_M;- matrix <float, head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;+ matrix <float, padded_head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;int causal_left = q_start;- constexpr uint slm_buff_size = kv_step * head_size * sizeof(half);+ constexpr uint slm_buff_size = kv_step * padded_head_size * sizeof(half);int slm_buff_id_write = 0;int slm_buff_id_read = 0;@@ -684,11 +702,24 @@ void sdpa_kernel(if (wg_local_id < local_size/2) {//if (kv_pos > 1024000) {matrix<half, 2*REG_M, REG_K> temp;- for(int k = REG_K * wg_local_id; k < head_size; k += REG_K*(local_size/2)) {- cm_load_2d(temp, key, k_off + k*sizeof(half), kv_pitch);+ // head_size is split into blocks of <half, REG_K>+ constexpr int num_full_blocks = head_size / REG_K;+ int i = wg_local_id;+ for (; i < num_full_blocks; i += local_size / 2) {+ int k = i * REG_K;+ cm_load_2d(temp, key, k_off + k * sizeof(half), kv_pitch);+ cm_slm_block_write(slm_K,+ slm_offset + k * 2 * REG_M * sizeof(half),+ temp.format<half>());+ }+ // if with tail, load with head_size % REG_K+ // following code will be optimized out when head_size_tail = 0+ if constexpr (head_size % REG_K > 0) {+ int k = num_full_blocks * REG_K;+ cm_load_2d_with_tail<2*REG_M, REG_K, head_size % REG_K>(temp, key, k_off + k * sizeof(half), kv_pitch);cm_slm_block_write(slm_K,- slm_offset + k * 2 * REG_M * sizeof(half),- temp.format<half>());+ slm_offset + k * 2 * REG_M * sizeof(half),+ temp.format<half>());}} else {//if (kv_pos > 1024000) {@@ -699,17 +730,31 @@ void sdpa_kernel(matrix<half, REG_K/2, REG_N*2> temp_vnni;//b2dV.set_block_y(kv_pos);- static_assert((head_size % VK_STEP) == 0);- #pragma unroll- for(int k = VK_STEP * (wg_local_id-local_size/2); k < head_size; k += VK_STEP * (local_size/2)) {- cm_load_2d(temp2, value, v_off + k*sizeof(half), kv_pitch);+ // head_size is split into blocks of <half, VK_STEP>+ constexpr int num_full_blocks = head_size / VK_STEP;+ int i = wg_local_id - local_size / 2;+ for (; i < num_full_blocks; i += local_size / 2) {+ int k = i * VK_STEP;+ cm_load_2d(temp2, value, v_off + k * sizeof(half), kv_pitch);+ #pragma unroll+ for (int p = 0; p < VK_STEP / REG_N; p++) {+ temp_vnni.select<REG_K / 2, 1, REG_N, 2>(0, 0) = temp2.select<REG_K / 2, 2, REG_N, 1>(0, p * REG_N);+ temp_vnni.select<REG_K / 2, 1, REG_N, 2>(0, 1) = temp2.select<REG_K / 2, 2, REG_N, 1>(1, p * REG_N);+ cm_slm_block_write(slm_V, slm_offset + (k + p * REG_N) * REG_K * sizeof(half), temp_vnni.format<half>());+ }+ }+ // if with tail, load with head_size_tail+ // following code will be optimized out when head_size_tail = 0+ if constexpr (head_size % VK_STEP > 0) {+ int k = num_full_blocks * VK_STEP;+ cm_load_2d_with_tail<REG_K, VK_STEP, head_size % VK_STEP>(temp2, value, v_off + k * sizeof(half), kv_pitch);#pragma unroll- for(int p = 0; p < VK_STEP/REG_N; p++) {- temp_vnni.select<REG_K/2, 1, REG_N, 2>(0, 0) = temp2.select<REG_K/2, 2, REG_N, 1>(0, p*REG_N);- temp_vnni.select<REG_K/2, 1, REG_N, 2>(0, 1) = temp2.select<REG_K/2, 2, REG_N, 1>(1, p*REG_N);- // show(temp_vnni);- cm_slm_block_write(slm_V, slm_offset + (k + p*REG_N) * REG_K * sizeof(half), temp_vnni.format<half>());+ for (int p = 0; p < VK_STEP / REG_N; p++) {+ temp_vnni.select<REG_K / 2, 1, REG_N, 2>(0, 0) = temp2.select<REG_K / 2, 2, REG_N, 1>(0, p * REG_N);+ temp_vnni.select<REG_K / 2, 1, REG_N, 2>(0, 1) = temp2.select<REG_K / 2, 2, REG_N, 1>(1, p * REG_N);++ cm_slm_block_write(slm_V, slm_offset + (k + p * REG_N) * REG_K * sizeof(half), temp_vnni.format<half>());}}}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/vlsdpa_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/vlsdpa_gpu_test.cppindex 01c73f19619..d52c6b848e6 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/vlsdpa_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/vlsdpa_gpu_test.cpp@@ -231,10 +231,13 @@ INSTANTIATE_TEST_SUITE_P(smoke_vlsdpa_gpu_test,vlsdpa_gpu_test,::testing::Values(vlsdpa_test_params{64 /*head_size*/, 1 /*num_head*/, {0, 16} /*cu_seqlens*/},+ vlsdpa_test_params{72 /*head_size*/, 1 /*num_head*/, {0, 16} /*cu_seqlens*/},vlsdpa_test_params{128 /*head_size*/, 1 /*num_head*/, {0, 16} /*cu_seqlens*/},vlsdpa_test_params{64 /*head_size*/, 2 /*num_head*/, {0, 16} /*cu_seqlens*/},+ vlsdpa_test_params{72 /*head_size*/, 2 /*num_head*/, {0, 16} /*cu_seqlens*/},vlsdpa_test_params{64 /*head_size*/, 1 /*num_head*/, {0, 16, 32} /*cu_seqlens*/},- vlsdpa_test_params{64 /*head_size*/, 2 /*num_head*/, {0, 16, 32} /*cu_seqlens*/}+ vlsdpa_test_params{64 /*head_size*/, 2 /*num_head*/, {0, 16, 32} /*cu_seqlens*/},+ vlsdpa_test_params{72 /*head_size*/, 2 /*num_head*/, {0, 16, 32} /*cu_seqlens*/}),vlsdpa_gpu_test::PrintToStringParamName);