Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Port SDPA micro config and some delta from oneDNN (#30517)

Chon Ming Leec3f5394aa382
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
index 24b03648548..81a0092c525 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
@@ -286,7 +286,7 @@ DEF_BLOCK2D_LOAD_STORE(half, ushort, 16, 16, u16_m8k32v1, 32, 8)
__attribute__((overloadable)) void tile_load_t(tile_type *t, \
const global element_type *ptr, int m, int n, int offset_r, \
int offset_c) { \
- tile_load(t, ptr, m, n, n, offset_r, offset_c); \
+ tile_load_t(t, ptr, m, n, n, offset_r, offset_c); \
} \
__attribute__((overloadable)) void tile_store_full(tile_type t, \
local element_type *ptr, int ld, int offset_r, int offset_c) { \
@@ -551,7 +551,7 @@ DEF_BLOCK2D_LOAD_STORE(half, ushort, 16, 16, u16_m8k32v1, 32, 8)
tile_load_block2d(t, ptr, m, n, m, offset_r, offset_c); \
} \
__attribute__((overloadable)) void tile_store_block2d(tile_type t, \
- global element_type *ptr, int m, int n, int ld, int offset_r, \
+ const global element_type *ptr, int m, int n, int ld, int offset_r, \
int offset_c) { \
const int e = sizeof(element_type); \
_Pragma("unroll") for (int jj = 0; jj < nbc; jj++) { \
@@ -627,7 +627,7 @@ __attribute__((overloadable)) void cooperative_prefetch_2d_internal(
const uint cl_iters = (cl_per_sg + sg_size - 1) / sg_size;
#pragma unroll
for (uint ii_cl = 0; ii_cl < cl_iters; ii_cl++) {
- uint i_cl = ii_cl + (sg_id * cl_per_sg) + get_sub_group_local_id();
+ uint i_cl = (ii_cl * cl_per_sg + sg_id) * sg_size + get_sub_group_local_id();
uint r_cl = i_cl % cl_per_col;
uint c_cl = i_cl / cl_per_col;
if (i_cl < cl) {
@@ -649,7 +649,7 @@ __attribute__((overloadable)) void cooperative_prefetch_2d_internal(
const uint max_off = rbytes - 1 + (c - 1) * ld_bytes;
#pragma unroll
for (uint ii_cl = 0; ii_cl < cl_iters; ii_cl++) {
- uint i_cl = ii_cl + (sg_id * cl_per_sg) + get_sub_group_local_id();
+ uint i_cl = (ii_cl * cl_per_sg + sg_id) * sg_size + get_sub_group_local_id();
uint r_cl = i_cl % cl_per_col;
uint c_cl = i_cl / cl_per_col;
uint pf_off = min(r_cl * 64 + c_cl * ld_bytes, max_off);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_micro.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_micro.cl
index 9f3df09003e..37daf6f9dbd 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_micro.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_micro.cl
@@ -201,9 +201,11 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
#if KEY_SCALES || KEY_ZERO_POINTS
uint ldkq = DIV_UP(d, KEY_GROUP_SIZE);
+ uint num_key_groups = d / KEY_GROUP_SIZE;
#endif
#if VAL_SCALES || VAL_ZERO_POINTS
uint ldvq = DIV_UP(d, VAL_GROUP_SIZE);
+ uint num_val_groups = d / VAL_GROUP_SIZE;
#endif
/* Subgroup IDs for each GEMM */
@@ -250,6 +252,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
V += (VAL_OFF(b1, b0_kv, 0, 0) + INPUT2_OFFSET) / VAL_ELEMENTS_PER_BYTE;
A += DST_OFF(b1, b0, 0, 0, 0);
#if WITH_ATTN_MASK
+ uint ldmsk = MSK_S2;
msk += MSK_OFF(b1 % MSK_D0, b0 % MSK_D1, 0, 0);
#endif
#endif
@@ -322,8 +325,44 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
#ifdef PREFETCH_K0
/* Prefetch first K tile. */
- cooperative_prefetch_2d_k(K, d, k, ugemm_kq_wg_tile_m, PREFETCH_D_MAX, ldk,
- sg_ij, sg_per_wg, SUBGROUP_SIZE, LSC_LDCC_L1C_L3C);
+ cooperative_prefetch_2d_k(
+ /* ptr */ K,
+ /* r */ k,
+ /* c */ d,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ PREFETCH_D_MAX,
+ /* ld */ ldk,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+
+#if KEY_SCALES == QUANTIZE_2D
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ K_scales,
+ /* r */ k,
+ /* c */ num_key_groups,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ D_MAX / KEY_GROUP_SIZE,
+ /* ld */ ldkq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
+#if KEY_ZERO_POINTS == QUANTIZE_2D
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ K_zp,
+ /* r */ k,
+ /* c */ num_key_groups,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ D_MAX / KEY_GROUP_SIZE,
+ /* ld */ ldkq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
#endif
/* Initialize S column sums in SLM to -inf */
@@ -363,8 +402,9 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
uint sg_j0_kq = sg_j_kq * ugemm_kq_sg_tile_n;
#if WITH_ATTN_MASK
+ /* Load mask. No remainder handling needed assuming k block size is a power of 2. */
mask_tile_type mask_tile;
- tile_load_t(&mask_tile, msk, q, k, q, sg_j0_kq + wg_j0, k0 + sg_i0_kq);
+ tile_load_t(&mask_tile, msk, q, k, sg_j0_kq + wg_j0, k0 + sg_i0_kq);
#endif
#if REMAINDER_K
@@ -436,11 +476,49 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
S_max_tile, S_max_slm, ugemm_kq_wg_tile_n, sg_j0_kq, 0);
intel_work_group_barrier_arrive(CLK_LOCAL_MEM_FENCE);
+ int k_chunk = min(k - k0, ugemm_kq_wg_tile_m);
#ifdef PREFETCH_V
/* Prefetch V tile. */
- cooperative_prefetch_2d_maybe_rem(V, d, k - k0, D_MAX,
- (ugemm_kq_wg_tile_m * PREFETCH_D_MAX) / D_MAX, ldv, sg_ij,
- sg_per_wg, SUBGROUP_SIZE, LSC_LDCC_L1C_L3C);
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ V,
+ /* r */ d,
+ /* c */ k - k0,
+ /* rmax */ PREFETCH_D_MAX,
+ /* cmax */ ugemm_kq_wg_tile_m,
+ /* ld */ ldv,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+
+#if VAL_SCALES == QUANTIZE_2D
+ /* Prefetch V scales. */
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ V_scales,
+ /* r */ num_val_groups,
+ /* c */ k - k0,
+ /* rmax */ PREFETCH_D_MAX / VAL_GROUP_SIZE,
+ /* cmax */ k_chunk,
+ /* ld */ ldvq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
+#if VAL_ZERO_POINTS == QUANTIZE_2D
+ /* Prefetch V zero points. */
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ V_zp,
+ /* r */ num_val_groups,
+ /* c */ k - k0,
+ /* rmax */ PREFETCH_D_MAX / VAL_GROUP_SIZE,
+ /* cmax */ k_chunk,
+ /* ld */ ldvq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
#endif
#ifndef ALT_MAX
@@ -504,7 +582,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
tile_hbroadcast_mul(&A_tile, A_scale_tile);
}
-/* Accumulate sums */
+ /* Accumulate sums */
tile_binary(S_sum_tile, S_sum_tile1, binary_add);
/* Save maxima */
@@ -524,18 +602,60 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
#else
const uint stride_k = 1;
#endif
- cooperative_prefetch_2d_k(K + (k0 + ugemm_kq_wg_tile_m) * stride_k,
- k - k0 - ugemm_kq_wg_tile_m, d, ugemm_kq_wg_tile_m,
- PREFETCH_D_MAX, ldk, sg_ij, sg_per_wg, SUBGROUP_SIZE,
- LSC_LDCC_L1C_L3C);
+
+ cooperative_prefetch_2d_k(
+ /* ptr */ K + (k0 + ugemm_kq_wg_tile_m) * stride_k,
+ /* r */ k - k0 - ugemm_kq_wg_tile_m,
+ /* c */ d,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ D_MAX,
+ /* ld*/ ldk,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache*/ LSC_LDCC_L1C_L3C);
+#if KEY_SCALES == QUANTIZE_2D
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ K_scales + (k0 + ugemm_kq_wg_tile_m),
+ /* r */ k - k0 - ugemm_kq_wg_tile_m,
+ /* c */ num_key_groups,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ D_MAX / KEY_GROUP_SIZE,
+ /* ld */ ldkq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
+#if KEY_ZERO_POINTS == QUANTIZE_2D
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ K_zp + (k0 + ugemm_kq_wg_tile_m),
+ /* r */ k - k0 - ugemm_kq_wg_tile_m,
+ /* c */ num_key_groups,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ D_MAX / KEY_GROUP_SIZE,
+ /* ld */ ldkq,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1C_L3C);
+#endif
}
#endif
#if WITH_ATTN_MASK && defined(PREFETCH_MASK)
/* Prefetch next mask tile. */
if (!last) {
- cooperative_prefetch_2d(msk + k0 + ugemm_kq_wg_tile_m + sg_i0_kq + (sg_j0_kq + wg_j0) * q,
- ugemm_kq_sg_tile_m, ugemm_kq_sg_tile_n, 0, 0, 1, SUBGROUP_SIZE,
- LSC_LDCC_L1UC_L3C);
+ cooperative_prefetch_2d_maybe_rem(
+ /* ptr */ msk + k0 + ugemm_kq_sg_tile_m + (wg_j0)*ldmsk,
+ /* r */ k - k0 - ugemm_kq_wg_tile_m,
+ /* c */ q - wg_j0,
+ /* rmax */ ugemm_kq_wg_tile_m,
+ /* cmax */ (ugemm_kq_wg_tile_n * PREFETCH_D_MAX) / D_MAX,
+ /* ld */ ldmsk,
+ /* sg_id */ sg_ij,
+ /* n_sg */ sg_per_wg,
+ /* sg_size */ SUBGROUP_SIZE,
+ /* cache */ LSC_LDCC_L1UC_L3C);
}
#endif
@@ -547,8 +667,6 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
intel_work_group_barrier_arrive(CLK_LOCAL_MEM_FENCE);
/* Accumulate A += V * S */
- int k_chunk = min(k - k0, ugemm_kq_wg_tile_m);
-
a_tile_type A_tile1 = ugemm_vs(
V, ldv, S_slm, ugemm_kq_wg_tile_m, d, ugemm_kq_wg_tile_n,
k_chunk, 0, 0, 0, sg_i_vs, sg_j_vs, (local char *)ugemm_slm
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_micro.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_micro.cpp
index 6790383c0a8..b3a91aa4adc 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_micro.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_micro.cpp
@@ -99,25 +99,52 @@ sdpa_config_t xehpg_h64_s128 = {16, 16, 16, 16, 4, 8, 4, 8};
sdpa_config_t xehpg_h64_s64 = {32, 16, 16, 8, 8, 4, 4, 8};
sdpa_config_t xehpg_h64_2nd = {8, 16, 16, 8, 8, 1, 4, 2};
-sdpa_config_t xehpg_q_h64 = {32, 16, 16, 16, 4, 4, 4, 4};
-sdpa_config_t xehpg_q_h64_2nd = {16, 16, 8, 8, 16, 1, 8, 2};
+sdpa_config_t xehpg_q_h64 = {32, 16, 16, 16, 4, 8, 4, 8};
+sdpa_config_t xehpg_q_h64_s128 = {16, 16, 16, 8, 8, 4, 4, 8};
+sdpa_config_t xehpg_q_h64_s64 = {32, 8, 32, 8, 2, 8, 2, 8};
+sdpa_config_t xehpg_q_h64_s32 = {8, 8, 16, 8, 4, 8, 4, 8};
+
+sdpa_config_t xehpg_q_h64_s64_2nd = {8, 8, 8, 8, 8, 2, 8, 2};
+sdpa_config_t xehpg_q_h64_s128_2nd = {16, 8, 8, 8, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h64_2nd = {16, 16, 8, 8, 16, 2, 8, 4};
sdpa_config_t xehpg_h128 = {16, 16, 32, 8, 8, 4, 4, 8};
sdpa_config_t xehpg_h128_s32 = {16, 16, 16, 8, 16, 2, 8, 4};
sdpa_config_t xehpg_h128_2nd = {8, 16, 16, 8, 16, 1, 8, 2};
-sdpa_config_t xehpg_h128_s256_2nd = {8, 16, 32, 8, 8, 1, 4, 2};
-sdpa_config_t xehpg_q_h128 = {32, 16, 16, 16, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h128 = {8, 32, 16, 32, 8, 2, 8, 2};
+sdpa_config_t xehpg_q_h128_s64 = {8, 8, 16, 8, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h128_s512 = {16, 16, 16, 16, 8, 4, 8, 4};
sdpa_config_t xehpg_q_h128_2nd = {32, 16, 16, 8, 16, 1, 8, 2};
-sdpa_config_t xehpg_q_h128_s64_2nd = {16, 16, 16, 8, 16, 1, 8, 2};
+sdpa_config_t xehpg_q_h128_s96_2nd = {8, 8, 8, 8, 16, 2, 16, 2};
sdpa_config_t xehpg_h256 = {16, 16, 32, 8, 16, 2, 8, 4};
sdpa_config_t xehpg_h256_s128 = {8, 16, 32, 16, 8, 4, 8, 4};
sdpa_config_t xehpg_h256_s32 = {8, 16, 32, 8, 16, 2, 8, 4};
+
+sdpa_config_t xehpg_q_h256 = {16, 16, 64, 8, 8, 4, 4, 8};
+sdpa_config_t xehpg_q_h256_s512 = {16, 16, 32, 16, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h256_s64 = {8, 8, 32, 8, 8, 4, 8, 4};
+
sdpa_config_t xehpg_h256_2nd = {8, 8, 16, 8, 16, 1, 16, 1};
sdpa_config_t xehpg_h256_s64_2nd = {16, 8, 16, 8, 16, 1, 16, 1};
sdpa_config_t xehpg_h256_s32_2nd = {16, 16, 32, 8, 16, 1, 8, 2};
+sdpa_config_t xehpg_q_h256_2nd = {32, 8, 32, 8, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h256_s96_2nd = {8, 8, 16, 8, 16, 2, 16, 2};
+
+sdpa_config_t xehpg_q_h512_s64 = {8, 8, 64, 8, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h512_s128 = {8, 16, 32, 16, 16, 2, 16, 2};
+sdpa_config_t xehpg_q_h512_s256 = {16, 8, 64, 8, 8, 4, 8, 4};
+sdpa_config_t xehpg_q_h512 = {8, 16, 64, 8, 16, 2, 8, 4};
+
+sdpa_config_t xehpg_q_h512_s64_2nd = {8, 16, 32, 8, 32, 1, 16, 2};
+sdpa_config_t xehpg_q_h512_s256_2nd = {16, 8, 32, 8, 16, 2, 16, 2};
+sdpa_config_t xehpg_q_h512_2nd = {16, 8, 16, 8, 32, 1, 32, 1};
+
+sdpa_config_t xehpg_h512 = {8, 16, 32, 16, 16, 2, 16, 2};
+sdpa_config_t xehpg_h512_2nd = {8, 8, 32, 8, 16, 1, 16, 1};
+
sdpa_config_t xehpc_h32 = {16, 64, 32, 16, 4, 2, 1, 8};
sdpa_config_t xehpc_h32_s32 = {16, 16, 16, 16, 2, 4, 2, 4};
sdpa_config_t xehpc_h32_2nd = {16, 64, 16, 16, 8, 1, 2, 4};
@@ -128,23 +155,96 @@ sdpa_config_t xehpc_h64_s32 = {16, 16, 16, 16, 4, 2, 4, 2};
sdpa_config_t xehpc_h64_2nd = {32, 32, 32, 16, 4, 1, 2, 2};
sdpa_config_t xehpc_h64_s64_2nd = {16, 16, 16, 16, 4, 1, 4, 1};
-sdpa_config_t xehpc_q_h64 = {16, 64, 32, 16, 8, 4, 2, 16};
+sdpa_config_t xehpc_q_h64_s64 = {16, 16, 16, 16, 4, 4, 4, 4};
+sdpa_config_t xehpc_q_h64_s384 = {16, 64, 16, 32, 8, 2, 4, 4};
+sdpa_config_t xehpc_q_h64_s1024 = {16, 64, 16, 16, 16, 1, 4, 4};
+sdpa_config_t xehpc_q_h64 = {16, 64, 16, 32, 8, 1, 4, 2};
+
+sdpa_config_t xehpc_q_h64_s96_2nd = {16, 16, 16, 16, 8, 1, 4, 1};
+sdpa_config_t xehpc_q_h64_s256_2nd = {16, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_q_h64_s1152_2nd = {16, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_q_h64_2nd = {64, 16, 16, 16, 16, 2, 16, 2};
sdpa_config_t xehpc_h128 = {16, 64, 32, 16, 16, 2, 4, 8};
sdpa_config_t xehpc_h128_s64 = {16, 32, 32, 32, 4, 2, 4, 2};
sdpa_config_t xehpc_h128_s32 = {16, 16, 16, 16, 8, 2, 8, 2};
sdpa_config_t xehpc_h128_2nd = {32, 32, 32, 16, 8, 1, 4, 2};
-sdpa_config_t xehpc_q_h128 = {16, 64, 16, 32, 16, 2, 8, 4};
-sdpa_config_t xehpc_q_h128_s64 = {16, 16, 32, 16, 4, 4, 4, 4};
-sdpa_config_t xehpc_q_h128_s32 = {16, 16, 32, 16, 4, 2, 4, 2};
-sdpa_config_t xehpc_q_h128_2nd = {32, 32, 16, 32, 4, 1, 4, 1};
-sdpa_config_t xehpc_q_h128_s32_2nd = {16, 32, 16, 16, 8, 1, 4, 2};
+sdpa_config_t xehpc_q_h128 = {16, 64, 16, 32, 16, 1, 8, 2};
+sdpa_config_t xehpc_q_h128_s32 = {16, 16, 16, 16, 8, 2, 8, 2};
+sdpa_config_t xehpc_q_h128_s128 = {16, 16, 16, 16, 8, 4, 8, 4};
+sdpa_config_t xehpc_q_h128_s128_integrated = {16, 16, 16, 16, 8, 2, 8, 2};
+
+sdpa_config_t xehpc_q_h128_2nd = {16, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_q_h128_2nd_integrated = {16, 16, 16, 16, 8, 1, 8, 1};
+sdpa_config_t xehpc_q_h128_s96_2nd = {16, 16, 16, 16, 8, 1, 8, 1};
+sdpa_config_t xehpc_q_h128_s512_2nd = {16, 16, 16, 16, 16, 2, 8, 2};
sdpa_config_t xehpc_h256 = {16, 32, 32, 32, 8, 4, 8, 4};
sdpa_config_t xehpc_h256_s64 = {16, 32, 32, 32, 8, 1, 8, 1};
sdpa_config_t xehpc_h256_2nd = {16, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_h512 = {32, 16, 64, 16, 8, 4, 8, 4};
+sdpa_config_t xehpc_h512_s64 = {16, 16, 64, 16, 8, 2, 8, 2};
+sdpa_config_t xehpc_h512_s128_2nd = {16, 16, 64, 16, 8, 1, 8, 1};
+sdpa_config_t xehpc_h512_s512_2nd = {32, 16, 64, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_h512_s1024_2nd = {64, 16, 32, 16, 16, 2, 16, 2};
+sdpa_config_t xehpc_h512_2nd = {32, 16, 64, 16, 16, 1, 16, 1};
+
+sdpa_config_t xehpc_h512_integrated = {16, 16, 32, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_h512_s128_integrated = {16, 16, 64, 16, 8, 2, 8, 2};
+sdpa_config_t xehpc_h512_s256_2nd_integrated = {16, 16, 64, 16, 8, 1, 8, 1};
+sdpa_config_t xehpc_h512_s1024_2nd_integrated = {16, 16, 64, 16, 8, 2, 8, 2};
+sdpa_config_t xehpc_h512_2nd_integrated = {16, 16, 64, 16, 16, 2, 16, 2};
+
+sdpa_config_t xehpc_q_h512_s64_2nd_integrated = {16, 32, 64, 32, 16, 2, 8, 2};
+sdpa_config_t xehpc_q_h512_s128_2nd_integrated = {16, 16, 64, 16, 8, 1, 32, 1};
+sdpa_config_t xehpc_q_h512_s256_2nd_integrated = {16, 32, 64, 32, 16, 2, 8, 2};
+sdpa_config_t xehpc_q_h512_s512_2nd_integrated = {16, 16, 64, 16, 4, 4, 8, 4};
+sdpa_config_t xehpc_q_h512_s1024_2nd_integrated
+ = {16, 16, 64, 16, 16, 1, 16, 1};
+sdpa_config_t xehpc_q_h512_2nd_integrated = {32, 16, 64, 16, 8, 1, 16, 1};
+
+sdpa_config_t xehpc_q_h512_integrated = {16, 32, 32, 32, 16, 1, 16, 1};
+
+sdpa_config_t xehpc_q_h512 = {16, 32, 64, 16, 16, 2, 8, 4};
+sdpa_config_t xehpc_q_h512_s128 = {16, 16, 64, 16, 8, 2, 8, 2};
+
+sdpa_config_t xehpc_q_h512_s512_2nd = {16, 16, 64, 16, 8, 2, 8, 2};
+sdpa_config_t xehpc_q_h512_s1024_2nd = {64, 16, 64, 16, 16, 2, 16, 2};
+sdpa_config_t xehpc_q_h512_2nd = {16, 16, 64, 16, 16, 2, 16, 2};
+
+sdpa_config_t xe2_q_h64 = {16, 64, 16, 32, 16, 1, 8, 2};
+sdpa_config_t xe2_q_h64_s1024_integrated = {16, 64, 16, 32, 8, 4, 4, 8};
+sdpa_config_t xe2_q_h64_s512 = {16, 64, 16, 32, 8, 4, 4, 8};
+sdpa_config_t xe2_q_h64_s384 = {16, 64, 16, 16, 16, 1, 4, 4};
+sdpa_config_t xe2_q_h64_s128 = {16, 64, 16, 32, 8, 1, 4, 2};
+sdpa_config_t xe2_q_h64_s128_integrated = {16, 16, 16, 16, 4, 4, 4, 4};
+sdpa_config_t xe2_q_h64_s32 = {16, 16, 16, 16, 4, 4, 4, 4};
+
+sdpa_config_t xe2_q_h64_2nd = {16, 16, 16, 16, 16, 1, 8, 1};
+sdpa_config_t xe2_q_h64_2nd_integrated = {16, 16, 16, 16, 8, 1, 8, 1};
+sdpa_config_t xe2_q_h64_s96_2nd_integrated = {16, 16, 16, 16, 8, 1, 4, 1};
+sdpa_config_t xe2_q_h64_s384_2nd_integrated = {64, 16, 16, 16, 4, 1, 4, 1};
+sdpa_config_t xe2_q_h64_s64_2nd = {16, 16, 16, 16, 4, 2, 4, 2};
+sdpa_config_t xe2_q_h64_s128_2nd = {16, 16, 16, 16, 8, 2, 8, 2};
+sdpa_config_t xe2_q_h64_s384_2nd = {16, 16, 16, 16, 16, 1, 4, 1};
+sdpa_config_t xe2_q_h64_s512_2nd = {64, 16, 16, 16, 8, 1, 8, 1};
+sdpa_config_t xe2_q_h64_s768_2nd = {64, 16, 16, 16, 16, 1, 8, 1};
+
+sdpa_config_t xe2_q_h256 = {16, 64, 16, 32, 32, 1, 16, 2};
+sdpa_config_t xe2_q_h256_s384 = {16, 32, 32, 32, 8, 2, 8, 2};
+sdpa_config_t xe2_q_h256_s128 = {16, 32, 32, 32, 8, 1, 8, 1};
+sdpa_config_t xe2_q_h256_s128_integrated = {16, 32, 32, 32, 8, 2, 8, 2};
+sdpa_config_t xe2_q_h256_s64_integrated = {16, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xe2_q_h256_s64 = {16, 32, 64, 16, 8, 2, 4, 4};
+
+sdpa_config_t xe2_q_h256_2nd_integrated = {32, 16, 64, 16, 4, 1, 4, 1};
+sdpa_config_t xe2_q_h256_s1152_2nd_integrated = {16, 16, 64, 16, 4, 1, 4, 1};
+sdpa_config_t xe2_q_h256_s768_2nd_integrated = {64, 16, 16, 16, 16, 1, 16, 1};
+sdpa_config_t xe2_q_h256_s512_2nd_integrated = {32, 32, 32, 16, 16, 1, 8, 2};
+sdpa_config_t xe2_q_h256_s384_2nd_integrated = {16, 16, 16, 16, 16, 1, 16, 1};
+
sdpa_config_t *choose_config_xehpg(int head_size, int seq, bool thin_q, bool quantized, bool is_pa) {
if (head_size <= 32) {
if (quantized && seq >= 128) {
@@ -159,8 +259,16 @@ sdpa_config_t *choose_config_xehpg(int head_size, int seq, bool thin_q, bool qua
return &xehpg_h32;
} else if (head_size <= 64) {
if (quantized) {
- if (thin_q) return &xehpg_q_h64_2nd;
- return &xehpg_q_h64;
+ if (thin_q) {
+ if (seq <= 64) return &xehpg_q_h64_s64_2nd;
+ if (seq <= 128) return &xehpg_q_h64_s128_2nd;
+ return &xehpg_q_h64_2nd;
+ } else {
+ if (seq <= 32) return &xehpg_q_h64_s32;
+ if (seq <= 64) return &xehpg_q_h64_s64;
+ if (seq <= 128) return &xehpg_q_h64_s128;
+ return &xehpg_q_h64;
+ }
}
if (thin_q) return &xehpg_h64_2nd;
if (seq <= 0 && is_pa) return &xehpg_h64;
@@ -170,14 +278,16 @@ sdpa_config_t *choose_config_xehpg(int head_size, int seq, bool thin_q, bool qua
} else if (head_size <= 128) {
if (quantized) {
if (thin_q) {
- if (seq <= 64) return &xehpg_q_h128_s64_2nd;
+ if (seq <= 1) return &xehpg_q_h128_2nd;
+ if (seq <= 96) return &xehpg_q_h128_s96_2nd;
return &xehpg_q_h128_2nd;
}
- if (seq <= 32) return &xehpg_h128_s32;
+ if (seq <= 64) return &xehpg_q_h128_s64;
+ if (seq <= 512) return &xehpg_q_h128_s512;
return &xehpg_q_h128;
}
if (thin_q) {
- if (seq <= 256) return &xehpg_h128_s256_2nd;
+ if (seq <= 256) return &xehpg_q_h128_2nd;
return &xehpg_h128_2nd;
}
if (seq <= 0 && is_pa) return &xehpg_h128;
@@ -185,19 +295,43 @@ sdpa_config_t *choose_config_xehpg(int head_size, int seq, bool thin_q, bool qua
return &xehpg_h128;
} else if (head_size <= 256) {
if (thin_q) {
+ if (quantized) {
+ if (seq <= 96) return &xehpg_q_h256_s96_2nd;
+ return &xehpg_q_h256_2nd;
+ }
if (seq <= 32) return &xehpg_h256_s32_2nd;
if (seq <= 64) return &xehpg_h256_s64_2nd;
return &xehpg_h256_2nd;
}
+ if (quantized) {
+ if (seq <= 64) return &xehpg_q_h256_s64;
+ if (seq <= 512) return &xehpg_q_h256_s512;
+ return &xehpg_q_h256;
+ }
if (seq <= 0 && is_pa) return &xehpg_h256;
if (seq <= 32) return &xehpg_h256_s32;
if (seq <= 128) return &xehpg_h256_s128;
return &xehpg_h256;
+ } else if (head_size <= 512) {
+ if (quantized) {
+ if (thin_q) {
+ if (seq <= 64) return &xehpg_q_h512_s64_2nd;
+ if (seq <= 256) return &xehpg_q_h512_s256_2nd;
+ return &xehpg_q_h512_2nd;
+ }
+ if (seq <= 64) return &xehpg_q_h512_s64;
+ if (seq <= 128) return &xehpg_q_h512_s128;
+ if (seq <= 256) return &xehpg_q_h512_s256;
+ return &xehpg_q_h512;
+ }
+ if (thin_q) { return &xehpg_h512_2nd; }
+ if (seq <= 0 && is_pa) return &xehpg_h512;
+ return &xehpg_h512;
}
return nullptr;
}
-sdpa_config_t *choose_config_xehpc(int head_size, int seq, bool thin_q, bool quantized, bool is_pa) {
+sdpa_config_t *choose_config_xehpc(int head_size, int seq, bool thin_q, bool quantized, bool is_integrated, bool is_pa) {
if (head_size <= 32) {
if (thin_q) return &xehpc_h32_2nd;
if (seq <= 0 && is_pa) return &xehpc_h32;
@@ -205,10 +339,22 @@ sdpa_config_t *choose_config_xehpc(int head_size, int seq, bool thin_q, bool qua
return &xehpc_h32;
} else if (head_size <= 64) {
if (thin_q) {
+ if (quantized) {
+ if (seq <= 96) return &xehpc_q_h64_s96_2nd;
+ if (seq <= 256) return &xehpc_q_h64_s256_2nd;
+ if (seq <= 1152) return &xehpc_q_h64_s1152_2nd;
+ return &xehpc_q_h64_2nd;
+ }
+
if (seq <= 64) return &xehpc_h64_s64_2nd;
return &xehpc_h64_2nd;
}
- if (quantized && seq >= 256) return &xehpc_q_h64;
+ if (quantized) {
+ if (seq <= 64) return &xehpc_q_h64_s64;
+ if (seq <= 384) return &xehpc_q_h64_s384;
+ if (seq <= 1024) return &xehpc_q_h64_s1024;
+ return &xehpc_q_h64;
+ }
if (seq <= 0 && is_pa) return &xehpc_h64;
if (seq <= 32) return &xehpc_h64_s32;
if (seq <= 64) return &xehpc_h64_s64;
@@ -216,13 +362,19 @@ sdpa_config_t *choose_config_xehpc(int head_size, int seq, bool thin_q, bool qua
} else if (head_size <= 128) {
if (quantized) {
if (thin_q) {
- if (seq <= 32) return &xehpc_q_h128_s32_2nd;
+ if (is_integrated) { return &xehpc_q_h128_2nd_integrated; }
+ if (seq <= 96) return &xehpc_q_h128_s96_2nd;
+ if (seq <= 512) return &xehpc_q_h128_s512_2nd;
return &xehpc_q_h128_2nd;
}
+ if (is_integrated) {
+ if (seq <= 128) { return &xehpc_q_h128_s128_integrated; }
+ }
if (seq <= 32) return &xehpc_q_h128_s32;
- if (seq <= 64) return &xehpc_q_h128_s64;
+ if (seq <= 128) return &xehpc_q_h128_s128;
return &xehpc_q_h128;
}
+ if (is_integrated) return &xehpc_q_h128_2nd_integrated;
if (thin_q) return &xehpc_h128_2nd;
if (seq <= 0 && is_pa) return &xehpc_h128;
if (seq <= 32) return &xehpc_h128_s32;
@@ -233,10 +385,110 @@ sdpa_config_t *choose_config_xehpc(int head_size, int seq, bool thin_q, bool qua
if (seq <= 0 && is_pa) return &xehpc_h256;
if (seq <= 64) return &xehpc_h256_s64;
return &xehpc_h256;
+ } else if (head_size <= 512) {
+ if (thin_q) {
+ if (quantized) {
+ if (is_integrated) {
+ if (seq <= 64) return &xehpc_q_h512_s64_2nd_integrated;
+ if (seq <= 128) return &xehpc_q_h512_s128_2nd_integrated;
+ if (seq <= 256) return &xehpc_q_h512_s256_2nd_integrated;
+ if (seq <= 512) return &xehpc_q_h512_s512_2nd_integrated;
+ if (seq <= 1024) return &xehpc_q_h512_s1024_2nd_integrated;
+ return &xehpc_q_h512_2nd_integrated;
+ }
+ if (seq <= 512) return &xehpc_q_h512_s512_2nd;
+ if (seq <= 1024) return &xehpc_q_h512_s1024_2nd;
+ return &xehpc_q_h512_2nd;
+ }
+
+ if (is_integrated) {
+ if (seq <= 256) return &xehpc_h512_s256_2nd_integrated;
+ if (seq <= 1024) return &xehpc_h512_s1024_2nd_integrated;
+ return &xehpc_h512_2nd_integrated;
+ }
+ if (seq <= 128) return &xehpc_h512_s128_2nd;
+ if (seq <= 512) return &xehpc_h512_s512_2nd;
+ if (seq <= 1024) return &xehpc_h512_s1024_2nd;
+ return &xehpc_h512_2nd;
+ }
+
+ if (quantized) {
+ if (is_integrated) return &xehpc_q_h512_integrated;
+ if (seq <= 128) return &xehpc_q_h512_s128;
+ return &xehpc_q_h512;
+ }
+ if (is_integrated) {
+ if (seq <= 128) return &xehpc_h512_s128_integrated;
+ return &xehpc_h512_integrated;
+ }
+ if (seq <= 0 && is_pa) return &xehpc_h512;
+ if (seq <= 64) return &xehpc_h512_s64;
+ return &xehpc_h512;
}
return nullptr;
}
+sdpa_config_t *choose_config_xe2(int head_size, int seq, bool thin_q,
+ bool quantized, bool is_integrated, bool is_pa) {
+ if (head_size <= 64) {
+ if (quantized) {
+ if (thin_q) {
+ if (is_integrated) {
+ if (seq <= 96) return &xe2_q_h64_s96_2nd_integrated;
+ if (seq <= 384) return &xe2_q_h64_s384_2nd_integrated;
+ return &xe2_q_h64_2nd_integrated;
+ }
+ if (seq <= 64) return &xe2_q_h64_s64_2nd;
+ if (seq <= 128) return &xe2_q_h64_s128_2nd;
+ if (seq <= 384) return &xe2_q_h64_s384_2nd;
+ if (seq <= 512) return &xe2_q_h64_s512_2nd;
+ if (seq <= 768) return &xe2_q_h64_s768_2nd;
+ return &xe2_q_h64_2nd;
+ }
+ if (seq <= 32) return &xe2_q_h64_s32;
+ if (is_integrated) {
+ if (seq <= 128) return &xe2_q_h64_s128_integrated;
+ }
+ if (seq <= 128) return &xe2_q_h64_s128;
+ if (seq <= 384) return &xe2_q_h64_s384;
+ if (seq <= 512) return &xe2_q_h64_s512;
+ if (is_integrated) {
+ if (seq <= 1024) return &xe2_q_h64_s1024_integrated;
+ }
+ return &xe2_q_h64;
+ }
+ }
+
+ if (head_size <= 128) {
+ return choose_config_xehpc(
+ head_size, seq, thin_q, quantized, is_integrated, is_pa);
+ }
+
+ if (head_size <= 256) {
+ if (quantized) {
+ if (is_integrated) {
+ if (thin_q) {
+ if (seq < 384) return &xe2_q_h256_s384_2nd_integrated;
+ if (seq < 512) return &xe2_q_h256_s512_2nd_integrated;
+ if (seq < 768) return &xe2_q_h256_s768_2nd_integrated;
+ if (seq < 1152) return &xe2_q_h256_s1152_2nd_integrated;
+ return &xe2_q_h256_2nd_integrated;
+ }
+ if (seq <= 64) return &xe2_q_h256_s64_integrated;
+ if (seq <= 128) return &xe2_q_h256_s128_integrated;
+ }
+ if (!thin_q) {
+ if (seq <= 64) return &xe2_q_h256_s64;
+ if (seq <= 128) return &xe2_q_h256_s128;
+ if (seq <= 384) return &xe2_q_h256_s384;
+ return &xe2_q_h256;
+ }
+ }
+ }
+ return choose_config_xehpc(
+ head_size, seq, thin_q, quantized, is_integrated, is_pa);
+}
+
} // namespace
const bool kq_common_scales = false;
@@ -265,6 +517,7 @@ void SDPAKernelMicro::init_microkernels(const sdpa_params& params, micro::Packag
/* Retrieve pre-tuned kernel configuration */
sdpa_config_t *config = nullptr;
bool thin_q = (!n_queries.is_dynamic && (n_queries.v <= 16)) || !is_prefill;
+ bool is_integrated = params.engineInfo.deviceType == dev_type::integrated_gpu;
bool is_quantized = (K.GetDType() == Datatype::UINT8 || K.GetDType() == Datatype::INT8) ||
(V.GetDType() == Datatype::UINT8 || V.GetDType() == Datatype::INT8);
@@ -275,10 +528,13 @@ void SDPAKernelMicro::init_microkernels(const sdpa_params& params, micro::Packag
break;
}
case gpu_arch::xe_hpc:
+ config = choose_config_xehpc(static_cast<int32_t>(k_head_size), static_cast<int32_t>(n_keys.v), thin_q,
+ is_quantized, is_integrated, params.conf.is_paged_attention);
+ break;
case gpu_arch::xe2:
case gpu_arch::xe3: {
- config = choose_config_xehpc(static_cast<int32_t>(k_head_size), static_cast<int32_t>(n_keys.v), thin_q,
- is_quantized, params.conf.is_paged_attention);
+ config = choose_config_xe2(static_cast<int32_t>(k_head_size), static_cast<int32_t>(n_keys.v), thin_q,
+ is_quantized, is_integrated, params.conf.is_paged_attention);
break;
}
default: break;
@@ -312,17 +568,16 @@ void SDPAKernelMicro::init_microkernels(const sdpa_params& params, micro::Packag
if (params.conf.is_kv_compressed && !kq_common_scales) {
const auto scale_dt = convert_type(params.key_cache_comp_scale.GetDType());
problem_kq.Ta_scale = scale_dt;
- problem_kq.A_scale.alignment = scale_dt.size();
-
- problem_kq.A_scale.layout = micro::MatrixLayout::T;
+ problem_kq.A_scale.setAlignment(scale_dt.size());
+ problem_kq.A_scale.layout = micro::MatrixLayout::N;
problem_kq.aScale2D = true;
}
if (params.conf.is_kv_compressed && params.conf.use_asymmetric_quantization) {
const auto zp_dt = convert_type(params.key_cache_comp_zp.GetDType());
problem_kq.Tao = zp_dt;
- problem_kq.AO.alignment = zp_dt.size();
- problem_kq.AO.layout = micro::MatrixLayout::T;
+ problem_kq.AO.setAlignment(zp_dt.size());
+ problem_kq.AO.layout = micro::MatrixLayout::N;
problem_kq.aoPtrDims = kq_common_zp ? 0 : 2;
problem_kq.aOffset = micro::ABOffset::Calc;
}
@@ -378,7 +633,7 @@ void SDPAKernelMicro::init_microkernels(const sdpa_params& params, micro::Packag
if (params.conf.is_kv_compressed && !vs_common_scales) {
auto scale_dt = convert_type(params.value_cache_comp_scale.GetDType());
problem_vs.Ta_scale = scale_dt;
- problem_vs.A_scale.alignment = scale_dt.size();
+ problem_vs.A_scale.setAlignment(scale_dt.size());
problem_vs.A_scale.layout = micro::MatrixLayout::N;
problem_vs.aScale2D = true;
}
@@ -386,7 +641,7 @@ void SDPAKernelMicro::init_microkernels(const sdpa_params& params, micro::Packag
if (params.conf.is_kv_compressed && params.conf.use_asymmetric_quantization) {
auto zp_dt = convert_type(params.value_cache_comp_zp.GetDType());
problem_vs.Tao = zp_dt;
- problem_vs.AO.alignment = zp_dt.size();
+ problem_vs.AO.setAlignment(zp_dt.size());
problem_vs.AO.layout = micro::MatrixLayout::N;
problem_vs.aoPtrDims = vs_common_zp ? 0 : 2;
problem_vs.aOffset = micro::ABOffset::Calc;
@@ -893,6 +1148,7 @@ KernelsData SDPAKernelMicro::GetKernelsData(const Params& params) const {
void SDPAKernelMicro::GetUpdateDispatchDataFunc(KernelData& kd) const {
kd.update_dispatch_data_func = [this](const Params& params, KernelData& kernel_data) {
const auto& prim_params = static_cast<const sdpa_params&>(params);
+
const auto& Q = prim_params.inputs[0];
const auto& K = prim_params.inputs[1];
@@ -913,15 +1169,14 @@ void SDPAKernelMicro::GetUpdateDispatchDataFunc(KernelData& kd) const {
s_q.t = ScalarDescriptor::Types::INT32;
s_q.v.s32 = static_cast<uint32_t>(n_queries.v);
- // TODO: Currently 2nd token version works slower than prefill version
- const bool is_prefill = true;//n_queries.v > 1;
+ const bool is_prefill = true; //n_queries.v > 1;
OPENVINO_ASSERT(kernel_data.kernels.size() == 2, "[GPU] Invalid kernels size for update dispatch data func");
size_t target_kernel = is_prefill ? prefill_id : generate_id;
- kernel_data.kernels[prefill_id].skip_execution = true;
kernel_data.kernels[generate_id].skip_execution = true;
+ kernel_data.kernels[prefill_id].skip_execution = true;
const auto& gemms = kernel_data.kernels[target_kernel].micro_kernels;
auto dispatchData = SetDefault(prim_params, gemms[kq_id]->p, gemms[vs_id]->p);