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] xattention_block_size 256 support. (#33485)
cecilia peng4e9a607468f7
diff --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 06948407766..2872b8398cf 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@@ -29,10 +29,10 @@ void pa_lsc_u8(svmptr_t q_base [[type("svmptr_t")]],svmptr_t k_cache_base [[type("svmptr_t")]],svmptr_t v_cache_base [[type("svmptr_t")]],-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEsvmptr_t sparse_mask_base [[type("svmptr_t")]],svmptr_t wg_sparse_mask_base [[type("svmptr_t")]],- bool validate,+ int SPARSE_BLOCK_SIZE,#endifsvmptr_t o_base [[type("svmptr_t")]],int32_t past_lens,@@ -77,7 +77,7 @@ void pa_lsc_u8(int slm_buff_id_write = 0;int slm_buff_id_read = 0;-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEauto skip_compute = [&](int kv_pos) {auto kv_start_block = kv_pos / SPARSE_BLOCK_SIZE;bool sparse_mask = *(reinterpret_cast<bool*>(sparse_mask_base) + kv_start_block);@@ -93,12 +93,10 @@ void pa_lsc_u8(auto load_slm_KV = [&](int kv_pos) {if (kv_pos < kv_stop) {-#if SPARSE_BLOCK_SIZE > 1- if (validate) {- if (skip_load(kv_pos)) {- slm_buff_id_write++;- return;- }+#if IS_BLOCK_SPARSE+ if (SPARSE_BLOCK_SIZE > 1 && skip_load(kv_pos)) {+ slm_buff_id_write++;+ return;}#endifauto cur_block_id = block_indices[kv_pos / CMPA_BLOCK_SZ];@@ -203,13 +201,11 @@ void pa_lsc_u8(load_slm_KV(kv_pos + kv_step*2);-#if SPARSE_BLOCK_SIZE > 1- if (validate) {- if (skip_compute(kv_pos)) {- if constexpr (use_causal_mask)- causal_left -= kv_step;- continue;- }+#if IS_BLOCK_SPARSE+ if (SPARSE_BLOCK_SIZE > 1 && skip_compute(kv_pos)) {+ if constexpr (use_causal_mask)+ causal_left -= kv_step;+ continue;}#endif{@@ -279,10 +275,10 @@ void pa_kernel_lsc_prefetch_f16(svmptr_t q_base [[type("svmptr_t")]],svmptr_t k_cache_base [[type("svmptr_t")]],svmptr_t v_cache_base [[type("svmptr_t")]],-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEsvmptr_t sparse_mask_base [[type("svmptr_t")]],svmptr_t wg_sparse_mask_base [[type("svmptr_t")]],- bool validate,+ int SPARSE_BLOCK_SIZE,#endifsvmptr_t o_base [[type("svmptr_t")]],int32_t past_lens,@@ -346,8 +342,8 @@ void pa_kernel_lsc_prefetch_f16(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));-#if SPARSE_BLOCK_SIZE > 1- if (validate)+#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);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 e34351db241..8068ac79743 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@@ -734,9 +734,10 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {uint N_block = (N + BLOCK_WG_N - 1) / BLOCK_WG_N;uint M_aligned = (M + BLOCK_WG_M - 1) / BLOCK_WG_M * BLOCK_WG_M;uint K_block_pad = N_block * (BLOCK_WG_N / (BLOCK_SIZE / STRIDE));- const uint block_size_div_stride = BLOCK_SIZE / STRIDE;+ const uint block_size_div_stride = BLOCK_SIZE / STRIDE; // 8constexpr SOFTMAX_TYPE log2e = 1.4426950408889634f;- //static_assert(BLOCK_SG_M / block_size_div_stride == 8, "BLOCK_SG_M / block_size_div_stride should be 8");+ constexpr uint k_block_in_group = BLOCK_WG_N / block_size_div_stride; // 32+ constexpr uint k_block_in_sg = k_block_in_group / SG_N; // 4#if IS_CAUSAL == 1if ((int)(id_wg_n * BLOCK_WG_N) >= ((int)id_wg_m + 1) * BLOCK_WG_M + q_start_strided) {@@ -811,12 +812,10 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {uint offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));lsc::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 };- // printf("===============0 lid:%d.%d, block_idx=%d, offset=%u\n", id_sg_n, id_sg_m, block_idx, offset);block_idx = MYMIN(block_idx + 1, max_block_idx);offset = block_indices_p[block_idx] * (HK * KV_BLOCK_SIZE * HEAD_SIZE * (uint)sizeof(half));lsc::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 };- // printf("===============1 lid:%d.%d, block_idx=%d, offset=%u\n", id_sg_n, id_sg_m, block_idx, offset);// 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);@@ -824,7 +823,6 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {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),0, 0 };- // printf("===============2 lid:%d.%d, block_idx=%d, offset=%u\n", id_sg_n, id_sg_m, block_idx, offset);// 0~2 M[:]xK[0:16] 2~4 K[16:32] --> 32 * 2 regsmatrix<half, 2, BLOCK_REG_B> b0, b1;#endif@@ -1038,7 +1036,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, 4> sum_t;+ matrix<SOFTMAX_TYPE, 64, k_block_in_sg> 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>();@@ -1124,19 +1122,19 @@ uint M, uint N, uint K, uint query_stride, uint q_start_strided) {} SIMD_IF_END;}}- sum_t.select<32, 1, 4, 1>( 0).format<SOFTMAX_TYPE>() = reduce2d<4, 1, 4>(acc_half.select<32, 1, 32, 1>( 0)).format<SOFTMAX_TYPE>();- sum_t.select<32, 1, 4, 1>(32).format<SOFTMAX_TYPE>() = reduce2d<4, 1, 4>(acc_half.select<32, 1, 32, 1>(32)).format<SOFTMAX_TYPE>();+ 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>();}// store- 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) };- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 0>(desc_c, sum_t.select<8, 1, 4, 1>( 0).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 1>(desc_c, sum_t.select<8, 1, 4, 1>( 8).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 2>(desc_c, sum_t.select<8, 1, 4, 1>(16).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 3>(desc_c, sum_t.select<8, 1, 4, 1>(24).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 4>(desc_c, sum_t.select<8, 1, 4, 1>(32).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 5>(desc_c, sum_t.select<8, 1, 4, 1>(40).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 6>(desc_c, sum_t.select<8, 1, 4, 1>(48).format<SOFTMAX_TYPE>());- cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 7>(desc_c, sum_t.select<8, 1, 4, 1>(56).format<SOFTMAX_TYPE>());+ lsc::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>());+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 1>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>( 8).format<SOFTMAX_TYPE>());+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 2>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(16).format<SOFTMAX_TYPE>());+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 3>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(24).format<SOFTMAX_TYPE>());+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8 * 4>(desc_c, sum_t.select<8, 1, k_block_in_sg, 1>(32).format<SOFTMAX_TYPE>());+ 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>());}#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 a4c57a19d1e..0c6f80d95ae 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@@ -40,29 +40,16 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p, svmptr_t kq_sum#endif) {- constexpr int SG_SIZE = 16;-#ifndef BLOCK_SG_M- #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-- const int TOKEN_IN_BLOCK = (BLOCK_SIZE / STRIDE);+ constexpr int TOKEN_IN_BLOCK = (BLOCK_SIZE / STRIDE); // 8 -> 16int m = m_block * TOKEN_IN_BLOCK;vector<SOFTMAX_TYPE, TOKEN_IN_BLOCK> max_m;- const int TOKEN_SHARE_MAX = BLOCK_SHARE_MAX / TOKEN_IN_BLOCK;+ 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;- matrix<SOFTMAX_TYPE, TOKEN_IN_BLOCK, TOKEN_SHARE_MAX> data;+ matrix<SOFTMAX_TYPE, TOKEN_IN_BLOCK, TOKEN_SHARE_MAX> data; // (8, 32) -> (16, 16)int m_start = MYMIN(m, q_stride);int m_end = MYMIN(m_start + TOKEN_SHARE_MAX, q_stride);int valid_m = m_end - m_start;@@ -77,8 +64,13 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p}return;}+#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+ lsc::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{// find max: (k_block_pad / TOKEN_SHARE_MAX) * q_stride_padmax_m = SOFTMAX_TYPE{-60000};@@ -94,9 +86,14 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_pfor (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));-#if CUR_TYPE == IS_float+#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+ // 2x(8, 16) -> (16, 16)+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(0, 0).format<SOFTMAX_TYPE>(), desc_sum);+ cm_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@@ -106,13 +103,16 @@ 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);}}-#if CUR_TYPE == IS_float+#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>());+#elif CUR_TYPE == IS_float && BLOCK_SIZE == 256+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 0>(desc_sum, data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(0, 0).format<SOFTMAX_TYPE>());+ cm_store<CacheHint::Uncached, CacheHint::WriteBack, 0, 8>(desc_sum, data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(TOKEN_IN_BLOCK / 2, 0).format<SOFTMAX_TYPE>());#elsecm_store(desc_sum, data.format<SOFTMAX_TYPE>());#endif- desc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX);+ desc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX); // k_block_in_group? 32 ->16}// exp/sum@@ -131,9 +131,12 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p#endifvector<uchar, TOKEN_SHARE_MAX> zero = 0;for (int j = 0; j < k_block_pad; j += TOKEN_SHARE_MAX) {-#if CUR_TYPE == IS_float+#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);+#elif CUR_TYPE == IS_float && BLOCK_SIZE == 256+ cm_load<lsc::Normal, CacheHint::Cached, CacheHint::Cached, 0, 0>(data.select<TOKEN_IN_BLOCK / 2, 1, TOKEN_SHARE_MAX, 1>(0, 0).format<SOFTMAX_TYPE>(), desc_sum);+ cm_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@@ -143,7 +146,7 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p}desc_sum.set_block_x(desc_sum.get_block_x() + TOKEN_SHARE_MAX);// 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#L248- vector<half, TOKEN_SHARE_MAX> data_half = data.row(0);+ vector<half, TOKEN_SHARE_MAX> data_half = data.row(0); // 32 -> 16sum_m_after_add += data_half;cm_ptr_store<int, TOKEN_SHARE_MAX / 2>((int*)kq_exp_partial_sum, j * (int)sizeof(half), data_half.format<int>());#if DEBUG_ACC == 1@@ -212,7 +215,6 @@ CM_INLINE void find(uint slm, int m_block, svmptr_t kq_max_wg, svmptr_t kq_exp_p// block_mask_p[j] = 0;#else-sort<half>(slm, score, sorted_value, sorted_index, sorted_tmp, k_block_pad);uchar* block_mask_p = (uchar*)block_mask;auto sorted_value_p = (half*)sorted_value;diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/include/sort.hpp b/src/plugins/intel_gpu/src/graph/impls/cm/include/sort.hppindex 97f680e5cba..375c228b753 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/include/sort.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/cm/include/sort.hpp@@ -58,7 +58,7 @@ void show(const matrix<T, M, N> mat) {// https://gpuopen.com/download/Introduction_to_GPU_Radix_Sort.pdftemplate<typename TYPE>CM_INLINE void sort(uint slm, svmptr_t src, svmptr_t sorted_value, svmptr_t sorted_index, svmptr_t sort_tmp, uint n) {- const ushort THREADS = 32;+ const ushort THREADS = NUM_THREADS;vector<unsigned, THREADS> seq_u32;vector<ushort, THREADS> seq;cmtl::cm_vector_assign(seq.select_all(), 0, 1);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 424b17a68d9..19374b94180 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@@ -87,10 +87,10 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(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);- half scale_val = half(0.0);+ float scale_val = float(0.0);half zp_val = half(0.0);if(max_val == min_val) {- scale_val = half(0.0);+ scale_val = float(0.0);zp_val = max_val;} else {scale_val = 255.0 / (max_val - min_val);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 6bf7ca0ec55..947f70cb235 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@@ -39,14 +39,13 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(int32_t* block_indices_begins [[type("svmptr_t")]],int32_t* subsequence_begins [[type("svmptr_t")]],half* output [[type("svmptr_t")]],-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEbool* sparse_block_mask [[type("svmptr_t")]],bool* sparse_block_mask_wg [[type("svmptr_t")]],int q_len,int num_q_blocks,int num_k_blocks,- // validate sparse atten process- bool validate) {+ int SPARSE_BLOCK_SIZE) {#elseint q_len) {#endif@@ -124,15 +123,13 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(//Q/O[B, L, H, S]uint q_offset = (q_start_sg*num_heads + h)*head_size;-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEbool *block_mask_base, *wg_block_mask_base;- if (validate) {- //# sparse_block_mask [num_heads, num_q_blocks, num_k_blocks]- //# sparse_block_mask_wg [num_heads, wg_count_along_query, num_k_blocks]- auto q_start_block = q_start_sg/ SPARSE_BLOCK_SIZE;- block_mask_base = sparse_block_mask + (h * num_q_blocks + q_start_block) * num_k_blocks;- wg_block_mask_base = sparse_block_mask_wg + (h * cm_group_count(2) + wg_id) * num_k_blocks;- }+ //# sparse_block_mask [num_heads, num_q_blocks, num_k_blocks]+ //# sparse_block_mask_wg [num_heads, wg_count_along_query, num_k_blocks]+ auto q_start_block = q_start_sg/ SPARSE_BLOCK_SIZE;+ block_mask_base = sparse_block_mask + (h * num_q_blocks + q_start_block) * num_k_blocks;+ wg_block_mask_base = sparse_block_mask_wg + (h * cm_group_count(2) + wg_id) * num_k_blocks;#endif#if CMPA_KVCACHE_U8@@ -149,10 +146,10 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(reinterpret_cast<svmptr_t>(query + q_offset),reinterpret_cast<svmptr_t>(k_cache + kv_offset),reinterpret_cast<svmptr_t>(v_cache + kv_offset),-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEreinterpret_cast<svmptr_t>(block_mask_base),reinterpret_cast<svmptr_t>(wg_block_mask_base),- validate,+ SPARSE_BLOCK_SIZE,#endifreinterpret_cast<svmptr_t>(output + q_offset),past_q_lens,@@ -168,10 +165,10 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(reinterpret_cast<svmptr_t>(query + q_offset),reinterpret_cast<svmptr_t>(k_cache + kv_offset),reinterpret_cast<svmptr_t>(v_cache + kv_offset),-#if SPARSE_BLOCK_SIZE > 1+#if IS_BLOCK_SPARSEreinterpret_cast<svmptr_t>(block_mask_base),reinterpret_cast<svmptr_t>(wg_block_mask_base),- validate,+ SPARSE_BLOCK_SIZE,#endifreinterpret_cast<svmptr_t>(output + q_offset),past_q_lens,diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cpp b/src/plugins/intel_gpu/src/graph/impls/cm/paged_attention.cppindex 20e9e2732b7..caf57824ab6 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@@ -30,9 +30,12 @@ public:Stage::Ptr pa_single_token = make_stage<PagedAttentionGeneratorSingleToken>();Stage::Ptr pa_single_token_finalization = make_stage<PagedAttentionGeneratorSingleTokenFinalization>();Stage::Ptr pa_multi_token = make_stage<PagedAttentionGeneratorMultiToken>();- Stage::Ptr xattn_estimate_gemmqk = make_stage<XAttentionEstimateGEMMQK>();- Stage::Ptr xattn_estimate_find_block = make_stage<XAttentionEstimateFindBlock>();- Stage::Ptr xattn_estimate_post_proc = make_stage<XAttentionEstimatePostProc>();+ Stage::Ptr xattn_estimate_gemmqk = make_stage<XAttentionEstimateGEMMQK>(128);+ Stage::Ptr xattn_estimate_find_block = make_stage<XAttentionEstimateFindBlock>(128);+ Stage::Ptr xattn_estimate_post_proc = make_stage<XAttentionEstimatePostProc>(128);+ Stage::Ptr xattn_estimate_gemmqk_256 = make_stage<XAttentionEstimateGEMMQK>(256);+ Stage::Ptr xattn_estimate_find_block_256 = make_stage<XAttentionEstimateFindBlock>(256);+ Stage::Ptr xattn_estimate_post_proc_256 = make_stage<XAttentionEstimatePostProc>(256);PagedAttentionCmImpl() : PrimitiveImplCM(PagedAttentionImplementationManager::get_type_info_static()) {m_rt_params = std::make_unique<PagedAttentionRuntimeParams>();@@ -49,6 +52,10 @@ public:add_stage(xattn_estimate_gemmqk, params);add_stage(xattn_estimate_find_block, params);add_stage(xattn_estimate_post_proc, params);++ add_stage(xattn_estimate_gemmqk_256, params);+ add_stage(xattn_estimate_find_block_256, params);+ add_stage(xattn_estimate_post_proc_256, params);}}@@ -71,6 +78,7 @@ public:auto rt_params = static_cast<PagedAttentionRuntimeParams*>(m_rt_params.get());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_procrt_params->q_block_pad_merged = ceil_div(q_block_pad, MERGED_Q_NUM);const size_t head_size = desc->k_head_size;@@ -85,6 +93,7 @@ public:rt_params->N = N;rt_params->K = K;rt_params->q_stride_pad = q_stride_pad;+ rt_params->xattn_block_size = block_size;}void update_rt_params(const primitive_inst& instance) override {@@ -112,6 +121,8 @@ public:} else {if (desc->has_xattention) {update_xattn_rt_params(params);+ } else {+ rt_params->xattn_block_size = 1; // disable xattn for pa}}}@@ -135,10 +146,16 @@ public:res_event = {execute_stage(res_event, instance, kv_cache_update)};if (rt_params->stage == PagedAttentionStage::PREFILL || rt_params->stage == PagedAttentionStage::MIXED) {- if (has_stage(xattn_estimate_gemmqk) && !bypass_xattn(params)) {- res_event = {execute_stage(res_event, instance, xattn_estimate_gemmqk)};- res_event = {execute_stage(res_event, instance, xattn_estimate_find_block)};- res_event = {execute_stage(res_event, instance, xattn_estimate_post_proc)};+ if (!bypass_xattn(params)) {+ if (rt_params->xattn_block_size == 128) {+ res_event = {execute_stage(res_event, instance, xattn_estimate_gemmqk)};+ res_event = {execute_stage(res_event, instance, xattn_estimate_find_block)};+ res_event = {execute_stage(res_event, instance, xattn_estimate_post_proc)};+ } else {+ res_event = {execute_stage(res_event, instance, xattn_estimate_gemmqk_256)};+ res_event = {execute_stage(res_event, instance, xattn_estimate_find_block_256)};+ res_event = {execute_stage(res_event, instance, xattn_estimate_post_proc_256)};+ }}res_event = {execute_stage(res_event, instance, pa_multi_token)};} else if (rt_params->stage == PagedAttentionStage::GENERATE) {@@ -210,6 +227,18 @@ public:[[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {return make_deep_copy<PagedAttentionCmImpl>(this);}++private:+ size_t get_xattn_block_size(const kernel_impl_params& params, const size_t seq_idx = 0) {+ const auto& input_mem = params.memory_deps;+ const auto blocksize_mem = input_mem.at(PagedAttentionInputIdx::XATTENTION_BLOCK_SIZE);+ mem_lock<int32_t, mem_lock_type::read> lock(blocksize_mem, *params.strm); // converted+ auto xattn_block_size = static_cast<int32_t>(lock[seq_idx]);+ if (xattn_block_size != 128 && xattn_block_size != 256) {+ xattn_block_size = 128; // default+ }+ return xattn_block_size;+ }};std::unique_ptr<primitive_impl> PagedAttentionImplementationManager::create_impl(const program_node& node, const kernel_impl_params& params) const {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 65c6c563d85..7cec82766cb 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@@ -316,7 +316,7 @@ Arguments PagedAttentionGeneratorMultiToken::get_arguments_desc(const kernel_impif (desc->has_xattention) {args.push_back({ArgumentDescriptor::Types::SCALAR, 1}); // q_block_padargs.push_back({ArgumentDescriptor::Types::SCALAR, 2}); // k_block_pad- args.push_back({ArgumentDescriptor::Types::SCALAR, 3}); // validate+ args.push_back({ArgumentDescriptor::Types::SCALAR, 3}); // SPARSE_BLOCK_SIZE}return args;}@@ -327,8 +327,6 @@ JitConstants PagedAttentionGeneratorMultiToken::get_jit_constants(const kernel_iconst 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;- const size_t xattn_block_size = get_xattn_block_size(params);-jit.make("CMFLA_NUM_HEADS", desc->heads_num);jit.make("CMFLA_NUM_KV_HEADS", desc->kv_heads_num);jit.make("CMFLA_HEAD_SIZE", desc->k_head_size);@@ -336,10 +334,10 @@ JitConstants PagedAttentionGeneratorMultiToken::get_jit_constants(const kernel_ijit.make("CMFLA_IS_CAUSAL", 1);if (desc->has_xattention) {jit.make("CMPA_BLOCK_SZ", PA_KV_CACHE_BLOCK_SIZE_XATTN);- jit.make("SPARSE_BLOCK_SIZE", xattn_block_size);+ jit.make("IS_BLOCK_SPARSE", 1);} else {jit.make("CMPA_BLOCK_SZ", PA_KV_CACHE_BLOCK_SIZE);- jit.make("SPARSE_BLOCK_SIZE", 1);+ jit.make("IS_BLOCK_SPARSE", 0);}jit.make("Q_STEP", get_q_step(xe_arch, true));@@ -390,9 +388,9 @@ DispatchDataFunc PagedAttentionGeneratorMultiToken::get_dispatch_data_func() conscalars[2].t = ScalarDescriptor::Types::INT32;scalars[2].v.s32 = static_cast<int32_t>(rtp->k_block_pad);- scalars[3].t = ScalarDescriptor::Types::UINT8;+ scalars[3].t = ScalarDescriptor::Types::INT32;const bool validate = !bypass_xattn(params);- scalars[3].v.u8 = static_cast<uint8_t>(validate); // validate depending on xattn_threshold+ scalars[3].v.s32 = static_cast<int32_t>(validate ? rtp->xattn_block_size : 1);}}};}@@ -572,9 +570,8 @@ JitConstants XAttentionEstimateGeneratorBase::get_jit_constants(const kernel_impconst uint32_t wg_k = BLOCK_WG_M;const uint32_t wg_q = BLOCK_WG_N;- const size_t block_size = get_xattn_block_size(params);- OPENVINO_ASSERT(wg_k % block_size == 0, "wg_k should be multiple of block_size then there is no tails from block_size");- OPENVINO_ASSERT(wg_q % block_size == 0, "wg_q should be multiple of block_size then there is no tails from block_size");+ 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");jit.make("STRIDE", STRIDE);jit.make("HQ", desc->heads_num);@@ -584,7 +581,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_SIZE", block_size);+ jit.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);@@ -685,6 +682,15 @@ DispatchDataFunc XAttentionEstimateGEMMQK::get_dispatch_data_func() const {//-----------------------------------------------------------------------------------------------------------------// XAttention Estimate find_block generator//-----------------------------------------------------------------------------------------------------------------+JitConstants XAttentionEstimateFindBlock::get_jit_constants(const kernel_impl_params& params) const {+ auto jit = XAttentionEstimateGeneratorBase::get_jit_constants(params);++ const uint32_t NUM_THREADS = _xattn_block_size == 128 ? 32 : 16; // for xattn sort kernel+ jit.make("NUM_THREADS", NUM_THREADS);++ return jit;+}+Arguments XAttentionEstimateFindBlock::get_arguments_desc(const kernel_impl_params& params) const {Arguments args;@@ -721,8 +727,7 @@ DispatchDataFunc XAttentionEstimateFindBlock::get_dispatch_data_func() const {auto out_shape = params.output_layouts[0].get_shape();const size_t q_len = out_shape[0];- const size_t block_size = get_xattn_block_size(params);- const size_t sum_per_n_token_in_block = static_cast<size_t>(block_size / STRIDE);+ const size_t sum_per_n_token_in_block = static_cast<size_t>(rtp->xattn_block_size / STRIDE);const uint32_t q_block = ceil_div(rtp->M, sum_per_n_token_in_block);const uint32_t k_block = ceil_div(rtp->N, sum_per_n_token_in_block);@@ -750,6 +755,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);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 38cdc97f2ba..8083f6bb79c 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@@ -38,8 +38,6 @@ 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 int STRIDE = 16;-constexpr uint32_t XATTN_BLOCK_SIZE = 128;-constexpr uint32_t MERGED_Q_NUM = PA_KV_CACHE_BLOCK_SIZE_XATTN / XATTN_BLOCK_SIZE; // for xattn post_procenum class PagedAttentionStage : uint8_t { GENERATE = 0, PREFILL = 1, MIXED = 2, UNKNOWN = 3 };struct PagedAttentionRuntimeParams : public ImplRuntimeParams {@@ -56,6 +54,7 @@ struct PagedAttentionRuntimeParams : public ImplRuntimeParams {size_t M;size_t N;size_t K;+ size_t xattn_block_size;};enum PagedAttentionInternBuffIdx {@@ -80,9 +79,6 @@ size_t get_partition_size(const bool has_xattention);float get_xattn_thresh(const kernel_impl_params& impl_param, const size_t seq_idx = 0);bool bypass_xattn(const kernel_impl_params& impl_param);-inline size_t get_xattn_block_size(const kernel_impl_params& impl_param) {- return XATTN_BLOCK_SIZE;-}class PagedAttentionGeneratorBase : public KernelGenerator {public:@@ -130,30 +126,38 @@ public://-----------------------------------------------------------------------------------------------------------------class XAttentionEstimateGeneratorBase : public KernelGenerator {public:- explicit XAttentionEstimateGeneratorBase(std::string_view kernel_name, std::string_view stage_suffix = "_cm")- : KernelGenerator(kernel_name, stage_suffix) {}+ 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) {}[[nodiscard]] std::string get_build_options(const RuntimeParams& params) const override {return KernelGenerator::get_build_options(params) + get_pa_build_options();}[[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override;++protected:+ size_t _xattn_block_size;};class XAttentionEstimateGEMMQK : public XAttentionEstimateGeneratorBase {public:- XAttentionEstimateGEMMQK() : XAttentionEstimateGeneratorBase("xattn_gemm_qk") {}+ explicit XAttentionEstimateGEMMQK(size_t xattn_block_size) : XAttentionEstimateGeneratorBase("xattn_gemm_qk", xattn_block_size) {}+ XAttentionEstimateGEMMQK() = delete;[[nodiscard]] Arguments get_arguments_desc(const kernel_impl_params& params) const override;[[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override;};class XAttentionEstimateFindBlock : public XAttentionEstimateGeneratorBase {public:- XAttentionEstimateFindBlock() : XAttentionEstimateGeneratorBase("xattn_find_block") {}+ explicit XAttentionEstimateFindBlock(size_t xattn_block_size) : XAttentionEstimateGeneratorBase("xattn_find_block", xattn_block_size) {}+ XAttentionEstimateFindBlock() = delete;+ [[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override;[[nodiscard]] Arguments get_arguments_desc(const kernel_impl_params& params) const override;[[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override;};class XAttentionEstimatePostProc : public XAttentionEstimateGeneratorBase {public:- XAttentionEstimatePostProc() : XAttentionEstimateGeneratorBase("xattn_post_proc") {}+ explicit XAttentionEstimatePostProc(size_t xattn_block_size) : XAttentionEstimateGeneratorBase("xattn_post_proc", xattn_block_size) {}+ XAttentionEstimatePostProc() = delete;[[nodiscard]] JitConstants get_jit_constants(const kernel_impl_params& params) const override;[[nodiscard]] Arguments get_arguments_desc(const kernel_impl_params& params) const override;[[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override;diff --git a/src/plugins/intel_gpu/src/graph/impls/cm/xattn_find_block.cm b/src/plugins/intel_gpu/src/graph/impls/cm/xattn_find_block.cmindex 97bf4198a2d..0a3d19746ea 100644--- a/src/plugins/intel_gpu/src/graph/impls/cm/xattn_find_block.cm+++ b/src/plugins/intel_gpu/src/graph/impls/cm/xattn_find_block.cm@@ -56,7 +56,7 @@ extern "C" _GENX_MAIN_ void KERNEL_NAME(#endifblock_mask += (b * HQ + hq) * q_block_pad * k_block_pad;- const uint slm_size = 32 * 16 * sizeof(ushort);+ const uint slm_size = NUM_THREADS * 16 * sizeof(ushort);cm_slm_init(slm_size);auto slm = cm_slm_alloc(slm_size);diff --git a/src/plugins/intel_gpu/src/graph/include/paged_attention_inst.h b/src/plugins/intel_gpu/src/graph/include/paged_attention_inst.hindex 7707f70abd4..a2e7cc016d8 100644--- a/src/plugins/intel_gpu/src/graph/include/paged_attention_inst.h+++ b/src/plugins/intel_gpu/src/graph/include/paged_attention_inst.h@@ -23,6 +23,7 @@ public:std::set<size_t> input_ports = { PagedAttentionInputIdx::PAST_LENS,PagedAttentionInputIdx::SUBSEQUENCE_BEGINS,PagedAttentionInputIdx::XATTENTION_THRESHOLD,+ PagedAttentionInputIdx::XATTENTION_BLOCK_SIZE,PagedAttentionInputIdx::MAX_CONTEXT_LEN };if (typed_desc()->has_score_aggregation)