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] Implement the feature of head size 256 for cm xattention supported page attention (#36422)
Chen Xucccbdbc47956
diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hppindex 9e83b79f93d..ad98fc97992 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe1.hpp@@ -41,8 +41,18 @@ void pa_lsc_u8(constexpr int num_P_tiles = REG_N / REG_M;static_assert(num_P_tiles == 1, "XE1 kernels currently require REG_N/REG_M == 1");matrix<half, head_size/REG_K, REG_K*REG_N> rQ;+#if CMFLA_HEAD_SIZE == 256+ // head_size=256: split rO into lo/hi halves to stay under the CM+ // 8192-byte matrix-size limit on Xe1.+ constexpr int rO_half_rows = head_size / 2 / REG_N * num_P_tiles;+ matrix<float, rO_half_rows, REG_M * REG_N> rO_lo;+ matrix<float, rO_half_rows, REG_M * REG_N> rO_hi;+ rO_lo = 0;+ rO_hi = 0;+#elsematrix <float, head_size/REG_N, REG_M*REG_N> rO;rO = 0; // Initialize to prevent NaN from uninitialized values+#endifbool first_active = true;// clamp per-tile valid query tokens to [0, q_step]@@ -295,12 +305,25 @@ void pa_lsc_u8(matrix<half, REG_N, REG_K> P;Transpose2DMatrix(St, P);+#if CMFLA_HEAD_SIZE == 256+ constexpr uint slm_V_lo_offset = 0;+ constexpr uint slm_V_hi_offset = (head_size / 2) * REG_K * sizeof(half);+ if (first_active) {+ ugemm_PV0(slm_V, P, rO_lo, slm_offset + slm_V_lo_offset);+ ugemm_PV0(slm_V, P, rO_hi, slm_offset + slm_V_hi_offset);+ first_active = false;+ } else {+ ugemm_PV1(slm_V, P, max_comp, rO_lo, slm_offset + slm_V_lo_offset);+ ugemm_PV1(slm_V, P, max_comp, rO_hi, slm_offset + slm_V_hi_offset);+ }+#elseif (first_active) {ugemm_PV0(slm_V, P, rO, slm_offset);first_active = false;} else {ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);}+#endif}}// U8 path uses workgroup-level barriers in `pa_lsc_u8`, so every lane in@@ -317,6 +340,37 @@ void pa_lsc_u8(matrix<half, REG_M, REG_N> cur_O_f16;cur_sum = cm_inv(cur_sum);+#if CMFLA_HEAD_SIZE == 256+ #pragma unroll+ for(int k = 0, ri=0; k < head_size / 2; k += REG_N, ri++) {+ auto cO = rO_lo[ri].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r] = cm_mul<float>(cO.row(r), cur_sum[r]);+ }+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + k;++ for (int r = 0; r < q_tokens_in_tile; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());+ }+ }++ #pragma unroll+ for(int k = head_size / 2, ri=0; k < head_size; k += REG_N, ri++) {+ auto cO = rO_hi[ri].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r] = cm_mul<float>(cO.row(r), cur_sum[r]);+ }+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + k;++ for (int r = 0; r < q_tokens_in_tile; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());+ }+ }+#else#pragma unrollfor(int k = 0, ri=0; k < head_size; k += REG_N, ri++) {auto cO = rO[ri].format<float, REG_M, REG_N>();@@ -331,6 +385,7 @@ void pa_lsc_u8(cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());}}+#endif}#else@@ -371,8 +426,18 @@ void pa_kernel_lsc_prefetch_f16(static_assert(num_P_tiles == 1, "XE1 kernels currently require REG_N/REG_M == 1");constexpr int VALUE_TILE_NUM = 2;matrix<half, head_size/REG_K, REG_K*REG_N> rQ;+#if CMFLA_HEAD_SIZE == 256+ // head_size=256: split rO into lo/hi halves to stay under the CM+ // 8192-byte matrix-size limit on Xe1.+ constexpr int rO_half_rows_f16 = head_size / 2 / REG_M;+ matrix<float, rO_half_rows_f16, REG_M * REG_N> rO_lo;+ matrix<float, rO_half_rows_f16, REG_M * REG_N> rO_hi;+ rO_lo = 0;+ rO_hi = 0;+#elsematrix <float, head_size/REG_M, REG_M*REG_N> rO;rO = 0; // Initialize to prevent NaN from uninitialized values+#endifbool first_active = true;// clamp per-tile valid query tokens to [0, q_step]@@ -511,6 +576,126 @@ void pa_kernel_lsc_prefetch_f16(auto P_vec = P.format<half, REG_M, REG_K>();matrix<half, REG_K/2, REG_N*2*VALUE_TILE_NUM> Vmat;++#if CMFLA_HEAD_SIZE == 256+ // head_size=256: two loops (lower then upper half) writing rO_lo / rO_hi.+ #pragma unroll+ for(int k = 0, ri=0; k < head_size / 2; k += REG_N * VALUE_TILE_NUM, ri += VALUE_TILE_NUM) {+ matrix<half, REG_K, REG_N*VALUE_TILE_NUM> Vmat_tmp;+ constexpr uint elem_size = sizeof(half);+ constexpr int value_row_u32 = (REG_N * VALUE_TILE_NUM * sizeof(half)) / sizeof(uint);+ #pragma unroll+ for(int Vr = 0; Vr < REG_K; Vr++){+ uint elem_offset = cur_block_id * blk_stride+ + (kv_pos % CMPA_BLOCK_SZ) * head_size+ + Vr * head_size+ + k;+ uint cur_row_offset = v_cache_stateful_offset_bytes + elem_offset * elem_size;+ auto row_vec_u32 = cm_load<uint, value_row_u32>(v_cache_stateful, cur_row_offset);+ Vmat_tmp.row(Vr).format<uint>() = row_vec_u32;+ }+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ for (uint r = valid_rows; r < kv_step; r++)+ Vmat_tmp.row(r) = 0.f;+ }+ #pragma unroll+ for (int r = 0; r < REG_K/2; r++) {+ Vmat.row(r).select<REG_N*VALUE_TILE_NUM, 2>(0) = Vmat_tmp.row(r*2);+ Vmat.row(r).select<REG_N*VALUE_TILE_NUM, 2>(1) = Vmat_tmp.row(r*2+1);+ }++ if (first_active) {+ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto Vtile = Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>().select<REG_K/2, 1, REG_N*2, 1>(0, REG_N*2*tile);+ rO_lo[ri + tile] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vtile.format<int32_t>(),+ P_vec.format<int32_t>());+ }+ } else {+ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto cO = rO_lo[ri + tile].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r]);+ }++ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto Vtile =+ Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>()+ .select<REG_K/2, 1, REG_N*2, 1>(0, REG_N*2*tile);+ rO_lo[ri + tile] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_lo[ri + tile].format<float>(),+ Vtile.format<int32_t>(),+ P_vec.format<int32_t>());+ }+ }+ }++ #pragma unroll+ for(int k = head_size / 2, ri=0; k < head_size; k += REG_N * VALUE_TILE_NUM, ri += VALUE_TILE_NUM) {+ matrix<half, REG_K, REG_N*VALUE_TILE_NUM> Vmat_tmp;+ constexpr uint elem_size = sizeof(half);+ constexpr int value_row_u32 = (REG_N * VALUE_TILE_NUM * sizeof(half)) / sizeof(uint);+ #pragma unroll+ for(int Vr = 0; Vr < REG_K; Vr++){+ uint elem_offset = cur_block_id * blk_stride+ + (kv_pos % CMPA_BLOCK_SZ) * head_size+ + Vr * head_size+ + k;+ uint cur_row_offset = v_cache_stateful_offset_bytes + elem_offset * elem_size;+ auto row_vec_u32 = cm_load<uint, value_row_u32>(v_cache_stateful, cur_row_offset);+ Vmat_tmp.row(Vr).format<uint>() = row_vec_u32;+ }+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ for (uint r = valid_rows; r < kv_step; r++)+ Vmat_tmp.row(r) = 0.f;+ }+ #pragma unroll+ for (int r = 0; r < REG_K/2; r++) {+ Vmat.row(r).select<REG_N*VALUE_TILE_NUM, 2>(0) = Vmat_tmp.row(r*2);+ Vmat.row(r).select<REG_N*VALUE_TILE_NUM, 2>(1) = Vmat_tmp.row(r*2+1);+ }++ if (first_active) {+ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto Vtile = Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>().select<REG_K/2, 1, REG_N*2, 1>(0, REG_N*2*tile);+ rO_hi[ri + tile] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vtile.format<int32_t>(),+ P_vec.format<int32_t>());+ }+ } else {+ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto cO = rO_hi[ri + tile].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r]);+ }++ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ auto Vtile =+ Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>()+ .select<REG_K/2, 1, REG_N*2, 1>(0, REG_N*2*tile);+ rO_hi[ri + tile] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_hi[ri + tile].format<float>(),+ Vtile.format<int32_t>(),+ P_vec.format<int32_t>());+ }+ }+ }++ if (first_active) first_active = false;+#else+ // Baseline single-loop over full head_size, writing into single rO.#pragma unrollfor(int k = 0, ri=0; k < head_size; k += REG_N * VALUE_TILE_NUM, ri += VALUE_TILE_NUM) {matrix<half, REG_K, REG_N*VALUE_TILE_NUM> Vmat_tmp;@@ -568,6 +753,7 @@ void pa_kernel_lsc_prefetch_f16(}}}+#endif}#ifdef CMPA_DEBUG_ALL_MASKED@@ -579,6 +765,38 @@ void pa_kernel_lsc_prefetch_f16(//# save cur_O/cur_sum.transpose(0, 1)matrix<half, REG_M, REG_N> cur_O_f16;cur_sum = cm_inv(cur_sum);++#if CMFLA_HEAD_SIZE == 256+ #pragma unroll+ for(int k = 0, ri=0; k < head_size / 2; k += REG_N, ri++) {+ auto cO = rO_lo[ri].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r] = cm_mul<float>(cO.row(r), cur_sum[r]);+ }+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + k;++ for (int r = 0; r < q_tokens_in_tile; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());+ }+ }++ #pragma unroll+ for(int k = head_size / 2, ri=0; k < head_size; k += REG_N, ri++) {+ auto cO = rO_hi[ri].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r] = cm_mul<float>(cO.row(r), cur_sum[r]);+ }+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + k;++ for (int r = 0; r < q_tokens_in_tile; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());+ }+ }+#else#pragma unrollfor(int k = 0, ri=0; k < head_size; k += REG_N, ri++) {auto cO = rO[ri].format<float, REG_M, REG_N>();@@ -593,6 +811,7 @@ void pa_kernel_lsc_prefetch_f16(cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_f16.row(r).format<half>());}}+#endif}#endifdiff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hppindex 56b011f6f1b..cc88251d6ed 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_xe2.hpp@@ -47,6 +47,7 @@ template<bool use_causal_mask, int num_heads, int num_kv_heads, int head_size, ivoid pa_lsc_u8(uint slm_K,uint slm_V,+ uint slm_St_base,int wg_local_id,int local_size,int q_start,@@ -71,6 +72,17 @@ void pa_lsc_u8(constexpr uint kv_pitch = head_size * sizeof(uint8_t);+ constexpr bool enable_head_size_partition = (head_size == 256);+ constexpr int num_team = enable_head_size_partition ? 8 : 16;+ constexpr int num_worker = 16 / num_team;+ constexpr int process_head_size = head_size / num_worker;++ static_assert(head_size % num_worker == 0, "head_size must be divisible by num_worker");++ int team_id = enable_head_size_partition ? (wg_local_id / num_worker) : wg_local_id;+ int worker_id = enable_head_size_partition ? (wg_local_id % num_worker) : 0;+ int worker_offset = worker_id * process_head_size;+vector<float, q_step> cur_max;vector<float, q_step> cur_sum;@@ -78,8 +90,17 @@ void pa_lsc_u8(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, process_head_size / REG_K, REG_K * REG_N> rQ;+#if CMFLA_HEAD_SIZE == 256+ // head_size=256: split rO into lo/hi halves to stay under the CM+ // 16384-byte matrix-size limit.+ constexpr int rO_half_rows = process_head_size / 2 / REG_N * num_P_tiles;+ static_assert(process_head_size % (2 * REG_N) == 0, "process_head_size must be divisible by 2*REG_N for rO split");+ matrix<float, rO_half_rows, REG_M * REG_N> rO_lo;+ matrix<float, rO_half_rows, REG_M * REG_N> rO_hi;+#else+ matrix<float, process_head_size / REG_N * num_P_tiles, REG_M * REG_N> rO;+#endifbool first_active = true;// clamp per-tile valid query tokens to [0, q_step]@@ -93,7 +114,7 @@ void pa_lsc_u8(if (q_tokens_in_tile < 0) q_tokens_in_tile = 0;if (q_tokens_in_tile > q_step) q_tokens_in_tile = q_step;- // ---- Load Q (unchanged) ----+ // Each worker loads its 1/num_worker chunk of Qif (q_tokens_in_tile > 0) {lsc::block_2d_desc<uint, 1, REG_N, REG_K / 2> b2dQ(reinterpret_cast<uint*>(q_base),@@ -103,8 +124,8 @@ void pa_lsc_u8(0, 0);#pragma unroll- for (int k = 0, ri = 0; k < head_size / 2; k += REG_K / 2, ri++) {- cm_load<lsc::Transpose>(rQ[ri].format<uint>(), b2dQ.set_block_x(k));+ for (int k = 0, ri = 0; k < process_head_size / 2; k += REG_K / 2, ri++) {+ cm_load<lsc::Transpose>(rQ[ri].format<uint>(), b2dQ.set_block_x(worker_offset / 2 + k));rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);}}@@ -166,8 +187,9 @@ void pa_lsc_u8(if (kv_pos >= blk_end) return;if (kv_pos >= kv_stop) return;- // Ring slot for this load- uint slm_offset = (slm_buff_id_write & 3) * slm_buff_size;+ uint slm_offset = enable_head_size_partition+ ? (slm_buff_id_write % 3) * slm_buff_size+ : (slm_buff_id_write & 3) * slm_buff_size;// kv_left for tail within kv_stopint kv_left = kv_step;@@ -308,33 +330,67 @@ void pa_lsc_u8(// Prime pipeline (avoid 2nd prime if block too short)load_slm_KV_active(kv_blk, blk_end, kv_blk_pos_in_block, kv_blk_block_id);- if (kv_blk + kv_step < blk_end)- load_slm_KV_active(kv_blk + kv_step, blk_end, (kv_blk + kv_step) & cmpa_mask,- block_indices[(kv_blk + kv_step) >> cmpa_shift]);- cm_slm_fence(CM_LOCAL_BARRIER);- cm_sbarrier(1);-- for (int kv_pos = kv_blk; kv_pos < blk_end; kv_pos += kv_step, slm_buff_id_read++) {+ if constexpr (enable_head_size_partition) {+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();+ } else {+ if (kv_blk + kv_step < blk_end)+ load_slm_KV_active(kv_blk + kv_step, blk_end, (kv_blk + kv_step) & cmpa_mask,+ block_indices[(kv_blk + kv_step) >> cmpa_shift]);- cm_fence(CM_LOCAL_BARRIER);- cm_sbarrier(0);+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_sbarrier(1);+ }- // Prefetch 2 steps ahead only if it stays within this block- if (kv_pos + 2 * kv_step < blk_end) {- int prefetch_kv_pos = kv_pos + 2 * kv_step;+ for (int kv_pos = kv_blk; kv_pos < blk_end; kv_pos += kv_step, slm_buff_id_read++) {+ if constexpr (enable_head_size_partition) {+ int prefetch_kv_pos = kv_pos + kv_step;load_slm_KV_active(prefetch_kv_pos, blk_end, prefetch_kv_pos & cmpa_mask,block_indices[prefetch_kv_pos >> cmpa_shift]);- cm_slm_fence(CM_LOCAL_BARRIER);- }+ } else {+ cm_sbarrier(0);++ // Prefetch 2 steps ahead only if it stays within this block+ if (kv_pos + 2 * kv_step < blk_end) {+ int prefetch_kv_pos = kv_pos + 2 * kv_step;+ load_slm_KV_active(prefetch_kv_pos, blk_end, prefetch_kv_pos & cmpa_mask,+ block_indices[prefetch_kv_pos >> cmpa_shift]);+ }- if (kv_pos + kv_step < blk_end)- cm_sbarrier(1);+ if (kv_pos + kv_step < blk_end) {+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_sbarrier(1);+ }+ }{- uint slm_offset = (slm_buff_id_read & 3) * slm_buff_size;-- matrix<float, kv_step, q_step> St = ugemm_KQ(slm_K, rQ, slm_offset);+ uint slm_offset = enable_head_size_partition ?+ (slm_buff_id_read % 3) * slm_buff_size :+ (slm_buff_id_read & 3) * slm_buff_size;++ // Each worker computes partial St using its head_size chunk+ uint slm_K_worker_offset = slm_offset + worker_offset * kv_step * sizeof(half);+ matrix<float, kv_step, q_step> St = ugemm_KQ(slm_K, rQ, slm_K_worker_offset);++ // Head_size partitioning: synchronize and accumulate partial St+ if constexpr (enable_head_size_partition) {+ int slm_offset_bytes = wg_local_id * kv_step * q_step * sizeof(float);+ cm_slm_block_write(slm_St_base, slm_offset_bytes, St.format<float>());++ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();++ St = 0.0f;+ #pragma unroll+ for (int g = 0; g < num_worker; g++) {+ int src_wi = team_id * num_worker + g;+ int src_slm_offset_bytes = src_wi * kv_step * q_step * sizeof(float);+ matrix<float, kv_step, q_step> partial_st;+ cm_slm_block_read(slm_St_base, GENX_NONE, src_slm_offset_bytes, partial_st.format<float>());+ St += partial_st;+ }+ }if constexpr (use_causal_mask) {apply_causal_mask_with_offset(St, causal_left);@@ -347,12 +403,26 @@ void pa_lsc_u8(matrix<half, REG_N, REG_K> P;Transpose2DMatrix(St, P);+ // Each worker reads its chunk of V from SLM+ uint slm_V_worker_lo_offset = worker_offset * REG_K * sizeof(half);+ uint slm_V_worker_hi_offset = (worker_offset + process_head_size / 2) * REG_K * sizeof(half);+#if CMFLA_HEAD_SIZE == 256+ if (first_active) {+ ugemm_PV0(slm_V, P, rO_lo, slm_offset + slm_V_worker_lo_offset);+ ugemm_PV0(slm_V, P, rO_hi, slm_offset + slm_V_worker_hi_offset);+ first_active = false;+ } else {+ ugemm_PV1(slm_V, P, max_comp, rO_lo, slm_offset + slm_V_worker_lo_offset);+ ugemm_PV1(slm_V, P, max_comp, rO_hi, slm_offset + slm_V_worker_hi_offset);+ }+#elseif (first_active) {- ugemm_PV0(slm_V, P, rO, slm_offset);+ ugemm_PV0(slm_V, P, rO, slm_offset + slm_V_worker_lo_offset);first_active = false;} else {- ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);+ ugemm_PV1(slm_V, P, max_comp, rO, slm_offset + slm_V_worker_lo_offset);}+#endif}}}@@ -411,7 +481,9 @@ void pa_lsc_u8(kv_pos_in_block * sizeof(half);uint32_t v_zp_offset = v_dscale_offset + CMPA_BLOCK_SZ * sizeof(half);- uint slm_offset = (slm_buff_id_write & 3) * slm_buff_size;+ uint slm_offset = enable_head_size_partition+ ? (slm_buff_id_write % 3) * slm_buff_size+ : (slm_buff_id_write & 3) * slm_buff_size;vector<half, kv_step> dscale;vector<half, kv_step> zp;int kv_left = (kv_stop - kv_pos) > kv_step ? kv_step : (kv_stop - kv_pos);@@ -485,28 +557,37 @@ void pa_lsc_u8(};load_slm_KV(0);- load_slm_KV(kv_step);- cm_slm_fence(CM_LOCAL_BARRIER);- cm_sbarrier(1);+ if constexpr (enable_head_size_partition) {+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();+ } else {+ load_slm_KV(kv_step);+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_sbarrier(1);+ }for (int kv_pos = 0; kv_pos < kv_stop; kv_pos += kv_step, slm_buff_id_read++) {- // load0, load1, signal1,- // [wait1, signal2, load2, read0, compute0]- // [wait2, signal3, load3, read1, compute1]- // [wait3, signal4, load4, read2, compute2]- // [wait4, signal5, load5, read3, compute3]- //- // after wait3, all workers have reached signal3, so:- // - all workers have finished load2 & read0.- // - we can start to load 4 into SLM slot 0 (i & 3) safely- // - we can start to read 2 ((i-2) & 3) safely-- cm_fence(CM_LOCAL_BARRIER);- cm_sbarrier(0);-- if (kv_pos + kv_step < kv_stop)- cm_sbarrier(1);- load_slm_KV(kv_pos + kv_step * 2);+ if constexpr (enable_head_size_partition) {+ load_slm_KV(kv_pos + kv_step);+ } else {+ // load0, load1, signal1,+ // [wait1, signal2, load2, read0, compute0]+ // [wait2, signal3, load3, read1, compute1]+ // [wait3, signal4, load4, read2, compute2]+ // [wait4, signal5, load5, read3, compute3]+ //+ // after wait3, all workers have reached signal3, so:+ // - all workers have finished load2 & read0.+ // - we can start to load 4 into SLM slot 0 (i & 3) safely+ // - we can start to read 2 ((i-2) & 3) safely+ cm_sbarrier(0);++ if (kv_pos + kv_step < kv_stop) {+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_sbarrier(1);+ }+ load_slm_KV(kv_pos + kv_step * 2);+ }#if SPARSE_BLOCK_SIZE > 1if (skip_compute(kv_pos)) {@@ -518,9 +599,32 @@ void pa_lsc_u8(#endif{- uint slm_offset = (slm_buff_id_read & 3) * slm_buff_size;+ uint slm_offset = enable_head_size_partition ?+ (slm_buff_id_read % 3) * slm_buff_size :+ (slm_buff_id_read & 3) * slm_buff_size;++ // Each worker computes partial St using its head_size chunk+ uint slm_K_worker_offset = slm_offset + worker_offset * kv_step * sizeof(half);+ matrix<float, kv_step, q_step> St = ugemm_KQ(slm_K, rQ, slm_K_worker_offset);++ // Head_size partitioning: synchronize and accumulate partial St+ if constexpr (enable_head_size_partition) {+ int slm_offset_bytes = wg_local_id * kv_step * q_step * sizeof(float);+ cm_slm_block_write(slm_St_base, slm_offset_bytes, St.format<float>());++ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();- matrix<float, kv_step, q_step> St = ugemm_KQ(slm_K, rQ, slm_offset);+ St = 0.0f;+ #pragma unroll+ for (int g = 0; g < num_worker; g++) {+ int src_wi = team_id * num_worker + g;+ int src_slm_offset_bytes = src_wi * kv_step * q_step * sizeof(float);+ matrix<float, kv_step, q_step> partial_st;+ cm_slm_block_read(slm_St_base, GENX_NONE, src_slm_offset_bytes, partial_st.format<float>());+ St += partial_st;+ }+ }if constexpr (use_causal_mask) {apply_causal_mask_with_offset(St, causal_left);@@ -533,12 +637,26 @@ void pa_lsc_u8(matrix<half, REG_N, REG_K> P;Transpose2DMatrix(St, P);+ // Each worker reads its chunk of V from SLM+ uint slm_V_worker_lo_offset = worker_offset * REG_K * sizeof(half);+ uint slm_V_worker_hi_offset = (worker_offset + process_head_size / 2) * REG_K * sizeof(half);+#if CMFLA_HEAD_SIZE == 256+ if (first_active) {+ ugemm_PV0(slm_V, P, rO_lo, slm_offset + slm_V_worker_lo_offset);+ ugemm_PV0(slm_V, P, rO_hi, slm_offset + slm_V_worker_hi_offset);+ first_active = false;+ } else {+ ugemm_PV1(slm_V, P, max_comp, rO_lo, slm_offset + slm_V_worker_lo_offset);+ ugemm_PV1(slm_V, P, max_comp, rO_hi, slm_offset + slm_V_worker_hi_offset);+ }+#elseif (first_active) {- ugemm_PV0(slm_V, P, rO, slm_offset);+ ugemm_PV0(slm_V, P, rO, slm_offset + slm_V_worker_lo_offset);first_active = false;} else {- ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);+ ugemm_PV1(slm_V, P, max_comp, rO, slm_offset + slm_V_worker_lo_offset);}+#endif}}#endif@@ -566,8 +684,54 @@ void pa_lsc_u8(o_pitch - 1,0, 0);+#if CMFLA_HEAD_SIZE == 256+ // Store lower half of worker's chunk from rO_lo+ #pragma unroll+ for (int k = 0, ri = 0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {++ #pragma unroll+ for (int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_lo[ri + p].format<float, REG_M, REG_N>();++ #pragma unroll+ for (int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r + p * REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p * REG_M]);+ }+ }++ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);+ cm_store(b2dO.set_block_y(0),+ cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));+ cm_store(b2dO.set_block_y(REG_M),+ cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));+ }++ // Store upper half of worker's chunk from rO_hi+ #pragma unroll+ for (int k = process_head_size / 2, ri = 0; k < process_head_size; k += REG_N, ri += num_P_tiles) {++ #pragma unroll+ for (int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_hi[ri + p].format<float, REG_M, REG_N>();++ #pragma unroll+ for (int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r + p * REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p * REG_M]);+ }+ }++ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);+ cm_store(b2dO.set_block_y(0),+ cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));+ cm_store(b2dO.set_block_y(REG_M),+ cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));+ }+#else+ // Baseline single-loop over full process_head_size, storing from single rO.#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 < process_head_size; k += REG_N, ri += num_P_tiles) {#pragma unrollfor (int p = 0; p < num_P_tiles; p++) {@@ -579,18 +743,21 @@ void pa_lsc_u8(}}- b2dO.set_block_x(k);+ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);cm_store(b2dO.set_block_y(0),cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));cm_store(b2dO.set_block_y(REG_M),cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));}+#endif}#elsetemplate<bool use_causal_mask, int num_heads, int num_kv_heads, int head_size, int is_qkv_fused, int wg_local_size>void pa_kernel_lsc_prefetch_f16(+ uint slm_St_base,int wg_local_id,int q_start,int kv_stop, //@@ -614,14 +781,35 @@ void pa_kernel_lsc_prefetch_f16(constexpr uint k_pitch = head_size * sizeof(half);constexpr uint v_pitch = k_pitch;+ constexpr bool enable_head_size_partition = (head_size == 256);+ constexpr int num_team = enable_head_size_partition ? 8 : wg_local_size;+ constexpr int num_worker = wg_local_size / num_team;+ constexpr int process_head_size = head_size / num_worker;++ static_assert(wg_local_size == 16, "wg_local_size must be 16");+ static_assert(head_size % num_worker == 0, "head_size must be divisible by num_worker");++ int team_id = enable_head_size_partition ? (wg_local_id / num_worker) : wg_local_id;+ int worker_id = enable_head_size_partition ? (wg_local_id % num_worker) : 0;+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, process_head_size/REG_K, REG_K*REG_N> rQ;+#if CMFLA_HEAD_SIZE == 256+ // head_size=256: each worker only allocates 1/2 of head_size; split rO into+ // lo/hi halves to stay under the CM 16384-byte matrix-size limit.+ constexpr int rO_half_rows_f16 = process_head_size / 2 / REG_N * num_P_tiles;+ static_assert(process_head_size % (2 * REG_N) == 0, "process_head_size must be divisible by 2*REG_N for rO split");+ matrix<float, rO_half_rows_f16, REG_M * REG_N> rO_lo;+ matrix<float, rO_half_rows_f16, REG_M * REG_N> rO_hi;+#else+ matrix<float, process_head_size/REG_N*num_P_tiles, REG_M*REG_N> rO;+#endifbool first_active = true;#if SPARSE_BLOCK_SIZE > 1@@ -647,14 +835,20 @@ void pa_kernel_lsc_prefetch_f16(if (q_tokens_in_tile < 0) q_tokens_in_tile = 0;if (q_tokens_in_tile > q_step) q_tokens_in_tile = q_step;- // Fp16 path does not use workgroup-level barriers as in `pa_lsc_u8`, so lanes with zero valid query tokens can early exit.- if (q_tokens_in_tile == 0) return;+ // Threads with zero valid query tokens can early exit if there is no barrier.+ if constexpr (!enable_head_size_partition) {+ if (q_tokens_in_tile == 0) return;+ }- lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_in_tile - 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++) {- 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);+ // Each worker loads its 1/num_worker chunk of Q+ int worker_offset = worker_id * process_head_size;+ if (q_tokens_in_tile > 0) {+ lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_in_tile - 1, head_size*sizeof(half) - 1, q_pitch - 1, 0, 0);+ #pragma unroll+ for(int k = 0, ri = 0; k < process_head_size/2; k += REG_K/2, ri++) {+ cm_load<lsc::Transpose>(rQ[ri].format<uint>(), b2dQ.set_block_x(worker_offset / 2 + k));+ rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);+ }}lsc::block_2d_desc<half, 1, kv_step, REG_K> b2dK(k_cache_base, CMPA_BLOCK_SZ - 1, head_size*sizeof(half) - 1, k_pitch - 1, 0, 0);@@ -670,6 +864,10 @@ void pa_kernel_lsc_prefetch_f16(// ====================================================================================// Optimized block-granular sparse pipeline when SPARSE_BLOCK_SIZE == WG_SEQ_LEN// ====================================================================================+ // Use SLM passed from kernel for accumulating partial attention scores across workers+ constexpr int slm_size_per_wi = kv_step * q_step; // St matrix size+ auto slm_St = slm_St_base;+constexpr int kv_block = SPARSE_BLOCK_SIZE;for (int kv_blk = 0; kv_blk < kv_stop; kv_blk += kv_block) {int blk_end = kv_blk + kv_block;@@ -700,7 +898,7 @@ void pa_kernel_lsc_prefetch_f16(prefetch_K.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+prefetch_block_id*blk_stride));prefetch_K.set_block_y((prefetch_kv_pos + wg_local_id) % CMPA_BLOCK_SZ);- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(0));+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(worker_offset));if (skip_compute(kv_pos)) {if constexpr (use_causal_mask)@@ -709,7 +907,7 @@ void pa_kernel_lsc_prefetch_f16(}b2dK.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+cur_block_id*blk_stride));b2dK.set_block_y(kv_pos%CMPA_BLOCK_SZ);- cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(0));+ cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(worker_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused key data.if ((kv_pos + kv_step) > kv_stop) {auto valid_rows = kv_stop - kv_pos;@@ -724,9 +922,10 @@ void pa_kernel_lsc_prefetch_f16(Kmat[k].format<int32_t>());#pragma unroll- for(int ri = 1; ri < head_size/REG_K; ri++) {- 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));+ for(int ri = 1; ri < process_head_size/REG_K; ri++) {+ int k_offset = worker_offset + ri*REG_K;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(k_offset));+ cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(k_offset));#pragma unrollfor(int k = 0; k < num_K; k++) {St2.row(k) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(@@ -735,6 +934,31 @@ void pa_kernel_lsc_prefetch_f16(Kmat[k].format<int32_t>());}}++ // Head_size partitioning: synchronize and accumulate partial St+ // Work-items with same team_id accumulate across head_size chunks+ if constexpr (enable_head_size_partition) {+ // Store partial St to SLM for this work-item+ int slm_offset_bytes = wg_local_id * kv_step * q_step * sizeof(float);+ cm_slm_block_write(slm_St, slm_offset_bytes, St.format<float>());++ // Barrier: ensure all work-items have written their partial St+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();++ // Accumulate partial St from both of the 2 head_size chunks for this query slice+ // Work-items [team_id*2, team_id*2+1] cooperate+ St = 0.0f;+ #pragma unroll+ for(int g = 0; g < num_worker; g++) {+ int src_wi = team_id * num_worker + g; // Same query slice (team_id), different head_size chunk (g)+ int src_slm_offset_bytes = src_wi * kv_step * q_step * sizeof(float);+ matrix<float, kv_step, q_step> partial_st;+ cm_slm_block_read(slm_St, GENX_NONE, src_slm_offset_bytes, partial_st.format<float>());+ St += partial_st;+ }+ }+}if constexpr (use_causal_mask) {apply_causal_mask_with_offset(St, causal_left);@@ -754,14 +978,141 @@ void pa_kernel_lsc_prefetch_f16(b2dV.set_base_ptr((reinterpret_cast<half*>(v_cache_base)+cur_block_id*blk_stride));b2dV.set_block_y(kv_pos%CMPA_BLOCK_SZ);+#if CMFLA_HEAD_SIZE == 256+ if (first_active) {+ // ugemm_PV0(slm_V, P, rO, slm_offset);+ auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ // PV0 lower half - Each worker loads its 1/2 chunk of V+ #pragma unroll+ for(int k = 0, ri = 0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ // sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_lo[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ // PV0 upper half - Second half of this worker's chunk+ #pragma unroll+ for(int k = process_head_size / 2, ri = 0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_hi[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ first_active = false;+ }+ else {+ //ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);+ auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ // PV1 lower half - Each worker loads its 1/2 chunk of V+ #pragma unroll+ for(int k = 0, ri=0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;++ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ // sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ //# compensate cur_O+ // matrix <float, head_size/REG_K*2, REG_M*REG_N> rO;+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_lo[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r + p*REG_M]);+ }++ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_lo[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_lo[ri + p].format<float>(),+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ // PV1 upper half - Second half of this worker's chunk+ #pragma unroll+ for(int k = process_head_size / 2, ri=0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;++ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_hi[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r + p*REG_M]);+ }++ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_hi[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_hi[ri + p].format<float>(),+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ }+#else+ // Baseline single-loop over full process_head_size into single rO.if (first_active) {// 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 < process_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));+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.if ((kv_pos + kv_step) > kv_stop) {uint valid_rows = kv_stop - kv_pos;@@ -785,11 +1136,12 @@ void pa_kernel_lsc_prefetch_f16(//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 < process_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));+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.if ((kv_pos + kv_step) > kv_stop) {uint valid_rows = kv_stop - kv_pos;@@ -818,12 +1170,17 @@ void pa_kernel_lsc_prefetch_f16(}}}+#endif}}#else// ========================================================================// Legacy per-step pipeline for any SPARSE_BLOCK_SIZE (including 1)// ======================================================================+ // Use SLM passed from kernel for accumulating partial attention scores across workers+ constexpr int slm_size_per_wi = kv_step * q_step; // St matrix size+ auto slm_St = slm_St_base;+for(int kv_pos = 0; kv_pos < kv_stop; kv_pos += kv_step) {auto cur_block_id = block_indices[kv_pos / CMPA_BLOCK_SZ];//For the last step, duplicate prefetch here.@@ -839,7 +1196,7 @@ void pa_kernel_lsc_prefetch_f16(prefetch_K.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+prefetch_block_id*blk_stride));prefetch_K.set_block_y((prefetch_kv_pos + wg_local_id) % CMPA_BLOCK_SZ);- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(0));+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(worker_offset));#if SPARSE_BLOCK_SIZE > 1if (skip_compute(kv_pos)) {@@ -850,13 +1207,17 @@ void pa_kernel_lsc_prefetch_f16(#endifb2dK.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+cur_block_id*blk_stride));b2dK.set_block_y(kv_pos%CMPA_BLOCK_SZ);- cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(0));++ // Each work-item loads its 1/2 chunk of K and computes partial St+ cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(worker_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused key data.if ((kv_pos + kv_step) > kv_stop) {auto valid_rows = kv_stop - kv_pos;for (int r = valid_rows; r < kv_step; r++)Kmat.format<half, num_K*REG_M, REG_N>().row(r) = 0.f;}++ // Compute partial St with this worker's Q chunk#pragma unrollfor(int k = 0; k < num_K; k++)St2.row(k) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(@@ -865,9 +1226,10 @@ void pa_kernel_lsc_prefetch_f16(Kmat[k].format<int32_t>());#pragma unroll- for(int ri = 1; ri < head_size/REG_K; ri++) {- 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));+ for(int ri = 1; ri < process_head_size/REG_K; ri++) {+ int k_offset = worker_offset + ri*REG_K;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_K.set_block_x(k_offset));+ cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(k_offset));#pragma unrollfor(int k = 0; k < num_K; k++) {St2.row(k) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(@@ -876,6 +1238,31 @@ void pa_kernel_lsc_prefetch_f16(Kmat[k].format<int32_t>());}}++ // Head_size partitioning: synchronize and accumulate partial St+ // Work-items with same team_id (processing same query slice) accumulate across head_size chunks+ if constexpr (enable_head_size_partition) {+ // Store partial St to SLM for this work-item+ int slm_offset_bytes = wg_local_id * kv_step * q_step * sizeof(float);+ cm_slm_block_write(slm_St, slm_offset_bytes, St.format<float>());++ // Barrier: ensure all work-items have written their partial St+ cm_slm_fence(CM_LOCAL_BARRIER);+ cm_barrier();++ // Accumulate partial St from both of the 2 head_size chunks for this query slice+ // Work-items [team_id*2, team_id*2+1] cooperate+ St = 0.0f;+ #pragma unroll+ for(int g = 0; g < num_worker; g++) {+ int src_wi = team_id * num_worker + g; // Same query slice (team_id), different head_size chunk (g)+ int src_slm_offset_bytes = src_wi * kv_step * q_step * sizeof(float);+ matrix<float, kv_step, q_step> partial_st;+ cm_slm_block_read(slm_St, GENX_NONE, src_slm_offset_bytes, partial_st.format<float>());+ St += partial_st;+ }+ }+}if constexpr (use_causal_mask) {apply_causal_mask_with_offset(St, causal_left);@@ -895,14 +1282,141 @@ void pa_kernel_lsc_prefetch_f16(b2dV.set_base_ptr((reinterpret_cast<half*>(v_cache_base)+cur_block_id*blk_stride));b2dV.set_block_y(kv_pos%CMPA_BLOCK_SZ);+#if CMFLA_HEAD_SIZE == 256+ if (first_active) {+ // ugemm_PV0(slm_V, P, rO, slm_offset);+ auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ // PV0 lower half - Each worker loads its 1/2 chunk of V+ #pragma unroll+ for(int k = 0, ri = 0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ // sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_lo[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ // PV0 upper half - Second half of this worker's chunk+ #pragma unroll+ for(int k = process_head_size / 2, ri = 0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_hi[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ first_active = false;+ }+ else {+ //ugemm_PV1(slm_V, P, max_comp, rO, slm_offset);+ auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ // PV1 lower half - Each worker loads its 1/2 chunk of V+ #pragma unroll+ for(int k = 0, ri=0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;++ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ // sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ //# compensate cur_O+ // matrix <float, head_size/REG_K*2, REG_M*REG_N> rO;+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_lo[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r + p*REG_M]);+ }++ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_lo[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_lo[ri + p].format<float>(),+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ // PV1 upper half - Second half of this worker's chunk+ #pragma unroll+ for(int k = process_head_size / 2, ri=0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ matrix<half, REG_K/2, REG_N*2> Vmat;++ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));+ if ((kv_pos + kv_step) > kv_stop) {+ uint valid_rows = kv_stop - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ for (int r = valid_rows_vnni; r < kv_step / 2; r++)+ Vmat.row(r) = 0.f;+ if (valid_rows % 2 == 1)+ Vmat.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ }+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_hi[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_M; r++)+ cO.row(r) = cm_mul<float>(cO.row(r), max_comp[r + p*REG_M]);+ }++ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ rO_hi[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO_hi[ri + p].format<float>(),+ Vmat.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }+ }+ }+#else+ // Baseline single-loop over full process_head_size into single rO.if (first_active) {// 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 < process_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));+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.if ((kv_pos + kv_step) > kv_stop) {uint valid_rows = kv_stop - kv_pos;@@ -926,11 +1440,12 @@ void pa_kernel_lsc_prefetch_f16(//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 < process_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));+ int v_offset = worker_offset + k;+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(v_offset));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(v_offset));// sometimes KV cache would be filled with random Nan, so need to clean up the unused value data.if ((kv_pos + kv_step) > kv_stop) {uint valid_rows = kv_stop - kv_pos;@@ -959,6 +1474,7 @@ void pa_kernel_lsc_prefetch_f16(}}}+#endif}#endif@@ -972,22 +1488,63 @@ void pa_kernel_lsc_prefetch_f16(matrix<half, num_P_tiles*REG_M, REG_N> cur_O_f16;cur_sum = cm_inv(cur_sum);- lsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_in_tile - 1, head_size*sizeof(half) - 1, o_pitch - 1, 0, 0);+ // Inactive threads (q_tokens_in_tile==0) participated in barriers but must not write output.+ if (q_tokens_in_tile > 0) {+ lsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_in_tile - 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) {+#if CMFLA_HEAD_SIZE == 256+ // Each worker stores its 1/2 chunk of output+ // Store lower half of worker's chunk from rO_lo#pragma unroll- for(int p = 0; p < num_P_tiles; p++) {- auto cO = rO[ri + p].format<float, REG_M, REG_N>();+ for(int k = 0, ri=0; k < process_head_size / 2; k += REG_N, ri += num_P_tiles) {#pragma unroll- for(int r = 0; r < cO.n_rows(); r++) {- cur_O_f16[r + p*REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p*REG_M]);+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_lo[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r + p*REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p*REG_M]);+ }+ }+ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);+ cm_store(b2dO.set_block_y(0), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));+ cm_store(b2dO.set_block_y(REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));+ }+ // Store upper half of worker's chunk from rO_hi+ #pragma unroll+ for(int k = process_head_size / 2, ri=0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO_hi[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r + p*REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p*REG_M]);+ }+ }+ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);+ cm_store(b2dO.set_block_y(0), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));+ cm_store(b2dO.set_block_y(REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));+ }+#else+ // Baseline single-loop over full process_head_size from single rO.+ #pragma unroll+ for(int k = 0, ri=0; k < process_head_size; k += REG_N, ri += num_P_tiles) {+ #pragma unroll+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO[ri + p].format<float, REG_M, REG_N>();+ #pragma unroll+ for(int r = 0; r < cO.n_rows(); r++) {+ cur_O_f16[r + p*REG_M] = cm_mul<float>(cO.row(r), cur_sum[r + p*REG_M]);+ }}+ int o_offset = worker_offset + k;+ b2dO.set_block_x(o_offset);+ cm_store(b2dO.set_block_y(0), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));+ cm_store(b2dO.set_block_y(REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));}- b2dO.set_block_x(k);- cm_store(b2dO.set_block_y(0), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(0));- cm_store(b2dO.set_block_y(REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(1));+#endif}}diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm b/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cmindex 754645df570..9d7b2c62315 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm@@ -122,6 +122,122 @@ CM_INLINE void process_quantization_per_token(const half* in, uchar* out, uint i// [Step 6] Store the scales and zero points.// Note that [Step 2] and [Step 4] can be skipped for cases with no tail of past tokens.#if KV_CACHE_COMPRESSION == 2+#if (K_HEAD_SIZE == 256) && !defined(CM_HAS_LSC_UNTYPED_2D)+// HEAD_SIZE=256: split in_data into lo/hi halves to stay within the CM compiler's+// 8192-byte matrix-size limit on Xe1.+template <int HEAD_SIZE>+CM_INLINE void process_quantization_per_channel(const half* in,+ uchar* out,+ uint in_offset,+ uint data_offset,+ uint scale_offset,+ uint pitch,+ uint dequant_size,+ uint cur_sub_block_size) {+ constexpr int HALF_HEAD = HEAD_SIZE / 2;+ matrix<half, SUB_BLOCK_SIZE, HALF_HEAD> in_data_lo;+ matrix<half, SUB_BLOCK_SIZE, HALF_HEAD> in_data_hi;+ #pragma unroll+ for (int i = 0; i < cur_sub_block_size; i++) {+ uint offset = (in_offset + i * pitch) * (int)sizeof(half);+ load_kvcache<HALF_HEAD>(in_data_lo.row(dequant_size + i), in, offset);+ load_kvcache<HALF_HEAD>(in_data_hi.row(dequant_size + i), in, offset + HALF_HEAD * (int)sizeof(half));+ }++ vector<half, HEAD_SIZE> max_vals;+ vector<half, HEAD_SIZE> min_vals;+ max_vals.select<HALF_HEAD, 1>(0) = in_data_lo.row(dequant_size);+ max_vals.select<HALF_HEAD, 1>(HALF_HEAD) = in_data_hi.row(dequant_size);+ min_vals = max_vals;+ #pragma unroll+ for (int i = 1; i < cur_sub_block_size; i++) {+ vector<half, HEAD_SIZE> row_data;+ row_data.select<HALF_HEAD, 1>(0) = in_data_lo.row(dequant_size + i);+ row_data.select<HALF_HEAD, 1>(HALF_HEAD) = in_data_hi.row(dequant_size + i);+ max_vals = cm_max<half>(max_vals, row_data);+ min_vals = cm_min<half>(min_vals, row_data);+ }++ if (dequant_size) {+ matrix<uchar, SUB_BLOCK_SIZE, HEAD_SIZE> update_data_u8;+ uint zp_offset = scale_offset + BLOCK_SIZE / SUB_BLOCK_SIZE * HEAD_SIZE * sizeof(half);+ uint update_offset = data_offset - dequant_size * HEAD_SIZE;+ vector<half, HEAD_SIZE> scale_stale;+ vector<half, HEAD_SIZE> zp_stale;+ load_kvcache<HEAD_SIZE>(scale_stale, (half*)out, scale_offset);+ load_kvcache<HEAD_SIZE>(zp_stale, (half*)out, zp_offset);+ #pragma unroll+ for (int i = 0; i < SUB_BLOCK_SIZE; i++) {+ // Use SUB_BLOCK_SIZE in for-loop to avoid cm compiler bug regarding tail loop with data type conversion+ if (i < dequant_size) {+ load_kvcache<HEAD_SIZE>(update_data_u8.row(i), out, update_offset + i * HEAD_SIZE);+ vector<half, HEAD_SIZE> row_data = cm_mul<half>(update_data_u8.row(i) - zp_stale, scale_stale);+ in_data_lo.row(i) = row_data.select<HALF_HEAD, 1>(0);+ in_data_hi.row(i) = row_data.select<HALF_HEAD, 1>(HALF_HEAD);+ max_vals = cm_max<half>(max_vals, row_data);+ min_vals = cm_min<half>(min_vals, row_data);+ }+ }+ }++ vector<float, HEAD_SIZE> qrange = max_vals - min_vals;+ vector<ushort, HEAD_SIZE> mask = (qrange == (half)0.0f);++ // Apply small-range guard introduced by quantize_and_save_by_channel_block_with_requantize in ocl impl,+ // to avoid INF(-INF) caused by scale/zp's out of range of fp16, which cause NaN in dequantization+ vector<float, HEAD_SIZE> min_range = cm_abs<float>(max_vals) * 0.1f;+ vector<float, HEAD_SIZE> adjust_val = cm_max<float>(min_range, 1.0f);+ qrange.merge(qrange + adjust_val, qrange <= min_range);++#ifdef CM_HAS_IEEE_DIV_SQRT+ // scale_vals needs fp32 precision to perform cm_div_ieee (supported by Xe2+) to avoid accuracy loss caused+ // by reciprocal approximation division ('/') instruction+ vector<float, HEAD_SIZE> scale_vals = cm_div_ieee(255.0f, qrange);+#else+ vector<float, HEAD_SIZE> scale_vals = 255.0f / qrange;+#endif+ scale_vals.merge(1.0f, mask);+ vector<half, HEAD_SIZE> zp_vals = cm_mul<half>((0.0f - min_vals), scale_vals);++ if (dequant_size) {+ uint update_offset = data_offset - dequant_size * HEAD_SIZE;+ #pragma unroll+ for (int i = 0; i < SUB_BLOCK_SIZE; i++) {+ if (i < dequant_size) {+ vector<half, HEAD_SIZE> row_data;+ row_data.select<HALF_HEAD, 1>(0) = in_data_lo.row(i);+ row_data.select<HALF_HEAD, 1>(HALF_HEAD) = in_data_hi.row(i);+ vector<half, HEAD_SIZE> quant_data = cm_mul<half>(row_data, scale_vals) + zp_vals;+ quant_data = cm_min<half>(cm_max<half>(quant_data, (half)0.0f), (half)255.0f);+ vector<uchar, HEAD_SIZE> data_u8 = cm_rnde<uchar, HEAD_SIZE>(quant_data);+ store_kvcache<uchar, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + update_offset + i * HEAD_SIZE), 0, data_u8);+ }+ }+ }++ #pragma unroll+ for (int i = 0; i < cur_sub_block_size; i++) {+ vector<half, HEAD_SIZE> row_data;+ row_data.select<HALF_HEAD, 1>(0) = in_data_lo.row(dequant_size + i);+ row_data.select<HALF_HEAD, 1>(HALF_HEAD) = in_data_hi.row(dequant_size + i);+ vector<half, HEAD_SIZE> quant_data = cm_mul<half>(row_data, scale_vals) + zp_vals;+ quant_data = cm_min<half>(cm_max<half>(quant_data, (half)0.0f), (half)255.0f);+ vector<uchar, HEAD_SIZE> data_u8 = cm_rnde<uchar, HEAD_SIZE>(quant_data);+ store_kvcache<uchar, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + data_offset + i * HEAD_SIZE), 0, data_u8);+ }++#ifdef CM_HAS_IEEE_DIV_SQRT+ vector<half, HEAD_SIZE> scale_out = cm_div_ieee(1.0f, scale_vals);+#else+ vector<half, HEAD_SIZE> scale_out = 1.0f / scale_vals;+#endif+ vector<half, HEAD_SIZE> zp_out = zp_vals;+ uint zp_offset = scale_offset + BLOCK_SIZE / SUB_BLOCK_SIZE * HEAD_SIZE * sizeof(half);+ store_kvcache<half, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + scale_offset), 0, scale_out);+ store_kvcache<half, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + zp_offset), 0, zp_out);+}+#else+// HEAD_SIZE != 256 (typically <= 128): single-buffer in_data path, baseline 1e7e9bc.template <int HEAD_SIZE>CM_INLINE void process_quantization_per_channel(const half* in,uchar* out,@@ -219,7 +335,8 @@ CM_INLINE void process_quantization_per_channel(const half* in,store_kvcache<half, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + scale_offset), 0, scale_out);store_kvcache<half, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + zp_offset), 0, zp_out);}-#endif+#endif // (K_HEAD_SIZE == 256) && !defined(CM_HAS_LSC_UNTYPED_2D)+#endif // KV_CACHE_COMPRESSION == 2#if KV_CACHE_COMPRESSION == 0template <int HEAD_SIZE>diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/pa_multi_token.cm b/src/plugins/intel_gpu/src/graph/impls/cm/pa_multi_token.cmindex a4f6446196c..dbefadb13bd 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/pa_multi_token.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/pa_multi_token.cm@@ -117,15 +117,43 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(//# k_cache [kv_len, num_heads, S]//# v_cache [kv_len, num_heads, S]#if KV_CACHE_COMPRESSION- 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 ring_depth = (head_size == 256) ? 3 : 4;+ constexpr uint K_SLM_SIZE = (ring_depth * kv_step * head_size * sizeof(half));+ constexpr uint V_SLM_SIZE = (ring_depth * kv_step * head_size * sizeof(half));constexpr uint Q_SLM_SIZE = 0;-+#ifdef CM_HAS_LSC_UNTYPED_2D+ uint slm_St = 0;+ if constexpr (head_size == 256) {+ constexpr int wg_size_u8 = 16;+ constexpr uint St_SLM_SIZE = wg_size_u8 * kv_step * q_step * sizeof(float);+ cm_slm_init(K_SLM_SIZE + V_SLM_SIZE + Q_SLM_SIZE + St_SLM_SIZE);+ } else {+ cm_slm_init(K_SLM_SIZE + V_SLM_SIZE + Q_SLM_SIZE);+ }+#elsecm_slm_init(K_SLM_SIZE + V_SLM_SIZE + Q_SLM_SIZE);+#endifauto slm_K = cm_slm_alloc(K_SLM_SIZE);auto slm_V = cm_slm_alloc(V_SLM_SIZE);+#ifdef CM_HAS_LSC_UNTYPED_2D+ if constexpr (head_size == 256) {+ constexpr int wg_size_u8 = 16;+ constexpr uint St_SLM_SIZE = wg_size_u8 * kv_step * q_step * sizeof(float);+ slm_St = cm_slm_alloc(St_SLM_SIZE);+ }+#endif+#else+#ifdef CM_HAS_LSC_UNTYPED_2D+ uint slm_St_f16 = 0;+ if constexpr (head_size == 256) {+ constexpr int wg_size_f16 = 16;+ constexpr uint St_SLM_SIZE_F16 = wg_size_f16 * kv_step * q_step * sizeof(float);+ cm_slm_init(St_SLM_SIZE_F16);+ slm_St_f16 = cm_slm_alloc(St_SLM_SIZE_F16);+ }+#endif#endifauto batch = cm_group_id(0);auto h = cm_group_id(1);@@ -167,7 +195,14 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(kv_start = 0;kv_seq_len = subseq_q_len + past_q_lens;- int q_start_local = (block_start_pos - subseq_q_begin) + (wg_local_id * q_step);+ int q_start_local;+ if constexpr (head_size == 256) {+ constexpr int num_worker = 2;+ int team_id = wg_local_id / num_worker;+ q_start_local = (block_start_pos - subseq_q_begin) + team_id * q_step;+ } else {+ q_start_local = (block_start_pos - subseq_q_begin) + (wg_local_id * q_step);+ }#if KV_CACHE_COMPRESSION// U8 path uses workgroup-level barriers in `pa_lsc_u8`, so every lane in// the workgroup must participate. For out-of-range lanes, pass q_tokens_in_tile_sg=0@@ -184,13 +219,22 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(q_tokens_in_tile_sg = 0;}#else- if (q_start_local >= subseq_q_len) {- return;+ // Threads with zero valid query tokens can early exit if there is no barrier.+ bool thread_active_f16 = (q_start_local < subseq_q_len);+ if constexpr (head_size != 256) {+ if (!thread_active_f16) {+ return;+ }}- q_start_sg = subseq_q_begin + q_start_local;- q_tokens_in_tile_sg = q_step;- if (q_start_local + q_tokens_in_tile_sg > subseq_q_len) {- q_tokens_in_tile_sg = subseq_q_len - q_start_local;+ if (thread_active_f16) {+ q_start_sg = subseq_q_begin + q_start_local;+ q_tokens_in_tile_sg = q_step;+ if (q_start_local + q_tokens_in_tile_sg > subseq_q_len) {+ q_tokens_in_tile_sg = subseq_q_len - q_start_local;+ }+ } else {+ q_start_sg = subseq_q_begin + subseq_q_len;+ q_tokens_in_tile_sg = 0;}#endif@@ -266,6 +310,9 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(pa_lsc_u8<is_causal, num_heads, num_kv_heads, head_size, 0>(slm_K,slm_V,+#ifdef CM_HAS_LSC_UNTYPED_2D+ slm_St,+#endifwg_local_id,local_size,q_start_local, //q_start within current subsequence,@@ -298,6 +345,9 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(uint v_offset_bytes = kv_offset * sizeof(half);#endifpa_kernel_lsc_prefetch_f16<is_causal, num_heads, num_kv_heads, head_size, 0, 16>(+#ifdef CM_HAS_LSC_UNTYPED_2D+ slm_St_f16,+#endifwg_local_id,q_start_local, //q_start within current subsequence,kv_stop,diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cppindex 192c23eb83d..a402a938791 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cpp@@ -130,7 +130,7 @@ public:const uint32_t block_wg_n = XAttentionEstimateGeneratorBase::get_block_wg_n(params);const uint32_t block_wg_m = XAttentionEstimateGeneratorBase::get_block_wg_m(params);const size_t heads_num = desc->heads_num;- const size_t merged_q_num = PagedAttentionGeneratorMultiToken::get_wg_seq_len(params) / block_size;+ const size_t merged_q_num = XAttentionEstimateGeneratorBase::get_wg_seq_len(params) / block_size;const size_t sum_per_token_in_block = block_size / STRIDE;const size_t k_block_in_group = block_wg_n / sum_per_token_in_block;const size_t sizeof_softmax = sizeof(float);@@ -729,6 +729,13 @@ private:return block_size_128;}+ // head_size=256 partition path on Xe2+ shrinks wg_seq_len to num_team * q_step = 128.+ // SPARSE_BLOCK_SIZE must not exceed wg_seq_len, otherwise blocks_per_wg = 0 in the+ // kernel's sparse-mask indexing.+ if (desc->k_head_size == 256) {+ return block_size_128;+ }+xattn_block_size = (xattn_block_size == block_size_128 || xattn_block_size == block_size_256) ? xattn_block_size : block_size_256;return xattn_block_size;}diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cppindex 633187798c2..4d615fc1191 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.cpp@@ -756,7 +756,7 @@ DispatchDataFunc XAttentionEstimateFindBlock::get_dispatch_data_func() const {JitConstants XAttentionEstimatePostProc::get_jit_constants(const kernel_impl_params& params) const {auto jit = XAttentionEstimateGeneratorBase::get_jit_constants(params);- jit.make("MERGED_Q_NUM", PagedAttentionGeneratorMultiToken::get_wg_seq_len(params) / _xattn_block_size);+ jit.make("MERGED_Q_NUM", XAttentionEstimateGeneratorBase::get_wg_seq_len(params) / _xattn_block_size);return jit;}diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.hppindex 305fcb2b467..8bf062bbeed 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention_gen.hpp@@ -200,6 +200,12 @@ public:}static size_t get_wg_seq_len(const kernel_impl_params& params) {+ const auto desc = params.typed_desc<paged_attention>();+ const auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;+ if (desc->k_head_size == 256 && xe_arch >= 2) {+ constexpr size_t num_team = 8;+ return num_team * get_q_step(params);+ }return _wg_size * get_q_step(params);}@@ -261,6 +267,12 @@ public:return get_block_sg_n(params) * SG_N;}+ // XAttention metadata is built at WG-level Q-tile granularity, ignoring the+ // head_size=256 worker subdivision used inside the multi-token kernel.+ static size_t get_wg_seq_len(const kernel_impl_params& params) {+ return PagedAttentionGeneratorMultiToken::_wg_size * PagedAttentionGeneratorMultiToken::get_q_step(params);+ }+[[nodiscard]] std::string get_build_options(const RuntimeParams& params) const override {return KernelGenerator::get_build_options(params) + get_pa_build_options();}diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cppindex b30e34b4b9c..83b0c0039f7 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp@@ -512,6 +512,52 @@ INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention, xattention_test, ::testing::Valuespaged_attention_test_params{ {{5, 3}, {7, 3}, {9, 3}}, 4, 2, 64, 64, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f, 0.9f, 0.9f}, std::vector<int>{128, 128, 128} }, // multi-subsequence by-channel divergent [basic/112]}));+INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention_head_size, xattention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{+ //////////////////////////////////////////////////////////////////////////////// single-seq + bypass xattention ////////////////////////////////////////////////////////////////////////+ // single-seq prefill + uncompressed cache+ paged_attention_test_params{ {{32, 0}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },+ // single-seq generate + uncompressed cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },+ // single-seq prefill + by_token i8 cache+ paged_attention_test_params{ {{32, 0}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },+ // single-seq generate + by_token i8 cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },+ // single-seq prefill + by_channel i8 cache+ paged_attention_test_params{ {{32, 0}}, 4, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },+ // single-seq generate + by_channel i8 cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.0f}, std::vector<int>{128} },++ //////////////////////////////////////////////////////////////////////////////// single-seq + enable xattention ////////////////////////////////////////////////////////////////////////+ // single-seq prefill + uncompressed cache+ paged_attention_test_params{ {{32, 0}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },+ // single-seq generate + uncompressed cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },+ // single-seq prefill + by_token i8 cache+ paged_attention_test_params{ {{32, 0}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },+ // single-seq generate + by_token i8 cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },+ // single-seq prefill + by_channel i8 cache+ paged_attention_test_params{ {{32, 0}}, 4, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },+ // single-seq generate + by_channel i8 cache+ paged_attention_test_params{ {{1, 31}}, 2, 2, 256, 256, 256, 0, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },++ //////////////////////////////////////////////////////////////////////////////// multi-seq + bypass xattention ////////////////////////////////////////////////////////////////////////+ // multi-seq prefill-only+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.f, 100.f}, std::vector<int>{128, 128} },+ // multi-seq generate-only+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.f, 100.f}, std::vector<int>{128, 128} },+ // multi-seq mixed+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{100.f, 100.f, 100.f}, std::vector<int>{128, 128, 128} },++ //////////////////////////////////////////////////////////////////////////////// multi-seq + enable xattention ////////////////////////////////////////////////////////////////////////+ // multi-seq prefill-only+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f, 0.9f}, std::vector<int>{128, 128} },+ // multi-seq generate-only+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f, 0.9f}, std::vector<int>{128, 128} },+ // multi-seq mixed+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 4, 2, 256, 256, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_CHANNEL, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f, 0.9f, 0.9f}, std::vector<int>{128, 128, 128} },+}));+INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention_block_size, xattention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{// Valid valuespaged_attention_test_params{ {{1024, 0}}, 2, 2, 64, 64, 256, 0, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2, false, 0, {}, true, std::vector<float>{0.9f}, std::vector<int>{128} },