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 by-channel quantized kvcache for Xattetion supported Page Attention (#35205)
Chen Xu23d40629424e
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 2748f1aefc3..715da56c06a 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@@ -4,7 +4,7 @@#ifndef CM_HAS_LSC_UNTYPED_2D-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONtemplate<bool use_causal_mask, int num_heads, int num_kv_heads, int head_size, int is_q_fused = 0>void pa_lsc_u8(uint slm_K,@@ -47,6 +47,10 @@ void pa_lsc_u8(static_assert(q_step == REG_N);static_assert(kv_step == REG_K);static_assert(head_size % REG_N == 0, "head_size must be divisible by REG_N");+#if KV_CACHE_COMPRESSION == 2+ static_assert(SUB_BLOCK_SIZE % 16 == 0, "SUB_BLOCK_SIZE must be divisible by 16");+ static_assert(CMPA_BLOCK_SZ % SUB_BLOCK_SIZE == 0, "CMPA_BLOCK_SZ must be divisible by SUB_BLOCK_SIZE");+#endifif (q_tokens_left < 0) q_tokens_left = 0;if (q_tokens_left > q_step) q_tokens_left = q_step;@@ -84,7 +88,12 @@ void pa_lsc_u8(rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);}}- constexpr int quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE+4) * CMPA_BLOCK_SZ * sizeof(uint8_t);+#if KV_CACHE_COMPRESSION == 1+ constexpr int k_quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE + 4) * CMPA_BLOCK_SZ * sizeof(uint8_t);+#else+ constexpr int k_quan_blk_stride = CMFLA_NUM_KV_HEADS * CMFLA_HEAD_SIZE * (CMPA_BLOCK_SZ + CMPA_BLOCK_SZ / SUB_BLOCK_SIZE * 4) * sizeof(uint8_t);+#endif+ constexpr int v_quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE + 4) * CMPA_BLOCK_SZ * sizeof(uint8_t);int causal_left = q_start + past_lens;constexpr uint slm_buff_size = kv_step * head_size * sizeof(half);@@ -112,8 +121,24 @@ void pa_lsc_u8(}#endifauto cur_block_id = block_indices[kv_pos / CMPA_BLOCK_SZ];- uint32_t dscale_offset = cur_block_id*quan_blk_stride + \- CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) + kv_pos%CMPA_BLOCK_SZ*sizeof(half);+#if KV_CACHE_COMPRESSION == 1+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos % CMPA_BLOCK_SZ * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ * sizeof(half);+#else+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ (kv_pos % CMPA_BLOCK_SZ) / SUB_BLOCK_SIZE * head_size * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ / SUB_BLOCK_SIZE * head_size * sizeof(half);+#endif+ uint32_t v_dscale_offset =+ cur_block_id * v_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos % CMPA_BLOCK_SZ * 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;vector<half, kv_step> dscale;@@ -122,13 +147,19 @@ void pa_lsc_u8(slm_buff_id_write ++;if (wg_local_id < local_size/2) {- cm_svm_block_read(reinterpret_cast<svmptr_t>( k_cache_base + dscale_offset), dscale);- cm_svm_block_read(reinterpret_cast<svmptr_t>( k_cache_base + dscale_offset + CMPA_BLOCK_SZ*sizeof(half)), zp);+#if KV_CACHE_COMPRESSION == 1+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset), zp);+#endifmatrix<half, kv_step, REG_K> kmat;auto quanKmat = kmat.format<half, 2, kv_step * REG_K/2>()[1].format<uint8_t, kv_step, REG_K>();for(int k = REG_K*wg_local_id; k < head_size; k += REG_K*(local_size/2)) {- auto k_base = reinterpret_cast<svmptr_t>((int8_t*)k_cache_base + cur_block_id * quan_blk_stride + (kv_pos % CMPA_BLOCK_SZ) * kv_pitch + k);+ auto k_base = reinterpret_cast<svmptr_t>((int8_t*)k_cache_base + cur_block_id * k_quan_blk_stride + (kv_pos % CMPA_BLOCK_SZ) * kv_pitch + k);+#if KV_CACHE_COMPRESSION == 2+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset + k * sizeof(half)), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset + k * sizeof(half)), zp);+#endif#pragma unrollfor(int r = 0; r < kv_step; r++) {cm_svm_block_read(k_base + r * kv_pitch, quanKmat.row(r));@@ -143,8 +174,13 @@ void pa_lsc_u8(*/#pragma unrollfor(int r = 0; r < kv_step; r++) {- kmat[r] = quanKmat[r]-zp[r];+#if KV_CACHE_COMPRESSION == 1+ kmat[r] = quanKmat[r] - zp[r];kmat[r] = cm_mul<half>(kmat[r], dscale[r]);+#else+ kmat[r] = quanKmat[r] - zp;+ kmat[r] = cm_mul<half>(kmat[r], dscale);+#endif}//clear unused data to 0.for(int r = kv_step-1; r >= kv_left; r--)@@ -155,8 +191,8 @@ void pa_lsc_u8(// dscale/zp are half[kv_step] => 32 bytes each when kv_step=16constexpr int half16_bytes = 16 * sizeof(half); // 32constexpr int u32_per_half16 = half16_bytes / sizeof(uint); // 8- uint ds_off = v_cache_stateful_offset_bytes + dscale_offset;- uint zp_off = v_cache_stateful_offset_bytes + dscale_offset + CMPA_BLOCK_SZ*sizeof(half);+ uint ds_off = v_cache_stateful_offset_bytes + v_dscale_offset;+ uint zp_off = v_cache_stateful_offset_bytes + v_zp_offset;auto ds_u32 = cm_load<uint, u32_per_half16>(v_cache_stateful, ds_off);auto zp_u32 = cm_load<uint, u32_per_half16>(v_cache_stateful, zp_off);dscale.format<uint>() = ds_u32;@@ -172,7 +208,7 @@ void pa_lsc_u8(#pragma unrollfor (int r = 0; r < REG_K; r++) {uint elem_off_bytes =- cur_block_id * quan_blk_stride ++ cur_block_id * v_quan_blk_stride +(kv_pos % CMPA_BLOCK_SZ) * kv_pitch +r * kv_pitch +k;diff --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 e7531c2d53e..7d829e4f0be 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@@ -12,11 +12,11 @@#error "SPARSE_BLOCK_SIZE must be defined"#endif-#ifndef CMPA_KVCACHE_U8-#error "CMPA_KVCACHE_U8 must be defined"+#ifndef KV_CACHE_COMPRESSION+#error "KV_CACHE_COMPRESSION must be defined"#endif-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSION#define OPTIMIZED_SPARSE_PIPELINE ( \(SPARSE_BLOCK_SIZE == CMPA_WG_SEQ_LEN) && \(SPARSE_BLOCK_SIZE == 128 || SPARSE_BLOCK_SIZE == 256) && \@@ -34,13 +34,13 @@"This optimized pipeline requires SPARSE_BLOCK_SIZE to be the same as CMPA_WG_SEQ_LEN");static_assert(SPARSE_BLOCK_SIZE == 128 || SPARSE_BLOCK_SIZE == 256,"This optimized pipeline assumes SPARSE_BLOCK_SIZE is 128 or 256 to efficiently index sparse blocks");-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONstatic_assert((CMPA_BLOCK_SZ % SPARSE_BLOCK_SIZE) == 0,"U8 optimized sparse pipeline assumes CMPA_BLOCK_SZ is divisible by SPARSE_BLOCK_SIZE to efficiently index kvcache blocks");#endif#endif-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONtemplate<bool use_causal_mask, int num_heads, int num_kv_heads, int head_size, int is_q_fused = 0>void pa_lsc_u8(uint slm_K,@@ -83,6 +83,10 @@ void pa_lsc_u8(auto q_tokens_left = q_len;static_assert(q_step == REG_N);static_assert(kv_step == REG_K);+#if KV_CACHE_COMPRESSION == 2+ static_assert(SUB_BLOCK_SIZE % 16 == 0, "SUB_BLOCK_SIZE must be divisible by 16");+ static_assert(CMPA_BLOCK_SZ % SUB_BLOCK_SIZE == 0, "CMPA_BLOCK_SZ must be divisible by SUB_BLOCK_SIZE");+#endifif (q_tokens_left < 0) q_tokens_left = 0;if (q_tokens_left > q_step) q_tokens_left = q_step;@@ -108,8 +112,12 @@ void pa_lsc_u8(lsc::block_2d_desc<uint8_t, 1, REG_K, REG_N> b2dV(v_cache_base, CMPA_BLOCK_SZ - 1, head_size * sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);- constexpr int quan_blk_stride =- CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE + 4) * CMPA_BLOCK_SZ * sizeof(uint8_t);+#if KV_CACHE_COMPRESSION == 1+ constexpr int k_quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE + 4) * CMPA_BLOCK_SZ * sizeof(uint8_t);+#else+ constexpr int k_quan_blk_stride = CMFLA_NUM_KV_HEADS * CMFLA_HEAD_SIZE * (CMPA_BLOCK_SZ + CMPA_BLOCK_SZ / SUB_BLOCK_SIZE * 4) * sizeof(uint8_t);+#endif+ constexpr int v_quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE + 4) * CMPA_BLOCK_SZ * sizeof(uint8_t);int causal_left = q_start + past_lens;@@ -162,10 +170,24 @@ void pa_lsc_u8(int kv_left = kv_step;if (kv_pos + kv_step > kv_stop) kv_left = kv_stop - kv_pos;- uint32_t dscale_offset =- cur_block_id * quan_blk_stride ++#if KV_CACHE_COMPRESSION == 1+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride +CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) +kv_pos_in_block * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ * sizeof(half);+#else+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos_in_block / SUB_BLOCK_SIZE * head_size * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ / SUB_BLOCK_SIZE * head_size * sizeof(half);+#endif+ uint32_t v_dscale_offset =+ cur_block_id * v_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos_in_block * sizeof(half);+ uint32_t v_zp_offset = v_dscale_offset + CMPA_BLOCK_SZ * sizeof(half);// Advance write id exactly when we actually stage dataslm_buff_id_write++;@@ -175,23 +197,34 @@ void pa_lsc_u8(if (wg_local_id < local_size / 2) {// ---- Load K scales / zps ----- cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + dscale_offset), dscale);- cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + dscale_offset + CMPA_BLOCK_SZ * sizeof(half)), zp);+#if KV_CACHE_COMPRESSION == 1+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset), zp);+#endifmatrix<half, kv_step, REG_K> kmat;auto quanKmat =kmat.format<half, 2, kv_step * REG_K / 2>()[1].format<uint8_t, kv_step, REG_K>();- b2dK.set_base_ptr(reinterpret_cast<uint8_t*>(k_cache_base + cur_block_id * quan_blk_stride));+ b2dK.set_base_ptr(reinterpret_cast<uint8_t*>(k_cache_base + cur_block_id * k_quan_blk_stride));b2dK.set_block_y(kv_pos_in_block);for (int k = REG_K * wg_local_id; k < head_size; k += REG_K * (local_size / 2)) {+#if KV_CACHE_COMPRESSION == 2+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset + k * sizeof(half)), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset + k * sizeof(half)), zp);+#endifcm_load<lsc::Normal>(quanKmat.format<uint8_t>(), b2dK.set_block_x(k));#pragma unrollfor (int r = 0; r < kv_step; r++) {+#if KV_CACHE_COMPRESSION == 1kmat[r] = quanKmat[r] - zp[r];kmat[r] = cm_mul<half>(kmat[r], dscale[r]);+#else+ kmat[r] = quanKmat[r] - zp;+ kmat[r] = cm_mul<half>(kmat[r], dscale);+#endif}if (kv_left < kv_step) {@@ -202,15 +235,15 @@ void pa_lsc_u8(}} else {// ---- Load V scales / zps ----- cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + dscale_offset), dscale);- cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + dscale_offset + CMPA_BLOCK_SZ * sizeof(half)), zp);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + v_dscale_offset), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + v_zp_offset), zp);matrix<half, REG_K / 2, REG_N * 2> VmatVNNI;matrix<half, REG_K, REG_N> Vmat;auto quanVmat =Vmat.format<half, 2, REG_K * REG_N / 2>().row(1).format<uint8_t, REG_K, REG_N>();- b2dV.set_base_ptr(reinterpret_cast<uint8_t*>(v_cache_base + cur_block_id * quan_blk_stride));+ b2dV.set_base_ptr(reinterpret_cast<uint8_t*>(v_cache_base + cur_block_id * v_quan_blk_stride));b2dV.set_block_y(kv_pos_in_block);#pragma unroll@@ -361,10 +394,24 @@ void pa_lsc_u8(#endifint cur_block_id = block_indices[kv_pos / CMPA_BLOCK_SZ];int kv_pos_in_block = kv_pos - (kv_pos / CMPA_BLOCK_SZ) * CMPA_BLOCK_SZ;- uint32_t dscale_offset =- cur_block_id * quan_blk_stride ++#if KV_CACHE_COMPRESSION == 1+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos_in_block * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ * sizeof(half);+#else+ uint32_t k_dscale_offset =+ cur_block_id * k_quan_blk_stride ++ CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) ++ kv_pos_in_block / SUB_BLOCK_SIZE * head_size * sizeof(half);+ uint32_t k_zp_offset = k_dscale_offset + CMPA_BLOCK_SZ / SUB_BLOCK_SIZE * head_size * sizeof(half);+#endif+ uint32_t v_dscale_offset =+ cur_block_id * v_quan_blk_stride +CMPA_BLOCK_SZ * head_size * sizeof(uint8_t) +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;vector<half, kv_step> dscale;@@ -374,22 +421,33 @@ void pa_lsc_u8(slm_buff_id_write++;if (wg_local_id < local_size / 2) {- cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + dscale_offset), dscale);- cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + dscale_offset + CMPA_BLOCK_SZ * sizeof(half)), zp);+#if KV_CACHE_COMPRESSION == 1+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset), zp);+#endifmatrix<half, kv_step, REG_K> kmat;auto quanKmat =kmat.format<half, 2, kv_step * REG_K / 2>()[1].format<uint8_t, kv_step, REG_K>();- b2dK.set_base_ptr(reinterpret_cast<uint8_t*>(k_cache_base + cur_block_id * quan_blk_stride));+ b2dK.set_base_ptr(reinterpret_cast<uint8_t*>(k_cache_base + cur_block_id * k_quan_blk_stride));b2dK.set_block_y(kv_pos_in_block);for (int k = REG_K * wg_local_id; k < head_size; k += REG_K * (local_size / 2)) {+#if KV_CACHE_COMPRESSION == 2+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_dscale_offset + k * sizeof(half)), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(k_cache_base + k_zp_offset + k * sizeof(half)), zp);+#endifcm_load<lsc::Normal>(quanKmat.format<uint8_t>(), b2dK.set_block_x(k));#pragma unrollfor (int r = 0; r < kv_step; r++) {+#if KV_CACHE_COMPRESSION == 1kmat[r] = quanKmat[r] - zp[r];kmat[r] = cm_mul<half>(kmat[r], dscale[r]);+#else+ kmat[r] = quanKmat[r] - zp;+ kmat[r] = cm_mul<half>(kmat[r], dscale);+#endif}for (int r = kv_step - 1; r >= kv_left; r--) kmat[r] = 0;@@ -397,14 +455,14 @@ void pa_lsc_u8(cm_slm_block_write(slm_K, slm_offset + k * kv_step * sizeof(half), kmat.format<half>());}} else {- cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + dscale_offset), dscale);- cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + dscale_offset + CMPA_BLOCK_SZ * sizeof(half)), zp);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + v_dscale_offset), dscale);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(v_cache_base + v_zp_offset), zp);matrix<half, REG_K / 2, REG_N * 2> VmatVNNI;matrix<half, REG_K, REG_N> Vmat;auto quanVmat =Vmat.format<half, 2, REG_K * REG_N / 2>().row(1).format<uint8_t, REG_K, REG_N>();- b2dV.set_base_ptr(reinterpret_cast<uint8_t*>(v_cache_base + cur_block_id * quan_blk_stride));+ b2dV.set_base_ptr(reinterpret_cast<uint8_t*>(v_cache_base + cur_block_id * v_quan_blk_stride));b2dV.set_block_y(kv_pos_in_block);#pragma unrolldiff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/estimate.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/estimate.hppindex c897a9d5b8b..14985a0c947 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/estimate.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/estimate.hpp@@ -337,7 +337,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,uint block_idx = (uint)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * STRIDE / KV_BLOCK_SIZE;uint max_block_idx = (uint)(N * STRIDE + KV_BLOCK_SIZE - 1) / KV_BLOCK_SIZE - 1;block_idx = MYMIN(block_idx, max_block_idx);-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)uint offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char));#ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<int, 1, KEY_LINES_PER_LOAD, 8> desc_b0{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(char) - 1), (uint)(K * sizeof(char) - 1),@@ -381,8 +381,12 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,// N[:]xK[0:32] --> 16 * 1 regsmatrix<int, KEY_LINES_PER_LOAD, 8> b0_up_s8, b0_down_s8, b1_up_s8, b1_down_s8; //ping pongmatrix<half, REG_N, BLOCK_REG_B> b0; // after dequant+ #if (KV_CACHE_COMPRESSION == 1)matrix<half, REG_N, KEY_LINES_PER_LOAD * 2> scales, zps;matrix<half, REG_N, KEY_LINES_PER_LOAD*STRIDE> scales_block, zps_block;+ #else+ matrix<half, REG_N, BLOCK_REG_B> scales_block, zps_block;+ #endif#elseuint offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));#ifdef CM_HAS_LSC_UNTYPED_2D@@ -438,8 +442,9 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,}// load b: N[0:16]xK[0:16]-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0){+ #if (KV_CACHE_COMPRESSION == 1)#ifdef CM_HAS_LSC_UNTYPED_2D//16 * 16 half, 1D->2Dmatrix<half, KEY_LINES_PER_LOAD, STRIDE> tmp_scale, tmp_zp; //KV_BLOCK_SIZE/2@@ -480,6 +485,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,zps_block[1].format<half, 16, 8>().select<8, 2, 8, 1>(1) = tmp_zp.format<half, 4, 32>().select<4, 1, 16, 2>(0, 1);#endif+ #endif}#ifndef CM_HAS_LSC_UNTYPED_2Dvector<uint, KEY_LINES_PER_LOAD> offsets_0;@@ -533,7 +539,8 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,#endif}-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)+ #if (KV_CACHE_COMPRESSION == 1)auto dec = [&](vector<int, KEY_LINES_PER_LOAD*4> B0_i8, vector<int, KEY_LINES_PER_LOAD*4> B1_i8, matrix_ref<half, REG_N, BLOCK_REG_B> B0) {#pragma unrollfor (int n = 0; n < REG_N; n++) {@@ -553,6 +560,102 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,}}};+ #else+ #ifdef CM_HAS_LSC_UNTYPED_2D+ auto dec = [&](vector<int, KEY_LINES_PER_LOAD * 4> B0_i8, vector<int, KEY_LINES_PER_LOAD * 4> B1_i8, matrix_ref<half, REG_N, BLOCK_REG_B> B0) {+#pragma unroll+ for (int n = 0; n < REG_N; n++) {+ auto b = B0[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+#pragma unroll+ for (int m = 0; m < BLOCK_REG_K / 2; m++) {+ auto b_row = b[m];+ vector<ushort, BLOCK_REG_N> d0;+ if (n == 0)+ d0 = B0_i8.format<ushort, 4, BLOCK_REG_N * 2>()[m / 2].select<BLOCK_REG_N, 2>(m % 2);+ else+ d0 = B1_i8.format<ushort, 4, BLOCK_REG_N * 2>()[m / 2].select<BLOCK_REG_N, 2>(m % 2);+ b_row.format<ushort>() = d0.format<uchar>();+ b_row *= half{32768.0};+ b_row *= half{512.0};+ auto scale_mat = scales_block[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ auto zp_mat = zps_block[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ b_row = (b_row - zp_mat.row(m)) * scale_mat.row(m);+ }+ }+ };++ uint zp_offset0 = scale_offset0 + 16 * HEAD_SIZE * sizeof(half);+ uint zp_offset1 = scale_offset1 + 16 * HEAD_SIZE * sizeof(half);+ auto load_scale_zp = [&](uint channel_base) {+ uint scale_base0 = scale_offset0 + channel_base * sizeof(half);+ lsc::block_2d_desc<int, 1, BLOCK_REG_K / 2, BLOCK_REG_N> desc_scale{ key_cache + scale_base0,+ BLOCK_REG_K - 1, (uint)(BLOCK_REG_N * sizeof(half) - 1), (uint)(HEAD_SIZE * sizeof(half) - 1), 0, 0 };+ matrix<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2> tmp_scale, tmp_zp;++ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_scale.format<int>(), desc_scale);+ scales_block[0].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_scale.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();++ uint zp_base0 = zp_offset0 + channel_base * sizeof(half);+ desc_scale.set_base(key_cache + zp_base0);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_zp.format<int>(), desc_scale);+ zps_block[0].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_zp.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();++ uint scale_base1 = scale_offset1 + channel_base * sizeof(half);+ desc_scale.set_base(key_cache + scale_base1);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_scale.format<int>(), desc_scale);+ scales_block[1].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_scale.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ uint zp_base1 = zp_offset1 + channel_base * sizeof(half);+ desc_scale.set_base(key_cache + zp_base1);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_zp.format<int>(), desc_scale);+ zps_block[1].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_zp.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ };+ #else+ auto dec = [&](vector<int, KEY_LINES_PER_LOAD * 4> B0_i8, vector<int, KEY_LINES_PER_LOAD * 4> B1_i8, matrix_ref<half, REG_N, BLOCK_REG_B> B0) {+#pragma unroll+ for (int n = 0; n < REG_N; n++) {+ auto b = B0[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+#pragma unroll+ for (int m = 0; m < BLOCK_REG_K / 2; m++) {+ auto b_row = b[m];+ vector<ushort, BLOCK_REG_N> d0;+ if (n == 0)+ d0 = B0_i8.format<ushort, 4, BLOCK_REG_N * 2>()[m / 2].select<BLOCK_REG_N, 2>(m % 2);+ else+ d0 = B1_i8.format<ushort, 4, BLOCK_REG_N * 2>()[m / 2].select<BLOCK_REG_N, 2>(m % 2);+ b_row.format<ushort>() = d0.format<uchar>();+ b_row *= half{32768.0};+ b_row *= half{512.0};+ auto scale_mat = scales_block[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ auto zp_mat = zps_block[n].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ b_row = (b_row - zp_mat.row(m)) * scale_mat.row(m);+ }+ }+ };++ uint zp_offset0 = scale_offset0 + 16 * HEAD_SIZE * sizeof(half);+ uint zp_offset1 = scale_offset1 + 16 * HEAD_SIZE * sizeof(half);+ auto load_scale_zp = [&](uint channel_base) {+ matrix<half, KEY_LINES_PER_LOAD, BLOCK_REG_K> tmp_scale, tmp_zp;+ uint channel_base_bytes = channel_base * (uint)sizeof(half);++ vector<uint, KEY_LINES_PER_LOAD> offsets_scale0_ch = offsets_scale0 + channel_base_bytes;+ tmp_scale.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_scale0_ch);+ scales_block[0].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_scale.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();++ vector<uint, KEY_LINES_PER_LOAD> offsets_zp0_ch = offsets_scale0_ch + (zp_offset0 - scale_offset0);+ tmp_zp.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_zp0_ch);+ zps_block[0].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_zp.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();++ vector<uint, KEY_LINES_PER_LOAD> offsets_scale1_ch = offsets_scale1 + channel_base_bytes;+ tmp_scale.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_scale1_ch);+ scales_block[1].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_scale.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();++ vector<uint, KEY_LINES_PER_LOAD> offsets_zp1_ch = offsets_scale1_ch + (zp_offset1 - scale_offset1);+ tmp_zp.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_zp1_ch);+ zps_block[1].format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>() = tmp_zp.format<half, BLOCK_REG_K / 2, BLOCK_REG_N * 2>();+ };+ #endif+ #endif#endifauto dot = [&](matrix<half, REG_M, BLOCK_REG_A> A, matrix<half, REG_N, BLOCK_REG_B> B) {@@ -567,7 +670,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,};for (uint s = 0; s < STRIDE; s++) {-#if USE_INT8+#if (KV_CACHE_COMPRESSION == 1)auto tmp = scales_block[0].select<KEY_LINES_PER_LOAD, 1>(s * KEY_LINES_PER_LOAD);scales[0].select<KEY_LINES_PER_LOAD, 2>(0) = tmp;scales[0].select<KEY_LINES_PER_LOAD, 2>(1) = scales[0].select<KEY_LINES_PER_LOAD, 2>(0);@@ -583,6 +686,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,#endif#pragma unrollfor (uint hs = 0; hs < HEAD_SIZE / BLOCK_WG_K; hs++) {+ uint channel_base = hs * BLOCK_WG_K;if constexpr (IS64) {// --------------------------------------------- unroll 0 ? -----------------------------// prefetch@@ -620,7 +724,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,#endif// load b: N[0:16*2]xK[16:32]-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)#ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1_up_s8.format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1_down_s8.format<int>(), desc_b1);@@ -628,8 +732,15 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,b1_up_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);b1_down_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);#endif+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0);+ channel_base += BLOCK_REG_K;+ dot(a0, b0);+ #endif#else#ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[0].format<int>(), desc_b0);@@ -662,9 +773,16 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);#endif-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(KEY_LINES_PER_LOAD*4), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(KEY_LINES_PER_LOAD*4), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else#ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);@@ -709,7 +827,7 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,#endif// load b: N[0:16*2]xK[32:64]-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)#ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);@@ -717,8 +835,15 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,b0_up_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);b0_down_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);#endif+ #if (KV_CACHE_COMPRESSION == 1)dec(b1_up_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(), b1_down_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b1_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b1_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else#ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[0].format<int>(), desc_b0);@@ -758,9 +883,17 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,off_a += BLOCK_REG_K * sizeof(half);}#endif-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)+ #if (KV_CACHE_COMPRESSION == 1)dec(b1_up_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(KEY_LINES_PER_LOAD*4), b1_down_s8.format<int>().select<KEY_LINES_PER_LOAD*4, 1>(KEY_LINES_PER_LOAD*4), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b1_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b1_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif+#else#ifdef CM_HAS_LSC_UNTYPED_2D// load b: N[0:16*2]xK[16:32]@@ -789,12 +922,19 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,// load a: M[0:16*4]xK[0:16]cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)// load b: N[0:16*2]xK[0:32]cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<64, 1>(), b0_down_s8.format<int>().select<64, 1>(), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else// load b: N[0:16*2]xK[0:16]cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);@@ -823,9 +963,16 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,} else {desc_a.set_block_x(desc_a.get_block_x() + 16);}-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<64, 1>(64), b0_down_s8.format<int>().select<64, 1>(64), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else// load b: N[0:16*2]xK[16:32]cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);@@ -846,12 +993,19 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,// load a: M[0:16*4]xK[0:16]cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)// load b once: N[0:16*2]xK[0:32], then split into 2 phasesb0_up_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);b0_down_s8.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else// load b: N[0:16*2]xK[0:16]b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);@@ -880,9 +1034,16 @@ CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,off_a += PHASE_SIZE * sizeof(half);}-#if USE_INT8+#if (KV_CACHE_COMPRESSION != 0)+ #if (KV_CACHE_COMPRESSION == 1)dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0);dot(a0, b0);+ #else+ load_scale_zp(channel_base);+ dec(b0_up_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0_down_s8.format<int>().select<KEY_LINES_PER_LOAD * 4, 1>(KEY_LINES_PER_LOAD * 4), b0);+ dot(a0, b0);+ channel_base += BLOCK_REG_K;+ #endif#else// load b: N[0:16*2]xK[16:32]b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);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 c1c192d7adc..ab68fe1b7de 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@@ -24,6 +24,10 @@constexpr uint wg_size = WG_SIZE;+#if KV_CACHE_COMPRESSION == 2+constexpr uint MAX_SEQS = 256;+#endif+template <int HEAD_SIZE>CM_INLINE void load_kvcache(vector_ref<half, HEAD_SIZE> kv_data, const half* kv_ptr [[type("svmptr_t")]], uint offset) {if constexpr (HEAD_SIZE == 16 || HEAD_SIZE == 32 || HEAD_SIZE == 64 || HEAD_SIZE == 128) {@@ -40,6 +44,21 @@ CM_INLINE void load_kvcache(vector_ref<half, HEAD_SIZE> kv_data, const half* kv_}}+template <int HEAD_SIZE>+CM_INLINE void load_kvcache(vector_ref<uchar, HEAD_SIZE> kv_data, const uchar* kv_ptr [[type("svmptr_t")]], uint offset) {+ if constexpr (HEAD_SIZE == 16 || HEAD_SIZE == 32 || HEAD_SIZE == 64 || HEAD_SIZE == 128) {+ kv_data.format<int>() = cm_ptr_load<int, HEAD_SIZE / 4>((int*)kv_ptr, offset);+ } else if constexpr (HEAD_SIZE == 96) {+ kv_data.select<64, 1>(0).format<int>() = cm_ptr_load<int, 16>((int*)kv_ptr, offset);+ kv_data.select<32, 1>(64).format<int>() = cm_ptr_load<int, 8>((int*)kv_ptr, offset + 16 * sizeof(int));+ } else {+ #pragma unroll+ for(int i = 0; i < HEAD_SIZE / 16; i++) {+ kv_data.select<16, 1>(16*i).format<int>() = cm_ptr_load<int, 4>((int*)kv_ptr, offset + i * 4 * sizeof(int));+ }+ }+}+template <typename T, int HEAD_SIZE>CM_INLINE void store_kvcache(const svmptr_t kv_cache [[type("svmptr_t")]], uint offset, vector_ref<T, HEAD_SIZE> kv_data) {if constexpr(std::is_same<T, half>::value) {@@ -71,28 +90,207 @@ CM_INLINE void store_kvcache(const svmptr_t kv_cache [[type("svmptr_t")]], uint}}-#if KV_CACHE_COMPRESSION_PER_TOKEN+// Note that process_quantization_per_token is always needed by v-cache, either KV_CACHE_COMPRESSION is 1 or 2.+#if KV_CACHE_COMPRESSION+template <int HEAD_SIZE>+CM_INLINE void process_quantization_per_token(const half* in, uchar* out, uint in_offset, uint data_offset, uint scale_offset) {+ vector<half, HEAD_SIZE> in_data;+ load_kvcache<HEAD_SIZE>(in_data, in, in_offset * (int)sizeof(half));++ half max_val = cm_reduced_max<half>(in_data);+ half min_val = cm_reduced_min<half>(in_data);++ half qrange = max_val - min_val;+ float scale_val = qrange == (half)0.0f ? 1.0f : 255.0f / qrange;+ half zp_val = (0.0f - min_val) * scale_val;+ vector<half, HEAD_SIZE> dequant_data = cm_mul<half>(in_data, scale_val) + zp_val;+ vector<uchar, HEAD_SIZE> data_u8 = cm_rnde<uchar, HEAD_SIZE>(dequant_data);++ store_kvcache<uchar, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + data_offset), 0, data_u8);++ half *out_scale_zp = (half*)(out + scale_offset);+ out_scale_zp[0] = 1.0f / scale_val;+ out_scale_zp[BLOCK_SIZE] = zp_val;+}+#endif++// Typical cases of per channel quantization involve the following steps:+// [Step 1] Load the current tokens in this subblock, and evaluate the minimum and maximum values.+// [Step 2] Load and dequantize the tail of the past tokens in this subblock. The tail size plus the current token+// size mentioned in [Step 1] should equal SUB_BLOCK_SIZE. Update the minimum and maximum values.+// [Step 3] Calculate the (new) scales and zero points based on the minimum and maximum values.+// [Step 4] Use the scales and zero points to requantize and store the tail of the past tokens.+// [Step 5] Use the scales and zero points to quantize and store the current tokens.+// [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 == 2template <int HEAD_SIZE>-CM_INLINE void quantize_and_store(vector_ref<half, HEAD_SIZE> data, uchar* out, uint out_offset, uint token_pos) {- uint scale_offset = out_offset + HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE + token_pos * sizeof(half);- half max_val = cm_reduced_max<half>(data);- half min_val = cm_reduced_min<half>(data);+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) {+ matrix<half, SUB_BLOCK_SIZE, HEAD_SIZE> in_data;+ #pragma unroll+ for (int i = 0; i < cur_sub_block_size; i++) {+ load_kvcache<HEAD_SIZE>(in_data.row(dequant_size + i), in, (in_offset + i * pitch) * (int)sizeof(half));+ }++ vector<half, HEAD_SIZE> max_vals = in_data.row(dequant_size);+ vector<half, HEAD_SIZE> min_vals = in_data.row(dequant_size);+ #pragma unroll+ for (int i = 1; i < cur_sub_block_size; i++) {+ max_vals = cm_max<half>(max_vals, in_data.row(dequant_size + i));+ min_vals = cm_min<half>(min_vals, in_data.row(dequant_size + i));+ }++ 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);+ in_data.row(i) = cm_mul<half>(update_data_u8.row(i) - zp_stale, scale_stale);+ max_vals = cm_max<half>(max_vals, in_data.row(i));+ min_vals = cm_min<half>(min_vals, in_data.row(i));+ }+ }+ }++ 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);- half qrange = max_val - min_val;- float scale_val = qrange == (half)0.0f ? 1.0f : 255.0f / qrange;- half zp_val = (0.0f - min_val) * scale_val;+#if XE_ARCH == 1+ vector<float, HEAD_SIZE> scale_vals = 255.0f / qrange;+#else+ // 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);+#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> quant_data = cm_mul<half>(in_data.row(i), 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);+ }+ }+ }- vector<half, HEAD_SIZE> dequant_data = cm_mul<half>(data, scale_val) + zp_val;- vector<uchar, HEAD_SIZE> data_u8 = cm_rnde<uchar, HEAD_SIZE>(dequant_data);+ #pragma unroll+ for (int i = 0; i < cur_sub_block_size; i++) {+ vector<half, HEAD_SIZE> quant_data = cm_mul<half>(in_data.row(dequant_size + i), 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);+ }- store_kvcache<uchar, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + out_offset + token_pos * HEAD_SIZE), 0, data_u8);+#if XE_ARCH == 1+ vector<half, HEAD_SIZE> scale_out = 1.0f / scale_vals;+#else+ vector<half, HEAD_SIZE> scale_out = cm_div_ieee(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);+}+#endif- half *out_scale_zp = (half*)(out + scale_offset);- out_scale_zp[0] = 1.0f / scale_val;- out_scale_zp[PAGED_ATTENTION_BLOCK_SIZE] = zp_val;+#if KV_CACHE_COMPRESSION == 0+template <int HEAD_SIZE>+CM_INLINE void process_no_quantization(const half* in, uchar* out, uint in_offset, uint out_offset) {+ vector<half, HEAD_SIZE> in_data;+ load_kvcache<HEAD_SIZE>(in_data, in, in_offset * (int)sizeof(half));+ store_kvcache<half, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out), out_offset * (int)sizeof(half), in_data);}#endif+template <int HEAD_SIZE, int ADJUSTED_HEAD_SIZE, bool IS_KEY>+CM_INLINE void process_kv_cache_update(const half* data,+#if KV_CACHE_COMPRESSION+ uint8_t* cache,+#else+ half* cache,+#endif+ const int32_t* block_indices,+ uint token_idx,+ uint head_idx,+ uint pitch,+ uint offset,+ uint block_offset,+ uint token_start_pos,+ uint dequant_size,+ uint cur_sub_block_size) {+ if (cur_sub_block_size == 0) {+ return;+ }++ uint in_offset = token_idx * pitch + head_idx * HEAD_SIZE + offset;++#if KV_CACHE_COMPRESSION == 2+ uint block_base_offset = IS_KEY ?+ (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * ADJUSTED_BLOCK_SIZE * ADJUSTED_HEAD_SIZE :+ (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * BLOCK_SIZE * ADJUSTED_HEAD_SIZE;+#else+ uint block_base_offset = (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * BLOCK_SIZE * ADJUSTED_HEAD_SIZE;+#endif++ uint data_offset = block_base_offset + token_start_pos * HEAD_SIZE;++#if KV_CACHE_COMPRESSION == 1+ uint scale_offset = block_base_offset + HEAD_SIZE * BLOCK_SIZE + token_start_pos * sizeof(half);+ process_quantization_per_token<HEAD_SIZE>(data, (uchar*)cache, in_offset, data_offset, scale_offset);+#elif KV_CACHE_COMPRESSION == 2+ if constexpr (IS_KEY) {+ uint scale_start_pos = token_start_pos / SUB_BLOCK_SIZE * HEAD_SIZE;+ uint scale_offset = block_base_offset + HEAD_SIZE * BLOCK_SIZE + scale_start_pos * sizeof(half);+ process_quantization_per_channel<HEAD_SIZE>(data,+ (uchar*)cache,+ in_offset,+ data_offset,+ scale_offset,+ pitch,+ dequant_size,+ cur_sub_block_size);+ } else {+ #pragma unroll+ for (uint i = 0; i < SUB_BLOCK_SIZE; i++) {+ if (i < cur_sub_block_size) {+ uint token_in_offset = in_offset + i * pitch;+ uint token_data_offset = data_offset + i * HEAD_SIZE;+ uint token_scale_offset = block_base_offset + HEAD_SIZE * BLOCK_SIZE + (token_start_pos + i) * sizeof(half);+ process_quantization_per_token<HEAD_SIZE>(data, (uchar*)cache, token_in_offset, token_data_offset, token_scale_offset);+ }+ }+ }+#else+ process_no_quantization<HEAD_SIZE>(data, (uchar*)cache, in_offset, data_offset);+#endif+}+extern "C" _GENX_MAIN_ void KERNEL_NAME(const half* key [[type("svmptr_t")]],const half* value [[type("svmptr_t")]],@@ -100,7 +298,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(const int32_t* block_indices [[type("svmptr_t")]],const int32_t* block_indices_begins [[type("svmptr_t")]],const int32_t* subsequence_begins [[type("svmptr_t")]],-#if KV_CACHE_COMPRESSION_PER_TOKEN+#if KV_CACHE_COMPRESSIONuint8_t* key_cache [[type("svmptr_t")]],uint8_t* value_cache [[type("svmptr_t")]],#else@@ -131,7 +329,34 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(const auto head_idx = cm_group_id(1);const auto wg_id = cm_group_id(2);- const uint token_idx = cm_global_id(2);+#if KV_CACHE_COMPRESSION == 2+ const uint global_token_idx = cm_global_id(2) * SUB_BLOCK_SIZE;+ uint cur_sub_block_size = SUB_BLOCK_SIZE;+#else+ const uint global_token_idx = cm_global_id(2);+ uint cur_sub_block_size = 1;+#endif++ uint token_idx = global_token_idx;+ uint dequant_size = 0;++#if KV_CACHE_COMPRESSION == 2+ cm_assert(batch_size_in_sequences <= MAX_SEQS);+ uint past_tail_lens[MAX_SEQS];+ uint pad_lens[MAX_SEQS];+ for (uint i = 0; i < batch_size_in_sequences; i++) {+ past_tail_lens[i] = past_lens[i] % SUB_BLOCK_SIZE;+ pad_lens[i] = (SUB_BLOCK_SIZE - (past_tail_lens[i] + subsequence_begins[i + 1] - subsequence_begins[i]) % SUB_BLOCK_SIZE) % SUB_BLOCK_SIZE;+ }+ for (uint i = 0; i < batch_size_in_sequences; i++) {+ if (token_idx >= subsequence_begins[i] + past_tail_lens[i]) {+ token_idx -= past_tail_lens[i];+ }+ if (token_idx >= subsequence_begins[i + 1] + pad_lens[i]) {+ token_idx -= pad_lens[i];+ }+ }+#endif// token_idx -> subsequence_idxif (token_idx >= subsequence_begins[batch_size_in_sequences]) return;@@ -143,38 +368,44 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}}+#if KV_CACHE_COMPRESSION == 2+ // last sub-block+ if (token_idx + (SUB_BLOCK_SIZE - pad_lens[subsequence_idx]) >= subsequence_begins[subsequence_idx + 1]) {+ cur_sub_block_size -= pad_lens[subsequence_idx];+ }+ // first sub-block+ if (token_idx == subsequence_begins[subsequence_idx]) {+ dequant_size = past_tail_lens[subsequence_idx];+ cur_sub_block_size -= dequant_size;+ }+#endif+const uint subsequence_begin_idx = subsequence_begins[subsequence_idx];const uint past_len = past_lens[subsequence_idx];- const uint current_block_idx = (past_len + token_idx - subsequence_begin_idx) / PAGED_ATTENTION_BLOCK_SIZE;- const uint token_start_pos = (past_len + token_idx - subsequence_begin_idx) % PAGED_ATTENTION_BLOCK_SIZE;+ const uint current_block_idx = (past_len + token_idx - subsequence_begin_idx) / BLOCK_SIZE;+ const uint token_start_pos = (past_len + token_idx - subsequence_begin_idx) % BLOCK_SIZE;const uint block_offset = block_indices_begins[subsequence_idx] + current_block_idx;- {- uint block_k_base_offset = (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * ADJUSTED_K_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;- uint key_out_offset = block_k_base_offset + token_start_pos * K_HEAD_SIZE;- uint key_in_offset = token_idx * key_pitch + head_idx * K_HEAD_SIZE + key_offset;-- vector<half, K_HEAD_SIZE> key_data;- load_kvcache<K_HEAD_SIZE>(key_data, key, key_in_offset * (int)sizeof(half));-- #if KV_CACHE_COMPRESSION_PER_TOKEN- quantize_and_store<K_HEAD_SIZE>(key_data, (uchar*)key_cache, block_k_base_offset, token_start_pos);- #else- store_kvcache<half, K_HEAD_SIZE>(reinterpret_cast<svmptr_t>(key_cache), key_out_offset * (int)sizeof(half), key_data);- #endif- }- {- uint block_v_base_offset = (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * ADJUSTED_V_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;- uint value_out_offset = block_v_base_offset + token_start_pos * V_HEAD_SIZE;- uint value_in_offset = token_idx * value_pitch + head_idx * V_HEAD_SIZE + value_offset;-- vector<half, V_HEAD_SIZE> value_data;- load_kvcache<V_HEAD_SIZE>(value_data, value, value_in_offset * (int)sizeof(half));-- #if KV_CACHE_COMPRESSION_PER_TOKEN- quantize_and_store<V_HEAD_SIZE>(value_data, (uchar*)value_cache, block_v_base_offset, token_start_pos);- #else- store_kvcache<half, V_HEAD_SIZE>(reinterpret_cast<svmptr_t>(value_cache), value_out_offset * (int)sizeof(half), value_data);- #endif- }+ process_kv_cache_update<K_HEAD_SIZE, ADJUSTED_K_HEAD_SIZE, true>(key,+ key_cache,+ block_indices,+ token_idx,+ head_idx,+ key_pitch,+ key_offset,+ block_offset,+ token_start_pos,+ dequant_size,+ cur_sub_block_size);+ process_kv_cache_update<V_HEAD_SIZE, ADJUSTED_V_HEAD_SIZE, false>(value,+ value_cache,+ block_indices,+ token_idx,+ head_idx,+ value_pitch,+ value_offset,+ block_offset,+ token_start_pos,+ dequant_size,+ cur_sub_block_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 f8bd173df53..9c9404e89c4 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@@ -19,7 +19,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(SurfaceIndex q_gather [[type("buffer_t")]],#endif#ifdef CM_HAS_LSC_UNTYPED_2D-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONint8_t* k_cache [[type("svmptr_t")]],int8_t* v_cache [[type("svmptr_t")]],#else@@ -27,7 +27,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(half* v_cache [[type("svmptr_t")]],#endif#else-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONint8_t* k_cache [[type("svmptr_t")]],SurfaceIndex v_cache_stateful [[type("buffer_t")]],#else@@ -53,10 +53,13 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(constexpr int head_size = CMFLA_HEAD_SIZE;constexpr int num_kv_heads = CMFLA_NUM_KV_HEADS;constexpr int pa_block_sz = CMPA_BLOCK_SZ;+#if KV_CACHE_COMPRESSION == 2+ constexpr int sub_block_sz = SUB_BLOCK_SIZE;+#endif//# query [q_len, num_heads, S]//# k_cache [kv_len, num_heads, S]//# v_cache [kv_len, num_heads, S]-#if CMPA_KVCACHE_U8+#if KV_CACHE_COMPRESSIONconstexpr 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;@@ -136,9 +139,16 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(wg_block_mask_base = sparse_block_mask_wg + (h * cm_group_count(2) + wg_id) * num_k_blocks;#endif-#if CMPA_KVCACHE_U8- uint kv_offset = hkv*(head_size+4)*pa_block_sz;- uint kv_offset_bytes = kv_offset; // U8: byte offset+#if KV_CACHE_COMPRESSION+#if KV_CACHE_COMPRESSION == 1+ uint k_offset = hkv * (head_size + 4) * pa_block_sz;+#else+ uint k_offset = hkv * head_size * (pa_block_sz + pa_block_sz / sub_block_sz * 4);+#endif+ uint v_offset = hkv * (head_size + 4) * pa_block_sz;+#ifndef CM_HAS_LSC_UNTYPED_2D+ uint v_offset_bytes = v_offset; // U8: byte offset+#endifpa_lsc_u8<is_causal, num_heads, num_kv_heads, head_size, 0>(slm_K,slm_V,@@ -154,12 +164,12 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(q_gather,q_offset_bytes,#endif- reinterpret_cast<svmptr_t>(k_cache + kv_offset),+ reinterpret_cast<svmptr_t>(k_cache + k_offset),#ifdef CM_HAS_LSC_UNTYPED_2D- reinterpret_cast<svmptr_t>(v_cache + kv_offset),+ reinterpret_cast<svmptr_t>(v_cache + v_offset),#elsev_cache_stateful,- kv_offset_bytes,+ v_offset_bytes,#endif#if SPARSE_BLOCK_SIZE > 1reinterpret_cast<svmptr_t>(block_mask_base),@@ -170,7 +180,9 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(block_indices);#elseuint kv_offset = hkv*head_size*pa_block_sz;- uint kv_offset_bytes = kv_offset * sizeof(half);+#ifndef CM_HAS_LSC_UNTYPED_2D+ uint v_offset_bytes = kv_offset * sizeof(half);+#endifpa_kernel_lsc_prefetch_f16<is_causal, num_heads, num_kv_heads, head_size, 0, 16>(wg_local_id,q_start_sg, //q_start for SG,@@ -188,7 +200,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(reinterpret_cast<svmptr_t>(v_cache + kv_offset),#elsev_cache_stateful,- kv_offset_bytes,+ v_offset_bytes,#endif#if SPARSE_BLOCK_SIZE > 1reinterpret_cast<svmptr_t>(block_mask_base),diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token.cm b/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token.cmindex 594d5ce390a..4d76b52a1f6 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token.cm@@ -31,11 +31,19 @@#define KV_PARTITION_STEP_NUM (KV_PARTITION_SIZE / KV_STEP)#if KV_CACHE_COMPRESSION+ // Currently supports 'by channel' and 'by token' for K, V still uses 'by token'.// scale/zp is half-precision, so size = 2 * 2 = 4 bytes- #define KV_SCALE_ZP_SIZE 4 // scale/zp bytes+ #if KV_CACHE_COMPRESSION == 2+ #define GROUPS_PER_BLOCK (KV_BLOCK_SIZE / SUB_BLOCK_SIZE)+ #define K_SCALE_ZP_SIZE (4 * GROUPS_PER_BLOCK) // scale/zp bytes+ #define V_SCALE_ZP_SIZE 4 // scale/zp bytes+ #elif KV_CACHE_COMPRESSION == 1+ #define K_SCALE_ZP_SIZE 4 // scale/zp bytes+ #define V_SCALE_ZP_SIZE 4 // scale/zp bytes+ #endif#define KV_ELEMENT_TYPE uint8_t#else- #define KV_SCALE_ZP_SIZE 0 // no scale/zp+ #define V_SCALE_ZP_SIZE 0 // no scale/zp#define KV_ELEMENT_TYPE half#endif@@ -136,11 +144,13 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(matrix <half, Q_head_chunk_size, HEAD_SIZE> Qmat = 0;cm_svm_block_read<half, Q_head_chunk_size * HEAD_SIZE>((svmptr_t)(query + qo_offset), Qmat.format<half>());- constexpr uint per_v_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp- #if KV_CACHE_COMPRESSION_BY_TOKEN- constexpr uint per_k_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp+ constexpr uint per_v_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + V_SCALE_ZP_SIZE); // 0 or 4 bytes: scale/zp+ #if KV_CACHE_COMPRESSION == 1+ constexpr uint per_k_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + K_SCALE_ZP_SIZE); // 4 bytes: scale/zp+ #elif KV_CACHE_COMPRESSION == 2+ constexpr uint per_k_block_element_num = KV_HEADS_NUM * HEAD_SIZE * (KV_BLOCK_SIZE + K_SCALE_ZP_SIZE); // 4 * GROUPS_PER_BLOCK bytes: scale/zp#else- constexpr uint per_k_block_element_num = KV_HEADS_NUM * HEAD_SIZE * (KV_BLOCK_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp+ constexpr uint per_k_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * HEAD_SIZE;#endifuint block_num = KV_PARTITION_SIZE / KV_BLOCK_SIZE;@@ -186,7 +196,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}#if KV_CACHE_COMPRESSION- #if KV_CACHE_COMPRESSION_BY_TOKEN+ #if KV_CACHE_COMPRESSION == 1// load scale/zpvector<half, KV_BLOCK_SIZE> scale_vec;vector<half, KV_BLOCK_SIZE> zp_vec;@@ -200,17 +210,13 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(zp_vec[i] = 0.0;}}- #else- // load scale/zp- vector<half, HEAD_SIZE> scale_vec;- vector<half, HEAD_SIZE> zp_vec;- cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset), scale_vec);- cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset + HEAD_SIZE * sizeof(half)), zp_vec);+ #elif KV_CACHE_COMPRESSION == 2+ // TODO: Maybe using scale_vec and zp_vec would be better#endif#endiffor(int kv_pos = 0; kv_pos < kv_pos_end; kv_pos += KV_STEP, ki++) {- #if KV_CACHE_COMPRESSION && KV_CACHE_COMPRESSION_BY_TOKEN+ #if KV_CACHE_COMPRESSION == 1#if USE_LSC_BLOCK_2D_DESCvector<half, REG_N * 2> temp_scale, temp_zp;temp_scale.select<REG_N,2>(0) = scale_vec.select<REG_N,1>(kv_pos);@@ -230,7 +236,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#pragma unroll#if KV_CACHE_COMPRESSION- #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION_BY_TOKEN && XE_ARCH==1+ #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION == 1 && XE_ARCH==1for(int k = 0, ri = 0; k < HEAD_SIZE/4; k += (REG_K/4)*4, ri += 4 ) {#elsefor(int k = 0, ri = 0; k < HEAD_SIZE/4; k += REG_K/4, ri ++ ) {@@ -239,7 +245,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(for(int k = 0, ri = 0; k < HEAD_SIZE/2; k += REG_K/2, ri ++ ) {#endif// matrix<half, REG_K, REG_N> Kt = 0;- #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION && KV_CACHE_COMPRESSION_BY_TOKEN && XE_ARCH==1+ #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION == 1 && XE_ARCH==1matrix<uint8_t, REG_N, REG_K*4> temp4; // 8 x 64uint cur_kv_offset4 = kv_offset + kv_pos * kv_stride + k * 4;#pragma unroll@@ -291,16 +297,18 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(quant_dst.row(r ) = quant_src.select<2,1,8,2>(r,0);quant_dst.row(r+1) = quant_src.select<2,1,8,2>(r,1);}- #if KV_CACHE_COMPRESSION_BY_TOKEN- #pragma unroll- for(int r = 0; r < REG_K; r++) {- Kt[r] = Kt_quant[r] - temp_zp.format<half, 2, REG_N>()[r%2]; //vector - vector- Kt[r] = cm_mul<half>(Kt[r], temp_scale.format<half, 2, REG_N>()[r%2]); // vector * vector- }- #else+ #if KV_CACHE_COMPRESSION == 2+ uint group_idx = (kv_pos / SUB_BLOCK_SIZE);vector<half, REG_K> temp_scale, temp_zp;- temp_scale.select<REG_K, 1>(0) = scale_vec.select<REG_K, 1>(k * 4);- temp_zp.select<REG_K, 1>(0) = zp_vec.select<REG_K, 1>(k * 4);+ svmptr_t scale_ptr = (svmptr_t)(key + k_scale_zp_offset+ + group_idx * HEAD_SIZE * sizeof(half)+ + k * 4 * sizeof(half));+ svmptr_t zp_ptr = (svmptr_t)(key + k_scale_zp_offset+ + GROUPS_PER_BLOCK * HEAD_SIZE * sizeof(half)+ + group_idx * HEAD_SIZE * sizeof(half)+ + k * 4 * sizeof(half));+ cm_svm_block_read<half, REG_K>(scale_ptr, temp_scale);+ cm_svm_block_read<half, REG_K>(zp_ptr, temp_zp);auto Kt_dequant_out = Kt.format<half, REG_K/2, 2*REG_N>();auto Kt_dequant_tmp = Kt_quant.format<uint8_t, REG_K/2, 2*REG_N>();@@ -311,6 +319,12 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(Kt_dequant_out[r].select<REG_N, 2>(1) = Kt_dequant_tmp[r].select<REG_N, 2>(1) - temp_zp[r*2+1];Kt_dequant_out[r].select<REG_N, 2>(1) = cm_mul<half>(Kt_dequant_out[r].select<REG_N, 2>(1), temp_scale[r*2+1]);}+ #elif KV_CACHE_COMPRESSION == 1+ #pragma unroll+ for(int r = 0; r < REG_K; r++) {+ Kt[r] = Kt_quant[r] - temp_zp.format<half, 2, REG_N>()[r%2]; //vector - vector+ Kt[r] = cm_mul<half>(Kt[r], temp_scale.format<half, 2, REG_N>()[r%2]); // vector * vector+ }#endif#elsecm_load<lsc::Transpose>(Kt.format<uint>(), b2dK.set_block_y(kv_pos));@@ -323,14 +337,22 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(for(int kk = 0; kk < REG_N; kk++) {cm_svm_block_read<uint8_t, REG_K>((svmptr_t)(key + cur_kv_offset + kk * kv_stride), temp[kk].format<uint8_t>());}- #if KV_CACHE_COMPRESSION_BY_TOKEN+ #if KV_CACHE_COMPRESSION == 1matrix<half, REG_N, REG_K> KtNormal = temp;KtNormal = KtNormal - temp_zp;KtNormal = cm_mul<half>(KtNormal, temp_scale);#else+ uint group_idx = (kv_pos / SUB_GROUP_SIZE);vector<half, REG_K> temp_scale, temp_zp;- temp_scale.select<REG_K, 1>(0) = scale_vec.select<REG_K, 1>(k * 4);- temp_zp.select<REG_K, 1>(0) = zp_vec.select<REG_K, 1>(k * 4);+ svmptr_t scale_ptr = (svmptr_t)(key + k_scale_zp_offset+ + group_idx * HEAD_SIZE * sizeof(half)+ + k * 4 * sizeof(half));+ svmptr_t zp_ptr = (svmptr_t)(key + k_scale_zp_offset+ + GROUPS_PER_BLOCK * HEAD_SIZE * sizeof(half)+ + group_idx * HEAD_SIZE * sizeof(half)+ + k * 4 * sizeof(half));+ cm_svm_block_read<half, REG_K>(scale_ptr, temp_scale);+ cm_svm_block_read<half, REG_K>(zp_ptr, temp_zp);matrix<half, REG_N, REG_K> KtNormal;#pragma unrollfor(int kk = 0; kk < REG_N; kk++) {@@ -632,4 +654,3 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(lse[lse_offset] = cur_lse[qi];}}-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 65945f25f59..95b8fb61a15 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@@ -23,6 +23,7 @@ using namespace ov::intel_gpu::ocl;using namespace cldnn;namespace {constexpr size_t reduce_split_step = 16;+constexpr size_t KV_SUB_BLOCK_SIZE = 16;} // namespace#define DEBUG_ENABLED 0@@ -147,6 +148,20 @@ JitConstants PagedAttentionGeneratorBase::get_jit_constants(const kernel_impl_pajit.make("WG_SIZE", WG_SIZE);jit.make("CAUSAL_MASK", 1);++ // 0: no compression, 1: per-token compression for both key and value, 2: per-channel compression for key and per-token compression for value+ if (get_kv_compressed(params)) {+ const auto desc = params.typed_desc<paged_attention>();+ if (desc->is_key_by_channel) {+ jit.make("KV_CACHE_COMPRESSION", 2);+ jit.make("SUB_BLOCK_SIZE", KV_SUB_BLOCK_SIZE);+ } else {+ jit.make("KV_CACHE_COMPRESSION", 1);+ }+ } else {+ jit.make("KV_CACHE_COMPRESSION", 0);+ }+return jit;}@@ -161,17 +176,22 @@ JitConstants PagedAttentionGeneratorKVCacheUpdate::get_jit_constants(const kernejit.make("K_HEAD_SIZE", desc->k_head_size);jit.make("V_HEAD_SIZE", desc->v_head_size);if (desc->has_xattention) {- jit.make("PAGED_ATTENTION_BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_XATTN);+ jit.make("BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_XATTN);} else {- jit.make("PAGED_ATTENTION_BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_LEGACY);+ jit.make("BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_LEGACY);}if (get_kv_compressed(params)) {- jit.make("KV_CACHE_COMPRESSION_PER_TOKEN", 1);- jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size + 4);+ if (desc->is_key_by_channel) {+ jit.make("ADJUSTED_BLOCK_SIZE",+ desc->has_xattention ? PA_KV_CACHE_BLOCK_SIZE_XATTN + PA_KV_CACHE_BLOCK_SIZE_XATTN / KV_SUB_BLOCK_SIZE * 4+ : PA_KV_CACHE_BLOCK_SIZE_LEGACY + PA_KV_CACHE_BLOCK_SIZE_LEGACY / KV_SUB_BLOCK_SIZE * 4);+ jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size);+ } else {+ jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size + 4);+ }jit.make("ADJUSTED_V_HEAD_SIZE", desc->v_head_size + 4);} else {- jit.make("KV_CACHE_COMPRESSION_PER_TOKEN", 0);jit.make("ADJUSTED_K_HEAD_SIZE", desc->k_head_size);jit.make("ADJUSTED_V_HEAD_SIZE", desc->v_head_size);}@@ -207,7 +227,11 @@ DispatchDataFunc PagedAttentionGeneratorKVCacheUpdate::get_dispatch_data_func()const size_t kv_len = get_input_kv_len(params);const size_t kv_heads_num = desc->kv_heads_num;- const size_t wg_count = (kv_len + WG_SIZE - 1) / WG_SIZE;+ size_t kv_items = kv_len;+ if (get_kv_compressed(params) && desc->is_key_by_channel) {+ kv_items = ceil_div(kv_len, KV_SUB_BLOCK_SIZE);+ }+ const size_t wg_count = ceil_div(kv_items, WG_SIZE);wgs.global = {1, kv_heads_num, wg_count * WG_SIZE};wgs.local = {1, 1, WG_SIZE};@@ -318,13 +342,6 @@ JitConstants PagedAttentionGeneratorMultiToken::get_jit_constants(const kernel_i} else {jit.make("CMPA_BLOCK_SZ", PA_KV_CACHE_BLOCK_SIZE_LEGACY);}-- if (get_kv_compressed(params)) {- jit.make("CMPA_KVCACHE_U8", 1);- } else {- jit.make("CMPA_KVCACHE_U8", 0);- }-jit.make("CMPA_WG_SEQ_LEN", get_wg_seq_len(params));return jit;@@ -395,13 +412,6 @@ JitConstants PagedAttentionGeneratorSingleToken::get_jit_constants(const kernel_const auto q_chunking = get_single_token_q_chunking(params, *desc, kv_partition_size);jit.make("Q_head_chunks_per_kv_head", q_chunking.q_head_chunks_per_kv_head);jit.make("Q_head_chunk_size", q_chunking.q_head_chunk_size);- if (get_kv_compressed(params)) {- jit.make("KV_CACHE_COMPRESSION", 1);- jit.make("KV_CACHE_COMPRESSION_BY_TOKEN", 1);- } else {- jit.make("KV_CACHE_COMPRESSION", 0);- jit.make("KV_CACHE_COMPRESSION_BY_TOKEN", 0);- }return jit;}@@ -572,10 +582,12 @@ JitConstants XAttentionEstimateGeneratorBase::get_jit_constants(const kernel_impjit.make("WALK_HQ", desc->heads_num != desc->kv_heads_num ? 2 : 1);jit.make("IS_CAUSAL", 1);if (get_kv_compressed(params)) {- jit.make("USE_INT8", 1);- jit.make("HEAD_SIZE_KEY", desc->k_head_size + 2 * 2);+ if (desc->is_key_by_channel) {+ jit.make("HEAD_SIZE_KEY", desc->k_head_size + desc->k_head_size / 4);+ } else {+ jit.make("HEAD_SIZE_KEY", desc->k_head_size + 2 * 2);+ }} else {- jit.make("USE_INT8", 0);jit.make("HEAD_SIZE_KEY", desc->k_head_size);}jit.make("SOFTMAX_TYPE", "float");diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/xattn_gemm_qk.cm b/src/plugins/intel_gpu/src/graph/impls/cm/xattn_gemm_qk.cmindex 3a70838ebf0..f79c3066ead 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/xattn_gemm_qk.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/xattn_gemm_qk.cm@@ -94,7 +94,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#ifdef CM_HAS_LSC_UNTYPED_2D// key cache: [block, HQ, KV_BLOCK_SIZE, HEAD_SIZE_KEY]-#if USE_INT8+#if KV_CACHE_COMPRESSIONkey_cache += hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char));#elsekey_cache += hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half));diff --git a/src/plugins/intel_gpu/src/graph/paged_attention.cpp b/src/plugins/intel_gpu/src/graph/paged_attention.cppindex a2f888ccb88..6d80388bbb6 100644--- a/src/plugins/intel_gpu/src/graph/paged_attention.cpp+++ b/src/plugins/intel_gpu/src/graph/paged_attention.cpp@@ -46,10 +46,17 @@ std::vector<layout> paged_attention_inst::calc_output_layouts(paged_attention_nodata_type_traits::is_i4_u4(key_cache_dt);auto expected_block_size = desc->has_xattention ? paged_attention::block_size_xattn : paged_attention::block_size;if (key_cache_compressed && key_cache_quant_mode == ov::internal::CacheQuantMode::BY_CHANNEL) {- if (data_type_traits::is_i4_u4(key_cache_dt))+ if (data_type_traits::is_i4_u4(key_cache_dt)) {expected_block_size += 8;- else- expected_block_size += 4;+ } else {+ constexpr size_t kv_sub_block_size = 16;+ OPENVINO_ASSERT(expected_block_size % kv_sub_block_size == 0,+ "[GPU] Invalid block size for BY_CHANNEL key cache quantization: ",+ expected_block_size,+ ". Expected multiple of ",+ kv_sub_block_size);+ expected_block_size += expected_block_size / kv_sub_block_size * 4;+ }}OPENVINO_ASSERT((key_cache_quant_mode == ov::internal::CacheQuantMode::BY_CHANNEL) == desc->is_key_by_channel,diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex ee27db6b647..dd72028ce26 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -683,10 +683,6 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {kv_cache_config.keyCacheQuantBychannel = (key_cache_quant_mode == ov::internal::CacheQuantMode::BY_CHANNEL);kv_cache_config.keyCacheGroupSize = (key_cache_quant_mode == ov::internal::CacheQuantMode::BY_CHANNEL) ? 16 : 0;if (use_xattention) {- if (kv_cache_config.keyCacheQuantBychannel &&- ((kv_cache_precision == ov::element::i8 || kv_cache_precision == ov::element::u8)) )- OPENVINO_THROW("[GPU] XAttention does not currently support per-channel quantized key cache.");-kv_cache_config.valueCacheBlockSize = cldnn::paged_attention::block_size_xattn;kv_cache_config.valueCacheDimOrder = {0, 1, 2, 3};} else {@@ -706,7 +702,11 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {if (bychannel) {// TODO: need to handle group size != block size caseif (precision == ov::element::i8 || precision == ov::element::u8) {- block_size += infer_precision.size() * 2;+ constexpr int64_t kv_sub_block_size = 16;+ OPENVINO_ASSERT(block_size % kv_sub_block_size == 0,+ "[GPU] Invalid key cache block size for BY_CHANNEL quantization: ",+ block_size);+ block_size += (block_size / kv_sub_block_size) * infer_precision.size() * 2;} else if (precision == ov::element::i4 || precision == ov::element::u4) {head_size = align_to(head_size / 2, 16);block_size += infer_precision.size() * 4;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 e6fe3c34f47..f1d37fc5373 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@@ -243,14 +243,24 @@ struct PagedAttentionManager {}memory::ptr get_key_cache_memory_cm() {+ constexpr int kv_sub_block_size = 16;auto key_cache_dt = kv_cache_compression ? data_types::i8 : data_types::f16;const int head_size = k_head_size;- const int adjusted_head_size = head_size + (kv_cache_compression ? 4 : 0);+ int adjusted_head_size = head_size;+ int adjusted_block_size = block_size;+ if (kv_cache_compression) {+ if (key_cache_quant_mode == ov::internal::CacheQuantMode::BY_CHANNEL) {+ OPENVINO_ASSERT(block_size % kv_sub_block_size == 0);+ adjusted_block_size += block_size / kv_sub_block_size * 4;+ } else {+ adjusted_head_size += 4;+ }+ }const auto num_blocks = block_indices.back() + 1;auto key_cache_shape = ov::PartialShape{static_cast<int64_t>(num_blocks),static_cast<int64_t>(num_kv_heads),- static_cast<int64_t>(block_size),+ static_cast<int64_t>(adjusted_block_size),static_cast<int64_t>(adjusted_head_size)};auto key_cache_layout = layout{key_cache_shape, key_cache_dt, format::bfyx};auto memory = test_engine.allocate_memory(key_cache_layout);@@ -277,7 +287,7 @@ struct PagedAttentionManager {(static_cast<size_t>(head_idx) * block_size * head_size);const size_t off = base + static_cast<size_t>(token_idx) * head_size;set_values(test_stream, memory, src_ptr, head_size, off);- } else {+ } else if (key_cache_quant_mode == ov::internal::CacheQuantMode::BY_TOKEN) {// Compressed Key cache layout:// logical shape: [num_blocks, num_kv_heads, block_size, adjusted_head_size], dt=i8 (adjusted_head_size=head_size+4).// Per (block, head) region starts at block_base_i8, byte-packed as:@@ -302,6 +312,49 @@ struct PagedAttentionManager {set_values(test_stream, memory, &scale, 1, scale_off_f16);set_values(test_stream, memory, &zp, 1, zp_off_f16);+ } else {+ // Compressed Key cache layout for BY_CHANNEL:+ // shape: [num_blocks, num_kv_heads, adjusted_block_size, head_size], dt=i8.+ // Per (block, head):+ // data bytes region : [block_size * head_size]+ // scale fp16 region per-subblock per-channel : [(block_size / sub_block) * head_size]+ // zp fp16 region per-subblock per-channel : [(block_size / sub_block) * head_size]+ const size_t block_stride_i8 = static_cast<size_t>(adjusted_block_size) * static_cast<size_t>(head_size);+ const size_t block_base_i8 = (static_cast<size_t>(start_block_idx + block_idx) * static_cast<size_t>(num_kv_heads) ++ static_cast<size_t>(head_idx)) *+ block_stride_i8;++ const int subblock_count = block_size / kv_sub_block_size;+ const size_t scale_base_i8 = block_base_i8 + static_cast<size_t>(block_size) * static_cast<size_t>(head_size);+ const size_t zp_base_i8 = scale_base_i8 ++ static_cast<size_t>(subblock_count) * static_cast<size_t>(head_size) * sizeof(ov::float16);++ for (int channel = 0; channel < head_size; channel++) {+ for (int sub_start = 0; sub_start < last_token_idx; sub_start += kv_sub_block_size) {+ const int cur_sub_block_size = std::min(kv_sub_block_size, last_token_idx - sub_start);+ std::vector<ov::float16> token_block(cur_sub_block_size);++ for (int t = 0; t < cur_sub_block_size; t++) {+ const size_t input_token_offset = static_cast<size_t>(block_idx) * block_size + static_cast<size_t>(sub_start + t);+ token_block[t] = *(key_data[i].data() + input_token_offset * static_cast<size_t>(num_kv_heads) * head_size ++ static_cast<size_t>(head_idx) * head_size + channel);+ }++ auto [quantized_data, scale, zp] = quantize_data(token_block.data(), cur_sub_block_size, true, true);++ for (int t = 0; t < cur_sub_block_size; t++) {+ const size_t data_off_i8 = block_base_i8 + static_cast<size_t>(sub_start + t) * head_size + channel;+ set_values(test_stream, memory, quantized_data.data() + t, 1, data_off_i8);+ }++ const size_t sub_idx = static_cast<size_t>(sub_start / kv_sub_block_size);+ const size_t scale_off_f16 = scale_base_i8 / 2 + sub_idx * static_cast<size_t>(head_size) + channel;+ const size_t zp_off_f16 = zp_base_i8 / 2 + sub_idx * static_cast<size_t>(head_size) + channel;++ set_values(test_stream, memory, &scale, 1, scale_off_f16);+ set_values(test_stream, memory, &zp, 1, zp_off_f16);+ }+ }}}}@@ -2492,6 +2545,18 @@ INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention, xattention_test, ::testing::Valuespaged_attention_test_params{ {{1, 32}}, 2, 2, 64, 64, 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} }, // 2nd tokepaged_attention_test_params{ {{1, 1023}}, 2, 2, 64, 64, 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} }, // 2nd tokenpaged_attention_test_params{ {{1, 1024}}, 2, 2, 64, 64, 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} }, // 2nd token++ paged_attention_test_params{ {{32, 0}}, 2, 2, 64, 64, 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} }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 2, 2, 64, 64, 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} }, // 1st token+ paged_attention_test_params{ {{2048, 0}}, 2, 2, 64, 64, 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} }, // 1st token+ paged_attention_test_params{ {{32, 0}}, 4, 2, 64, 64, 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} }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 4, 2, 64, 64, 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} }, // 1st token+ paged_attention_test_params{ {{2048, 0}}, 4, 2, 64, 64, 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} }, // 1st token++ paged_attention_test_params{ {{1, 31}}, 2, 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}, std::vector<int>{128} }, // 2nd token+ paged_attention_test_params{ {{1, 32}}, 2, 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}, std::vector<int>{128} }, // 2nd token+ paged_attention_test_params{ {{1, 1023}}, 2, 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}, std::vector<int>{128} }, // 2nd token+ paged_attention_test_params{ {{1, 1024}}, 2, 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}, std::vector<int>{128} }, // 2nd token}));INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention_block_size, xattention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{