OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] some fixes and optimizations to CM PA and XAttention kernels (#33454)
cecilia peng40c1cadf84df
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 8068ac79743..17b8803629a 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@@ -286,407 +286,6 @@ CM_INLINE void write_2d(matrix_ref<TSRC, M, N> out, SurfaceIndex base, uint offs}}--#if USE_KQ == 1 && (BLOCK_SG_M == 64 && BLOCK_SG_N == 32)-// register tile: [8, 2] aka[(8*8,16), (16, 16*2)]-// src_a is key, src_b is query-CM_INLINE void gemm_kq_64x32_xe2(uint id_wg_m, uint id_wg_n, uint hq, uint slm, svmptr_t key_cache, svmptr_t query, svmptr_t block_indices ATTR, svmptr_t block_indices_begins ATTR, svmptr_t kq_max 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;- constexpr int BLOCK_WG_K = 64; // same in sg-#ifndef BLOCK_SG_M- #define BLOCK_SG_M 64- #define BLOCK_SG_N 32- #define SG_M 4- #define SG_N 4- #define HEAD_SIZE 128- #define KV_BLOCK_SIZE 256- #define STRIDE 16-#endif- // xehpg DPAS spec: dst: [8, 8], repeat: 1~8, depth: 8- static 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 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;- // 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_K = BLOCK_WG_K / BLOCK_REG_K;- static 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- 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;-- static_assert(REG_N == 2, "block_2d_desc for b is manually unrolled by 2");- static_assert(HEAD_SIZE % BLOCK_WG_K == 0, "K dimension must be multiple of BLOCK_WG_K");- static_assert(KV_BLOCK_SIZE == 256, "block size of key(key_cache) should be 256");- uint M_block = (M + BLOCK_WG_M - 1) / BLOCK_WG_M;- uint N_aligned = (N + BLOCK_WG_N - 1) / BLOCK_WG_N * BLOCK_WG_N;- uint M_block_aligned = M_block * (BLOCK_WG_M / (BLOCK_SIZE / STRIDE));- const uint block_size_div_stride = BLOCK_SIZE / STRIDE;- constexpr half log2e = 1.4426950408889634f;- static_assert(BLOCK_SG_M / block_size_div_stride == 8, "BLOCK_SG_M / block_size_div_stride should be 8");- static_assert(BLOCK_SG_N == 32, "BLOCK_SG_N should be 32");--#if IS_CAUSAL == 1- if ((int)(id_wg_m * BLOCK_WG_M) >= ((int)id_wg_n + 1) * BLOCK_WG_N + q_start_strided) {- // fill -inf -> max in group, 0 -> exp_sum to make compensation work- {- // current max -> mem- vector<half, BLOCK_SG_N> max_n = -60000;- // kq_max_wg: [b, hq, M/BLOCK_WG_M, N_aligned]- uint offset = (id_wg_m * N_aligned + id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * sizeof(half);- cm_ptr_store<int>((int*)kq_max_wg, offset, max_n.format<int>());- }- {- // store- matrix<half, 8, 8> sum_t = 0;- lsc::block_2d_desc<half, 1, 8, 8> desc_c{ kq_exp_partial_sum, N - 1, (uint)(M_block_aligned * sizeof(half) - 1), (uint)(M_block_aligned * sizeof(half) - 1),- (int)((id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) / block_size_div_stride), (int)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) };- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 0>(desc_c, sum_t.format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 1>(desc_c, sum_t.format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 2>(desc_c, sum_t.format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 3>(desc_c, sum_t.format<half>());- }-- return;- }-#endif- // assume block index coming from 0 in block_indices_begins- int 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;- // N[0:16*2]xK[0:16]- lsc::block_2d_desc<int, 1, BLOCK_REG_N, BLOCK_REG_K / 2> desc_b0{ query, N - 1, (uint)((query_stride - hq * HEAD_SIZE) * sizeof(half) - 1), (uint)(query_stride * sizeof(half) - 1),- (STRIDE - 1) * b_adjacent_between_head / 2, (int)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) };- // prefetch B- static constexpr int SG_MN = SG_M * SG_N;- lsc::block_2d_desc<half, 1, BLOCK_WG_N / SG_MN, 32> desc_prefetch_b{query, N - 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_n * BLOCK_WG_N + id_sg_mn * (BLOCK_WG_N / SG_MN)) };- // N[0:16*2]xK[0:16] --> 8+8 regs- matrix<half, REG_N, BLOCK_REG_B> b0, b1;-- // M[0:16]xK[0:32]- uint block_idx = (uint)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) * STRIDE / KV_BLOCK_SIZE;- uint offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));- lsc::block_2d_desc<half, 2, KEY_LINES_PER_LOAD, BLOCK_REG_K> desc_a0{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),- 0, 0 };- // M[16:32]xK[0:32]- offset = block_indices_p[block_idx + 1] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));- lsc::block_2d_desc<half, 2, KEY_LINES_PER_LOAD, BLOCK_REG_K> desc_a1{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),- 0, 0 };- // M[32:48]xK[0:32]- offset = block_indices_p[block_idx + 2] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));- lsc::block_2d_desc<half, 2, KEY_LINES_PER_LOAD, BLOCK_REG_K> desc_a2{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),- 0, 0 };- // M[48:64]xK[0:32]- offset = block_indices_p[block_idx + 3] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));- lsc::block_2d_desc<half, 2, KEY_LINES_PER_LOAD, BLOCK_REG_K> desc_a3{ key_cache + offset, KEY_LINES_PER_LOAD - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),- 0, 0 };- // prefetch A- block_idx = (uint)(id_wg_m * BLOCK_WG_M + id_sg_mn * (BLOCK_WG_M / SG_MN)) * STRIDE / KV_BLOCK_SIZE;- offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));- static_assert(BLOCK_WG_M / SG_MN <= KEY_LINES_PER_LOAD, "prefetch lines should be inside one block");- lsc::block_2d_desc<half, 1, BLOCK_WG_M / SG_MN, 32> desc_prefetch_a{ key_cache + offset, BLOCK_WG_M / SG_MN - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),- 0, 0 };- // 0~2 M[:]xK[0:16] 2~4 K[16:32] --> 32 * 2 regs- matrix<half, 4, BLOCK_REG_A> a0, a1, a2, a3;-- // warmup- // prefetch- cm_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);-- // load b: N[0:16]xK[0:16]- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0.row(0).format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(b0.row(1).format<int>(), desc_b0);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- cm_sbarrier(1);-- auto dot = [&](matrix_ref<half, 2, BLOCK_REG_A> A0, matrix_ref<half, 2, BLOCK_REG_A> A1, matrix_ref<half, 2, BLOCK_REG_A> A2, matrix_ref<half, 2, BLOCK_REG_A> A3, matrix_ref<half, REG_N, BLOCK_REG_B> B) {-#pragma unroll- for (int reg_n = 0; reg_n < REG_N; reg_n++) {-#pragma unroll- for (uint reg_m = 0; reg_m < 2; 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>(), A0.row((ushort)reg_m).format<int>());- }-#pragma unroll- for (uint reg_m = 0; reg_m < 2; reg_m++) {- acc.row((ushort)((reg_m + 2) * REG_N + reg_n)) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, 8, 8>(acc.row((ushort)((reg_m + 2) * REG_N + reg_n)),- B.row((ushort)reg_n).format<int>(), A1.row((ushort)reg_m).format<int>());- }-#pragma unroll- for (uint reg_m = 0; reg_m < 2; reg_m++) {- acc.row((ushort)((reg_m + 4) * REG_N + reg_n)) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, 8, 8>(acc.row((ushort)((reg_m + 4) * REG_N + reg_n)),- B.row((ushort)reg_n).format<int>(), A2.row((ushort)reg_m).format<int>());- }-#pragma unroll- for (uint reg_m = 0; reg_m < 2; reg_m++) {- acc.row((ushort)((reg_m + 6) * REG_N + reg_n)) = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, 8, 8>(acc.row((ushort)((reg_m + 6) * REG_N + reg_n)),- B.row((ushort)reg_n).format<int>(), A3.row((ushort)reg_m).format<int>());- }- }- };-- for (uint s = 0; s < STRIDE; s++) {- #pragma unroll- for (uint hs = 0; hs < HEAD_SIZE / BLOCK_WG_K; hs++) {- // prefetch- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);- cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);- desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);- if (hs == HEAD_SIZE / BLOCK_WG_K - 1)- desc_prefetch_b.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);- else- desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 32);-- // load b: N[0:16*2]xK[16:32]- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(b1.row(0).format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(b1.row(1).format<int>(), desc_b0);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);-- // load a: M[0:16*4]xK[0:32]- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a0.format<half>(), desc_a0);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a1.format<half>(), desc_a1);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a2.format<half>(), desc_a2);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a3.format<half>(), desc_a3);-- desc_a0.set_block_x(desc_a0.get_block_x() + 32);- desc_a1.set_block_x(desc_a1.get_block_x() + 32);- desc_a2.set_block_x(desc_a2.get_block_x() + 32);- desc_a3.set_block_x(desc_a3.get_block_x() + 32);-- dot(a0.select<2, 1, BLOCK_REG_A, 1>(), a1.select<2, 1, BLOCK_REG_A, 1>(),- a2.select<2, 1, BLOCK_REG_A, 1>(), a3.select<2, 1, BLOCK_REG_A, 1>(),- b0);-- // load b: N[0:16*2]xK[32:48]- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(b0.row(0).format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(b0.row(1).format<int>(), desc_b0);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);-- dot(a0.select<2, 1, BLOCK_REG_A, 1>(2), a1.select<2, 1, BLOCK_REG_A, 1>(2),- a2.select<2, 1, BLOCK_REG_A, 1>(2), a3.select<2, 1, BLOCK_REG_A, 1>(2),- b1);-- // prefetch- cm_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);- desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);-- // load b: N[0:16*2]xK[48:64]- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(b1.row(0).format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(b1.row(1).format<int>(), desc_b0);- if (hs == HEAD_SIZE / BLOCK_WG_K - 1)- desc_b0.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head / 2);- else- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- // load a: M[0:32]xK[32:64]- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a0.format<half>(), desc_a0);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a1.format<half>(), desc_a1);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a2.format<half>(), desc_a2);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached>(a3.format<half>(), desc_a3);- desc_a0.set_block_x(desc_a0.get_block_x() + 32);- desc_a1.set_block_x(desc_a1.get_block_x() + 32);- desc_a2.set_block_x(desc_a2.get_block_x() + 32);- desc_a3.set_block_x(desc_a3.get_block_x() + 32);-- dot(a0.select<2, 1, BLOCK_REG_A, 1>(), a1.select<2, 1, BLOCK_REG_A, 1>(),- a2.select<2, 1, BLOCK_REG_A, 1>(), a3.select<2, 1, BLOCK_REG_A, 1>(),- b0);-- // load b: N[0:16*4]xK[0:16]- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 0>(b0.row(0).format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached, 0, 16>(b0.row(1).format<int>(), desc_b0);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);-- dot(a0.select<2, 1, BLOCK_REG_A, 1>(2), a1.select<2, 1, BLOCK_REG_A, 1>(2),- a2.select<2, 1, BLOCK_REG_A, 1>(2), a3.select<2, 1, BLOCK_REG_A, 1>(2),- b1);- cm_sbarrier(0);- cm_sbarrier(1);- }- }-- cm_sbarrier(0);-- matrix<half, REG_M * BLOCK_REG_M, REG_N * BLOCK_REG_N> acc_half;- union {- float f;- int y;- } i2f;- i2f.y = INV_S;- const float inv_s = i2f.f;-#pragma unroll- for (uint reg_m = 0; reg_m < REG_M; reg_m++) {-#pragma unroll- for (int reg_n = 0; reg_n < REG_N; reg_n++) {- acc_half.select<BLOCK_REG_M, 1, BLOCK_REG_N, 1>(reg_m * BLOCK_REG_M, reg_n * BLOCK_REG_N) =- acc.row(reg_m * REG_N + reg_n) * inv_s;- }- }-- // if N(aka query) has tails, the following will not change the accuracy:- // gemm will compute results for the padding N(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- // all tails + padding results.- int m_start = (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M);- m_start = MYMIN(m_start, M);- int m_end = MYMIN(m_start + BLOCK_SG_M, M);- int valid_m = m_end - m_start;- matrix<half, 32, 8> sum_t;- vector<int, BLOCK_SG_N> seq;- cmtl::cm_vector_assign(seq.select_all(), 0, 1);-#if IS_CAUSAL == 1- bool skip_mask = false;- // in streaming scenario, the past kvcache length may be arbitrary so valid causal mask of a workgroup may start at arbitrary position- if (m_end <= (int)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N + q_start_strided)) {- // all are inside causal mask == 1- skip_mask = true;- } else {- vector<uint, BLOCK_SG_N> n_pos = (uint)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N + q_start_strided) + seq;- #pragma unroll- for (uint reg_m = 0; reg_m < REG_M * BLOCK_REG_M; reg_m++) {- SIMD_IF_BEGIN (m_start + reg_m > n_pos) {- acc_half.row(reg_m) = half{-60000};- } SIMD_IF_END;- }- }-#else- bool skip_mask = true;-#endif- // case for valid_m == BLOCK_SG_M but skip_mask == false which needs to handle causal mask:- // query = 128 * 2 + 1, key = 256 * 2- if (valid_m == BLOCK_SG_M && skip_mask) {- vector<half, BLOCK_SG_N> max_n = acc_half.row(0);- #pragma unroll- for (uint reg_m = 1; reg_m < REG_M * BLOCK_REG_M; reg_m++) {- max_n = cm_max<half>(max_n, acc_half.row(reg_m));- }-- {- uint slm_offset = (id_sg_m * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * (uint)sizeof(half);- // current max -> slm- cm_slm_block_write(slm, slm_offset, max_n.format<int>());- cm_slm_fence(CM_LOCAL_BARRIER);- cm_barrier();- // max inside wg- cm_slm_block_read(slm, id_sg_n * BLOCK_SG_N * (uint)sizeof(half), max_n.format<int>());- vector<half, BLOCK_SG_N> tmp;- #pragma unroll- for (uint i = 1; i < SG_M; i++) {- slm_offset = (i * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * (uint)sizeof(half);- cm_slm_block_read(slm, slm_offset, tmp.format<int>());- max_n = cm_max<half>(max_n, tmp);- }- // max across wg- // kq_max: [b, hq, N_aligned]- vector<uint, BLOCK_SG_N> max_offsets = (id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N + seq) * (uint)sizeof(half);- cm_ptr_atomic<AtomicOp::FMAX, half>((half*)kq_max, max_offsets, max_n);-- // current max -> mem- // kq_max_wg: [b, hq, M/BLOCK_WG_M, N_aligned]- uint offset = (id_wg_m * N_aligned + id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * sizeof(half);- cm_ptr_store<int>((int*)kq_max_wg, offset, max_n.format<int>());- }- {- // kq_exp_partial_sum: [b, hq, N_aligned, M/(BLOCK_SIZE/STRIDE)]- matrix<half, 8, 32> sum;- #pragma unroll- for (uint m = 0; m < BLOCK_SG_M / block_size_div_stride; m++) {- sum.row(m) = cm_exp((acc_half.row(m * block_size_div_stride) - max_n) * log2e);- #pragma unroll- for (uint sub_m = 1; sub_m < block_size_div_stride; sub_m++) {- uint real_m = m * block_size_div_stride + sub_m;- sum.row(m) += cm_exp((acc_half.row(real_m) - max_n) * log2e);- }- }-- Transpose_8x32(sum, sum_t);- }- } else {- // M tails- vector<half, BLOCK_SG_N> max_n = -60000;- for (uint reg_m = 0; reg_m < valid_m; reg_m++) {- max_n = cm_max<half>(max_n, acc_half.row(reg_m));- }-- {- uint slm_offset = (id_sg_m * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * (uint)sizeof(half);- // current max -> slm- cm_slm_block_write(slm, slm_offset, max_n.format<int>());- cm_slm_fence(CM_LOCAL_BARRIER);- cm_barrier();- // max inside wg- cm_slm_block_read(slm, id_sg_n * BLOCK_SG_N * (uint)sizeof(half), max_n.format<int>());- vector<half, BLOCK_SG_N> tmp;- #pragma unroll- for (uint i = 1; i < SG_M; i++) {- slm_offset = (i * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * (uint)sizeof(half);- cm_slm_block_read(slm, slm_offset, tmp.format<int>());- max_n = cm_max<half>(max_n, tmp);- }- // max across wg- // kq_max: [b, hq, N_aligned]- vector<uint, BLOCK_SG_N> max_offsets = (id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N + seq) * (uint)sizeof(half);- cm_ptr_atomic<AtomicOp::FMAX, half>((half*)kq_max, max_offsets, max_n);-- // current max -> mem- // kq_max_wg: [b, hq, M/BLOCK_WG_M, N_aligned]- uint offset = (id_wg_m * N_aligned + id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) * sizeof(half);- cm_ptr_store<int>((int*)kq_max_wg, offset, max_n.format<int>());- }- {- // kq_exp_partial_sum: [b, hq, N_aligned, M/(BLOCK_SIZE/STRIDE)]- matrix<half, 8, 32> sum = 0;- vector<uint, BLOCK_SG_N> n_pos = (uint)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N + q_start_strided) + seq;- #pragma unroll- for (uint m = 0; m < BLOCK_SG_M / block_size_div_stride; m++) {- #pragma unroll- for (uint sub_m = 0; sub_m < block_size_div_stride; sub_m++) {- uint real_m = m * block_size_div_stride + sub_m;-#if IS_CAUSAL == 1- // to following case:- // 0 0 1 1- // 0 0 0 1- // the acc value of first column should be -inf --> max(first column) == -inf --> exp(first column - max) == 1, this is incorrect- // so need to use simd_if to detect per element state- SIMD_IF_BEGIN ((m_start + real_m <= n_pos) & (real_m < valid_m)) {- sum.row(m) += cm_exp((acc_half.row(real_m) - max_n) * log2e);- } SIMD_IF_END;-#else- if (real_m < valid_m)- sum.row(m) += cm_exp((acc_half.row(real_m) - max_n) * log2e);-#endif- }- }- Transpose_8x32(sum, sum_t);- }- }- // store- lsc::block_2d_desc<half, 1, 8, 8> desc_c{ kq_exp_partial_sum, N - 1, (uint)(M_block_aligned * sizeof(half) - 1), (uint)(M_block_aligned * sizeof(half) - 1),- (int)((id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) / block_size_div_stride), (int)(id_wg_n * BLOCK_WG_N + id_sg_n * BLOCK_SG_N) };- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 0>(desc_c, sum_t.select<8, 1, 8, 1>( 0).format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 1>(desc_c, sum_t.select<8, 1, 8, 1>( 8).format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 2>(desc_c, sum_t.select<8, 1, 8, 1>(16).format<half>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 3>(desc_c, sum_t.select<8, 1, 8, 1>(24).format<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};@@ -694,16 +293,6 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {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;- constexpr int BLOCK_WG_K = 64; // same in sg-#ifndef BLOCK_SG_M- #define BLOCK_SG_M 64- #define BLOCK_SG_N 32- #define SG_M 4- #define SG_N 4- #define HEAD_SIZE 128- #define KV_BLOCK_SIZE 256- #define STRIDE 16-#endif// xehpg DPAS spec: dst: [8, 8], repeat: 1~8, depth: 8static constexpr int REPEAT = 8;static constexpr int DEPTH = 8;@@ -719,7 +308,6 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {// register blockingstatic 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_K = BLOCK_WG_K / BLOCK_REG_K;static constexpr int REG_MN = REG_M * REG_N;static constexpr int KEY_LINES_PER_LOAD = KV_BLOCK_SIZE / STRIDE;@@ -728,6 +316,11 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {uint id_sg_m = cm_local_id(1);uint id_sg_mn = id_sg_m * SG_N + id_sg_n;+ static_assert(BLOCK_WG_K == 32 || BLOCK_WG_K == 64, "Supported BLOCK_WG_K are 32 or 64");+ constexpr bool IS64 = (BLOCK_WG_K == 64);+ constexpr bool IS32 = (BLOCK_WG_K == 32);+ constexpr int PHASE_SIZE = IS64 ? 32 : 16;+static_assert(REG_N == 2, "block_2d_desc for b is manually unrolled by 2");static_assert(HEAD_SIZE % BLOCK_WG_K == 0, "K dimension must be multiple of BLOCK_WG_K");static_assert(KV_BLOCK_SIZE == 256, "block size of key(key_cache) should be 256");@@ -776,7 +369,7 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {(STRIDE - 1) * b_adjacent_between_head, (int)(id_wg_m * BLOCK_WG_M + id_sg_m * BLOCK_SG_M) };// prefetch Astatic constexpr int SG_MN = SG_M * SG_N;- lsc::block_2d_desc<half, 1, BLOCK_WG_M / SG_MN, 32> desc_prefetch_a{query, M - 1, (uint)((query_stride - hq * HEAD_SIZE) * sizeof(half) - 1), (uint)(query_stride * sizeof(half) - 1),+ 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 regsmatrix<half, REG_M, BLOCK_REG_A> a0;@@ -800,7 +393,7 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {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");- lsc::block_2d_desc<uchar, 1, BLOCK_WG_N / SG_MN, 32> desc_prefetch_b{ key_cache + offset, BLOCK_WG_N / SG_MN - 1, (uint)(K * sizeof(char) - 1), (uint)(K * sizeof(char) - 1),+ lsc::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 };// N[:]xK[0:32] --> 16 * 1 regs@@ -821,18 +414,20 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {block_idx = MYMIN(block_idx, max_block_idx);offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));static_assert(BLOCK_WG_N / SG_MN <= KEY_LINES_PER_LOAD, "prefetch lines should be inside one block");- lsc::block_2d_desc<half, 1, BLOCK_WG_N / SG_MN, 32> desc_prefetch_b{ key_cache + offset, BLOCK_WG_N / SG_MN - 1, (uint)(K * sizeof(half) - 1), (uint)(K * sizeof(half) - 1),+ lsc::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 };// 0~2 M[:]xK[0:16] 2~4 K[16:32] --> 32 * 2 regsmatrix<half, 2, BLOCK_REG_B> b0, b1;#endif- // warmup- // prefetch- cm_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);+ if constexpr (IS64) {+ // warmup+ // prefetch+ cm_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);+ }// load b: N[0:16]xK[0:16]#if USE_INT8@@ -855,16 +450,20 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {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);}- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);+ if constexpr (IS64) {+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);+ }#else- 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);+ if constexpr (IS64) {+ 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);+ }#endif- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- desc_b1.set_block_x(desc_b1.get_block_x() + 8);-- cm_sbarrier(1);+ if constexpr (IS64) {+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ }#if USE_INT8auto dec = [&](vector<int, 64> B0_i8, vector<int, 64> B1_i8, matrix_ref<half, REG_N, BLOCK_REG_B> B0) {@@ -915,102 +514,158 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {#endif#pragma unrollfor (uint hs = 0; hs < HEAD_SIZE / BLOCK_WG_K; hs++) {- // prefetch- cm_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);- if (hs == HEAD_SIZE / BLOCK_WG_K - 1)- desc_prefetch_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);- else- desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);+ if constexpr (IS64) {+ // 4 phases with b0, b1 ping-pong buffer+ // prefetch+ cm_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);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1)+ desc_prefetch_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);+ else+ desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);- // load a: M[0:16*4]xK[0:16]- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);- // load b: N[0:16*2]xK[16:32]+ // load a: M[0:16*4]xK[0:16]+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);+ // load b: N[0:16*2]xK[16:32]#if USE_INT8- cm_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);- dot(a0, b0);+ cm_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);+ dot(a0, b0);#else- cm_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);- dot(a0, b0);+ cm_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);+ dot(a0, b0);#endif- desc_a.set_block_x(desc_a.get_block_x() + 16);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ desc_a.set_block_x(desc_a.get_block_x() + 16);+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);- // load a: M[0:16*4]xK[16:32]- 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);+ // load a: M[0:16*4]xK[16:32]+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);#if USE_INT8- dec(b0_up_s8.format<int>().select<64, 1>(64), b0_down_s8.format<int>().select<64, 1>(64), b0);- dot(a0, b0);+ dec(b0_up_s8.format<int>().select<64, 1>(64), b0_down_s8.format<int>().select<64, 1>(64), b0);+ dot(a0, b0);#else- 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, b1);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ 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, b1);+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);#endif- desc_a.set_block_x(desc_a.get_block_x() + 16);+ desc_a.set_block_x(desc_a.get_block_x() + 16);- // prefetch- cm_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);- desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 32);+ // prefetch+ cm_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);+ 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);+ // 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);- // load b: N[0:16*2]xK[32:64]+ // load b: N[0:16*2]xK[32:64]#if USE_INT8- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);- cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);- dec(b1_up_s8.format<int>().select<64, 1>(), b1_down_s8.format<int>().select<64, 1>(), b0);- dot(a0, b0);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);+ dec(b1_up_s8.format<int>().select<64, 1>(), b1_down_s8.format<int>().select<64, 1>(), b0);+ dot(a0, b0);#else- cm_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);- dot(a0, b0);+ cm_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);+ dot(a0, b0);#endif- desc_a.set_block_x(desc_a.get_block_x() + 16);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ desc_a.set_block_x(desc_a.get_block_x() + 16);+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);++ // load a: M[0:16*4]xK[48:64]+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);+ if (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);+ }- // load a: M[0:16*4]xK[48:64]- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);- cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);- if (hs == HEAD_SIZE / BLOCK_WG_K - 1) {- desc_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);+#if USE_INT8+ dec(b1_up_s8.format<int>().select<64, 1>(64), b1_down_s8.format<int>().select<64, 1>(64), b0);+ dot(a0, b0);+#else+ 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);+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ dot(a0, b1);+#endif} else {+ // 2 phases with single buffer b0+ // prefetch+ desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 16);+ desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 16);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);++ // load a: M[0:16*4]xK[0:16]+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);+#if USE_INT8+ // load b: N[0:16*2]xK[0:32]+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_up_s8.format<int>(), desc_b0);+ cm_load<lsc::Transpose, CacheHint::Cached, CacheHint::Cached>(b0_down_s8.format<int>(), desc_b1);+ dec(b0_up_s8.format<int>().select<64, 1>(), b0_down_s8.format<int>().select<64, 1>(), b0);+ dot(a0, b0);+#else+ // load b: N[0:16*2]xK[0:16]+ 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);+#endif+desc_a.set_block_x(desc_a.get_block_x() + 16);- }+ desc_b0.set_block_x(desc_b0.get_block_x() + 8);+ desc_b1.set_block_x(desc_b1.get_block_x() + 8);+ // prefetch+ desc_prefetch_b.set_block_x(desc_prefetch_b.get_block_x() + 16);+ if (hs == HEAD_SIZE / BLOCK_WG_K - 1)+ desc_prefetch_a.set_block_x((STRIDE - 1 - s - 1) * b_adjacent_between_head);+ else+ desc_prefetch_a.set_block_x(desc_prefetch_a.get_block_x() + 16);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_b);+ cm_prefetch<CacheHint::Cached, CacheHint::Cached>(desc_prefetch_a);++ // load a: M[0:16*4]xK[16:32]+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(a0.select<4, 1, BLOCK_REG_A, 1>(0).format<half>(), desc_a);+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 32>(a0.select<4, 1, BLOCK_REG_A, 1>(4).format<half>(), desc_a);+ if (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);+ }#if USE_INT8- dec(b1_up_s8.format<int>().select<64, 1>(64), b1_down_s8.format<int>().select<64, 1>(64), b0);- dot(a0, b0);+ dec(b0_up_s8.format<int>().select<64, 1>(64), b0_down_s8.format<int>().select<64, 1>(64), b0);+ dot(a0, b0);#else- 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);- desc_b0.set_block_x(desc_b0.get_block_x() + 8);- desc_b1.set_block_x(desc_b1.get_block_x() + 8);- dot(a0, b1);+ // 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);#endif-- cm_sbarrier(0);- cm_sbarrier(1);+ }}}- cm_sbarrier(0);+ cm_barrier();matrix<SOFTMAX_TYPE, REG_M * BLOCK_REG_M, REG_N * BLOCK_REG_N> acc_half;union {diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm b/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cmindex 19374b94180..45192354c1d 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/pa_kv_cache_update_ref.cm@@ -23,7 +23,79 @@#endifconstexpr uint wg_size = WG_SIZE;-#define REG_K 16++template <int HEAD_SIZE>+CM_INLINE void load_kvcache(vector_ref<half, HEAD_SIZE> kv_data, const half* kv_ptr [[type("svmptr_t")]], uint offset) {+ if constexpr (HEAD_SIZE == 16 || HEAD_SIZE == 32 || HEAD_SIZE == 64 || HEAD_SIZE == 128) {+ kv_data.format<int>() = cm_ptr_load<int, HEAD_SIZE / 2>((int*)kv_ptr, offset);+ } else if constexpr (HEAD_SIZE == 96) {+ kv_data.select<64, 1>(0).format<int>() = cm_ptr_load<int, 32>((int*)kv_ptr, offset);+ kv_data.select<32, 1>(64).format<int>() = cm_ptr_load<int, 16>((int*)kv_ptr, offset + 32 * sizeof(int));+ } else {+ // # head_size is restricted to by be divisible by 16 in CM PA/Xattn pipeline.+ #pragma unroll+ for(int i = 0; i < HEAD_SIZE / 16; i++) {+ kv_data.select<16, 1>(16*i).format<int>() = cm_ptr_load<int, 8>((int*)kv_ptr, offset + i * 8 * sizeof(int));+ }+ }+}++template <typename T, int HEAD_SIZE>+CM_INLINE void store_kvcache(const svmptr_t kv_cache [[type("svmptr_t")]], uint offset, vector_ref<T, HEAD_SIZE> kv_data) {+ if constexpr(std::is_same<T, half>::value) {+ if constexpr (HEAD_SIZE == 16 || HEAD_SIZE == 32 || HEAD_SIZE == 64 || HEAD_SIZE == 128) {+ cm_ptr_store<int, HEAD_SIZE / 2>((int*)kv_cache, offset, kv_data.format<int>());+ } else if constexpr (HEAD_SIZE == 96) {+ cm_ptr_store<int, 32>((int*)kv_cache, offset, kv_data.select<64, 1>(0).format<int>());+ cm_ptr_store<int, 16>((int*)kv_cache, offset + 32 * sizeof(int), kv_data.select<32, 1>(64).format<int>());+ } else {+ // # head_size is restricted to by be divisible by 16 in CM PA/Xattn pipeline.+ #pragma unroll+ for(int i = 0; i < HEAD_SIZE / 16; i++) {+ cm_ptr_store<int, 8>((int*)kv_cache, offset + i * 8 * sizeof(int), kv_data.select<16, 1>(16*i).format<int>());+ }+ }+ } else {+ if constexpr (HEAD_SIZE == 32 || HEAD_SIZE == 64 || HEAD_SIZE == 128 || HEAD_SIZE == 256) {+ cm_ptr_store<int, HEAD_SIZE / 4>((int*)kv_cache, offset, kv_data.format<int>());+ } else if constexpr (HEAD_SIZE == 96) {+ cm_ptr_store<int, 16>((int*)kv_cache, offset, kv_data.select<64, 1>(0).format<int>());+ cm_ptr_store<int, 8>((int*)kv_cache, offset + 16 * sizeof(int), kv_data.select<32, 1>(64).format<int>());+ } else {+ // # head_size is restricted to by be divisible by 16 in CM PA/Xattn pipeline.+ #pragma unroll+ for(int i = 0; i < HEAD_SIZE / 16; i++) {+ cm_ptr_store<int, 4>((int*)kv_cache, offset + i * 4 * sizeof(int), kv_data.select<16, 1>(16*i).format<int>());+ }+ }+ }+}++#if KV_CACHE_COMPRESSION_PER_TOKEN+template <int HEAD_SIZE>+CM_INLINE void quantize_and_store(vector_ref<half, HEAD_SIZE> data, uchar* out, uint out_offset, uint token_pos) {+ uint scale_offset = out_offset + HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE + token_pos * sizeof(half);+ half max_val = cm_reduced_max<half>(data);+ half min_val = cm_reduced_min<half>(data);+ float scale_val = float(0.0);+ half zp_val = half(0.0);+ if(max_val == min_val) {+ scale_val = float(0.0);+ zp_val = max_val;+ } else {+ scale_val = 255.0 / (max_val - min_val);+ zp_val = (0.0 - min_val) * scale_val;+ }+ vector<half, HEAD_SIZE> dequant_data = cm_mul<half>(data, scale_val) + zp_val;+ vector<uchar, HEAD_SIZE> data_u8 = cm_rnde<uchar, HEAD_SIZE>(dequant_data);++ store_kvcache<uchar, HEAD_SIZE>(reinterpret_cast<svmptr_t>(out + out_offset + token_pos * HEAD_SIZE), 0, data_u8);++ half *out_scale_zp = (half*)(out + scale_offset);+ out_scale_zp[0] = (max_val - min_val) / 255.0;+ out_scale_zp[PAGED_ATTENTION_BLOCK_SIZE] = zp_val;+}+#endifextern "C" _GENX_MAIN_ void KERNEL_NAME(const half* key [[type("svmptr_t")]],@@ -38,7 +110,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#elsehalf* key_cache [[type("svmptr_t")]],half* value_cache [[type("svmptr_t")]],-#endif+#endifuint32_t key_pitch,uint32_t key_offset,uint32_t value_pitch,@@ -58,10 +130,10 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(// # GWS [1, num_heads, wg_count * wg_size]// # LWS [1, 1, wg_size]+ static_assert(K_HEAD_SIZE % 16 == 0 && V_HEAD_SIZE % 16 == 0);+const auto head_idx = cm_group_id(1);const auto wg_id = cm_group_id(2);- const auto wg_local_id = cm_local_id(2);- const auto local_size = cm_local_size(2);const uint token_idx = cm_global_id(2);@@ -81,42 +153,18 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(const uint token_start_pos = (past_len + token_idx - subsequence_begin_idx) % PAGED_ATTENTION_BLOCK_SIZE;const uint block_offset = block_indices_begins[subsequence_idx] + current_block_idx;- #if KV_CACHE_COMPRESSION_PER_TOKEN- // Assume: K_HEAD_SIZE == K_HEAD_SIZE- auto quantize_and_store = [&](vector<half, K_HEAD_SIZE> data, uchar* out, uint out_offset, uint token_pos) {- uint scale_offset = out_offset + K_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE + token_pos * sizeof(half);- half max_val = cm_reduced_max<half>(data);- half min_val = cm_reduced_min<half>(data);- float scale_val = float(0.0);- half zp_val = half(0.0);- if(max_val == min_val) {- scale_val = float(0.0);- zp_val = max_val;- } else {- scale_val = 255.0 / (max_val - min_val);- zp_val = (0.0 - min_val) * scale_val;- }- vector<half, K_HEAD_SIZE> dequant_data = cm_mul<half>(data, scale_val) + zp_val;- vector<uchar, K_HEAD_SIZE> data_u8 = cm_rnde<uchar, K_HEAD_SIZE>(dequant_data);- cm_ptr_store<uint32_t, K_HEAD_SIZE / 4>((uint32_t*)(out + out_offset + token_pos * K_HEAD_SIZE), 0, data_u8.format<uint32_t>());- half *out_scale_zp = (half*)(out + scale_offset);- out_scale_zp[0] = (max_val - min_val) / 255.0;- out_scale_zp[PAGED_ATTENTION_BLOCK_SIZE] = zp_val;- };- #endif-{uint block_k_base_offset = (block_indices[block_offset] * KV_HEADS_NUM + head_idx) * ADJUSTED_K_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE;uint key_out_offset = block_k_base_offset + token_start_pos * K_HEAD_SIZE;uint key_in_offset = token_idx * key_pitch + head_idx * K_HEAD_SIZE + key_offset;vector<half, K_HEAD_SIZE> key_data;- key_data.format<int>() = cm_ptr_load<int, K_HEAD_SIZE / 2>((int*)key, key_in_offset * (int)sizeof(half));+ load_kvcache<K_HEAD_SIZE>(key_data, key, key_in_offset * (int)sizeof(half));#if KV_CACHE_COMPRESSION_PER_TOKEN- quantize_and_store(key_data, (uchar*)key_cache, block_k_base_offset, token_start_pos);+ quantize_and_store<K_HEAD_SIZE>(key_data, (uchar*)key_cache, block_k_base_offset, token_start_pos);#else- cm_ptr_store<int, K_HEAD_SIZE / 2>((int*)key_cache, key_out_offset * (int)sizeof(half), key_data.format<int>());+ store_kvcache<half, K_HEAD_SIZE>(reinterpret_cast<svmptr_t>(key_cache), key_out_offset * (int)sizeof(half), key_data);#endif}{@@ -125,11 +173,12 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(uint value_in_offset = token_idx * value_pitch + head_idx * V_HEAD_SIZE + value_offset;vector<half, V_HEAD_SIZE> value_data;- value_data.format<int>() = cm_ptr_load<int, V_HEAD_SIZE / 2>((int*)value, value_in_offset * (int)sizeof(half));+ load_kvcache<V_HEAD_SIZE>(value_data, value, value_in_offset * (int)sizeof(half));+#if KV_CACHE_COMPRESSION_PER_TOKEN- quantize_and_store(value_data, (uchar*)value_cache, block_v_base_offset, token_start_pos);+ quantize_and_store<V_HEAD_SIZE>(value_data, (uchar*)value_cache, block_v_base_offset, token_start_pos);#else- cm_ptr_store<int, V_HEAD_SIZE / 2>((int*)value_cache, value_out_offset * (int)sizeof(half), value_data.format<int>());+ store_kvcache<half, V_HEAD_SIZE>(reinterpret_cast<svmptr_t>(value_cache), value_out_offset * (int)sizeof(half), value_data);#endif}}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 59fcbb2a9e1..6561ce53f83 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@@ -30,14 +30,6 @@#define REG_M RepeatCount#define KV_PARTITION_STEP_NUM (KV_PARTITION_SIZE / KV_STEP)--#define Q_SLICE_NUM (HEADS_NUM / KV_HEADS_NUM)-#if Q_SLICE_NUM > 8 || Q_SLICE_NUM == 1-#define Q_RepeatCount 1-#else-#define Q_RepeatCount Q_SLICE_NUM-#endif-#if KV_CACHE_COMPRESSION// scale/zp is half-precision, so size = 2 * 2 = 4 bytes#define KV_SCALE_ZP_SIZE 4 // scale/zp bytes@@ -78,20 +70,26 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(//# KV_PARTITION_SIZE should be multiple of kv_block_size(KV_BLOCK_SIZE)//# kv_len dimision will be split into multiple partitions, each WG process a partition//# total_partitions_num = kv_len // KV_PARTITION_SIZE- //# GWS=[seq_num, num_heads, total_partitions_num]+ //+ //# 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)+ //# GWS=[seq_num, num_kv_heads * q_head_chunks_per_kv_head, total_partitions_num]//# LWS=[1, 1, 1]//# Each WG processes a partition, which is KV_PARTITION_SIZE long and multiple of KV_BLOCK_SIZE.//# KV_BLOCK_SIZE can be 32/64/128/256, etc.- const auto seq_idx = cm_global_id(0);- const auto kv_head_num_idx = cm_global_id(1);- const auto head_num_idx = kv_head_num_idx * (HEADS_NUM/KV_HEADS_NUM);+ const uint seq_idx = cm_global_id(0);+ const uint kv_head_num_idx = cm_global_id(1) / Q_head_chunks_per_kv_head;+ const uint head_num_idx = cm_global_id(1) * Q_head_chunk_size;//# KV_PARTITION_SIZE --> EU thread- const auto wg_thread_id = cm_global_id(2);+ const uint wg_thread_id = cm_global_id(2);const uint kv_partition_num = cm_group_count(2);const uint kv_partition_idx = cm_group_id(2);const uint kv_len = past_lens[seq_idx] + 1;+ // The code here requires KV_PARTITION_SIZE to be an integer multiple of KV_BLOCK_SIZE.const uint start_block_idx = block_indices_begins[seq_idx] + kv_partition_idx * (KV_PARTITION_SIZE / KV_BLOCK_SIZE);if(kv_partition_idx * KV_PARTITION_SIZE > kv_len) {@@ -101,21 +99,22 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(constexpr uint kv_pitch = HEAD_SIZE * sizeof(KV_ELEMENT_TYPE);//# Load Q into register(as dpas-A tile)- const uint qo_offset = (seq_idx*HEADS_NUM*q_len + head_num_idx)*HEAD_SIZE;+ const uint qo_offset = (seq_idx * HEADS_NUM * q_len + head_num_idx) * HEAD_SIZE;++ matrix <half, Q_head_chunk_size, HEAD_SIZE> Qmat = 0;+ cm_svm_block_read<half, Q_head_chunk_size * HEAD_SIZE>((svmptr_t)(query + qo_offset), Qmat.format<half>());- #if Q_RepeatCount != 1- matrix <half,Q_RepeatCount, HEAD_SIZE> Qmat = 0;- cm_svm_block_read<half, Q_RepeatCount * HEAD_SIZE>((svmptr_t)(query + qo_offset), Qmat.format<half>());+ constexpr uint per_v_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp+ #if KV_CACHE_COMPRESSION_BY_TOKEN+ constexpr uint per_k_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp#else- matrix <half,Q_SLICE_NUM, HEAD_SIZE> Qmat = 0;- cm_svm_block_read<half, Q_SLICE_NUM * HEAD_SIZE>((svmptr_t)(query + qo_offset), Qmat.format<half>());+ constexpr uint per_k_block_element_num = KV_HEADS_NUM * HEAD_SIZE * (KV_BLOCK_SIZE + KV_SCALE_ZP_SIZE); // 4 bytes: scale/zp#endif-- constexpr uint per_kv_block_element_num = KV_BLOCK_SIZE * KV_HEADS_NUM * (HEAD_SIZE + KV_SCALE_ZP_SIZE / sizeof(KV_ELEMENT_TYPE)); // 4 bytes: scale/zpuint block_num = KV_PARTITION_SIZE / KV_BLOCK_SIZE;uint leftover_size = 0;if(kv_partition_idx == kv_partition_num - 1) {+ // last partitionleftover_size = (kv_len - KV_PARTITION_SIZE * kv_partition_idx) % KV_PARTITION_SIZE;}if(block_num > total_blocks_num - start_block_idx) {@@ -123,27 +122,25 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}//# rS = Q @ Kt- #if Q_RepeatCount != 1- matrix<float, Q_RepeatCount, REG_M * KV_PARTITION_STEP_NUM * REG_N> rS = 0;- #else- matrix<float, Q_SLICE_NUM, REG_M * KV_PARTITION_STEP_NUM * REG_N> rS = 0;- #endif+ matrix<float, Q_head_chunk_size, REG_M * KV_PARTITION_STEP_NUM * REG_N> rS = 0;+// # Each SG can process multiple blocks#pragma unrollfor(uint block_idx = 0, ki = 0; block_idx < block_num; block_idx++) {+ // split kv_partition into multi kv_blockuint blk_indices = block_indices[start_block_idx + block_idx];- uint kv_base_offset = blk_indices * per_kv_block_element_num + kv_head_num_idx * (per_kv_block_element_num / KV_HEADS_NUM);- uint kv_scale_zp_offset = kv_base_offset + KV_BLOCK_SIZE * HEAD_SIZE; // scale/zp offset+ uint k_base_offset = blk_indices * per_k_block_element_num + kv_head_num_idx * (per_k_block_element_num / KV_HEADS_NUM);+ uint k_scale_zp_offset = k_base_offset + KV_BLOCK_SIZE * HEAD_SIZE; // scale/zp offset#if USE_LSC_BLOCK_2D_DESC#if KV_CACHE_COMPRESSION- // Transpose only support dword and qword- lsc::block_2d_desc<uint, 1, REG_N, REG_K/4> b2dK(reinterpret_cast<uint*>(key + kv_base_offset), KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);+ // Transpose only support dword and qwork+ lsc::block_2d_desc<uint, 1, REG_N, REG_K/4> b2dK(reinterpret_cast<uint*>(key + k_base_offset), KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);#else- lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dK(reinterpret_cast<uint*>(key + kv_base_offset), KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(half) - 1, kv_pitch - 1, 0, 0);+ lsc::block_2d_desc<uint, 1, REG_N, REG_K/2> b2dK(reinterpret_cast<uint*>(key + k_base_offset), KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(half) - 1, kv_pitch - 1, 0, 0);#endif#else- uint kv_offset = kv_base_offset;+ uint kv_offset = k_base_offset;uint kv_stride = HEAD_SIZE;uint kv_x0 = 0, kv_y0 = 0;uint kv_x1 = HEAD_SIZE*sizeof(KV_ELEMENT_TYPE);@@ -157,11 +154,12 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}#if KV_CACHE_COMPRESSION+ #if KV_CACHE_COMPRESSION_BY_TOKEN// load scale/zpvector<half, KV_BLOCK_SIZE> scale_vec;vector<half, KV_BLOCK_SIZE> zp_vec;- cm_svm_block_read(reinterpret_cast<svmptr_t>(key + kv_scale_zp_offset), scale_vec);- cm_svm_block_read(reinterpret_cast<svmptr_t>(key + kv_scale_zp_offset + KV_BLOCK_SIZE * sizeof(half)), zp_vec);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset), scale_vec);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset + KV_BLOCK_SIZE * sizeof(half)), zp_vec);if(kv_pos_end < KV_BLOCK_SIZE) {// fill leftover with last valid scale/zp#pragma unroll@@ -170,10 +168,17 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(zp_vec[i] = 0.0;}}+ #else+ // load scale/zp+ vector<half, HEAD_SIZE> scale_vec;+ vector<half, HEAD_SIZE> zp_vec;+ cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset), scale_vec);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(key + k_scale_zp_offset + HEAD_SIZE * sizeof(half)), zp_vec);+ #endif#endiffor(int kv_pos = 0; kv_pos < kv_pos_end; kv_pos += KV_STEP, ki++) {- #if KV_CACHE_COMPRESSION+ #if KV_CACHE_COMPRESSION && KV_CACHE_COMPRESSION_BY_TOKENvector<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);@@ -206,11 +211,27 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(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++) {Kt[r] = Kt_quant[r] - temp_zp.format<half, 2, REG_N>()[r%2]; //vector - vectorKt[r] = cm_mul<half>(Kt[r], temp_scale.format<half, 2, REG_N>()[r%2]); // vector * vector}+ #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);++ auto Kt_dequant_out = Kt.format<half, REG_K/2, 2*REG_N>();+ auto Kt_dequant_tmp = Kt_quant.format<uint8_t, REG_K/2, 2*REG_N>();+ #pragma unroll+ for(int r = 0; r < REG_K/2; r++) {+ Kt_dequant_out[r].select<REG_N, 2>(0) = Kt_dequant_tmp[r].select<REG_N, 2>(0) - temp_zp[r*2];+ Kt_dequant_out[r].select<REG_N, 2>(0) = cm_mul<half>(Kt_dequant_out[r].select<REG_N, 2>(0), temp_scale[r*2]);+ Kt_dequant_out[r].select<REG_N, 2>(1) = Kt_dequant_tmp[r].select<REG_N, 2>(1) - temp_zp[r*2+1];+ Kt_dequant_out[r].select<REG_N, 2>(1) = cm_mul<half>(Kt_dequant_out[r].select<REG_N, 2>(1), temp_scale[r*2+1]);+ }+ #endif#elsecm_load<lsc::Transpose>(Kt.format<uint>(), b2dK.set_block_y(kv_pos));#endif@@ -228,46 +249,29 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(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));#endif#endif- #if Q_RepeatCount != 1- matrix<half, Q_RepeatCount, REG_K> Qmat_data = Qmat.select<Q_RepeatCount,1,REG_K,1>(0, ri*REG_K);- matrix<float, Q_RepeatCount, REG_N> rS_data = 0;- for(int qi = 0; qi < Q_SLICE_NUM; qi ++) {- Qmat_data[qi] = Qmat[qi].format<half, HEAD_SIZE / REG_K, REG_K>()[ri];- }- rS_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_RepeatCount>(+ {+ matrix<half, Q_head_chunk_size, REG_K> Qmat_data = Qmat.select<Q_head_chunk_size,1,REG_K,1>(0, ri*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_RepeatCount,1,REG_N,1>(0, ki*REG_N) += rS_data;- #else- #pragma unroll- for(int qi = 0; qi < Q_SLICE_NUM; qi ++) {- auto Qmat_slice = Qmat[qi].format<half, HEAD_SIZE / REG_K, REG_K>();- auto rSvec = rS[qi].format<float, REG_M * KV_PARTITION_STEP_NUM, REG_N>()[ki].format<float>();- rSvec = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(- rSvec,- Kt.format<int32_t>(),- Qmat_slice[ri].format<int32_t>());- }- #endif+ rS.select<Q_head_chunk_size, 1, REG_N, 1>(0, ki*REG_N) += rS_data;+ }}}}// online softmax- vector<float, Q_SLICE_NUM> cur_sum = 0.0f;- vector<float, Q_SLICE_NUM> cur_lse = 0.0f;+ vector<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_K> Pmat = 0;+ matrix<half, KV_PARTITION_STEP_NUM / 2 * REG_M, REG_N> Pmat = 0;#else- #if Q_RepeatCount != 1- matrix<half, Q_RepeatCount, KV_PARTITION_STEP_NUM * REG_M * REG_N> Pmat = 0;- #else- matrix<half, Q_SLICE_NUM, KV_PARTITION_STEP_NUM * REG_M * REG_N> Pmat = 0;- #endif+ matrix<half, Q_head_chunk_size, KV_PARTITION_STEP_NUM * REG_M * REG_N> Pmat = 0;#endif#pragma unroll- for(int qi = 0; qi < Q_SLICE_NUM; qi++) {+ for(int qi = 0; qi < Q_head_chunk_size; qi++) {auto rS_slice = rS[qi].format<float, KV_PARTITION_STEP_NUM, REG_N>();rS_slice = cm_mul<float>(rS_slice, (float)SCALE_FACTOR); // convert scale_factor into (float), or it will be promoted to double@@ -281,7 +285,6 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(// compute lseconstexpr float log2e = 1.4426950408889634f;constexpr float loge2 = 0.6931471805599453f;- vector<float, KV_PARTITION_STEP_NUM * REG_N> rS_exp = cm_exp(rS_slice.format<float>()*log2e);// compute row_maxauto rSv = rS_slice.format<float>();@@ -293,14 +296,14 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(for(int r = 1; r < rSv.n_elems(); r++)row_max = cm_max<float>(row_max, rSv[r]);- // compute P = exp(rS_slice - row_max)+ // 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);+ 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- Pmat[qi].format<half, KV_PARTITION_STEP_NUM * REG_M, REG_N>() = cm_exp((rS_slice - row_max)*log2e);+ Pmat[qi].format<half, KV_PARTITION_STEP_NUM * REG_M, REG_N>() = rS_exp_temp;#endif- vector<float, KV_PARTITION_STEP_NUM * REG_N> rS_exp_temp = cm_exp((rS_slice.format<float>() - row_max)*log2e);cur_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)))@@ -310,27 +313,23 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}//# rO = P * V- #if Q_RepeatCount != 1- matrix <float, Q_RepeatCount, HEAD_SIZE/REG_N * REG_M*REG_N> Omat = 0;- #else- matrix <float, Q_SLICE_NUM, HEAD_SIZE/REG_N * REG_M*REG_N> Omat = 0;- #endif+ matrix <float, Q_head_chunk_size, HEAD_SIZE/REG_N * REG_M*REG_N> Omat = 0;#pragma unrollfor(uint block_idx = 0, ki = 0; block_idx < block_num; block_idx++) {uint blk_indices = block_indices[start_block_idx + block_idx];- uint kv_base_offset = blk_indices * per_kv_block_element_num + kv_head_num_idx * (per_kv_block_element_num / KV_HEADS_NUM);- uint kv_scale_zp_offset = kv_base_offset + KV_BLOCK_SIZE * HEAD_SIZE; // scale/zp offset+ uint v_base_offset = blk_indices * per_v_block_element_num + kv_head_num_idx * (per_v_block_element_num / KV_HEADS_NUM);+ uint v_scale_zp_offset = v_base_offset + KV_BLOCK_SIZE * HEAD_SIZE; // scale/zp offset#if USE_LSC_BLOCK_2D_DESC//# vector load cannot be used for block_2d_desc//# note: candidate template ignored: deduced type 'details::Block2DRefTy<half, 1U, 16U, 1U, (LoadOp)0U>' (aka 'vector_ref<half,32>') of 1st parameter#if KV_CACHE_COMPRESSION- lsc::block_2d_desc<uint8_t, 1, REG_K, REG_N> b2dV(value + kv_base_offset, KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);+ lsc::block_2d_desc<uint8_t, 1, REG_K, REG_N> b2dV(value + v_base_offset, KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(uint8_t) - 1, kv_pitch - 1, 0, 0);#else- lsc::block_2d_desc<half, 1, REG_K, REG_N> b2dV(value + kv_base_offset, KV_BLOCK_SIZE - 1, HEAD_SIZE*sizeof(half) - 1, kv_pitch - 1, 0, 0);+ lsc::block_2d_desc<half, 1, REG_K, REG_N> b2dV(value + v_base_offset, KV_BLOCK_SIZE - 1, HEAD_SIZE * sizeof(half) - 1, kv_pitch - 1, 0, 0);#endif#else- uint kv_offset = kv_base_offset;+ uint kv_offset = v_base_offset;uint kv_stride = HEAD_SIZE;uint kv_x0 = 0, kv_y0 = 0;uint kv_x1 = HEAD_SIZE*sizeof(half);@@ -344,18 +343,18 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(}#if KV_CACHE_COMPRESSION- // load scale/zp- vector<half, KV_BLOCK_SIZE> scale_vec;- vector<half, KV_BLOCK_SIZE> zp_vec;- cm_svm_block_read(reinterpret_cast<svmptr_t>(value + kv_scale_zp_offset), scale_vec);- cm_svm_block_read(reinterpret_cast<svmptr_t>(value + kv_scale_zp_offset + KV_BLOCK_SIZE * sizeof(half)), zp_vec);- if(kv_pos_end < KV_BLOCK_SIZE) {- // fill leftover with last valid scale/zp- for(int i = kv_pos_end; i < KV_BLOCK_SIZE; i++) {- scale_vec[i] = 0.0;- zp_vec[i] = 0.0;+ // load scale/zp+ vector<half, KV_BLOCK_SIZE> scale_vec;+ vector<half, KV_BLOCK_SIZE> zp_vec;+ cm_svm_block_read(reinterpret_cast<svmptr_t>(value + v_scale_zp_offset), scale_vec);+ cm_svm_block_read(reinterpret_cast<svmptr_t>(value + v_scale_zp_offset + KV_BLOCK_SIZE * sizeof(half)), zp_vec);+ if(kv_pos_end < KV_BLOCK_SIZE) {+ // fill leftover with last valid scale/zp+ for(int i = kv_pos_end; i < KV_BLOCK_SIZE; i++) {+ scale_vec[i] = 0.0;+ zp_vec[i] = 0.0;+ }}- }#endif#pragma unrollfor(int kv_pos = 0; kv_pos < kv_pos_end; kv_pos += REG_K, ki++) {@@ -369,8 +368,10 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(matrix<half, REG_K, REG_N> VmatNormal;matrix<half, REG_M, REG_K*REG_N> Vmat;#if USE_LSC_BLOCK_2D_DESC- b2dV.set_block_x(k);+ {+ b2dV.set_block_x(k); // x is the column index#if KV_CACHE_COMPRESSION+ {// dequantizematrix<uint8_t, REG_K, REG_N> Vt_quant;cm_load<lsc::Normal>(Vt_quant.format<uint8_t>(), b2dV.set_block_y(kv_pos));@@ -383,12 +384,14 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(if(kv_pos_end - kv_pos < KV_STEP) {#pragma unroll- for(int r = kv_pos_end; r<KV_STEP; r++) {+ for(int r = kv_pos_end; r < KV_STEP; r++) {VmatNormal[r] = 0;}}prepack_to_VNNI_W2(VmatNormal, Vmat.format<half, REG_K/2, REG_N*2>());+ }#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) {@@ -400,8 +403,11 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(if (valid_rows % 2 == 1)VmatRef.row(valid_rows_vnni-1).select<REG_N,2>(1) = 0.f;}+ }#endif+ }#else+ {matrix<half, REG_K, REG_N> temp;uint cur_kv_offset = kv_offset + kv_pos * kv_stride + k;#pragma unroll@@ -411,31 +417,25 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(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);+ }#endif- #if Q_RepeatCount != 1- matrix<half, Q_RepeatCount, REG_K> Pmat_data = Pmat.select<Q_RepeatCount,1,REG_K,1>(0, ki*REG_K);- matrix<float, Q_RepeatCount, REG_N> Omat_data = 0;- Omat_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_RepeatCount>(++ {+ 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;+ Omat_data = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, Q_head_chunk_size>(Omat_data.format<float>(),Vmat[0].format<int32_t>(),Pmat_data.format<int32_t>());- Omat.select<Q_RepeatCount,1,REG_N,1>(0, ri*REG_N) += Omat_data;- #else- for(int qi = 0; qi < Q_SLICE_NUM; qi ++) {- auto Pmat_slice = Pmat[qi].format<half, KV_PARTITION_STEP_NUM * REG_M, REG_K>();- auto Omat_slice = Omat[qi].format<float, HEAD_SIZE/REG_N * REG_M, REG_N>();- Omat_slice[ri] = cm_dpas<CM_PRECISION_HF, CM_PRECISION_HF, SystolicDepth, RepeatCount>(- Omat_slice[ri],- Vmat[0].format<int32_t>(),- Pmat_slice[ki].format<int32_t>());- }- #endif+ Omat.select<Q_head_chunk_size,1,REG_N,1>(0, ri*REG_N) += Omat_data;+ }}}}//# save Output- for (int qi = 0; qi < Q_SLICE_NUM; qi++) {+ #pragma unroll+ for (int qi = 0; qi < Q_head_chunk_size; qi++) {matrix<float, REG_M, REG_N> cur_O_f32;uint o_offset = seq_idx * kv_partition_num * HEADS_NUM * HEAD_SIZE + kv_partition_num * (head_num_idx + qi) * HEAD_SIZE + wg_thread_id * HEAD_SIZE;float div_cur_sum = 1.0/cur_sum[qi];diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.hppindex 2dfbfc273b2..80e1e7bb5c3 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.hpp@@ -37,10 +37,9 @@ struct PagedAttentionImplementationManager : public ImplementationManager {return false;}- // TODO: Remove this limitation when PA CM kernel supports more "heads_num / kv_heads_num" cases.- // PA 2nd token CM kernel only supports case of "heads_num / kv_heads_num <= 8"- if (desc->heads_num / desc->kv_heads_num > 8) {- GPU_DEBUG_TRACE_DETAIL << "validate_impl() - false because heads_num / kv_heads_num > 8. " << std::endl;+ // PA CM kernel only supports cases when kv_head_size is divisible by 16+ if (desc->k_head_size % 16 != 0 && desc->v_head_size % 16 != 0) {+ GPU_DEBUG_TRACE_DETAIL << "validate_impl() - false because kv_head_size is not divisible by 16. " << std::endl;return false;}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 7cec82766cb..25befc83869 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@@ -418,8 +418,16 @@ JitConstants PagedAttentionGeneratorSingleToken::get_jit_constants(const kernel_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);+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);}@@ -465,7 +473,10 @@ 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;- wgs.global = {batch, kv_heads_num, partition_num};+ 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};wgs.local = {1, 1, 1};// generate stage: q_len=1@@ -581,6 +592,7 @@ JitConstants XAttentionEstimateGeneratorBase::get_jit_constants(const kernel_impjit.make("SG_N", 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));