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
XAttention for XE1 platform (#33307)
Wang Wangwang5eee33a3e87d
diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hppindex caacc5948c8..1847f0213ba 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_attention_common.hpp@@ -1,18 +1,7 @@-/*******************************************************************************- * Copyright (c) 2018-2026 Intel Corporation- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- *******************************************************************************/+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//+#include <cm/cm.h>#include <cm/cmtl.h>@@ -86,24 +75,18 @@ CM_INLINE void Transpose_16x16(matrix_ref<T1, 16, 16> in,template <typename T1, typename T2>CM_INLINE void Transpose_8x8(matrix_ref<T1, 8, 8> in, matrix_ref<T2, 8, 8> out) {- matrix<T2, 8, 8> temp;- temp.row(0) = in.template select<2, 1, 4, 2>(0, 0);- temp.row(1) = in.template select<2, 1, 4, 2>(2, 0);- temp.row(2) = in.template select<2, 1, 4, 2>(4, 0);- temp.row(3) = in.template select<2, 1, 4, 2>(6, 0);- temp.row(4) = in.template select<2, 1, 4, 2>(0, 1);- temp.row(5) = in.template select<2, 1, 4, 2>(2, 1);- temp.row(6) = in.template select<2, 1, 4, 2>(4, 1);- temp.row(7) = in.template select<2, 1, 4, 2>(6, 1);-- out.row(0) = temp.template select<4, 1, 2, 4>(0, 0);- out.row(2) = temp.template select<4, 1, 2, 4>(0, 1);- out.row(4) = temp.template select<4, 1, 2, 4>(0, 2);- out.row(6) = temp.template select<4, 1, 2, 4>(0, 3);- out.row(1) = temp.template select<4, 1, 2, 4>(4, 0);- out.row(3) = temp.template select<4, 1, 2, 4>(4, 1);- out.row(5) = temp.template select<4, 1, 2, 4>(4, 2);- out.row(7) = temp.template select<4, 1, 2, 4>(4, 3);+ matrix<T2, 8, 8> temp;+ #pragma unroll+ for (int i = 0; i < 4; ++i) {+ temp.row(i) = in.template select<2, 1, 4, 2>(i * 2, 0);+ temp.row(i + 4) = in.template select<2, 1, 4, 2>(i * 2, 1);+ }++ #pragma unroll+ for (int i = 0; i < 4; ++i) {+ out.row(i * 2) = temp.template select<4, 1, 2, 4>(0, i);+ out.row(i * 2 + 1) = temp.template select<4, 1, 2, 4>(4, i);+ }}// function templates cannot be partially specialized; use overloading to achieve the same effect@@ -338,65 +321,64 @@ vector<float, cols> online_softmax_update(matrix_ref<T, rows, cols> St, vector_r#define cm_load_transpose cm_load<lsc::Transpose>#define cm_load_vnni cm_load<lsc::VNNI>#define cm_store_normal cm_store-#else- // simulation of LSC API using SVM API- template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>- inline void cm_load_normal(vector_ref<T, NBlocks*BlockH*BlockW> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {- static_assert(NBlocks == 1);- auto pitch = Desc.get_pitch() + 1;- auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));- #pragma unroll- for(int i = 0; i < BlockH; i++) {- cm_svm_block_read(base + i * pitch, Res.select<BlockW, 1>(i*BlockW));- }- }-- template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>- inline void cm_load_transpose(vector_ref<T, NBlocks*BlockW*BlockH> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {- static_assert(NBlocks == 1);- auto pitch = Desc.get_pitch() + 1;- auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));- matrix<T, BlockH, BlockW> temp;- #pragma unroll- for(int i = 0; i < BlockH; i++) {- cm_svm_block_read(base + i * pitch, temp[i]);- }- Transpose2DMatrix(temp, Res.format<T, BlockW, BlockH>());- }+ // // simulation of LSC API using SVM API+ // template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>+ // inline void cm_load_normal(vector_ref<T, NBlocks*BlockH*BlockW> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {+ // static_assert(NBlocks == 1);+ // auto pitch = Desc.get_pitch() + 1;+ // auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));+ // #pragma unroll+ // for(int i = 0; i < BlockH; i++) {+ // cm_svm_block_read(base + i * pitch, Res.select<BlockW, 1>(i*BlockW));+ // }+ // }- // in VNNI case, NBlocks is increasing along X dimension (increase cache-line usage)- template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>- inline void cm_load_vnni(vector_ref<T, NBlocks*BlockW*BlockH> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {- static_assert(NBlocks == 1 || NBlocks == 2);- // each block must be a full XMX B matrix- static_assert(BlockH == REG_K);- static_assert(BlockW == REG_N);- auto pitch = Desc.get_pitch() + 1;- auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));- matrix<T, BlockH, NBlocks * BlockW> temp;- #pragma unroll- for(int i = 0; i < BlockH; i++) {- cm_svm_block_read(base + i * pitch, temp[i]);- }+ // template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>+ // inline void cm_load_transpose(vector_ref<T, NBlocks*BlockW*BlockH> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {+ // static_assert(NBlocks == 1);+ // auto pitch = Desc.get_pitch() + 1;+ // auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));+ // matrix<T, BlockH, BlockW> temp;+ // #pragma unroll+ // for(int i = 0; i < BlockH; i++) {+ // cm_svm_block_read(base + i * pitch, temp[i]);+ // }+ // Transpose2DMatrix(temp, Res.format<T, BlockW, BlockH>());+ // }- auto out_vnni = Res.format<T, NBlocks * (BlockH/2), 2*BlockW>();- #pragma unroll- for(int i = 0; i < NBlocks; i ++) {- out_vnni.select<BlockH/2, 1, BlockW, 2>(i*(BlockH/2), 0) = temp.select<BlockH/2, 2, BlockW, 1>(0, i*BlockW);- out_vnni.select<BlockH/2, 1, BlockW, 2>(i*(BlockH/2), 1) = temp.select<BlockH/2, 2, BlockW, 1>(1, i*BlockW);- }- }+ // // in VNNI case, NBlocks is increasing along X dimension (increase cache-line usage)+ // template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>+ // inline void cm_load_vnni(vector_ref<T, NBlocks*BlockW*BlockH> Res, const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, int16_t Pred = 1) {+ // static_assert(NBlocks == 1 || NBlocks == 2);+ // // each block must be a full XMX B matrix+ // static_assert(BlockH == REG_K);+ // static_assert(BlockW == REG_N);+ // auto pitch = Desc.get_pitch() + 1;+ // auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));+ // matrix<T, BlockH, NBlocks * BlockW> temp;+ // #pragma unroll+ // for(int i = 0; i < BlockH; i++) {+ // cm_svm_block_read(base + i * pitch, temp[i]);+ // }++ // auto out_vnni = Res.format<T, NBlocks * (BlockH/2), 2*BlockW>();+ // #pragma unroll+ // for(int i = 0; i < NBlocks; i ++) {+ // out_vnni.select<BlockH/2, 1, BlockW, 2>(i*(BlockH/2), 0) = temp.select<BlockH/2, 2, BlockW, 1>(0, i*BlockW);+ // out_vnni.select<BlockH/2, 1, BlockW, 2>(i*(BlockH/2), 1) = temp.select<BlockH/2, 2, BlockW, 1>(1, i*BlockW);+ // }+ // }- template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>- inline void cm_store_normal(const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, vector_ref<T, NBlocks*BlockW*BlockH> Res) {- static_assert(NBlocks == 1);- auto pitch = Desc.get_pitch() + 1;- auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));- #pragma unroll- for(int i = 0; i < BlockH; i++) {- cm_svm_block_write(base + i * pitch, Res.select<BlockW, 1>(i*BlockW));- }- }+ // template <typename T = int, unsigned NBlocks = 1, unsigned BlockH = 1, unsigned BlockW = 1>+ // inline void cm_store_normal(const lsc::block_2d_desc<T, NBlocks, BlockH, BlockW> &Desc, vector_ref<T, NBlocks*BlockW*BlockH> Res) {+ // static_assert(NBlocks == 1);+ // auto pitch = Desc.get_pitch() + 1;+ // auto base = reinterpret_cast<svmptr_t>(Desc.get_base() + Desc.get_block_y()*pitch + Desc.get_block_x() * sizeof(T));+ // #pragma unroll+ // for(int i = 0; i < BlockH; i++) {+ // cm_svm_block_write(base + i * pitch, Res.select<BlockW, 1>(i*BlockW));+ // }+ // }#endif//===============================================================================================@@ -416,4 +398,4 @@ inline void prepackAsVNNIWidth2(matrix_ref<T1, K, N> input, matrix_ref<T2, K/2,out.row(r).select<N, 2>(0) = input.row(r*2);out.row(r).select<N, 2>(1) = input.row(r*2+1);}-}+}\ No newline at end of filediff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_common.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_common.hppindex acbecd52a92..c6b889b3036 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_common.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/cm_pa_common.hpp@@ -1,18 +1,7 @@-/*******************************************************************************- * Copyright (c) 2018-2026 Intel Corporation- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- *******************************************************************************/+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//+#include "cm_attention_common.hpp"#if CMPA_KVCACHE_U8@@ -26,9 +15,19 @@ void pa_lsc_u8(int kv_stop,int q_len,int kv_len,+#ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t q_base [[type("svmptr_t")]],+#else+ SurfaceIndex q_gather,+ uint32_t q_gather_offset_bytes,+#endifsvmptr_t k_cache_base [[type("svmptr_t")]],+#ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t v_cache_base [[type("svmptr_t")]],+#else+ SurfaceIndex v_cache_stateful [[type("buffer_t")]],+ uint32_t v_cache_stateful_offset_bytes,+#endif#if IS_BLOCK_SPARSEsvmptr_t sparse_mask_base [[type("svmptr_t")]],svmptr_t wg_sparse_mask_base [[type("svmptr_t")]],@@ -55,23 +54,59 @@ void pa_lsc_u8(auto q_tokens_left = q_len;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 (q_tokens_left < 0) q_tokens_left = 0;if (q_tokens_left > q_step) q_tokens_left = q_step;-if (q_tokens_left > 0) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_left - 1, head_size*sizeof(half) - 1, q_pitch - 1, 0, 0);#pragma unrollfor(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);}- }+ #else+ constexpr int q_tile_uints = REG_K / 2;+ constexpr int q_tile_elems = q_tile_uints * REG_N;+ vector<ushort, q_tile_elems> gather_pred;++ #pragma unroll+ for (int ri = 0; ri < head_size/REG_K; ri++) {+ vector<uint, q_tile_elems> gather_offsets;+ uint col_uint_base = ri * q_tile_uints;++ #pragma unroll+ for (int col = 0; col < REG_N; col++) {+ bool active = (col < q_tokens_left);+ uint token_base = q_gather_offset_bytes + col * q_pitch;+ #pragma unroll+ for (int row = 0; row < q_tile_uints; row++) {+ int idx = row * REG_N + col;+ uint col_byte = (col_uint_base + row) * sizeof(uint);+ gather_offsets[idx] = token_base + col_byte;+ gather_pred[idx] = active * 0xFFFF;+ }+ }++ rQ[ri] = 0;+ auto gathered = cm_load<uint,+ VectorSize::N1,+ DataSize::U32,+ CacheHint::Cached,+ CacheHint::Cached>(q_gather, gather_offsets, gather_pred);+ rQ[ri].format<uint>() = gathered;+ rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);+ }+ #endif+ }+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<uint8_t, 1, kv_step, REG_K> b2dK(k_cache_base, CMPA_BLOCK_SZ - 1, head_size*sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);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);+ #endifconstexpr int quan_blk_stride = CMFLA_NUM_KV_HEADS * (CMFLA_HEAD_SIZE+4) * CMPA_BLOCK_SZ * sizeof(uint8_t);- int causal_left = q_start+past_lens;+ int causal_left = q_start + past_lens;constexpr uint slm_buff_size = kv_step * head_size * sizeof(half);int slm_buff_id_write = 0;@@ -113,11 +148,20 @@ void pa_lsc_u8(matrix<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>();+ #ifdef CM_HAS_LSC_UNTYPED_2Db2dK.set_base_ptr(reinterpret_cast<uint8_t*>(k_cache_base+cur_block_id*quan_blk_stride));b2dK.set_block_y(kv_pos%CMPA_BLOCK_SZ);-+ #endiffor(int k = REG_K*wg_local_id; k < head_size; k += REG_K*(local_size/2)) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Normal>(quanKmat.format<uint8_t>(), b2dK.set_block_x(k));+ #else+ 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);+ #pragma unroll+ for(int r = 0; r < kv_step; r++) {+ cm_svm_block_read(k_base + r * kv_pitch, quanKmat.row(r));+ }+ #endif/*@bug: cm compiler in the tail process.: loop combined with type convert.for(int r = 0; r < kv_left; r++) {@@ -137,18 +181,47 @@ 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);+#ifdef CM_HAS_LSC_UNTYPED_2D+ 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);+#else+ // dscale/zp are half[kv_step] => 32 bytes each when kv_step=16+ constexpr int half16_bytes = 16 * sizeof(half); // 32+ constexpr 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);+ 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;+ zp.format<uint>() = zp_u32;+#endifmatrix<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>();+ #ifdef CM_HAS_LSC_UNTYPED_2Db2dV.set_base_ptr(reinterpret_cast<uint8_t*>(v_cache_base+cur_block_id*quan_blk_stride));b2dV.set_block_y(kv_pos%CMPA_BLOCK_SZ);-+ #endif#pragma unrollfor(int k = REG_N*(wg_local_id-(local_size/2)); k < head_size; k += REG_N*(local_size/2)) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Normal>(quanVmat.format<uint8_t>(), b2dV.set_block_x(k));+ #else+ // Each row loads REG_N bytes (REG_N=16) => 4 dwords+ constexpr int v_row_u32 = REG_N / 4; // 4+ #pragma unroll+ for (int r = 0; r < REG_K; r++) {+ uint elem_off_bytes =+ cur_block_id * quan_blk_stride ++ (kv_pos % CMPA_BLOCK_SZ) * kv_pitch ++ r * kv_pitch ++ k;+ uint cur_off = v_cache_stateful_offset_bytes + elem_off_bytes;+ auto row_u32 = cm_load<uint, v_row_u32>(v_cache_stateful, cur_off);+ quanVmat.row(r).format<uint>() = row_u32;+ }+ #endif/*@bug: cm compiler in the tail process.: loop combined with type convert.for(int r = 0; r < kv_left; r++) {@@ -213,11 +286,28 @@ void pa_lsc_u8(//# St = k @ Qtmatrix<float, kv_step, q_step> St = ugemm_KQ(slm_K, rQ, slm_offset);if constexpr (use_causal_mask) {- // since kv_step == q_step == 16, causal_left is n*kv_step- if (causal_left == 0) {- apply_causal_mask<1>(St);- } else if (causal_left < 0) {- St = -3.4e38f;+ if constexpr (kv_step == q_step) {+ // since kv_step == q_step == 16, causal_left is n * kv_step+ if (causal_left == 0) {+ apply_causal_mask<1>(St);+ } else if (causal_left < 0) {+ St = -3.4e38f;+ }+ } else {+ if (causal_left == 0) {+ // q_step is half of kv_step+ // calsual mask first half of the kv+ apply_causal_mask<1>(St.select<q_step, 1, q_step, 1>(0, 0));+ St.select<q_step, 1, q_step, 1>(q_step, 0) = -3.4e38f;+ } else if (causal_left < 0) {+ St = -3.4e38f;+ } else if (causal_left < kv_step) {+ // q_step is half of kv_step+ // Workaround for an IGC ICE on ARL-H triggered by submatrix in-place masking.+ // Materialize St before applying the partial causal mask.+ St += 0.f;+ apply_causal_mask<1>(St.select<q_step, 1, q_step, 1>(q_step, 0));+ }}causal_left -= kv_step;} else {@@ -243,10 +333,14 @@ void pa_lsc_u8(matrix<half, num_P_tiles*REG_M, REG_N> cur_O_f16;cur_sum = cm_inv(cur_sum);+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_left - 1, head_size*sizeof(half) - 1, o_pitch - 1, 0, 0);-+ #endif#pragma unrollfor(int k = 0, ri=0; k < head_size; k += REG_N, ri += num_P_tiles) {+ #ifdef CM_HAS_LSC_UNTYPED_2D+ b2dO.set_block_x(k);+ #endif#pragma unrollfor(int p = 0; p < num_P_tiles; p++) {auto cO = rO[ri + p].format<float, REG_M, REG_N>();@@ -254,10 +348,18 @@ void pa_lsc_u8(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]);}+ #ifdef CM_HAS_LSC_UNTYPED_2D+ cm_store(b2dO.set_block_y(p * REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(p));+ #else+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + p * REG_M * o_stride_elems + k;+ auto cur_O_ref = cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(p).format<half, REG_M, REG_N>();+ #pragma unroll+ for (int r = 0; r < REG_M; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_ref.row(r).format<half>());+ }+ #endif}- 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));}}@@ -270,9 +372,19 @@ void pa_kernel_lsc_prefetch_f16(int kv_stop, //int q_len, //q_stepint kv_len, //not used for now+#ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t q_base [[type("svmptr_t")]],+#else+ SurfaceIndex q_gather,+ uint32_t q_gather_offset_bytes,+#endifsvmptr_t k_cache_base [[type("svmptr_t")]],+#ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t v_cache_base [[type("svmptr_t")]],+ #else+ SurfaceIndex v_cache_stateful,+ uint32_t v_cache_stateful_offset_bytes,+#endif#if IS_BLOCK_SPARSEsvmptr_t sparse_mask_base [[type("svmptr_t")]],svmptr_t wg_sparse_mask_base [[type("svmptr_t")]],@@ -286,7 +398,7 @@ void pa_kernel_lsc_prefetch_f16(// constexpr uint k_pitch = is_qkv_fused ? q_pitch : (num_kv_heads * head_size * sizeof(half));// constexpr uint v_pitch = is_qkv_fused ? q_pitch : (num_kv_heads * head_size * sizeof(half));//[block_num, kv_heads, block_size, head_size]- constexpr uint k_pitch = head_size * sizeof(half);+ constexpr uint k_pitch = head_size * sizeof(half);constexpr uint v_pitch = k_pitch;vector<float, q_step> cur_max;@@ -295,32 +407,75 @@ void pa_kernel_lsc_prefetch_f16(cur_max = -3e38f;cur_sum = 0;constexpr int num_P_tiles = REG_N / REG_M;+#ifdef CM_HAS_LSC_UNTYPED_2D+ constexpr int VALUE_TILE_NUM = 1;+#else+ constexpr int VALUE_TILE_NUM = 2;+#endifmatrix<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 <float, head_size/REG_M, REG_M*REG_N> rO;auto q_tokens_left = q_len;// - q_start;static_assert(q_step == REG_N);static_assert(kv_step == REG_K);+ static_assert(REG_N % REG_M == 0, "REG_N must be divisible by REG_M");+ static_assert(head_size % REG_M == 0, "head_size must be divisible by REG_M");if (q_tokens_left < 0) q_tokens_left = 0;if (q_tokens_left > q_step) q_tokens_left = q_step;+ #ifndef CM_HAS_LSC_UNTYPED_2D+ if (q_tokens_left == 0) return;+ #endifif (q_tokens_left > 0) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dQ(reinterpret_cast<uint*>(q_base), q_tokens_left - 1, head_size*sizeof(half) - 1, q_pitch - 1, 0, 0);#pragma unrollfor(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);}+ #else+ constexpr int q_tile_uints = REG_K / 2;+ constexpr int q_tile_elems = q_tile_uints * REG_N;+ vector<ushort, q_tile_elems> gather_pred;++ #pragma unroll+ for (int ri = 0; ri < head_size/REG_K; ri++) {+ vector<unsigned, q_tile_elems> gather_offsets;+ uint col_uint_base = ri * q_tile_uints;+ #pragma unroll+ for (int col = 0; col < REG_N; col++) {+ bool active = (col < q_tokens_left);+ uint token_base = q_gather_offset_bytes + col * q_pitch;+ #pragma unroll+ for (int row = 0; row < q_tile_uints; row++) {+ int idx = row * REG_N + col;+ uint col_byte = (col_uint_base + row) * sizeof(uint);+ gather_offsets[idx] = token_base + col_byte;+ gather_pred[idx] = active * 0xFFFF;+ }+ }+ rQ[ri] = 0;+ auto gathered =cm_load<uint,+ VectorSize::N1,+ DataSize::U32,+ CacheHint::Cached,+ CacheHint::Cached>(q_gather, gather_offsets, gather_pred);+ rQ[ri].format<uint>() = gathered;+ rQ[ri].format<half>() = cm_mul<half>(rQ[ri].format<half>(), (half)scale_factor);+ }+ #endif}+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::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);lsc::block_2d_desc<half, 1, REG_K, REG_N> b2dV(v_cache_base, CMPA_BLOCK_SZ - 1, head_size*sizeof(half) - 1, v_pitch - 1, 0, 0);-static_assert(wg_local_size == 16);lsc::block_2d_desc<half, 1, kv_step/wg_local_size, REG_K> prefetch_K(k_cache_base, CMPA_BLOCK_SZ - 1, head_size*sizeof(half) - 1, k_pitch - 1, 0, 0);lsc::block_2d_desc<half, 1, REG_K/wg_local_size, REG_N> prefetch_V(v_cache_base, CMPA_BLOCK_SZ - 1, head_size*sizeof(half) - 1, v_pitch - 1, 0, 0);- constexpr int blk_stride = CMFLA_NUM_KV_HEADS*CMFLA_HEAD_SIZE*CMPA_BLOCK_SZ;+ #endif+ constexpr int blk_stride = CMFLA_NUM_KV_HEADS * CMFLA_HEAD_SIZE*CMPA_BLOCK_SZ;int causal_left = q_start+past_lens;for(int kv_pos = 0; kv_pos < kv_stop; kv_pos += kv_step) {@@ -331,36 +486,52 @@ void pa_kernel_lsc_prefetch_f16(//# St = k @ Qtmatrix<float, kv_step, q_step> St;{- constexpr int num_K = kv_step/REG_M;+ constexpr int num_K = kv_step / REG_M;auto St2 = St.format<float, num_K, REG_M*REG_N>();matrix<half, num_K, REG_M * REG_K> Kmat;- prefetch_K.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+prefetch_block_id*blk_stride));+ #ifdef CM_HAS_LSC_UNTYPED_2D+ 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));+ #else+ half* prefetch_k_pos = (half*)k_cache_base + prefetch_block_id * blk_stride + ((prefetch_kv_pos + wg_local_id) % CMPA_BLOCK_SZ) * head_size;+ cm_ptr_prefetch<REG_K/2, DataSize::U32, CacheHint::Cached, CacheHint::Cached>((const unsigned int *const)prefetch_k_pos, 0);+ #endif#if IS_BLOCK_SPARSE- if (SPARSE_BLOCK_SIZE > 1)- {- auto kv_start_block = kv_pos/ SPARSE_BLOCK_SIZE;- bool sparse_mask = *(reinterpret_cast<bool*>(sparse_mask_base) + kv_start_block);- if (!sparse_mask) {- if constexpr (use_causal_mask) {- causal_left -= kv_step;- }- continue;- }+ const int sb_shift = (SPARSE_BLOCK_SIZE == 128) ? 7 :+ (SPARSE_BLOCK_SIZE == 256) ? 8 : -1;++ auto skip_compute = [&](int pos) -> bool {+ if (sb_shift < 0) return false;+ return !*(reinterpret_cast<bool*>(sparse_mask_base) + ((uint)pos >> sb_shift));+ };++ if (SPARSE_BLOCK_SIZE > 1 && skip_compute(kv_pos)) {+ if constexpr (use_causal_mask)+ causal_left -= kv_step;+ continue;}#endif+ #ifdef CM_HAS_LSC_UNTYPED_2Db2dK.set_base_ptr((reinterpret_cast<half*>(k_cache_base)+cur_block_id*blk_stride));- b2dK.set_block_y(kv_pos%CMPA_BLOCK_SZ);+ b2dK.set_block_y(kv_pos % CMPA_BLOCK_SZ);cm_load<lsc::Normal>(Kmat.format<half>(), b2dK.set_block_x(0));+ #else+ half* base_k_cache_ptr = (half*)k_cache_base + cur_block_id*blk_stride + (kv_pos % CMPA_BLOCK_SZ)*head_size;+ auto kmatref = Kmat.format<half, kv_step, REG_K>();+ #pragma unroll+ for(int kr = 0; kr < kv_step; kr++){+ cm_svm_block_read<half, REG_K>((svmptr_t)((half*)base_k_cache_ptr + kr * head_size), kmatref.row(kr));+ }+ #endif// somtimes 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;+ Kmat.format<half, num_K * REG_M, REG_K>().row(r) = 0.f;}#pragma unrollfor(int k = 0; k < num_K; k++)@@ -371,8 +542,22 @@ void pa_kernel_lsc_prefetch_f16(#pragma unrollfor(int ri = 1; ri < head_size/REG_K; ri++) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_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));+ #else+ cm_ptr_prefetch<REG_K/2, DataSize::U32, CacheHint::Cached, CacheHint::Cached>((const unsigned int *const)prefetch_k_pos, ri*REG_K/2);+ #pragma unroll+ for(int kr = 0; kr < kv_step; kr++){+ cm_svm_block_read<half, REG_K>((svmptr_t)((half*)base_k_cache_ptr + kr * head_size + ri * REG_K), kmatref.row(kr));+ }+ #endif+ // somtimes 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_K>().row(r) = 0.f;+ }#pragma unrollfor(int k = 0; k < num_K; k++) {St2.row(k) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(@@ -383,12 +568,29 @@ void pa_kernel_lsc_prefetch_f16(}}if constexpr (use_causal_mask) {- // since kv_step == q_step == 16, causal_left is n*kv_step+ if constexpr (kv_step == q_step) {+ // since kv_step == q_step == 16, causal_left is n * kv_stepif (causal_left == 0) {apply_causal_mask<1>(St);} else if (causal_left < 0) {St = -3.4e38f;}+ } else {+ if (causal_left == 0) {+ // q_step is half of kv_step+ // calsual mask first half of the kv+ apply_causal_mask<1>(St.select<q_step, 1, q_step, 1>(0, 0));+ St.select<q_step, 1, q_step, 1>(q_step, 0) = -3.4e38f;+ } else if (causal_left < 0) {+ St = -3.4e38f;+ } else if (causal_left < kv_step) {+ // q_step is half of kv_step+ // calsual mask second half of the kv+ // if w/o St += 0.f;, I will meet IGC: Internal Compiler Error: Access violation on ARL-H+ St += 0.f;+ apply_causal_mask<1>(St.select<q_step, 1, q_step, 1>(q_step, 0));+ }+ }causal_left -= kv_step;} else {int kv_tokens = kv_stop - kv_pos;@@ -396,103 +598,141 @@ void pa_kernel_lsc_prefetch_f16(for(int p = kv_tokens; p < kv_step; p++) St[p] = -3.4e38f;}- //show(St);+ // show(St);auto max_comp = online_softmax_update(St, cur_max, cur_sum);matrix<half, REG_N, REG_K> P;Transpose2DMatrix(St, P);+ #ifdef CM_HAS_LSC_UNTYPED_2Dprefetch_V.set_base_ptr((reinterpret_cast<half*>(v_cache_base)+prefetch_block_id*blk_stride));prefetch_V.set_block_y((prefetch_kv_pos + wg_local_id) % CMPA_BLOCK_SZ);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 (kv_pos == 0) {- // ugemm_PV0(slm_V, P, rO, slm_offset);- auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ #endif+ auto P2 = P.format<half, num_P_tiles, REG_M * REG_K>();+ matrix<half, REG_K/2, REG_N*2*VALUE_TILE_NUM> Vmat;+ #pragma unroll+ for(int k = 0, ri=0; k < head_size; k += REG_N * VALUE_TILE_NUM, ri += num_P_tiles * VALUE_TILE_NUM) {+ #ifdef CM_HAS_LSC_UNTYPED_2D+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(k));+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(k));+ #else+ 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 k = 0, ri = 0; k < 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));- // somtimes 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[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(- 0,- Vmat.format<int32_t>(),- P2.row(p).format<int32_t>());- }+ 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;}- }- else {- //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) {- matrix<half, REG_K/2, REG_N*2> Vmat;+ 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);+ }+ #endif+ #ifdef CM_HAS_LSC_UNTYPED_2D+ // somtimes 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 < REG_K/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;+ }+ #endif- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(prefetch_V.set_block_x(k));- cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_x(k));- // somtimes 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;+ if (kv_pos == 0) {+ #pragma unroll+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ int rO_base = ri + tile * num_P_tiles;+ 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);+ #pragma unroll+ for (int p = 0; p < num_P_tiles; p++) {+ rO[rO_base + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount, float>(+ 0,+ Vtile.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }}- //# compensate cur_O- // matrix <float, head_size/REG_K*2, REG_M*REG_N> rO;+ } else {#pragma unroll- for(int p = 0; p < num_P_tiles; p++) {- auto cO = rO[ri + p].format<float, REG_M, REG_N>();+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ int rO_base = ri + tile * num_P_tiles;#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]);+ for(int p = 0; p < num_P_tiles; p++) {+ auto cO = rO[rO_base + 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[ri + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(- rO[ri + p].format<float>(),- Vmat.format<int32_t>(),- P2.row(p).format<int32_t>());+ for (int tile = 0; tile < VALUE_TILE_NUM; tile++) {+ int rO_base = ri + tile * num_P_tiles;+ 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);+ #pragma unroll+ for (int p = 0; p < num_P_tiles; p++) {+ rO[rO_base + p] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(+ rO[rO_base + p].format<float>(),+ Vtile.format<int32_t>(),+ P2.row(p).format<int32_t>());+ }}}}}+ #ifdef CM_HAS_LSC_UNTYPED_2Dif (q_tokens_left == 0) return;+ #endif//# save cur_O/cur_sum.transpose(0, 1)- matrix<half, num_P_tiles*REG_M, REG_N> cur_O_f16;+ matrix<half, num_P_tiles * REG_M, REG_N> cur_O_f16;cur_sum = cm_inv(cur_sum);+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<half, 1, REG_M, REG_N> b2dO(o_base, q_tokens_left - 1, head_size*sizeof(half) - 1, o_pitch - 1, 0, 0);-+ #endif#pragma unrollfor(int k = 0, ri=0; k < head_size; k += REG_N, ri += num_P_tiles) {+ #ifdef CM_HAS_LSC_UNTYPED_2D+ b2dO.set_block_x(k);+ #endif#pragma unrollfor(int p = 0; p < num_P_tiles; p++) {auto cO = rO[ri + p].format<float, REG_M, REG_N>();#pragma unrollfor(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]);-}+ #ifdef CM_HAS_LSC_UNTYPED_2D+ cm_store(b2dO.set_block_y(p * REG_M), cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(p));+ #else+ int o_stride_elems = o_pitch / sizeof(half);+ half* output_ptr = (half*)o_base + p * REG_M * o_stride_elems + k;+ auto cur_O_ref = cur_O_f16.format<half, num_P_tiles, REG_M * REG_N>().row(p).format<half, REG_M, REG_N>();+ #pragma unroll+ for (int r = 0; r < REG_M; r++) {+ cm_svm_block_write<half, REG_N>((svmptr_t)(output_ptr + r * o_stride_elems), cur_O_ref.row(r).format<half>());+ }+ #endif}- 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));}}diff --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 17b8803629a..c897a9d5b8b 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@@ -1,24 +1,6 @@-/*- * Copyright (c) 2020-2023, Intel Corporation- *- * Permission is hereby granted, free of charge, to any person obtaining a- * copy of this software and associated documentation files (the "Software"),- * to deal in the Software without restriction, including without limitation- * the rights to use, copy, modify, merge, publish, distribute, sublicense,- * and/or sell copies of the Software, and to permit persons to whom the- * Software is furnished to do so, subject to the following conditions:- *- * The above copyright notice and this permission notice shall be included- * in all copies or substantial portions of the Software.- *- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR- * OTHER DEALINGS IN THE SOFTWARE.- */+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//#include <cm/cm.h>#include <cm/cmtl.h>@@ -79,155 +61,13 @@ void show(const matrix_ref<T, M, N> mat) {printf("]\n");}-template <typename T1, typename T2>-CM_INLINE void Transpose_8x8(matrix_ref<T1, 8, 8> in, matrix_ref<T2, 8, 8> out) {- matrix<T2, 8, 8> temp;- temp.row(0) = in.template select<2, 1, 4, 2>(0, 0);- temp.row(1) = in.template select<2, 1, 4, 2>(2, 0);- temp.row(2) = in.template select<2, 1, 4, 2>(4, 0);- temp.row(3) = in.template select<2, 1, 4, 2>(6, 0);- temp.row(4) = in.template select<2, 1, 4, 2>(0, 1);- temp.row(5) = in.template select<2, 1, 4, 2>(2, 1);- temp.row(6) = in.template select<2, 1, 4, 2>(4, 1);- temp.row(7) = in.template select<2, 1, 4, 2>(6, 1);-- out.row(0) = temp.template select<4, 1, 2, 4>(0, 0);- out.row(2) = temp.template select<4, 1, 2, 4>(0, 1);- out.row(4) = temp.template select<4, 1, 2, 4>(0, 2);- out.row(6) = temp.template select<4, 1, 2, 4>(0, 3);- out.row(1) = temp.template select<4, 1, 2, 4>(4, 0);- out.row(3) = temp.template select<4, 1, 2, 4>(4, 1);- out.row(5) = temp.template select<4, 1, 2, 4>(4, 2);- out.row(7) = temp.template select<4, 1, 2, 4>(4, 3);-}--template <typename T1, typename T2>-CM_INLINE void Transpose_8x32(matrix_ref<T1, 8, 32> in, matrix_ref<T2, 32, 8> out) {- Transpose_8x8(in.template select<8, 1, 8, 1>(0, 0), out.template select<8, 1, 8, 1>( 0, 0));- Transpose_8x8(in.template select<8, 1, 8, 1>(0, 8), out.template select<8, 1, 8, 1>( 8, 0));- Transpose_8x8(in.template select<8, 1, 8, 1>(0, 16), out.template select<8, 1, 8, 1>(16, 0));- Transpose_8x8(in.template select<8, 1, 8, 1>(0, 24), out.template select<8, 1, 8, 1>(24, 0));-}--template <typename T1, typename T2>-CM_INLINE void Transpose_4x32(matrix_ref<T1, 4, 32> in, matrix_ref<T2, 32, 4> out) {- matrix<T2, 4, 32> temp;- temp.row(0) = in.template select<4, 1, 8, 4>(0, 0);- temp.row(1) = in.template select<4, 1, 8, 4>(0, 1);- temp.row(2) = in.template select<4, 1, 8, 4>(0, 2);- temp.row(3) = in.template select<4, 1, 8, 4>(0, 3);-- out.row( 0) = temp.template select<1, 1, 4, 8>(0, 0);- out.row( 1) = temp.template select<1, 1, 4, 8>(1, 0);- out.row( 2) = temp.template select<1, 1, 4, 8>(2, 0);- out.row( 3) = temp.template select<1, 1, 4, 8>(3, 0);- out.row( 4) = temp.template select<1, 1, 4, 8>(0, 1);- out.row( 5) = temp.template select<1, 1, 4, 8>(1, 1);- out.row( 6) = temp.template select<1, 1, 4, 8>(2, 1);- out.row( 7) = temp.template select<1, 1, 4, 8>(3, 1);- out.row( 8) = temp.template select<1, 1, 4, 8>(0, 2);- out.row( 9) = temp.template select<1, 1, 4, 8>(1, 2);- out.row(10) = temp.template select<1, 1, 4, 8>(2, 2);- out.row(11) = temp.template select<1, 1, 4, 8>(3, 2);- out.row(12) = temp.template select<1, 1, 4, 8>(0, 3);- out.row(13) = temp.template select<1, 1, 4, 8>(1, 3);- out.row(14) = temp.template select<1, 1, 4, 8>(2, 3);- out.row(15) = temp.template select<1, 1, 4, 8>(3, 3);- out.row(16) = temp.template select<1, 1, 4, 8>(0, 4);- out.row(17) = temp.template select<1, 1, 4, 8>(1, 4);- out.row(18) = temp.template select<1, 1, 4, 8>(2, 4);- out.row(19) = temp.template select<1, 1, 4, 8>(3, 4);- out.row(20) = temp.template select<1, 1, 4, 8>(0, 5);- out.row(21) = temp.template select<1, 1, 4, 8>(1, 5);- out.row(22) = temp.template select<1, 1, 4, 8>(2, 5);- out.row(23) = temp.template select<1, 1, 4, 8>(3, 5);- out.row(24) = temp.template select<1, 1, 4, 8>(0, 6);- out.row(25) = temp.template select<1, 1, 4, 8>(1, 6);- out.row(26) = temp.template select<1, 1, 4, 8>(2, 6);- out.row(27) = temp.template select<1, 1, 4, 8>(3, 6);- out.row(28) = temp.template select<1, 1, 4, 8>(0, 7);- out.row(29) = temp.template select<1, 1, 4, 8>(1, 7);- out.row(30) = temp.template select<1, 1, 4, 8>(2, 7);- out.row(31) = temp.template select<1, 1, 4, 8>(3, 7);-}--template <typename T1, typename T2>-CM_INLINE void Transpose_32x32(matrix_ref<T1, 32, 32> in, matrix_ref<T2, 32, 32> out) {- matrix<T2, 32, 32> temp;- temp.row( 0) = in.template select<8, 1, 4, 8>( 0, 0);- temp.row( 1) = in.template select<8, 1, 4, 8>( 8, 0);- temp.row( 2) = in.template select<8, 1, 4, 8>(16, 0);- temp.row( 3) = in.template select<8, 1, 4, 8>(24, 0);- temp.row( 4) = in.template select<8, 1, 4, 8>( 0, 1);- temp.row( 5) = in.template select<8, 1, 4, 8>( 8, 1);- temp.row( 6) = in.template select<8, 1, 4, 8>(16, 1);- temp.row( 7) = in.template select<8, 1, 4, 8>(24, 1);- temp.row( 8) = in.template select<8, 1, 4, 8>( 0, 2);- temp.row( 9) = in.template select<8, 1, 4, 8>( 8, 2);- temp.row(10) = in.template select<8, 1, 4, 8>(16, 2);- temp.row(11) = in.template select<8, 1, 4, 8>(24, 2);- temp.row(12) = in.template select<8, 1, 4, 8>( 0, 3);- temp.row(13) = in.template select<8, 1, 4, 8>( 8, 3);- temp.row(14) = in.template select<8, 1, 4, 8>(16, 3);- temp.row(15) = in.template select<8, 1, 4, 8>(24, 3);- temp.row(16) = in.template select<8, 1, 4, 8>( 0, 4);- temp.row(17) = in.template select<8, 1, 4, 8>( 8, 4);- temp.row(18) = in.template select<8, 1, 4, 8>(16, 4);- temp.row(19) = in.template select<8, 1, 4, 8>(24, 4);- temp.row(20) = in.template select<8, 1, 4, 8>( 0, 5);- temp.row(21) = in.template select<8, 1, 4, 8>( 8, 5);- temp.row(22) = in.template select<8, 1, 4, 8>(16, 5);- temp.row(23) = in.template select<8, 1, 4, 8>(24, 5);- temp.row(24) = in.template select<8, 1, 4, 8>( 0, 6);- temp.row(25) = in.template select<8, 1, 4, 8>( 8, 6);- temp.row(26) = in.template select<8, 1, 4, 8>(16, 6);- temp.row(27) = in.template select<8, 1, 4, 8>(24, 6);- temp.row(28) = in.template select<8, 1, 4, 8>( 0, 7);- temp.row(29) = in.template select<8, 1, 4, 8>( 8, 7);- temp.row(30) = in.template select<8, 1, 4, 8>(16, 7);- temp.row(31) = in.template select<8, 1, 4, 8>(24, 7);-- out.row( 0) = temp.template select<4, 1, 8, 4>( 0, 0);- out.row( 1) = temp.template select<4, 1, 8, 4>( 4, 0);- out.row( 2) = temp.template select<4, 1, 8, 4>( 8, 0);- out.row( 3) = temp.template select<4, 1, 8, 4>(12, 0);- out.row( 4) = temp.template select<4, 1, 8, 4>(16, 0);- out.row( 5) = temp.template select<4, 1, 8, 4>(20, 0);- out.row( 6) = temp.template select<4, 1, 8, 4>(24, 0);- out.row( 7) = temp.template select<4, 1, 8, 4>(28, 0);- out.row( 8) = temp.template select<4, 1, 8, 4>( 0, 1);- out.row( 9) = temp.template select<4, 1, 8, 4>( 4, 1);- out.row(10) = temp.template select<4, 1, 8, 4>( 8, 1);- out.row(11) = temp.template select<4, 1, 8, 4>(12, 1);- out.row(12) = temp.template select<4, 1, 8, 4>(16, 1);- out.row(13) = temp.template select<4, 1, 8, 4>(20, 1);- out.row(14) = temp.template select<4, 1, 8, 4>(24, 1);- out.row(15) = temp.template select<4, 1, 8, 4>(28, 1);- out.row(16) = temp.template select<4, 1, 8, 4>( 0, 2);- out.row(17) = temp.template select<4, 1, 8, 4>( 4, 2);- out.row(18) = temp.template select<4, 1, 8, 4>( 8, 2);- out.row(19) = temp.template select<4, 1, 8, 4>(12, 2);- out.row(20) = temp.template select<4, 1, 8, 4>(16, 2);- out.row(21) = temp.template select<4, 1, 8, 4>(20, 2);- out.row(22) = temp.template select<4, 1, 8, 4>(24, 2);- out.row(23) = temp.template select<4, 1, 8, 4>(28, 2);- out.row(24) = temp.template select<4, 1, 8, 4>( 0, 3);- out.row(25) = temp.template select<4, 1, 8, 4>( 4, 3);- out.row(26) = temp.template select<4, 1, 8, 4>( 8, 3);- out.row(27) = temp.template select<4, 1, 8, 4>(12, 3);- out.row(28) = temp.template select<4, 1, 8, 4>(16, 3);- out.row(29) = temp.template select<4, 1, 8, 4>(20, 3);- out.row(30) = temp.template select<4, 1, 8, 4>(24, 3);- out.row(31) = temp.template select<4, 1, 8, 4>(28, 3);-}-// group_count: M = group_count * group_size, group_size is the element count of current reduction// op: 0-max, 1-sum// M: before reduce element count, N: row count, stop: element count M must be larger thantemplate <int group_count, int op, int stop, typename T, int N, int M>CM_INLINE constexpr auto reduce2d(matrix_ref<T, N, M> src) {constexpr int group_size = M / group_count;- if constexpr (N > stop) {+ if constexpr (N > stop && group_size > 1) {matrix<T, N / 2, M> result;// half of group will be reducedconstexpr int new_group_size = group_size / 2;@@ -286,32 +126,124 @@ CM_INLINE void write_2d(matrix_ref<TSRC, M, N> out, SurfaceIndex base, uint offs}}+template <int M, int N>+CM_INLINE void cm_load_2d(matrix_ref<uint, M, N> out, SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ out.row(i).format<uint>() = cm_load<uint, N>(base, offset + i * pitch);+ }+}++template <int M, int N>+CM_INLINE void cm_load_2d(matrix_ref<half, M, N> out, SurfaceIndex base, uint offset, uint pitch, bool showit = 0) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ out.row(i).format<uint>() = cm_load<uint, N/2,DataSize::U32, CacheHint::Cached, CacheHint::Cached>(base, offset + i * pitch);+ }+}++//half+template <int M, int N>+CM_INLINE void cm_prefetch_2d(SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < M; i++) {+ cm_prefetch<N/2, DataSize::U32, CacheHint::Cached, CacheHint::Cached>(base, offset + i * pitch);+ }+}++template <int M, int N>+CM_INLINE void cm_gather_2d(matrix_ref<half, M, N> out, SurfaceIndex base, uint offset, uint pitch, bool showit = 0) {+ vector<uint, M> offsets;+ #pragma unroll+ for(int i = 0; i < M; i++) {+ offsets[i] = offset + i * pitch;+ }+ // if (showit) {+ // // printf("M=%d", M);+ // show_i(offsets);+ // }+ out.format<uint>() = cm_load<uint, VectorSize::N8>(base, offsets);+ // if (showit) {+ // show(out.format<half, M, N>());+ // }+}++template <int M, int N>+CM_INLINE void cm_store_2d(matrix_ref<half, M, N> out, SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ cm_store<uint, N/2>(base, offset + i * pitch, out.row(i).format<uint>());+ }+}++template <int M, int N>+CM_INLINE void cm_store_2d(matrix_ref<uint, M, N> out, SurfaceIndex base, uint offset, uint pitch) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ cm_store<uint, N>(base, offset + i * pitch, out.row(i).format<uint>());+ }+}++#if USE_INT8+ #define KV_ELEMENT_TYPE uint8_t+#else+ #define KV_ELEMENT_TYPE half+#endif+#if 1 || (BLOCK_SG_M == 64 && BLOCK_SG_N == 32)// const static int channels_reduce_32[] = { 0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30,// 1, 17, 9, 25, 5, 21, 13, 29, 3, 19, 11, 27, 7, 23, 15, 31};// src_a is query, src_b is key-CM_INLINE void gemm_qk_64x32_xe2(uint id_wg_m, uint id_wg_n, uint hq, uint slm, svmptr_t key_cache ATTR, svmptr_t query ATTR, svmptr_t block_indices ATTR, svmptr_t block_indices_begins ATTR, svmptr_t kq_max_wg ATTR, svmptr_t kq_exp_partial_sum ATTR,-uint M, uint N, uint K, uint query_stride, uint q_start_strided) {- constexpr int SG_SIZE = 16;++CM_INLINE void gemm_qk(uint id_wg_m, uint id_wg_n, uint hq, uint slm,+ #ifdef CM_HAS_LSC_UNTYPED_2D+ svmptr_t key_cache ATTR,+ svmptr_t query ATTR,+ #else+ SurfaceIndex key_cache [[type("buffer_t")]],+ SurfaceIndex query [[type("buffer_t")]],+ #endif+ svmptr_t block_indices ATTR,+ svmptr_t block_indices_begins ATTR,+ svmptr_t kq_max_wg ATTR,+ #ifdef CM_HAS_LSC_UNTYPED_2D+ svmptr_t kq_exp_partial_sum ATTR,+ #else+ SurfaceIndex kq_exp_partial_sum [[type("buffer_t")]],+ #endif+ const uint M, const uint N, const uint K, const uint query_stride, const uint q_start_strided, const uint offset_partial_sum) {++ constexpr int SG_SIZE = details::get_dpas_execution_size((CmPrecisionType)9);+ // constexpr int BLOCK_WG_K = 64; // same in sg // because unroll 4 times along K ??+ constexpr int SUM_N = BLOCK_SG_N / (BLOCK_SIZE/STRIDE);// xehpg DPAS spec: dst: [8, 8], repeat: 1~8, depth: 8static constexpr int REPEAT = 8;static constexpr int DEPTH = 8;- static constexpr int BLOCK_REG_M = REPEAT;- static constexpr int BLOCK_REG_N = SG_SIZE;- static constexpr int BLOCK_DPAS_C = BLOCK_REG_M * BLOCK_REG_N;+ static constexpr int BLOCK_REG_M = REPEAT; // 8+ static constexpr int BLOCK_REG_N = SG_SIZE; // 16 Xe1?+ static constexpr int BLOCK_DPAS_C = BLOCK_REG_M * BLOCK_REG_N; // src0static constexpr int VNNI = sizeof(half);- static constexpr int BLOCK_REG_K = DEPTH * sizeof(int) / VNNI;- static constexpr int BLOCK_REG_A = BLOCK_REG_M * BLOCK_REG_K;- static constexpr int BLOCK_REG_B = BLOCK_REG_N * BLOCK_REG_K;- static constexpr int BLOCK_WG_M = SG_M * BLOCK_SG_M;- static constexpr int BLOCK_WG_N = SG_N * BLOCK_SG_N;+ static constexpr int BLOCK_REG_K = DEPTH * sizeof(int) / VNNI; // 8*2+ static constexpr int BLOCK_REG_A = BLOCK_REG_M * BLOCK_REG_K; // src2+ static constexpr int BLOCK_REG_B = BLOCK_REG_N * BLOCK_REG_K; // scr1++ static constexpr int BLOCK_WG_M = SG_M * BLOCK_SG_M; // 4*64=256+ static constexpr int BLOCK_WG_N = SG_N * BLOCK_SG_N; // 8*32=256// register blocking- static constexpr int REG_M = BLOCK_SG_M / BLOCK_REG_M;- static constexpr int REG_N = BLOCK_SG_N / BLOCK_REG_N;+ static constexpr int REG_M = BLOCK_SG_M / BLOCK_REG_M; // 64/8=8 times per thread+ static constexpr int REG_N = BLOCK_SG_N / BLOCK_REG_N; // 32/16=2 times per thread Xe1?+ static constexpr int REG_K = BLOCK_WG_K / BLOCK_REG_K; // 64/16=4static constexpr int REG_MN = REG_M * REG_N;- static constexpr int KEY_LINES_PER_LOAD = KV_BLOCK_SIZE / STRIDE;-- matrix<float, REG_M * REG_N, BLOCK_DPAS_C> acc = 0; // --> 64*2 regs+ #ifdef CM_HAS_LSC_UNTYPED_2D+ static constexpr int KEY_LINES_PER_LOAD = KV_BLOCK_SIZE / STRIDE; // 256/16=16, i.e. BLOCK_REG_N because KV_BLOCK_SIZE=BLOCK_REG_N*STRIDE ?+ #else+ static constexpr int KEY_LINES_PER_LOAD = SG_SIZE;+ vector<uint, KEY_LINES_PER_LOAD> gather_offsets_b;+ cmtl::cm_vector_assign(gather_offsets_b.select_all(), 0, 1);+ vector<uint, BLOCK_SG_M> block_offsets_a;+ cmtl::cm_vector_assign(block_offsets_a.select_all(), 0, 1);+ #endif+ matrix<float, REG_M * REG_N, BLOCK_DPAS_C> acc = 0; // --> 64*2 regs // 8*2 x (8*16) Xe1?uint id_sg_n = cm_local_id(0);uint id_sg_m = cm_local_id(1);uint id_sg_mn = id_sg_m * SG_N + id_sg_n;@@ -332,6 +264,8 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {constexpr uint k_block_in_group = BLOCK_WG_N / block_size_div_stride; // 32constexpr uint k_block_in_sg = k_block_in_group / SG_N; // 4+ static constexpr int SG_MN = SG_M * SG_N;+#if IS_CAUSAL == 1if ((int)(id_wg_n * BLOCK_WG_N) >= ((int)id_wg_m + 1) * BLOCK_WG_M + q_start_strided) {// fill -inf -> max in group, 0 -> exp_sum to make compensation work@@ -344,6 +278,7 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {}{// store+ #ifdef CM_HAS_LSC_UNTYPED_2Dmatrix<SOFTMAX_TYPE, 8, 4> sum_t = 0;lsc::block_2d_desc<SOFTMAX_TYPE, 1, 8, 4> desc_c{ kq_exp_partial_sum, M - 1, (uint)(K_block_pad * sizeof(SOFTMAX_TYPE) - 1), (uint)(K_block_pad * sizeof(SOFTMAX_TYPE) - 1),(int)((id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) / block_size_div_stride), (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) };@@ -355,6 +290,19 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 5>(desc_c, sum_t.format<SOFTMAX_TYPE>());cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 6>(desc_c, sum_t.format<SOFTMAX_TYPE>());cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 7>(desc_c, sum_t.format<SOFTMAX_TYPE>());+ #else+ matrix<SOFTMAX_TYPE, 8, SUM_N> sum_t = 0;+ const uint pitch_c = K_block_pad * sizeof(SOFTMAX_TYPE);+ uint off_c = offset_partial_sum;+ off_c += pitch_c * (id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M);+ off_c += ((id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) / block_size_div_stride) * sizeof(SOFTMAX_TYPE);++ #pragma unroll+ for (uint i = 0; i < BLOCK_SG_M/8; i++) {+ cm_store_2d(sum_t.format<uint, 8, SUM_N>(), kq_exp_partial_sum, off_c + (8 * i) * pitch_c, pitch_c);+ }+ #endif+}return;@@ -363,15 +311,26 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {// assume block index coming from 0 in block_indices_beginsint block_index_begin = ((int*)block_indices_begins)[0];int* block_indices_p = (int*)block_indices + block_index_begin;- int b_adjacent_between_head = query_stride / STRIDE;+ int b_adjacent_between_head = query_stride / STRIDE; // HEAD_SIZE * HQ + HEAD_SIZE * HQ (padding 0)// M[0:16*2]xK[0:16]+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<half, 1, 32, BLOCK_REG_K> desc_a{ query, M - 1, (uint)((query_stride - hq * HEAD_SIZE) * sizeof(half) - 1), (uint)(query_stride * sizeof(half) - 1),- (STRIDE - 1) * b_adjacent_between_head, (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) };+ (STRIDE - 1) * b_adjacent_between_head, (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) }; // 32 = BLOCK_SG_M/2 ?// prefetch A- static constexpr int SG_MN = SG_M * SG_N;lsc::block_2d_desc<half, 1, BLOCK_WG_M / SG_MN, PHASE_SIZE> desc_prefetch_a{query, M - 1, (uint)((query_stride - hq * HEAD_SIZE) * sizeof(half) - 1), (uint)(query_stride * sizeof(half) - 1),(STRIDE - 1) * b_adjacent_between_head, (int)(id_wg_m * BLOCK_WG_M + id_sg_mn * (BLOCK_WG_M / SG_MN)) };- // M[0:16*2]xK[0:16] --> 8+8 regs+ #else+ const uint pitch_a = query_stride * sizeof(half);+ uint base_off_a = hq * HEAD_SIZE * (uint)sizeof(half); /*q header base*/+ uint base_off_prefetch_a = base_off_a;++ base_off_a += pitch_a * /*Y_offset*/(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M);+ uint off_a = base_off_a + /*X_offset*/ ((STRIDE - 1) * b_adjacent_between_head) * sizeof(half);++ base_off_prefetch_a += pitch_a * (id_wg_m * BLOCK_WG_M + id_sg_mn * (BLOCK_WG_M / SG_MN));+ uint off_prefetch_a = base_off_prefetch_a + ((STRIDE - 1) * b_adjacent_between_head) * sizeof(half);+ #endif+ // M[0:16*2]xK[0:16] // should be M[0:32*2]xK[0:16] instead ? --> 8+8 regsmatrix<half, REG_M, BLOCK_REG_A> a0;// M[0:16]xK[0:32]@@ -380,67 +339,120 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {block_idx = MYMIN(block_idx, max_block_idx);#if USE_INT8uint 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),0, 0 };- uint scale_offset0 = offset + KV_BLOCK_SIZE * HEAD_SIZE;+ uint scale_offset0 = offset + KV_BLOCK_SIZE * HEAD_SIZE; //KV_BLOCK_SIZE *128(int8) + KV_BLOCK_SIZE(half) + KV_BLOCK_SIZE(half)+ #else+ const uint pitch_b = K * sizeof(char);+ uint hk = hq / (HQ / HK);+ uint off_b0 = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char)) + offset;+ uint scale_offset0 = off_b0 + KV_BLOCK_SIZE * HEAD_SIZE; //KV_BLOCK_SIZE *128(int8) + KV_BLOCK_SIZE(half) + KV_BLOCK_SIZE(half)+ #endif+block_idx = MYMIN(block_idx + 1, max_block_idx);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_b1{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(char) - 1), (uint)(K * sizeof(char) - 1),0, 0 };uint scale_offset1 = offset + KV_BLOCK_SIZE * HEAD_SIZE;+ #else+ uint off_b1 = off_b0 + KEY_LINES_PER_LOAD * pitch_b;+ //uint off_b1 = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half)) + offset;+ uint scale_offset1 = scale_offset0 + KV_BLOCK_SIZE * 2;+ //off_b1 += offset;+ #endif+// prefetch Bblock_idx = (uint)(id_wg_n * BLOCK_WG_N + id_sg_mn * (BLOCK_WG_N / SG_MN)) * STRIDE / KV_BLOCK_SIZE;block_idx = MYMIN(block_idx, max_block_idx);offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char));static_assert(BLOCK_WG_N / SG_MN <= KEY_LINES_PER_LOAD, "prefetch lines should be inside one block");+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<uchar, 1, BLOCK_WG_N / SG_MN, PHASE_SIZE> desc_prefetch_b{ key_cache + offset, BLOCK_WG_N / SG_MN - 1, (uint)(K * sizeof(char) - 1), (uint)(K * sizeof(char) - 1),0, 0 };+ #else+ uint off_prefetch_b = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char));+ off_prefetch_b += offset;+ vector<uint, KEY_LINES_PER_LOAD> offsets_scale0 = (gather_offsets_b) * STRIDE * sizeof(half) + scale_offset0;+ vector<uint, KEY_LINES_PER_LOAD> offsets_scale1 = offsets_scale0 + KV_BLOCK_SIZE*2;+ #endif// N[:]xK[0:32] --> 16 * 1 regs- matrix<int, KEY_LINES_PER_LOAD, 8> b0_up_s8, b0_down_s8, b1_up_s8, b1_down_s8;- matrix<half, 2, BLOCK_REG_B> b0; // --> 16 regs- matrix<half, 2, KEY_LINES_PER_LOAD * 2> scales, zps;- matrix<half, 2, KV_BLOCK_SIZE> scales_block, zps_block;+ matrix<int, KEY_LINES_PER_LOAD, 8> b0_up_s8, b0_down_s8, b1_up_s8, b1_down_s8; //ping pong+ matrix<half, REG_N, BLOCK_REG_B> b0; // after dequant+ matrix<half, REG_N, KEY_LINES_PER_LOAD * 2> scales, zps;+ matrix<half, REG_N, KEY_LINES_PER_LOAD*STRIDE> scales_block, zps_block;#elseuint offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));+ #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(half) - 1), (uint)(K * sizeof(half) - 1),0, 0 };+ #else+ const uint pitch_b = K * sizeof(half);+ uint hk = hq / (HQ / HK);+ uint off_b0 = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half)) + offset;+ //off_b0 += offset;+ #endifblock_idx = MYMIN(block_idx + 1, max_block_idx);offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<int, 1, KEY_LINES_PER_LOAD, 8> desc_b1{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),0, 0 };+ #else+ //uint off_b1 = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half)) + offset;+ uint off_b1 = off_b0 + KEY_LINES_PER_LOAD * pitch_b;+ //off_b1 += offset;+ #endif// prefetch Bblock_idx = (uint)(id_wg_n * BLOCK_WG_N + id_sg_mn * (BLOCK_WG_N / SG_MN)) * STRIDE / KV_BLOCK_SIZE;block_idx = MYMIN(block_idx, max_block_idx);- offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));+ offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half)); //paged offsetstatic_assert(BLOCK_WG_N / SG_MN <= KEY_LINES_PER_LOAD, "prefetch lines should be inside one block");+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<half, 1, BLOCK_WG_N / SG_MN, PHASE_SIZE> desc_prefetch_b{ key_cache + offset, BLOCK_WG_N / SG_MN - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),0, 0 };+ #else+ uint off_prefetch_b = hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half));+ off_prefetch_b += offset;++ #endif// 0~2 M[:]xK[0:16] 2~4 K[16:32] --> 32 * 2 regs- matrix<half, 2, BLOCK_REG_B> b0, b1;+ matrix<half, REG_N, BLOCK_REG_B> b0, b1; // ping-pong B#endifif constexpr (IS64) {// warmup// prefetch+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 32);cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);+ #else+ cm_prefetch_2d<BLOCK_WG_N / SG_MN, 32>(key_cache,off_prefetch_b,pitch_b);+ off_prefetch_b += 32 * sizeof(half);+ cm_prefetch_2d<BLOCK_WG_M / SG_MN, 32>(query,off_prefetch_a,pitch_a);+ off_prefetch_a += 32 * sizeof(half);+ #endif}// load b: N[0:16]xK[0:16]#if USE_INT8{- lsc::block_2d_desc<int, 1, 16, 16 / 2> desc_scale{ key_cache + scale_offset0, 16 * 2 - 1, (uint)(16 * sizeof(half) - 1), (uint)(16 * sizeof(half) - 1),+ #ifdef CM_HAS_LSC_UNTYPED_2D+ //16 * 16 half, 1D->2D+ matrix<half, KEY_LINES_PER_LOAD, STRIDE> tmp_scale, tmp_zp; //KV_BLOCK_SIZE/2+ lsc::block_2d_desc<int, 1, 16, 16 / 2> desc_scale{ key_cache + scale_offset0, KV_BLOCK_SIZE/16 * 2 - 1, (uint)(16 * sizeof(half) - 1), (uint)(16 * sizeof(half) - 1),0, 0 };- matrix<half, 16, 16> tmp_scale, tmp_zp;cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_scale.format<int>(), desc_scale);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(tmp_zp.format<int>(), desc_scale);+scales_block[0].format<half, 16, 16>().select<8, 2, 16, 1>(0) = tmp_scale.format<half, 8, 32>().select<8, 1, 16, 2>(0, 0);scales_block[0].format<half, 16, 16>().select<8, 2, 16, 1>(1) = tmp_scale.format<half, 8, 32>().select<8, 1, 16, 2>(0, 1);zps_block[0].format<half, 16, 16>().select<8, 2, 16, 1>(0) = tmp_zp.format<half, 8, 32>().select<8, 1, 16, 2>(0, 0);zps_block[0].format<half, 16, 16>().select<8, 2, 16, 1>(1) = tmp_zp.format<half, 8, 32>().select<8, 1, 16, 2>(0, 1);+desc_scale.set_base(key_cache + scale_offset1);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(tmp_scale.format<int>(), desc_scale);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(tmp_zp.format<int>(), desc_scale);@@ -448,36 +460,92 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {scales_block[1].format<half, 16, 16>().select<8, 2, 16, 1>(1) = tmp_scale.format<half, 8, 32>().select<8, 1, 16, 2>(0, 1);zps_block[1].format<half, 16, 16>().select<8, 2, 16, 1>(0) = tmp_zp.format<half, 8, 32>().select<8, 1, 16, 2>(0, 0);zps_block[1].format<half, 16, 16>().select<8, 2, 16, 1>(1) = tmp_zp.format<half, 8, 32>().select<8, 1, 16, 2>(0, 1);- }+ #else+ matrix<half, KEY_LINES_PER_LOAD, STRIDE> tmp_scale, tmp_zp; //KV_BLOCK_SIZE/2+ tmp_scale.format<uint>() = cm_load<uint, VectorSize::N8 /*(STRIDE / 2)*/>(key_cache, offsets_scale0);+ scales_block[0].format<half, 16, 8>().select<8, 2, 8, 1>(0) = tmp_scale.format<half, 4, 32>().select<4, 1, 16, 2>(0, 0);+ scales_block[0].format<half, 16, 8>().select<8, 2, 8, 1>(1) = tmp_scale.format<half, 4, 32>().select<4, 1, 16, 2>(0, 1);++ vector<uint, KEY_LINES_PER_LOAD> offsets_scale0_ = offsets_scale0 + KEY_LINES_PER_LOAD*STRIDE * sizeof(half);+ tmp_scale.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_scale0_);+ scales_block[1].format<half, 16, 8>().select<8, 2, 8, 1>(0) = tmp_scale.format<half, 4, 32>().select<4, 1, 16, 2>(0, 0);+ scales_block[1].format<half, 16, 8>().select<8, 2, 8, 1>(1) = tmp_scale.format<half, 4, 32>().select<4, 1, 16, 2>(0, 1);+ tmp_zp.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_scale1);+ zps_block[0].format<half, 16, 8>().select<8, 2, 8, 1>(0) = tmp_zp.format<half, 4, 32>().select<4, 1, 16, 2>(0, 0);+ zps_block[0].format<half, 16, 8>().select<8, 2, 8, 1>(1) = tmp_zp.format<half, 4, 32>().select<4, 1, 16, 2>(0, 1);++ vector<uint, KEY_LINES_PER_LOAD> offsets_scale1_ = offsets_scale1 + KEY_LINES_PER_LOAD*STRIDE * sizeof(half);+ tmp_zp.format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_scale1_);+ zps_block[1].format<half, 16, 8>().select<8, 2, 8, 1>(0) = tmp_zp.format<half, 4, 32>().select<4, 1, 16, 2>(0, 0);+ 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+ }+ #ifndef CM_HAS_LSC_UNTYPED_2D+ vector<uint, KEY_LINES_PER_LOAD> offsets_0;+ vector<uint, KEY_LINES_PER_LOAD> offsets_1;+ #endifif constexpr (IS64) {+ #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);+ #else+ offsets_0 = (gather_offsets_b) * pitch_b + off_b0;+ offsets_1 = (gather_offsets_b) * pitch_b + off_b1;+ 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+ } else {+ #ifndef CM_HAS_LSC_UNTYPED_2D+ offsets_0 = (gather_offsets_b) * pitch_b + off_b0;+ offsets_1 = (gather_offsets_b) * pitch_b + off_b1;+ #endif}#else+ #ifndef CM_HAS_LSC_UNTYPED_2D+ vector<uint, KEY_LINES_PER_LOAD> offsets_0;+ vector<uint, KEY_LINES_PER_LOAD> offsets_1;+ #endifif constexpr (IS64) {+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[1].format<int>(), desc_b1);+ #else+ offsets_0 = (gather_offsets_b) * pitch_b + off_b0;+ offsets_1 = (gather_offsets_b) * pitch_b + off_b1;+ b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b0[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ #endif+ } else {+ #ifndef CM_HAS_LSC_UNTYPED_2D+ offsets_0 = (gather_offsets_b) * pitch_b + off_b0;+ offsets_1 = (gather_offsets_b) * pitch_b + off_b1;+ #endif}#endifif constexpr (IS64) {+ #ifdef CM_HAS_LSC_UNTYPED_2Ddesc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ #else+ offsets_0 += 8 * sizeof(uint);+ offsets_1 += 8 * sizeof(uint);+ #endif}#if USE_INT8- auto dec = [&](vector<int, 64> B0_i8, vector<int, 64> B1_i8, matrix_ref<half, REG_N, BLOCK_REG_B> B0) {+ 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++) {- auto b = B0[n].format<half, 8, 32>();+ auto b = B0[n].format<half, BLOCK_REG_K/2, BLOCK_REG_N*2>();#pragma unroll- for (int m = 0; m < 8; m++) {+ for (int m = 0; m < BLOCK_REG_K/2; m++) {auto b_row = b[m];- vector<ushort, 16> d0;+ vector<ushort, BLOCK_REG_N> d0;if (n == 0)- d0 = B0_i8.format<ushort, 4, 32>()[m / 2].select<16, 2>(m % 2);+ 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, 32>()[m / 2].select<16, 2>(m % 2);+ 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};@@ -486,126 +554,231 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {}};#endif+auto dot = [&](matrix<half, REG_M, BLOCK_REG_A> A, matrix<half, REG_N, BLOCK_REG_B> B) {#pragma unrollfor (int reg_n = 0; reg_n < REG_N; reg_n++) {#pragma unrollfor (uint reg_m = 0; reg_m < REG_M; reg_m++) {acc.row((ushort)(reg_m * REG_N + reg_n)) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, 8, 8>(acc.row((ushort)(reg_m * REG_N + reg_n)),- B.row((ushort)reg_n).format<int>(), A.row((ushort)reg_m).format<int>());+ B.row((ushort)reg_n).format<int>(), A.row((ushort)reg_m).format<int>());}}};for (uint s = 0; s < STRIDE; s++) {#if USE_INT8- auto tmp = scales_block[0].select<16, 1>(s * 16);- scales[0].select<16, 2>(0) = tmp;- scales[0].select<16, 2>(1) = scales[0].select<16, 2>(0);- tmp = scales_block[1].select<16, 1>(s * 16);- scales[1].select<16, 2>(0) = tmp;- scales[1].select<16, 2>(1) = scales[1].select<16, 2>(0);- tmp = zps_block[0].select<16, 1>(s * 16);- zps[0].select<16, 2>(0) = tmp;- zps[0].select<16, 2>(1) = zps[0].select<16, 2>(0);- tmp = zps_block[1].select<16, 1>(s * 16);- zps[1].select<16, 2>(0) = tmp;- zps[1].select<16, 2>(1) = zps[1].select<16, 2>(0);+ 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);+ tmp = scales_block[1].select<KEY_LINES_PER_LOAD, 1>(s * KEY_LINES_PER_LOAD);+ scales[1].select<KEY_LINES_PER_LOAD, 2>(0) = tmp;+ scales[1].select<KEY_LINES_PER_LOAD, 2>(1) = scales[1].select<KEY_LINES_PER_LOAD, 2>(0);+ tmp = zps_block[0].select<KEY_LINES_PER_LOAD, 1>(s * KEY_LINES_PER_LOAD);+ zps[0].select<KEY_LINES_PER_LOAD, 2>(0) = tmp;+ zps[0].select<KEY_LINES_PER_LOAD, 2>(1) = zps[0].select<KEY_LINES_PER_LOAD, 2>(0);+ tmp = zps_block[1].select<KEY_LINES_PER_LOAD, 1>(s * KEY_LINES_PER_LOAD);+ zps[1].select<KEY_LINES_PER_LOAD, 2>(0) = tmp;+ zps[1].select<KEY_LINES_PER_LOAD, 2>(1) = zps[1].select<KEY_LINES_PER_LOAD, 2>(0);#endif#pragma unrollfor (uint hs = 0; hs < HEAD_SIZE / BLOCK_WG_K; hs++) {if constexpr (IS64) {- // 4 phases with b0, b1 ping-pong buffer+ // --------------------------------------------- unroll 0 ? -----------------------------// prefetch+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 32);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);if (hs == HEAD_SIZE / BLOCK_WG_K - 1)desc_prefetch_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);elsedesc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);+ #else+ //cm_prefetch<16, DataSize::U32, CacheHint::Cached, CacheHint::Cached>(key_cache, off_prefetch_b);+ cm_prefetch_2d<BLOCK_WG_N / SG_MN, 32>(key_cache,off_prefetch_b,pitch_b);+ cm_prefetch_2d<BLOCK_WG_M / SG_MN, 32>(query,off_prefetch_a,pitch_a);+ off_prefetch_b += 32 * sizeof(half);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1)+ off_prefetch_a = base_off_prefetch_a + ((STRIDE - 1 - s - 1) * b_adjacent_between_head) * sizeof(half);+ else+ off_prefetch_a += 32 * sizeof(half);+ #endif// load a: M[0:16*4]xK[0:16]+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_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);+ #else+ cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a, (id_sg_mn==0 && s==0));++ uint query_header_xbase = hq * HEAD_SIZE;+ uint offset_a_x = ((STRIDE - 1) * b_adjacent_between_head + query_header_xbase) * sizeof(half);++ matrix_ref<half, BLOCK_SG_M, BLOCK_REG_K> aa = a0.format<half, BLOCK_SG_M, BLOCK_REG_K>();++ #endif+// load b: N[0:16*2]xK[16:32]#if USE_INT8+ #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);- dec(b0_up_s8.format<int>().select<64, 1>(), b0_down_s8.format<int>().select<64, 1>(), b0);+ #else+ 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+ 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+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[0].format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[1].format<int>(), desc_b1);+ #else++ b1[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b1[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ #endif+ // if(id_sg_mn==0 && s==0) show(b0.format<half, 2, 16*16>());dot(a0, b0);#endif-- desc_a.set_block_x(desc_a.get_block_x() + 16);+ #ifdef CM_HAS_LSC_UNTYPED_2D+ desc_a.set_block_x(desc_a.get_block_x() + BLOCK_REG_K);desc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ #else+ off_a += BLOCK_REG_K * sizeof(half);+ offsets_0 += 8 * sizeof(uint); //16 fp16, 32 int8+ offsets_1 += 8 * sizeof(uint);+ #endif++ // --------------------------------------------- unroll 1 ? -----------------------------// load a: M[0:16*4]xK[16:32]+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_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);+ #else+ cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);+ #endif#if USE_INT8- dec(b0_up_s8.format<int>().select<64, 1>(64), b0_down_s8.format<int>().select<64, 1>(64), b0);+ 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+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[1].format<int>(), desc_b1);- dot(a0, b1);desc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ #else+ b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b0[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ offsets_0 += 8 * sizeof(uint); //16 fp16, 32 int8+ offsets_1 += 8 * sizeof(uint);+ #endif+ // if(id_sg_mn==0 && s==0) show(b1.format<half, 2, 16*16>());+ dot(a0, b1);#endif- desc_a.set_block_x(desc_a.get_block_x() + 16);+ #ifdef CM_HAS_LSC_UNTYPED_2D+ desc_a.set_block_x(desc_a.get_block_x() + BLOCK_REG_K);+ #else+ off_a += BLOCK_REG_K * sizeof(half);+ #endif++ // --------------------------------------------- unroll 2 ? -----------------------------// prefetch+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 32);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);// load a: M[0:16*4]xK[32:48]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);+ #else+ cm_prefetch_2d<BLOCK_WG_N / SG_MN, 32>(key_cache,off_prefetch_b,pitch_b);+ cm_prefetch_2d<BLOCK_WG_M / SG_MN, 32>(query,off_prefetch_a,pitch_a);+ off_prefetch_b += 32 * sizeof(half);+ off_prefetch_a += 32 * sizeof(half);++ // load a: M[0:16*4]xK[32:48]+ cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);+ #endif// load b: N[0:16*2]xK[32:64]#if USE_INT8+ #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);- dec(b1_up_s8.format<int>().select<64, 1>(), b1_down_s8.format<int>().select<64, 1>(), b0);+ #else+ 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+ 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+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[0].format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b1[1].format<int>(), desc_b1);+ #else+ b1[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b1[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ #endifdot(a0, b0);#endif-- desc_a.set_block_x(desc_a.get_block_x() + 16);+ #ifdef CM_HAS_LSC_UNTYPED_2D+ desc_a.set_block_x(desc_a.get_block_x() + BLOCK_REG_K);desc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ #else+ off_a += BLOCK_REG_K * sizeof(half);+ offsets_0 += 8 * sizeof(uint); //16 fp16, 32 int8+ offsets_1 += 8 * sizeof(uint);+ #endif++ // --------------------------------------------- unroll 3 ? -----------------------------// load a: M[0:16*4]xK[48:64]+ #ifdef CM_HAS_LSC_UNTYPED_2Dcm_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 (hs == HEAD_SIZE / BLOCK_WG_K - 1) {desc_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);} else {- desc_a.set_block_x(desc_a.get_block_x() + 16);+ desc_a.set_block_x(desc_a.get_block_x() + BLOCK_REG_K);}-+ #else+ cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1) {+ off_a = base_off_a + ((STRIDE - 1 - s - 1) * b_adjacent_between_head) * sizeof(half);+ } else {+ off_a += BLOCK_REG_K * sizeof(half);+ }+ #endif#if USE_INT8- dec(b1_up_s8.format<int>().select<64, 1>(64), b1_down_s8.format<int>().select<64, 1>(64), b0);+ 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+ #ifdef CM_HAS_LSC_UNTYPED_2D+ // load b: N[0:16*2]xK[16:32]cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[0].format<int>(), desc_b0);cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0[1].format<int>(), desc_b1);+ dot(a0, b0);desc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ #else+ b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b0[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ offsets_0 += 8 * sizeof(uint);+ offsets_1 += 8 * sizeof(uint);+ #endifdot(a0, b1);#endif} else {+ #ifdef CM_HAS_LSC_UNTYPED_2D// 2 phases with single buffer b0// prefetchdesc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 16);@@ -661,6 +834,64 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {desc_b0.set_block_x(desc_b0.get_block_x() + 8);desc_b1.set_block_x(desc_b1.get_block_x() + 8);#endif+ #else+ // 2 phases with single buffer b0 (non-lsc)++ // prefetch+ cm_prefetch_2d<BLOCK_WG_N / SG_MN, PHASE_SIZE>(key_cache, off_prefetch_b, pitch_b);+ cm_prefetch_2d<BLOCK_WG_M / SG_MN, PHASE_SIZE>(query, off_prefetch_a, pitch_a);+ off_prefetch_b += PHASE_SIZE * sizeof(half);+ off_prefetch_a += PHASE_SIZE * sizeof(half);++ // 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+ // load b once: N[0:16*2]xK[0:32], then split into 2 phases+ 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);+ 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 b: N[0:16*2]xK[0:16]+ b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b0[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ dot(a0, b0);+#endif++ off_a += PHASE_SIZE * sizeof(half);+ offsets_0 += 8 * sizeof(uint);+ offsets_1 += 8 * sizeof(uint);++ // prefetch+ cm_prefetch_2d<BLOCK_WG_N / SG_MN, PHASE_SIZE>(key_cache, off_prefetch_b, pitch_b);+ cm_prefetch_2d<BLOCK_WG_M / SG_MN, PHASE_SIZE>(query, off_prefetch_a, pitch_a);+ off_prefetch_b += PHASE_SIZE * sizeof(half);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1)+ off_prefetch_a = base_off_prefetch_a + ((STRIDE - 1 - s - 1) * b_adjacent_between_head) * sizeof(half);+ else+ off_prefetch_a += PHASE_SIZE * sizeof(half);++ // load a: M[0:16*4]xK[16:32]+ cm_load_2d(a0.format<half, BLOCK_SG_M, BLOCK_REG_K>(), query, off_a, pitch_a);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1) {+ off_a = base_off_a + ((STRIDE - 1 - s - 1) * b_adjacent_between_head) * sizeof(half);+ } else {+ off_a += PHASE_SIZE * sizeof(half);+ }++#if USE_INT8+ 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 b: N[0:16*2]xK[16:32]+ b0[0].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_0);+ b0[1].format<uint>() = cm_load<uint, VectorSize::N8>(key_cache, offsets_1);+ dot(a0, b0);+#endif+ offsets_0 += 8 * sizeof(uint);+ offsets_1 += 8 * sizeof(uint);+ #endif}}}@@ -683,6 +914,9 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {}}+ //if(id_sg_m==0 && id_sg_n==0) show(acc.format<float, REG_M * REG_N, BLOCK_DPAS_C>());+ // if(id_sg_m==0 && id_sg_n==0) show(acc_half.format<float, REG_M * BLOCK_REG_M, REG_N * BLOCK_REG_N>());+// if M(aka query) has tails, the following will not change the accuracy:// gemm will compute results for the padding M(all should be zeros), the kq_max/kq_max_wg/kq_exp_partial_sum are along the query dimension and// the results can be dropped in the future stage. To simplify the logic, the size of kq_max/kq_max_wg/kq_exp_partial_sum must be enough to hold@@ -691,7 +925,7 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {n_start = MYMIN(n_start, N);int n_end = MYMIN(n_start + BLOCK_SG_N, N);int valid_n = n_end - n_start;- matrix<SOFTMAX_TYPE, 64, k_block_in_sg> sum_t;+ matrix<SOFTMAX_TYPE, BLOCK_SG_M, SUM_N> sum_t;vector<int, BLOCK_SG_M> seq_m;cmtl::cm_vector_assign(seq_m.select_all(), 0, 1);vector_ref<int, BLOCK_SG_N> seq = seq_m.select<BLOCK_SG_N, 1>();@@ -721,8 +955,8 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {acc_half.row(reg_m).merge(SOFTMAX_TYPE{-60000}, n_pos >= N);}}- max_m.select<32, 1>() = reduce2d<1, 0, 1>(acc_half.select<32, 1, 32, 1>()).format<SOFTMAX_TYPE>();- max_m.select<32, 1>(32) = reduce2d<1, 0, 1>(acc_half.select<32, 1, 32, 1>(32)).format<SOFTMAX_TYPE>();++ max_m.select<BLOCK_SG_M, 1>() = reduce2d<1, 0, 1>(acc_half.select<BLOCK_SG_M, 1, BLOCK_SG_N, 1>()).format<SOFTMAX_TYPE>();{uint slm_offset = (id_sg_n * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) * (uint)sizeof(SOFTMAX_TYPE);@@ -739,6 +973,10 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {cm_slm_block_read(slm, slm_offset, tmp.format<int>());max_m = cm_max<SOFTMAX_TYPE>(max_m, tmp);}++ // if(id_sg_m==0 && id_sg_n==0)+ // show(max_m.format<SOFTMAX_TYPE, 1, BLOCK_SG_M>());+// max across wg// kq_max: [b, hq, M_aligned]//auto max_offsets = (id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M + seq_m) * (uint)sizeof(half);@@ -749,6 +987,7 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {uint offset = (id_wg_n * M_aligned + id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) * sizeof(SOFTMAX_TYPE);cm_ptr_store<int>((int*)kq_max_wg, offset, max_m.format<int>());}+{// kq_exp_partial_sum: [b, hq, M_aligned, N/(BLOCK_SIZE/STRIDE)]if (valid_n == BLOCK_SG_N && skip_mask) {@@ -777,10 +1016,15 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {} SIMD_IF_END;}}- sum_t.select<32, 1, k_block_in_sg, 1>( 0).format<SOFTMAX_TYPE>() = reduce2d<k_block_in_sg, 1, k_block_in_sg>(acc_half.select<32, 1, 32, 1>( 0)).format<SOFTMAX_TYPE>();- sum_t.select<32, 1, k_block_in_sg, 1>(32).format<SOFTMAX_TYPE>() = reduce2d<k_block_in_sg, 1, k_block_in_sg>(acc_half.select<32, 1, 32, 1>(32)).format<SOFTMAX_TYPE>();+ #ifdef CM_HAS_LSC_UNTYPED_2D+ sum_t.select<32, 1, SUM_N, 1>(0).format<SOFTMAX_TYPE>() = reduce2d<SUM_N, 1, SUM_N>(acc_half.select<32, 1, BLOCK_SG_N, 1>(0)).format<SOFTMAX_TYPE>();+ sum_t.select<32, 1, SUM_N, 1>(32).format<SOFTMAX_TYPE>() = reduce2d<SUM_N, 1, SUM_N>(acc_half.select<32, 1, BLOCK_SG_N, 1>(32)).format<SOFTMAX_TYPE>();+ #else+ sum_t.select<BLOCK_SG_M, 1, SUM_N, 1>(0).format<SOFTMAX_TYPE>() = reduce2d<SUM_N, 1, SUM_N>(acc_half.select<BLOCK_SG_M, 1, BLOCK_SG_N, 1>(0)).format<SOFTMAX_TYPE>();+ #endif}// store+ #ifdef CM_HAS_LSC_UNTYPED_2Dlsc::block_2d_desc<SOFTMAX_TYPE, 1, 8, k_block_in_sg> desc_c{ kq_exp_partial_sum, M - 1 /*height*/, (uint)(K_block_pad * sizeof(SOFTMAX_TYPE) - 1) /*width*/, (uint)(K_block_pad * sizeof(SOFTMAX_TYPE) - 1),(int)((id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) / block_size_div_stride) /*x*/, (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) /*y*/ };cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 0>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>( 0).format<SOFTMAX_TYPE>());@@ -791,5 +1035,17 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 5>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(40).format<SOFTMAX_TYPE>());cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 6>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(48).format<SOFTMAX_TYPE>());cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 7>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(56).format<SOFTMAX_TYPE>());+ #else+ const uint pitch_c = K_block_pad * sizeof(SOFTMAX_TYPE);+ uint off_c = offset_partial_sum;+ off_c += pitch_c * (id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M);+ off_c += ((id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) / block_size_div_stride) * sizeof(SOFTMAX_TYPE);++ #pragma unroll+ for (uint i = 0; i < BLOCK_SG_M/8; i++) {+ cm_store_2d(sum_t.select<8, 1, SUM_N, 1>( i*8).format<uint, 8, SUM_N>(), kq_exp_partial_sum, off_c + (8 * i) * pitch_c, pitch_c);+ }++ #endif}#endifdiff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/find_block.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/find_block.hppindex 0c6f80d95ae..a91ba8740cd 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/find_block.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/find_block.hpp@@ -1,24 +1,6 @@-/*- * Copyright (c) 2020-2023, Intel Corporation- *- * Permission is hereby granted, free of charge, to any person obtaining a- * copy of this software and associated documentation files (the "Software"),- * to deal in the Software without restriction, including without limitation- * the rights to use, copy, modify, merge, publish, distribute, sublicense,- * and/or sell copies of the Software, and to permit persons to whom the- * Software is furnished to do so, subject to the following conditions:- *- * The above copyright notice and this permission notice shall be included- * in all copies or substantial portions of the Software.- *- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR- * OTHER DEALINGS IN THE SOFTWARE.- */+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//#include <cm/cm.h>#include <cm/cmtl.h>@@ -32,20 +14,64 @@#define CUR_TYPE_(a) MYCONCAT(IS_, a)#define CUR_TYPE CUR_TYPE_(SOFTMAX_TYPE)+template <int M, int N>+CM_INLINE void cm_load_2d(matrix_ref<SOFTMAX_TYPE, M, N> out,+ svmptr_t base, uint offset, uint pitch, uint valid_m) {+ #pragma unroll+ for (int i = 0; i < out.n_rows(); i++) {+ if (i < (int)valid_m) {+ out.row(i).format<uint>() =+ cm_ptr_load<uint, N, DataSize::U32, CacheHint::Cached, CacheHint::Cached>(+ (uint*)base, offset + i * pitch);+ } else {+ out.row(i) = SOFTMAX_TYPE(0);+ }+ }+}++template <int M, int N>+CM_INLINE void cm_store_2d(matrix_ref<SOFTMAX_TYPE, M, N> out, svmptr_t base, uint offset, uint pitch, uint valid_m) {+ #pragma unroll+ for(int i = 0; i < out.n_rows(); i++) {+ if(offset + i * pitch < valid_m*pitch)+ cm_ptr_store<uint, N>((uint*)base, offset + i * pitch, out.row(i).format<uint>());+ }+}+// kq_max_wg: [b, hq, n_groups, q_stride_pad]// kq_exp_partial_sum: [b, hq, q_stride_pad, k_block_pad]// kq_sum: [b, hq, q_stride_pad/TOKEN_IN_BLOCK, k_block_pad]-CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_partial_sum, svmptr_t block_mask, uint q_len, uint q_stride, uint q_stride_pad, uint k_block_pad, float thresh, uint causal_start_index+CM_INLINE void find(uint slm, int m_block,+ svmptr_t kq_max_wg,+ //#ifdef CM_HAS_LSC_UNTYPED_2D+ svmptr_t kq_exp_partial_sum,+ // #else+ // SurfaceIndex kq_exp_partial_sum [[type("buffer_t")]],+ // #endif+ svmptr_t block_mask, uint q_len, uint q_stride, uint q_stride_pad, uint k_block_pad, float thresh, uint causal_start_index#if DEBUG_ACC == 1, svmptr_t kq_sum#endif) {+#ifndef BLOCK_SHARE_MAX+ #define BLOCK_SG_M 64+ #define BLOCK_SG_N 32+ #define SG_M 2+ #define SG_N 4+ #define HEAD_SIZE 128+ #define KV_BLOCK_SIZE 256+ #define STRIDE 16+ #define BLOCK_SIZE 128+ #define BLOCK_SHARE_MAX 256+#endif+constexpr int TOKEN_IN_BLOCK = (BLOCK_SIZE / STRIDE); // 8 -> 16int m = m_block * TOKEN_IN_BLOCK;vector<SOFTMAX_TYPE, TOKEN_IN_BLOCK> max_m;constexpr int TOKEN_SHARE_MAX = BLOCK_SHARE_MAX / TOKEN_IN_BLOCK; // 32 -> 16kq_exp_partial_sum += m * k_block_pad * (int)sizeof(SOFTMAX_TYPE);+kq_max_wg += m * (int)sizeof(SOFTMAX_TYPE);constexpr SOFTMAX_TYPE log2e = 1.4426950408889634f;matrix<float, TOKEN_IN_BLOCK, TOKEN_SHARE_MAX> sum_m = 0;@@ -64,13 +90,18 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p}return;}-#if BLOCK_SIZE == 128+ #ifdef CM_HAS_LSC_UNTYPED_2D+ #if BLOCK_SIZE == 128lsc::block_2d_desc<SOFTMAX_TYPE, 1, TOKEN_IN_BLOCK, TOKEN_SHARE_MAX / (sizeof(SOFTMAX_TYPE) / sizeof(half))> desc_sum{ kq_exp_partial_sum, (uint)valid_m - 1, (uint)(k_block_pad * sizeof(SOFTMAX_TYPE) - 1), (uint)(k_block_pad * sizeof(SOFTMAX_TYPE) - 1),0, 0 };-#else+ #elselsc::block_2d_desc<SOFTMAX_TYPE, 1, TOKEN_IN_BLOCK / 2, TOKEN_SHARE_MAX> desc_sum{ kq_exp_partial_sum, (uint)valid_m - 1, (uint)(k_block_pad * sizeof(SOFTMAX_TYPE) - 1), (uint)(k_block_pad * sizeof(SOFTMAX_TYPE) - 1),0, 0 };-#endif+ #endif+ #else+ const uint pitch_sum = k_block_pad * sizeof(SOFTMAX_TYPE);+ uint off_sum = 0;// m * k_block_pad * (int)sizeof(SOFTMAX_TYPE);+ #endif{// find max: (k_block_pad / TOKEN_SHARE_MAX) * q_stride_padmax_m = SOFTMAX_TYPE{-60000};@@ -82,12 +113,16 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p}}// compensation: val*exp(local - global)+ #ifdef CM_HAS_LSC_UNTYPED_2Ddesc_sum.set_block_x(0);+ #else+ off_sum = 0;+ #endiffor (int j = 0, idx = 0; j < k_block_pad; j += TOKEN_SHARE_MAX, idx++) {vector<SOFTMAX_TYPE, TOKEN_IN_BLOCK> max_m_in_group;max_m_in_group.format<int>() = cm_ptr_load<int, TOKEN_IN_BLOCK / (sizeof(int) / sizeof(SOFTMAX_TYPE))>((int*)kq_max_wg, q_stride_pad * idx * (int)sizeof(SOFTMAX_TYPE));+#ifdef CM_HAS_LSC_UNTYPED_2D#if CUR_TYPE == IS_float && BLOCK_SIZE == 128- // 2x(8, 16) -> (8, 32)cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, 0).format<SOFTMAX_TYPE>(), desc_sum);cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 16, 0>(data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, TOKEN_SHARE_MAX / 2).format<SOFTMAX_TYPE>(), desc_sum);#elif CUR_TYPE == IS_float && BLOCK_SIZE == 256@@ -96,6 +131,10 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_pcm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 8>(data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(TOKEN_IN_BLOCK / 2, 0).format<SOFTMAX_TYPE>(), desc_sum);#elsecm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(data.format<SOFTMAX_TYPE>(), desc_sum);+#endif+#else+//xe1+ cm_load_2d(data, kq_exp_partial_sum, off_sum, pitch_sum, valid_m);#endiffor (int i = 0; i < TOKEN_IN_BLOCK; i++) {if (i < valid_m) {@@ -103,6 +142,7 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_psum_m.row(i) += data.row(i);}}+#ifdef CM_HAS_LSC_UNTYPED_2D#if CUR_TYPE == IS_float && BLOCK_SIZE == 128cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 0>(desc_sum, data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, 0).format<SOFTMAX_TYPE>());cm_store<CacheHint::Uncached, CacheHint::WriteBack, 16, 0>(desc_sum, data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, TOKEN_SHARE_MAX / 2).format<SOFTMAX_TYPE>());@@ -112,7 +152,15 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p#elsecm_store(desc_sum, data.format<SOFTMAX_TYPE>());#endif- desc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX); // k_block_in_group? 32 ->16+#else+//xe1+ cm_store_2d(data, kq_exp_partial_sum, off_sum, pitch_sum, valid_m);+#endif+#ifdef CM_HAS_LSC_UNTYPED_2D+ desc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX);+#else+ off_sum += TOKEN_SHARE_MAX * sizeof(SOFTMAX_TYPE);+#endif}// exp/sum@@ -125,12 +173,18 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p}// compensation: sum(val*inv_sum_v)vector<float, TOKEN_SHARE_MAX> sum_m_after_add = 0;+#ifdef CM_HAS_LSC_UNTYPED_2Ddesc_sum.set_block_x(0);+#else+ off_sum = 0;+#endif+#if DEBUG_ACC == 1kq_sum += m_block * k_block_pad * (int)sizeof(half);#endifvector<uchar, TOKEN_SHARE_MAX> zero = 0;for (int j = 0; j < k_block_pad; j += TOKEN_SHARE_MAX) {+#ifdef CM_HAS_LSC_UNTYPED_2D#if CUR_TYPE == IS_float && BLOCK_SIZE == 128cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, 0).format<SOFTMAX_TYPE>(), desc_sum);cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 16, 0>(data.select<TOKEN_IN_BLOCK, 1, TOKEN_SHARE_MAX / 2, 1>(0, TOKEN_SHARE_MAX / 2).format<SOFTMAX_TYPE>(), desc_sum);@@ -139,12 +193,23 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_pcm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 8>(data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(TOKEN_IN_BLOCK / 2, 0).format<SOFTMAX_TYPE>(), desc_sum);#elsecm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(data.format<SOFTMAX_TYPE>(), desc_sum);+#endif+#else++//xe1+ cm_load_2d(data, kq_exp_partial_sum, off_sum, pitch_sum, valid_m);+#endifdata.row(0) *= inv_sum_v[0];for (int i = 1; i < TOKEN_IN_BLOCK; i++) {data.row(0) += data.row(i) * inv_sum_v[i];}++#ifdef CM_HAS_LSC_UNTYPED_2Ddesc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX);+#else+ off_sum += TOKEN_SHARE_MAX * sizeof(SOFTMAX_TYPE);+#endif// the sum type is always half in the reference code of the paper: https://github.com/mit-han-lab/x-attention/blob/fb2ac200a23d20568f7d166ddb5ee247926d2b2b/xattn/src/kernels.py#L248vector<half, TOKEN_SHARE_MAX> data_half = data.row(0); // 32 -> 16sum_m_after_add += data_half;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 947f70cb235..09b3162238a 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@@ -26,13 +26,27 @@ namespace KERNEL_NAME {extern "C" _GENX_MAIN_ void KERNEL_NAME(//query [q_len, num_heads, S]+#ifdef CM_HAS_LSC_UNTYPED_2Dhalf* query [[type("svmptr_t")]],+#else+ SurfaceIndex q_gather [[type("buffer_t")]],+#endif+#ifdef CM_HAS_LSC_UNTYPED_2D#if CMPA_KVCACHE_U8int8_t* k_cache [[type("svmptr_t")]],int8_t* v_cache [[type("svmptr_t")]],#elsehalf* k_cache [[type("svmptr_t")]],half* v_cache [[type("svmptr_t")]],+#endif+#else+#if CMPA_KVCACHE_U8+ int8_t* k_cache [[type("svmptr_t")]],+ SurfaceIndex v_cache_stateful [[type("buffer_t")]],+#else+ half* k_cache [[type("svmptr_t")]],+ SurfaceIndex v_cache_stateful [[type("buffer_t")]],+#endif#endifint32_t* past_lens [[type("svmptr_t")]],int32_t* block_indices [[type("svmptr_t")]],@@ -122,6 +136,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(//Q/O[B, L, H, S]uint q_offset = (q_start_sg*num_heads + h)*head_size;+ uint q_offset_bytes = q_offset * sizeof(half);#if IS_BLOCK_SPARSEbool *block_mask_base, *wg_block_mask_base;@@ -134,6 +149,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#if CMPA_KVCACHE_U8uint kv_offset = hkv*(head_size+4)*pa_block_sz;+ uint kv_offset_bytes = kv_offset; // U8: byte offsetpa_lsc_u8<is_causal, num_heads, num_kv_heads, head_size, 0>(slm_K,slm_V,@@ -143,9 +159,19 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(kv_stop,q_len_sg, //q_step,kv_seq_len, //kv_len,+#ifdef CM_HAS_LSC_UNTYPED_2Dreinterpret_cast<svmptr_t>(query + q_offset),+#else+ q_gather,+ q_offset_bytes,+#endifreinterpret_cast<svmptr_t>(k_cache + kv_offset),+#ifdef CM_HAS_LSC_UNTYPED_2Dreinterpret_cast<svmptr_t>(v_cache + kv_offset),+#else+ v_cache_stateful,+ kv_offset_bytes,+#endif#if IS_BLOCK_SPARSEreinterpret_cast<svmptr_t>(block_mask_base),reinterpret_cast<svmptr_t>(wg_block_mask_base),@@ -156,15 +182,26 @@ 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);pa_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,kv_stop,q_len_sg, //q_step,kv_seq_len, //kv_len,+#ifdef CM_HAS_LSC_UNTYPED_2Dreinterpret_cast<svmptr_t>(query + q_offset),+#else+ q_gather,+ q_offset_bytes,+#endifreinterpret_cast<svmptr_t>(k_cache + kv_offset),+#ifdef CM_HAS_LSC_UNTYPED_2Dreinterpret_cast<svmptr_t>(v_cache + kv_offset),+#else+ v_cache_stateful,+ kv_offset_bytes,+#endif#if IS_BLOCK_SPARSEreinterpret_cast<svmptr_t>(block_mask_base),reinterpret_cast<svmptr_t>(wg_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 6561ce53f83..594d5ce390a 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@@ -49,6 +49,22 @@ inline void prepack_to_VNNI_W2(matrix_ref<T1, K, N> input, matrix_ref<T2, K/2, N}}+template <typename T1, typename T2>+CM_INLINE void Transpose_8x8(matrix_ref<T1, 8, 8> in, matrix_ref<T2, 8, 8> out) {+ matrix<T2, 8, 8> temp;+ #pragma unroll+ for (int i = 0; i < 4; ++i) {+ temp.row(i) = in.template select<2, 1, 4, 2>(i * 2, 0);+ temp.row(i + 4) = in.template select<2, 1, 4, 2>(i * 2, 1);+ }++ #pragma unroll+ for (int i = 0; i < 4; ++i) {+ out.row(i * 2) = temp.template select<4, 1, 2, 4>(0, i);+ out.row(i * 2 + 1) = temp.template select<4, 1, 2, 4>(4, i);+ }+}+extern "C" _GENX_MAIN_ void KERNEL_NAME(half* query [[type("svmptr_t")]],KV_ELEMENT_TYPE* key [[type("svmptr_t")]],@@ -71,10 +87,26 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(//# kv_len dimision will be split into multiple partitions, each WG process a partition//# total_partitions_num = kv_len // KV_PARTITION_SIZE//- //# MaxRepeatCount=8- //# q_heads_per_kv_head = num_heads // num_kv_heads- //# q_head_chunks_per_kv_head = (q_heads_per_kv_head + (MaxRepeatCount - 1)) // MaxRepeatCount- //# q_head_chunk_size = num_heads // (num_kv_heads * q_head_chunks_per_kv_head)+ //# MaxRepeatCount = 8+ //# q_heads_per_kv_head = num_heads / num_kv_heads+ //#+ //# Q_head_chunk_size is bounded not only by MaxRepeatCount,+ //# but also by the per-thread register file budget implied by+ //# -Qxcm_register_file_size and architecture GRF width+ //# (xe1: 32B/GRF, xe2: 64B/GRF).+ //#+ //# In particular, the rS matrix:+ //# matrix<float, Q_head_chunk_size,+ //# REG_M * (KV_PARTITION_SIZE / KV_STEP) * REG_N>+ //# must fit within the available register budget to avoid+ //# excessive register pressure or CM build failures.+ //#+ //# Host side derives the maximum valid Q_head_chunk_size from+ //# this constraint and adjusts q_head_chunks_per_kv_head+ //# accordingly:+ //# q_head_chunks_per_kv_head = ceil_div(q_heads_per_kv_head, target_chunk)+ //# q_head_chunk_size = ceil_div(q_heads_per_kv_head,+ //# q_head_chunks_per_kv_head)//# GWS=[seq_num, num_kv_heads * q_head_chunks_per_kv_head, total_partitions_num]//# LWS=[1, 1, 1]@@ -179,27 +211,76 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(for(int kv_pos = 0; kv_pos < kv_pos_end; kv_pos += KV_STEP, ki++) {#if KV_CACHE_COMPRESSION && KV_CACHE_COMPRESSION_BY_TOKEN- vector<half, REG_N * 2> temp_scale, temp_zp;- temp_scale.select<REG_N,2>(0) = scale_vec.select<REG_N,1>(kv_pos);- temp_scale.select<REG_N,2>(1) = scale_vec.select<REG_N,1>(kv_pos);- temp_zp.select<REG_N,2>(0) = zp_vec.select<REG_N,1>(kv_pos);- temp_zp.select<REG_N,2>(1) = zp_vec.select<REG_N,1>(kv_pos);+ #if USE_LSC_BLOCK_2D_DESC+ vector<half, REG_N * 2> temp_scale, temp_zp;+ temp_scale.select<REG_N,2>(0) = scale_vec.select<REG_N,1>(kv_pos);+ temp_scale.select<REG_N,2>(1) = scale_vec.select<REG_N,1>(kv_pos);+ temp_zp.select<REG_N,2>(0) = zp_vec.select<REG_N,1>(kv_pos);+ temp_zp.select<REG_N,2>(1) = zp_vec.select<REG_N,1>(kv_pos);+ #else+ matrix<half, REG_N, REG_K> temp_scale;+ matrix<half, REG_N, REG_K> temp_zp;+ #pragma unroll+ for(int kk=0; kk < REG_K; kk++) {+ temp_scale.select<REG_N, 1, 1, 1>(0, kk) = scale_vec.select<REG_N,1>(kv_pos);+ temp_zp.select<REG_N, 1, 1, 1>(0, kk) = zp_vec.select<REG_N,1>(kv_pos);+ }+ #endif#endif#pragma unroll#if KV_CACHE_COMPRESSION- for(int k = 0, ri = 0; k < HEAD_SIZE/4; k += REG_K/4, ri ++ ) {+ #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION_BY_TOKEN && XE_ARCH==1+ for(int k = 0, ri = 0; k < HEAD_SIZE/4; k += (REG_K/4)*4, ri += 4 ) {+ #else+ for(int k = 0, ri = 0; k < HEAD_SIZE/4; k += REG_K/4, ri ++ ) {+ #endif#else- for(int k = 0, ri = 0; k < HEAD_SIZE/2; k += REG_K/2, ri ++ ) {+ 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+ matrix<uint8_t, REG_N, REG_K*4> temp4; // 8 x 64+ uint cur_kv_offset4 = kv_offset + kv_pos * kv_stride + k * 4;+ #pragma unroll+ for(int kk = 0; kk < REG_N; kk++) {+ vector<uint, (REG_K*4)/4> tmp_dw;+ cm_svm_block_read<uint, (REG_K*4)/4>((svmptr_t)(reinterpret_cast<uint*>(key + cur_kv_offset4 + kk * kv_stride)), tmp_dw);+ temp4[kk].format<uint>() = tmp_dw;+ }+ #pragma unroll+ for(int ur = 0; ur < 4; ur++) {+ if (ri + ur >= HEAD_SIZE / REG_K) break;+ matrix<half, REG_K, REG_N> Kt = 0;+ matrix<uint8_t, REG_N, REG_K> temp; // 8 x 16+ #pragma unroll+ for(int kk = 0; kk < REG_N; kk++) {+ temp[kk] = temp4[kk].select<REG_K,1>(ur*REG_K);+ }+ matrix<half, REG_N, REG_K> KtNormal = temp;+ KtNormal = KtNormal - temp_zp;+ KtNormal = cm_mul<half>(KtNormal, temp_scale);+ Transpose_8x8(KtNormal.format<uint, REG_N, REG_K/2>(), Kt.format<uint, REG_K/2, REG_N>());+ {+ // matrix<half, Q_head_chunk_size, REG_K> Qmat_data = Qmat.select<Q_head_chunk_size,1,REG_K,1>(0, (ri+ur)*REG_K);+ matrix<half, Q_head_chunk_size, REG_K> Qmat_data = Qmat.select<Q_head_chunk_size,1,REG_K,1>(0, (ri+ur)*REG_K);+ matrix<float, Q_head_chunk_size, REG_N> rS_data = 0;+ rS_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_head_chunk_size>(+ rS_data.format<float>(),+ Kt.format<int32_t>(),+ Qmat_data.format<int32_t>());+ // rS.select<Q_head_chunk_size, 1, REG_N, 1>(0, (ki+ur)*REG_N) += rS_data;+ rS.select<Q_head_chunk_size, 1, REG_N, 1>(0, ki*REG_N) += rS_data;+ }+ }+ continue;+ #endifmatrix<half, REG_K, REG_N> Kt = 0;#if USE_LSC_BLOCK_2D_DESC//# Load Kt into register & pack as VNNI(as dpas-B tile)//# DWORD transposed load == (transposed + VNNI) loadb2dK.set_block_x(k);-#if KV_CACHE_COMPRESSION- // dequantizematrix<uint8_t, REG_K, REG_N> Kt_quant_temp, Kt_quant;cm_load<lsc::Transpose>(Kt_quant_temp.format<uint>(), b2dK.set_block_y(kv_pos));auto quant_src = Kt_quant_temp.format<ushort, REG_K/2, REG_N>();@@ -210,7 +291,6 @@ 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 unrollfor(int r = 0; r < REG_K; r++) {@@ -236,17 +316,41 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(cm_load<lsc::Transpose>(Kt.format<uint>(), b2dK.set_block_y(kv_pos));#endif#else- matrix<uint, REG_N, REG_K/2> temp;- uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k * 2;// uint --> half- #pragma unroll- for(int kk = 0; kk < REG_N; kk++) {- cm_svm_block_read<uint, REG_K/2>((svmptr_t)(key + cur_kv_offset + kk * kv_stride), temp[kk].format<uint>());- }+ #if KV_CACHE_COMPRESSION+ matrix<uint8_t, REG_N, REG_K> temp; // 8 x 16+ uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k * 4;+ #pragma unroll+ 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+ matrix<half, REG_N, REG_K> KtNormal = temp;+ KtNormal = KtNormal - temp_zp;+ KtNormal = cm_mul<half>(KtNormal, temp_scale);+ #else+ 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);+ matrix<half, REG_N, REG_K> KtNormal;+ #pragma unroll+ for(int kk = 0; kk < REG_N; kk++) {+ KtNormal[kk] = temp[kk] - temp_zp;+ KtNormal[kk] = cm_mul<half>(KtNormal[kk], temp_scale);+ }+ #endif+ #else+ matrix<uint, REG_N, REG_K/2> KtNormal;+ uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k * 2;+ #pragma unroll+ for(int kk = 0; kk < REG_N; kk++) {+ cm_svm_block_read<uint, REG_K/2>((svmptr_t)(key + cur_kv_offset + kk * kv_stride), KtNormal[kk].format<uint>());+ }+ #endif#if XE_ARCH==1- Transpose_8x8(temp.select<8,1,8,1>(0,0), Kt.format<uint, REG_K/2, REG_N>().select<8,1,8,1>(0,0));+ Transpose_8x8(KtNormal.format<uint, REG_N, REG_K/2>(), Kt.format<uint, REG_K/2, REG_N>());#else- Transpose_8x8(temp.select<8,1,8,1>(0,0), Kt.format<uint, REG_K/2, REG_N>().select<8,1,8,1>(0,0));- Transpose_8x8(temp.select<8,1,8,1>(8,0), Kt.format<uint, REG_K/2, REG_N>().select<8,1,8,1>(0,8));+ Transpose_8x8(KtNormal.format<uint, REG_N, REG_K/2>().select<8,1,8,1>(0,0), Kt.format<uint, REG_K/2, REG_N>().select<8,1,8,1>(0,0));+ Transpose_8x8(KtNormal.format<uint, REG_N, REG_K/2>().select<8,1,8,1>(8,0), Kt.format<uint, REG_K/2, REG_N>().select<8,1,8,1>(0,8));#endif#endif{@@ -265,11 +369,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(// online softmaxvector<float, Q_head_chunk_size> cur_sum = 0.0f;vector<float, Q_head_chunk_size> cur_lse = 0.0f;- #if XE_ARCH==1- matrix<half, KV_PARTITION_STEP_NUM / 2 * REG_M, REG_N> Pmat = 0;- #elsematrix<half, Q_head_chunk_size, KV_PARTITION_STEP_NUM * REG_M * REG_N> Pmat = 0;- #endif#pragma unrollfor(int qi = 0; qi < Q_head_chunk_size; qi++) {auto rS_slice = rS[qi].format<float, KV_PARTITION_STEP_NUM, REG_N>();@@ -288,6 +388,9 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(// compute row_maxauto rSv = rS_slice.format<float>();+ #if XE_ARCH==1+ float row_max = cm_reduced_max<float>(rSv);+ #elsefloat row_max = rSv[0];// It is performance hotspot for u8, must add unroll#if KV_CACHE_COMPRESSION@@ -295,23 +398,24 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#endiffor(int r = 1; r < rSv.n_elems(); r++)row_max = cm_max<float>(row_max, rSv[r]);+ #endif// compute Pmat = exp(rS_slice - row_max)- vector<float, KV_PARTITION_STEP_NUM * REG_N> rS_exp_temp = cm_exp((rS_slice.format<float>() - row_max)*log2e);- #if XE_ARCH==1- Pmat[qi].format<half, KV_PARTITION_STEP_NUM / 2 * REG_M, REG_K>() = cm_exp((rS_slice.format<float, KV_PARTITION_STEP_NUM / 2 * REG_M, REG_K>() - row_max)*log2e);- #else+ vector<float, KV_PARTITION_STEP_NUM * REG_N> rS_exp_temp = cm_exp((rSv - row_max)*log2e);Pmat[qi].format<half, KV_PARTITION_STEP_NUM * REG_M, REG_N>() = rS_exp_temp;- #endifcur_lse[qi] = cm_sum<float>(rS_exp_temp.format<float>());cur_lse[qi] = cm_log<float>(cur_lse[qi]) * loge2 + row_max; // log2(sum(exp(x))) = log2e * log(sum(exp(x)))// compute row sum of P- auto rPv = Pmat[qi].format<half, 1, KV_PARTITION_STEP_NUM * REG_N>();- cur_sum[qi] = cm_sum<float>(rPv[0]);+ cur_sum[qi] = cm_sum<float>(Pmat[qi]);}-++ #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION+ constexpr uint VALUE_TILE_NUM=2;+ #else+ constexpr uint VALUE_TILE_NUM=1;+ #endif//# rO = P * Vmatrix <float, Q_head_chunk_size, HEAD_SIZE/REG_N * REG_M*REG_N> Omat = 0;#pragma unroll@@ -335,7 +439,6 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(uint kv_x1 = HEAD_SIZE*sizeof(half);uint kv_y1 = KV_BLOCK_SIZE;#endif-uint kv_pos_end = KV_BLOCK_SIZE;if(block_idx == block_num - 1 && leftover_size > 0) {kv_pos_end = leftover_size % KV_BLOCK_SIZE;@@ -361,79 +464,154 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#if KV_CACHE_COMPRESSIONvector<half, REG_K> temp_scale = scale_vec.select<REG_K, 1>(kv_pos);vector<half, REG_K> temp_zp = zp_vec.select<REG_K, 1>(kv_pos);+ matrix<uint8_t, REG_K, REG_N * VALUE_TILE_NUM> Vt_quant;#endif#pragma unroll- for(int k = 0, ri = 0; k < HEAD_SIZE; k += REG_N, ri ++ ) {+ #if !USE_LSC_BLOCK_2D_DESC && KV_CACHE_COMPRESSION && XE_ARCH==1+ for(int k = 0, ri = 0; k < HEAD_SIZE; k += (REG_N * VALUE_TILE_NUM) * 4, ri += VALUE_TILE_NUM * 4 ) {+ #else+ for(int k = 0, ri = 0; k < HEAD_SIZE; k += REG_N * VALUE_TILE_NUM, ri += VALUE_TILE_NUM ) {+ #endif// Load V into register & pack as VNNI(as dpas-B tile)- matrix<half, REG_K, REG_N> VmatNormal;- matrix<half, REG_M, REG_K*REG_N> Vmat;+ matrix<half, REG_K, REG_N * VALUE_TILE_NUM> VmatNormal;+ matrix<half, REG_K, REG_N * VALUE_TILE_NUM> Vmat;#if USE_LSC_BLOCK_2D_DESC- {b2dV.set_block_x(k); // x is the column index#if KV_CACHE_COMPRESSION- {- // dequantize- matrix<uint8_t, REG_K, REG_N> Vt_quant;cm_load<lsc::Normal>(Vt_quant.format<uint8_t>(), b2dV.set_block_y(kv_pos));-+ #else+ cm_load<lsc::VNNI>(Vmat.format<half>(), b2dV.set_block_y(kv_pos)); // y is the row index+ #endif+ #else+ #if KV_CACHE_COMPRESSION+ #if XE_ARCH==1+ matrix<uint8_t, REG_K, REG_N * VALUE_TILE_NUM * 4> Vt_quant4;+ uint cur_kv_offset4 = kv_offset + kv_pos * kv_stride + k;#pragma unroll- for(int r = 0; r < REG_K; r++) {- VmatNormal[r] = Vt_quant[r] - temp_zp[r]; // vector - scalar- VmatNormal[r] = cm_mul<half>(VmatNormal[r], temp_scale[r]); // vector * scalar+ for(int kk = 0; kk < REG_K; kk++) {+ vector<uint, (REG_N * VALUE_TILE_NUM * 4)/4> tmp_dw;+ cm_svm_block_read<uint, (REG_N * VALUE_TILE_NUM * 4)/4>((svmptr_t)(reinterpret_cast<uint*>(value + cur_kv_offset4 + kk * kv_stride)), tmp_dw);+ Vt_quant4[kk].format<uint>() = tmp_dw;}+ #pragma unroll+ for(int ur = 0; ur < 4; ur++) {+ if (k + ur * (REG_N * VALUE_TILE_NUM) >= HEAD_SIZE) break;+ #pragma unroll+ for(int kk = 0; kk < REG_K; kk++) {+ Vt_quant[kk] = Vt_quant4[kk].select<REG_N * VALUE_TILE_NUM, 1>(ur * (REG_N * VALUE_TILE_NUM));+ }+ int ri_ur = ri + ur * VALUE_TILE_NUM;- if(kv_pos_end - kv_pos < KV_STEP) {+ VmatNormal = Vt_quant;#pragma unroll- for(int r = kv_pos_end; r < KV_STEP; r++) {- VmatNormal[r] = 0;+ for(int r = 0; r < REG_K; r++) {+ VmatNormal[r] = VmatNormal[r] - temp_zp[r]; // vector - scalar+ VmatNormal[r] = cm_mul<half>(VmatNormal[r], temp_scale[r]); // vector * scalar+ }++ if(kv_pos_end - kv_pos < KV_STEP) {+ #pragma unroll+ for(int r = kv_pos_end; r < KV_STEP; r++) {+ VmatNormal[r] = 0;+ }+ }+ auto Vref = Vmat.format<half, REG_K/2, 2 * REG_N * VALUE_TILE_NUM>();+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 0) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(0, 0);+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 1) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(1, 0);++ // somtimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ #if CLEAN_UNUSED_KVCACHE+ if(kv_pos_end - kv_pos < KV_STEP) {+ auto VmatRef = Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>();+ uint valid_rows = kv_pos_end - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ #pragma unroll+ for (int r = valid_rows_vnni; r < REG_K/2; r++) {+ VmatRef.row(r) = 0.0f;+ }+ if (valid_rows % 2 == 1) {+ VmatRef.row(valid_rows_vnni-1).select<REG_N*VALUE_TILE_NUM, 2>(1) = 0.0f;+ }+ }+ #endif+ matrix<half, Q_head_chunk_size, REG_K> Pmat_data = Pmat.select<Q_head_chunk_size,1,REG_K,1>(0, ki*REG_K);+ #pragma unroll+ for (int tile=0; tile < VALUE_TILE_NUM; tile ++) {+ auto Omat_data = Omat.select<Q_head_chunk_size,1,REG_N,1>(0, (ri_ur + tile)*REG_N);+ Omat_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_head_chunk_size>(+ Omat_data.format<float>(),+ 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).format<int32_t>(),+ Pmat_data.format<int32_t>());}}- prepack_to_VNNI_W2(VmatNormal, Vmat.format<half, REG_K/2, REG_N*2>());- }+ continue;+ #else+ uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k;+ #pragma unroll+ for(int kk = 0; kk < REG_K; kk++) {+ cm_svm_block_read<uint8_t, REG_N * VALUE_TILE_NUM>((svmptr_t)(value + cur_kv_offset + kk * kv_stride), Vt_quant[kk].format<uint8_t>());+ }+ #endif#else- {- cm_load<lsc::VNNI>(Vmat[0].format<half>(), b2dV.set_block_y(kv_pos));- // Sometimes KV cache would be filled with random NAN(found in PTL), so need to clean up the unused value data.- if(kv_pos_end - kv_pos < KV_STEP) {- auto VmatRef = Vmat[0].format<half, REG_K/2, REG_N*2>();- uint valid_rows = kv_pos_end - kv_pos;- uint valid_rows_vnni = (valid_rows+1)/2;- for (int r = valid_rows_vnni; r < KV_STEP / 2; r++)- VmatRef.row(r) = 0.f;- if (valid_rows % 2 == 1)- VmatRef.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;+ uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k;+ #pragma unroll+ for(int kk = 0; kk < REG_K; kk++) {+ cm_svm_block_read<half, REG_N * VALUE_TILE_NUM>((svmptr_t)(value + cur_kv_offset + kk * kv_stride), VmatNormal[kk].format<half>());}- }+ auto Vref = Vmat.format<half, REG_K/2, 2 * REG_N * VALUE_TILE_NUM>();+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 0) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(0, 0);+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 1) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(1, 0);#endif- }- #else- {- matrix<half, REG_K, REG_N> temp;- uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k;+ #endif++ #if KV_CACHE_COMPRESSION+ VmatNormal = Vt_quant;#pragma unroll- for(int kk = 0; kk < REG_K; kk++) {- cm_svm_block_read<half, REG_N>((svmptr_t)(value + cur_kv_offset + kk * kv_stride), temp[kk].format<half>());+ for(int r = 0; r < REG_K; r++) {+ VmatNormal[r] = VmatNormal[r] - temp_zp[r]; // vector - scalar+ VmatNormal[r] = cm_mul<half>(VmatNormal[r], temp_scale[r]); // vector * scalar}- auto Vref = Vmat[0].format<half, REG_K/2, 2*REG_N>();- Vref.select<REG_K/2, 1, REG_N, 2>(0, 0) = temp.select<REG_K/2, 2, REG_N, 1>(0, 0);- Vref.select<REG_K/2, 1, REG_N, 2>(0, 1) = temp.select<REG_K/2, 2, REG_N, 1>(1, 0);- }++ if(kv_pos_end - kv_pos < KV_STEP) {+ #pragma unroll+ for(int r = kv_pos_end; r < KV_STEP; r++) {+ VmatNormal[r] = 0;+ }+ }+ auto Vref = Vmat.format<half, REG_K/2, 2 * REG_N * VALUE_TILE_NUM>();+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 0) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(0, 0);+ Vref.select<REG_K/2, 1, REG_N * VALUE_TILE_NUM, 2>(0, 1) = VmatNormal.select<REG_K/2, 2, REG_N * VALUE_TILE_NUM, 1>(1, 0);#endif- {- matrix<half, Q_head_chunk_size, REG_K> Pmat_data = Pmat.select<Q_head_chunk_size,1,REG_K,1>(0, ki*REG_K);- matrix<float, Q_head_chunk_size, REG_N> Omat_data = 0;+ // somtimes KV cache would be filled with random Nan, so need to clean up the unused value data.+ #if CLEAN_UNUSED_KVCACHE+ if(kv_pos_end - kv_pos < KV_STEP) {+ auto VmatRef = Vmat.format<half, REG_K/2, REG_N*2*VALUE_TILE_NUM>();+ uint valid_rows = kv_pos_end - kv_pos;+ uint valid_rows_vnni = (valid_rows+1)/2;+ #pragma unroll+ for (int r = valid_rows_vnni; r < REG_K/2; r++) {+ VmatRef.row(r) = 0.0f;+ }+ if (valid_rows % 2 == 1) {+ VmatRef.row(valid_rows_vnni-1).select<REG_N*VALUE_TILE_NUM, 2>(1) = 0.0f;+ }+ }+ #endif+ matrix<half, Q_head_chunk_size, REG_K> Pmat_data = Pmat.select<Q_head_chunk_size,1,REG_K,1>(0, ki*REG_K);+ #pragma unroll+ for (int tile=0; tile < VALUE_TILE_NUM; tile ++) {+ auto Omat_data = Omat.select<Q_head_chunk_size,1,REG_N,1>(0, (ri + tile)*REG_N);Omat_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_head_chunk_size>(Omat_data.format<float>(),- Vmat[0].format<int32_t>(),+ 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).format<int32_t>(),Pmat_data.format<int32_t>());- Omat.select<Q_head_chunk_size,1,REG_N,1>(0, ri*REG_N) += Omat_data;}}}}- //# save Output+ // save Output#pragma unrollfor (int qi = 0; qi < Q_head_chunk_size; qi++) {matrix<float, REG_M, REG_N> cur_O_f32;@@ -444,13 +622,14 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(for(int k = 0, ri=0; k < HEAD_SIZE; k += REG_N, ri++) {auto cO = Omat_slice[ri].format<float, REG_M, REG_N>();#if XE_ARCH==1- cur_O_f32= cm_mul<float>(cO, div_cur_sum);+ cur_O_f32 = cm_mul<float>(cO, div_cur_sum);#else- cur_O_f32= cm_div_ieee(cO, cur_sum[qi]);+ cur_O_f32 = cm_div_ieee(cO, cur_sum[qi]);#endif- cm_svm_block_write<float, REG_N>((svmptr_t)(output + o_offset + k),cur_O_f32.format<float>());+ cm_svm_block_write<float, REG_N>((svmptr_t)(output + o_offset + k), cur_O_f32.format<float>());}uint lse_offset = seq_idx * HEADS_NUM * kv_partition_num + (head_num_idx + qi) * kv_partition_num + wg_thread_id;lse[lse_offset] = cur_lse[qi];}}+diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token_finalization.cm b/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token_finalization.cmindex 642f6e807d5..5768eda3e56 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token_finalization.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/pa_single_token_finalization.cm@@ -50,8 +50,8 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}// load input, total_partition_num = head_nums * kv_partition_num;- matrix<half, 1, REDUCE_SPLIT_SIZE> out_mat_f32 = 0;matrix<half, 1, REDUCE_SPLIT_SIZE> out_mat = 0;+ matrix<float, 1, REDUCE_SPLIT_SIZE> out_mat_f32 = 0;matrix<float, 1, REDUCE_SPLIT_SIZE> data_mat;uint input_offset = batch * total_partition_num * HEAD_SIZE + head * kv_partition_num * HEAD_SIZE + offset;@@ -61,18 +61,18 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(cm_svm_block_read<float, REDUCE_SPLIT_SIZE>((svmptr_t)(input + input_offset), data_mat.format<float>());input_offset += HEAD_SIZE;float lse_value = cm_exp<float>((lse_vec[ki] - lse_max)*log2e);- out_mat_f32 += cm_mul<half>(data_mat, (float)(lse_value/total_lse));+ out_mat_f32 += cm_mul<float>(data_mat, (float)(lse_value/total_lse));}}for(int k = iter * 16; k < kv_partition_num; k ++) {cm_svm_block_read<float, REDUCE_SPLIT_SIZE>((svmptr_t)(input + input_offset), data_mat.format<float>());input_offset += HEAD_SIZE;float lse_value = cm_exp<float>((lse_vec[k] - lse_max)*log2e);- out_mat_f32 += cm_mul<half>(data_mat, (float)(lse_value/total_lse));+ out_mat_f32 += cm_mul<float>(data_mat, (float)(lse_value/total_lse));}- out_mat = out_mat_f32.format<half>();+ out_mat = cm_mul<half>(out_mat_f32, (float)1.0f);// write outputuint output_offset = batch * HEADS_NUM * HEAD_SIZE + head * HEAD_SIZE + offset;- cm_svm_block_write<half, REDUCE_SPLIT_SIZE>((svmptr_t)(output + output_offset),out_mat.format<half>());+ cm_svm_block_write<half, REDUCE_SPLIT_SIZE>((svmptr_t)(output + output_offset), out_mat.format<half>());}\ No newline at end of filediff --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 d8ed2dd4be4..4ef3206a6a3 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@@ -73,28 +73,32 @@ public:// XAttention estimate is following afer kvcache_update.auto out_shape = params.output_layouts[0].get_shape();const size_t block_size = get_xattn_block_size(params);+ 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 kv_len = get_max_context_len(params);const size_t q_len = out_shape[0];const size_t N = kv_len / STRIDE;- const size_t N_kq_groups = ceil_div(N, BLOCK_WG_N);+ const size_t N_kq_groups = ceil_div(N, block_wg_n);const auto q_block_pad = ceil_div(q_len, block_size);const auto sum_per_token_in_block = block_size / STRIDE;- const auto k_block_in_group = BLOCK_WG_N / sum_per_token_in_block;+ const auto k_block_in_group = block_wg_n / sum_per_token_in_block;const auto k_block_pad = k_block_in_group * N_kq_groups;auto rt_params = static_cast<PagedAttentionRuntimeParams*>(m_rt_params.get());+ rt_params->block_wg_m = block_wg_m;rt_params->q_block_pad = q_block_pad;rt_params->k_block_pad = k_block_pad;- const size_t MERGED_Q_NUM = PA_KV_CACHE_BLOCK_SIZE_XATTN / block_size; // for xattn post_proc- rt_params->q_block_pad_merged = ceil_div(q_block_pad, MERGED_Q_NUM);++ const size_t merged_q_num = PagedAttentionGeneratorMultiToken::get_wg_seq_len(params) / block_size;+ rt_params->q_block_pad_merged = ceil_div(q_block_pad, merged_q_num);const size_t head_size = desc->k_head_size;const auto M = q_len / STRIDE; //# will slient drop the tails which is less than `stride`const auto K = STRIDE * head_size;- const size_t q_stride_pad = round_up_to(M, BLOCK_WG_M);+ const size_t q_stride_pad = round_up_to(M, block_wg_m);rt_params->N_kq_groups = N_kq_groups;rt_params->M = M;@@ -124,7 +128,7 @@ public:if (rt_params->stage == PagedAttentionStage::GENERATE) {auto partition_size = get_partition_size(desc->has_xattention);rt_params->num_of_partitions = ceil_div(max_context_len, partition_size);-+ rt_params->q_chunking = get_single_token_q_chunking(params, *desc, partition_size);GPU_DEBUG_TRACE_DETAIL << " partition_size: " << partition_size << " num_of_partitions: " << rt_params->num_of_partitions << std::endl;} else {if (desc->has_xattention) {@@ -261,6 +265,9 @@ private:if (xattn_block_size != 128 && xattn_block_size != 256) {xattn_block_size = 128; // default}+ if (xattn_block_size == 256 && params.get_device_info().arch < gpu_arch::xe2) {+ xattn_block_size = 128; // on pre-XE2, only support 128+ }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 7070b3f0b80..3a7d6711c82 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@@ -22,35 +22,11 @@ using namespace ov;using namespace ov::intel_gpu::ocl;using namespace cldnn;namespace {-constexpr size_t WG_SIZE = 16;constexpr size_t reduce_split_step = 16;} // namespace#define DEBUG_ENABLED 0-// This function returns the kv_step and kv_split_len based on the architecture.-// return {kv_step, kv_split_len}-inline std::pair<size_t, size_t> get_kv_split_size(size_t arch) {- if (arch == 1) {- return {8, 32}; // For Xe1- } else if (arch == 2) {- return {16, 32}; // For Xe2- }- OPENVINO_ASSERT(false, "Unsupported architecture for KV split size");- return {0, 0}; // Fallback case, should not be reached-}--inline size_t get_q_step(size_t arch, bool is_single_token = false) {- if (arch == 1) {- return is_single_token ? 1 : 8; // For Xe1- } else if (arch == 2) {- // For Xe2, q_step = CM_GRF_WIDTH / 32- return is_single_token ? 1 : 16; // For Xe2- }- OPENVINO_ASSERT(false, "Unsupported architecture for Q step");- return 0; // Fallback case, should not be reached-}-inline size_t get_kv_len(const RuntimeParams& params, const PagedAttentionStage& stage) {if (stage == PagedAttentionStage::PREFILL) {auto key_shape = params.input_layouts[PagedAttentionInputIdx::KEY].get_shape();@@ -325,7 +301,6 @@ JitConstants PagedAttentionGeneratorMultiToken::get_jit_constants(const kernel_iauto jit = PagedAttentionGeneratorBase::get_jit_constants(params);const auto desc = params.typed_desc<paged_attention>();const float scale_factor = 1.0 / std::sqrt(static_cast<double>(desc->k_head_size));- auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;jit.make("CMFLA_NUM_HEADS", desc->heads_num);jit.make("CMFLA_NUM_KV_HEADS", desc->kv_heads_num);@@ -339,7 +314,6 @@ JitConstants PagedAttentionGeneratorMultiToken::get_jit_constants(const kernel_ijit.make("CMPA_BLOCK_SZ", PA_KV_CACHE_BLOCK_SIZE);jit.make("IS_BLOCK_SPARSE", 0);}- jit.make("Q_STEP", get_q_step(xe_arch, true));if (get_kv_compressed(params)) {jit.make("CMPA_KVCACHE_U8", 1);@@ -363,9 +337,8 @@ DispatchDataFunc PagedAttentionGeneratorMultiToken::get_dispatch_data_func() conconst size_t batch = out_shape.size() < 4 ? 1 : out_shape[0];const size_t q_len = out_shape[0];- auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;- const size_t q_step = get_q_step(xe_arch, false);- const size_t wg_seq_len = WG_SIZE * q_step;+ const size_t q_step = get_q_step(params);+ const size_t wg_seq_len = get_wg_seq_len(params);const size_t wg_count = align_to(q_len, wg_seq_len) / wg_seq_len;wgs.global = {batch, heads_num, wg_count * WG_SIZE};@@ -404,8 +377,6 @@ JitConstants PagedAttentionGeneratorSingleToken::get_jit_constants(const kernel_auto desc = params.typed_desc<paged_attention>();const float scale_factor = 1.0 / std::sqrt(static_cast<double>(desc->k_head_size));const size_t kv_partition_size = get_partition_size(desc->has_xattention);- auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;-jit.make("KV_PARTITION_SIZE", kv_partition_size);if (desc->has_xattention) {jit.make("KV_BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_XATTN);@@ -416,20 +387,16 @@ JitConstants PagedAttentionGeneratorSingleToken::get_jit_constants(const kernel_jit.make("HEAD_SIZE", desc->k_head_size);jit.make("HEADS_NUM", desc->heads_num);jit.make("KV_HEADS_NUM", desc->kv_heads_num);- jit.make("Q_STEP", get_q_step(xe_arch, true));-- constexpr int32_t MaxRepeatCount = 8;- int32_t q_heads_per_kv_head = static_cast<int32_t>(desc->heads_num / desc->kv_heads_num);- int32_t q_head_chunks_per_kv_head = ceil_div(q_heads_per_kv_head, MaxRepeatCount);- int32_t q_head_chunk_size = static_cast<int32_t>(desc->heads_num / (desc->kv_heads_num * q_head_chunks_per_kv_head));- jit.make("Q_head_chunks_per_kv_head", q_head_chunks_per_kv_head);- jit.make("Q_head_chunk_size", q_head_chunk_size);+ 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;@@ -473,10 +440,8 @@ DispatchDataFunc PagedAttentionGeneratorSingleToken::get_dispatch_data_func() coconst size_t kv_heads_num = desc->kv_heads_num;const size_t partition_num = rtp->num_of_partitions;- constexpr int32_t MaxRepeatCount = 8;- int32_t q_heads_per_kv_head = static_cast<int32_t>(heads_num / kv_heads_num);- int32_t q_head_chunks_per_kv_head = ceil_div(q_heads_per_kv_head, MaxRepeatCount);- wgs.global = {batch, kv_heads_num * q_head_chunks_per_kv_head, partition_num};+ OPENVINO_ASSERT(rtp->q_chunking.q_head_chunks_per_kv_head > 0, "Invalid q_head_chunks_per_kv_head in runtime params");+ wgs.global = {batch, kv_heads_num * static_cast<size_t>(rtp->q_chunking.q_head_chunks_per_kv_head), partition_num};wgs.local = {1, 1, 1};// generate stage: q_len=1@@ -579,8 +544,11 @@ JitConstants XAttentionEstimateGeneratorBase::get_jit_constants(const kernel_impint scale_factor_i;std::memcpy(static_cast<void*>(&scale_factor_i), &scale_factor, sizeof(scale_factor));- const uint32_t wg_k = BLOCK_WG_M;- const uint32_t wg_q = BLOCK_WG_N;+ const uint32_t block_sg_m = get_block_sg_m(params);+ const uint32_t block_sg_n = get_block_sg_n(params);+ const uint32_t wg_k = get_block_wg_m(params);+ const uint32_t wg_q = get_block_wg_n(params);+ // const size_t block_size = get_xattn_block_size(params);OPENVINO_ASSERT(wg_k % _xattn_block_size == 0, "wg_k should be multiple of block_size then there is no tails from block_size");OPENVINO_ASSERT(wg_q % _xattn_block_size == 0, "wg_q should be multiple of block_size then there is no tails from block_size");@@ -590,13 +558,13 @@ JitConstants XAttentionEstimateGeneratorBase::get_jit_constants(const kernel_impjit.make("HEAD_SIZE", desc->k_head_size);jit.make("SG_M", SG_M);jit.make("SG_N", SG_N);- jit.make("BLOCK_SG_M", BLOCK_SG_M);- jit.make("BLOCK_SG_N", BLOCK_SG_N);+ jit.make("BLOCK_SG_M", block_sg_m);+ jit.make("BLOCK_SG_N", block_sg_n);jit.make("BLOCK_WG_K", desc->k_head_size % 64 == 0 ? 64 : 32); // GEMM QK kernel unrolls HEAD_SIZE with a step of BLOCK_WG_Kjit.make("BLOCK_SIZE", _xattn_block_size);jit.make("KV_BLOCK_SIZE", PA_KV_CACHE_BLOCK_SIZE_XATTN);jit.add(make_jit_constant("INV_S", scale_factor_i));- jit.make("BLOCK_SHARE_MAX", BLOCK_WG_N);+ jit.make("BLOCK_SHARE_MAX", wg_q);//# loop order walks HQ first and the step is WALK_HQ, 1 means not walk HQ, 2 means walks 2 heads first. Valid value: 1, 2, 4...jit.make("WALK_HQ", desc->heads_num != desc->kv_heads_num ? 2 : 1);jit.make("IS_CAUSAL", 1);@@ -669,7 +637,8 @@ DispatchDataFunc XAttentionEstimateGEMMQK::get_dispatch_data_func() const {const size_t WALK_HQ = desc->heads_num != desc->kv_heads_num ? 2 : 1;auto& wgs = kd.params.workGroups;- wgs.global = {rtp->N_kq_groups * (rtp->q_stride_pad / BLOCK_WG_M) * SG_N * WALK_HQ, SG_M, desc->heads_num / WALK_HQ};+ OPENVINO_ASSERT(rtp->block_wg_m != 0, "Invalid block_wg_m in runtime params");+ wgs.global = {rtp->N_kq_groups * (rtp->q_stride_pad / rtp->block_wg_m) * SG_N * WALK_HQ, SG_M, desc->heads_num / WALK_HQ};wgs.local = {SG_N, SG_M, 1};const size_t q_start_strided = N - M;@@ -774,8 +743,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);- const uint32_t MERGED_Q_NUM = static_cast<uint32_t>(PA_KV_CACHE_BLOCK_SIZE_XATTN / _xattn_block_size);- jit.make("MERGED_Q_NUM", MERGED_Q_NUM);+ jit.make("MERGED_Q_NUM", PagedAttentionGeneratorMultiToken::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 86e44de8c52..8a263597a55 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@@ -22,9 +22,66 @@using namespace cldnn; // TODO: Remove once namespaces are alignednamespace ov::intel_gpu::cm {+constexpr int32_t PA_CM_REGISTER_FILE_SIZE = 256;-constexpr auto get_pa_build_options() {- return " -cmc -Qxcm_register_file_size=256";+// This function returns the kv_step and kv_split_len based on the architecture.+// return {kv_step, kv_split_len}+inline std::pair<size_t, size_t> get_kv_split_size(size_t arch) {+ if (arch == 1) {+ return {8, 32}; // For Xe1+ } else if (arch == 2) {+ return {16, 32}; // For Xe2+ }+ OPENVINO_ASSERT(false, "Unsupported architecture for KV split size");+ return {0, 0}; // Fallback case, should not be reached+}++struct SingleTokenQChunking {+ int32_t q_head_chunks_per_kv_head;+ int32_t q_head_chunk_size;+};++inline SingleTokenQChunking get_single_token_q_chunking(const kernel_impl_params& params, const paged_attention& desc, size_t kv_partition_size) {+ // Must match kernel mapping in pa_single_token.cm:+ // kv_head_num_idx = gid1 / Q_head_chunks_per_kv_head+ // head_num_idx = gid1 * Q_head_chunk_size+ // Kernel does not guard extra heads, so we must ensure exact coverage:+ // Q_head_chunks_per_kv_head * Q_head_chunk_size == q_heads_per_kv_head+ constexpr int32_t MaxRepeatCount = 8;++ const auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;+ const int32_t q_heads_per_kv_head = static_cast<int32_t>(desc.heads_num / desc.kv_heads_num);++ // Match kernel arch-dependent params+ const int32_t reg_n = (xe_arch == 1) ? 8 : 16;+ const int32_t kv_step = static_cast<int32_t>(get_kv_split_size(xe_arch).first);+ constexpr int32_t reg_m = 1; // RepeatCount+ constexpr int32_t bytes_per_float = 4;++ const int32_t kv_partition_step_num = static_cast<int32_t>(kv_partition_size / kv_step);+ const int32_t rs_cols = reg_m * kv_partition_step_num * reg_n;++ const int32_t reg_file_size = PA_CM_REGISTER_FILE_SIZE;+ const int32_t grf_bytes = (xe_arch == 1) ? 32 : 64;+ const int32_t budget_bytes = reg_file_size * grf_bytes - 1;++ int32_t max_q_by_matrix = budget_bytes / (bytes_per_float * rs_cols);+ if (max_q_by_matrix < 1)+ max_q_by_matrix = 1;++ const int32_t target_chunk = std::min<int32_t>(MaxRepeatCount, max_q_by_matrix);++ int32_t q_head_chunk_size = std::min<int32_t>(q_heads_per_kv_head, target_chunk);+ while (q_head_chunk_size > 1 && (q_heads_per_kv_head % q_head_chunk_size) != 0) {+ --q_head_chunk_size;+ }+ const int32_t q_head_chunks_per_kv_head = q_heads_per_kv_head / q_head_chunk_size;++ return {q_head_chunks_per_kv_head, q_head_chunk_size};+}++inline std::string get_pa_build_options() {+ return " -cmc -Qxcm_register_file_size=" + std::to_string(PA_CM_REGISTER_FILE_SIZE);}#define FIND_DEBUG_ACC 0@@ -32,12 +89,9 @@ constexpr auto get_pa_build_options() {#define PA_KV_CACHE_BLOCK_SIZE 16#define PA_KV_CACHE_BLOCK_SIZE_XATTN 256-constexpr uint32_t BLOCK_SG_M = 64;-constexpr uint32_t BLOCK_SG_N = 32;constexpr uint32_t SG_M = 4;constexpr uint32_t SG_N = 8;-constexpr uint32_t BLOCK_WG_M = BLOCK_SG_M * SG_M;-constexpr uint32_t BLOCK_WG_N = BLOCK_SG_N * SG_N;+constexpr size_t WG_SIZE = 16;constexpr int STRIDE = 16;enum class PagedAttentionStage : uint8_t { GENERATE = 0, PREFILL = 1, MIXED = 2, UNKNOWN = 3 };@@ -46,7 +100,10 @@ struct PagedAttentionRuntimeParams : public ImplRuntimeParams {size_t max_context_len;// below are rt params for decodingsize_t num_of_partitions;+ // cached single-token Q chunking+ SingleTokenQChunking q_chunking;// below are rt params for xattn+ size_t block_wg_m;size_t q_block_pad;size_t k_block_pad;size_t q_stride_pad;@@ -104,6 +161,20 @@ public:class PagedAttentionGeneratorMultiToken : public PagedAttentionGeneratorBase {public:PagedAttentionGeneratorMultiToken() : PagedAttentionGeneratorBase("pa_multi_token") {}++ static size_t get_q_step(const kernel_impl_params& params) {+ const auto xe_arch = params.get_device_info().arch < gpu_arch::xe2 ? 1 : 2;+ if (xe_arch == 1) {+ return 8; // For Xe1+ }+ // For Xe2, q_step = CM_GRF_WIDTH / 32+ return 16; // For Xe2++ }++ static size_t get_wg_seq_len(const kernel_impl_params& params) {+ return WG_SIZE * get_q_step(params);+ }+[[nodiscard]] Arguments get_arguments_desc(const kernel_impl_params& params) const override;[[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override;[[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override;@@ -133,6 +204,23 @@ public:explicit XAttentionEstimateGeneratorBase(std::string_view kernel_name, size_t xattn_block_size, std::string_view stage_suffix = "_cm"): KernelGenerator(kernel_name, stage_suffix),_xattn_block_size(xattn_block_size) {}++ static uint32_t get_block_sg_m(const kernel_impl_params& params) {+ return is_xe2_or_xe3(params) ? 64u : 32u;+ }++ static uint32_t get_block_sg_n(const kernel_impl_params& params) {+ return is_xe2_or_xe3(params) ? 32u : 16u;+ }++ static uint32_t get_block_wg_m(const kernel_impl_params& params) {+ return get_block_sg_m(params) * SG_M;+ }++ static uint32_t get_block_wg_n(const kernel_impl_params& params) {+ return get_block_sg_n(params) * SG_N;+ }+[[nodiscard]] std::string get_build_options(const RuntimeParams& params) const override {return KernelGenerator::get_build_options(params) + get_pa_build_options();}@@ -140,6 +228,12 @@ public:protected:size_t _xattn_block_size;++private:+ static bool is_xe2_or_xe3(const kernel_impl_params& params) {+ const auto arch = params.get_device_info().arch;+ return arch == gpu_arch::xe2 || arch == gpu_arch::xe3;+ }};class XAttentionEstimateGEMMQK : public XAttentionEstimateGeneratorBase {public: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 4da88f60254..3a70838ebf0 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@@ -59,17 +59,26 @@ CM_INLINE void get_mn(uint& id_wg_m, uint& id_wg_n, uint M, uint N, int slice_no}}+extern "C" _GENX_MAIN_ void KERNEL_NAME(+ #ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t key_cache ATTR,svmptr_t query ATTR,+ #else+ SurfaceIndex key_cache [[type("buffer_t")]],+ SurfaceIndex query [[type("buffer_t")]],+ #endifsvmptr_t block_indices ATTR,svmptr_t block_indices_begins ATTR,svmptr_t kq_max_wg ATTR,+ #ifdef CM_HAS_LSC_UNTYPED_2Dsvmptr_t kq_exp_partial_sum ATTR,+ #else+ SurfaceIndex kq_exp_partial_sum [[type("buffer_t")]],+ #endifuint M, uint N, uint K, uint query_stride, int slice_no, int slice, uint q_start_strided) {const uint BLOCK_WG_M = BLOCK_SG_M * SG_M;const uint BLOCK_WG_N = BLOCK_SG_N * SG_N;- const uint size_slm_b = 0;uint hq = cm_group_id(2) * WALK_HQ;hq += cm_group_id(0) & (WALK_HQ - 1);if (hq >= HQ) return;@@ -83,14 +92,16 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(uint id_wg_m, id_wg_n;get_mn(id_wg_m, id_wg_n, M, N, slice_no, slice, BLOCK_WG_M, BLOCK_WG_N);+ #ifdef CM_HAS_LSC_UNTYPED_2D// key cache: [block, HQ, KV_BLOCK_SIZE, HEAD_SIZE_KEY]#if USE_INT8key_cache += hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(char));#elsekey_cache += hk * (KV_BLOCK_SIZE * HEAD_SIZE_KEY * (uint)sizeof(half));#endif- // query: [l_q, HQ * HEAD_SIZE]query += hq * HEAD_SIZE * (uint)sizeof(half);+ #endif+// kq_max: [hq, m_pad]// kq_max_wg: [hq, n_groups, m_pad]@@ -102,12 +113,15 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(const uint sum_per_n_token_in_block = BLOCK_SIZE / STRIDE;const uint n_after_sum_in_group = BLOCK_WG_N / sum_per_n_token_in_block;const uint n_after_sum_pad = n_after_sum_in_group * n_groups;- kq_exp_partial_sum += hq * n_after_sum_pad * m_pad * (uint)sizeof(SOFTMAX_TYPE);+ const uint offset_partial_sum = hq * n_after_sum_pad * m_pad * (uint)sizeof(SOFTMAX_TYPE);+ #ifdef CM_HAS_LSC_UNTYPED_2D+ kq_exp_partial_sum += offset_partial_sum;+ #endif-#define CONCAT_IMPL(a, b) gemm_qk_ ##a ##x ##b ##_xe2+#define CONCAT_IMPL(a, b) KERNEL_NAME::gemm_qk#define CONCAT(x, y) CONCAT_IMPL(x, y)#define FUNC CONCAT(BLOCK_SG_M, BLOCK_SG_N)- FUNC(id_wg_m, id_wg_n, hq, slm, key_cache, query, block_indices, block_indices_begins, kq_max_wg, kq_exp_partial_sum, M, N, K, query_stride, q_start_strided);+ FUNC(id_wg_m, id_wg_n, hq, slm, key_cache, query, block_indices, block_indices_begins, kq_max_wg, kq_exp_partial_sum, M, N, K, query_stride, q_start_strided, offset_partial_sum);}} // NAMESPACE\ No newline at end of filediff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex 61cdf2d9446..9e2ffa0f694 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -599,7 +599,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {auto check_xattn_gpu_compatibility = [&](void) -> bool {auto& engine = m_context->get_engine();const auto& info = engine.get_device_info();- if (info.arch != cldnn::gpu_arch::xe2 && info.arch != cldnn::gpu_arch::xe3) { // CM optimized for systolic-array architectures+ if (!(info.supports_immad)) { // CM optimized for systolic-array architecturesreturn false;}