Skip to content

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

[GPU] Update sdpa opt to support unaligned head size (#30099)

Taylor Yeonbok Lee5bed235bc25e
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
index 7cdf0903dba..f68e3337766 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_opt.cl
@@ -116,7 +116,7 @@ inline uint FUNC(get_bt_index_value)(OPTIONAL_SHAPE_INFO_ARG uint b, uint f, uin
#define OUTPUT_BLOCK_READ(ptr, offset) BLOCK_READN(OUTPUT_TYPE, 1, ptr, offset)
#define OUTPUT_BLOCK_WRITE(ptr, offset, val) BLOCK_WRITEN(OUTPUT_TYPE, 1, ptr, offset, val)
#define VALUE_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT2_TYPE, 1, ptr, offset)
-#define SUBGROUPS_PER_WG (HEAD_SIZE * SG_SCALE_FACTOR / SUBGROUP_SIZE)
+#define SUBGROUPS_PER_WG CEIL_DIV(HEAD_SIZE * SG_SCALE_FACTOR, SUBGROUP_SIZE)
#if IS_KV_COMPRESSED
#if COMPRESSED_PER_HEAD
@@ -883,7 +883,13 @@ KERNEL(sdpa_opt)(
#else
#define target_seq_idx ((uint)get_global_id(1) * TARGET_SEQ_LEN_BLOCK_SIZE)
#endif
+#if SG_SCALE_FACTOR > 1
#define head_size_idx ((uint)get_local_id(2) % HEAD_SIZE)
+#elif SG_SCALE_FACTOR == 1
+ #define head_size_idx ((uint)get_local_id(2))
+#else
+ #error "sdpa_opt.cl: Unsupported scale factor"
+#endif
#define sglid (uint)get_sub_group_local_id()
#define sgid (uint)get_sub_group_id()
@@ -948,6 +954,28 @@ KERNEL(sdpa_opt)(
#endif
if (seq_idx_end != TARGET_SEQ_LEN_BLOCK_SIZE) {
+ #ifdef HEAD_SIZE_LEFTOVER
+ if ((sgid + 1) * SUBGROUP_SIZE <= HEAD_SIZE) {
+ for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
+ INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
+
+ slm_query[query_local_offset] = val * scale_val;
+ query_offset += query_pitch;
+ query_local_offset++;
+ }
+ } else {
+ // remainder
+ int valid_workers = HEAD_SIZE - sgid * SUBGROUP_SIZE;
+ if (sglid < valid_workers) {
+ unroll_for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
+ INPUT0_TYPE val = query_input[query_offset];
+ slm_query[query_local_offset] = val * scale_val;
+ query_offset += query_pitch;
+ query_local_offset++;
+ }
+ }
+ }
+ #else
if (sgid * SUBGROUP_SIZE < HEAD_SIZE) {
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
@@ -957,6 +985,7 @@ KERNEL(sdpa_opt)(
query_local_offset++;
}
}
+ #endif
} else {
#if SG_SCALE_FACTOR == 2
if ((sgid < (SUBGROUPS_PER_WG / SG_SCALE_FACTOR))) {
@@ -988,7 +1017,22 @@ KERNEL(sdpa_opt)(
query_offset += query_pitch;
query_local_offset++;
}
- #else
+ #else
+ // Load query_input to slm_query. E.g., if TARGET_SEQ_LEN_BLOCK_SIZE = 16 && HEAD_SIZE == 72 && # subgroups = 5
+ // 16 (TARGET_SEQ_LEN_BLOCK_SIZE)
+ // ------------
+ // | sg0 | 16
+ // |-----------|
+ // | sg1 | 16
+ // |-----------|
+ // | sg2 | 16
+ // |-----------|
+ // | sg3 | 16
+ // |-----------|
+ // | sg4 | 8 // remainder of head
+ // -------------
+ // slm_query
+ if ((sgid + 1) * TARGET_SEQ_LEN_BLOCK_SIZE <= HEAD_SIZE) {
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
@@ -996,11 +1040,22 @@ KERNEL(sdpa_opt)(
query_offset += query_pitch;
query_local_offset++;
}
+ } else {
+ // remainder
+ int valid_workers = HEAD_SIZE - sgid * SUBGROUP_SIZE;
+ if (sglid < valid_workers) {
+ unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
+ INPUT0_TYPE val = query_input[query_offset];
+ slm_query[query_local_offset] = val * scale_val;
+ query_offset += query_pitch;
+ query_local_offset++;
+ }
+ }
+ }
#endif
}
barrier(CLK_LOCAL_MEM_FENCE);
}
-
{
#if TARGET_SEQ_LEN_BLOCK_SIZE <= SUBGROUP_SIZE
// Initialize slm buffers with MIN and ZERO values
@@ -1041,7 +1096,7 @@ KERNEL(sdpa_opt)(
KEY_SEQ_OFFSET * key_pitch +
heads_dim * HEAD_SIZE +
seq_len * key_pitch;
-#else
+#else // !IS_PAGED_ATTENTION
#ifdef BEAM_TABLE_TYPE
const uint b_idx = beam_table[FUNC_CALL(get_bt_index_key)(OPTIONAL_SHAPE_INFO_TENSOR b0_idx, b1_idx, 0, 0, seq_len + sglid, 0)];
const uint key_offset = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR b_idx, b1_idx, 0, 0, seq_len + sglid, 0);
@@ -1055,9 +1110,8 @@ KERNEL(sdpa_opt)(
uint key_offset = INPUT1_GET_INDEX(b0_idx, b1_idx, seq_len, 0);
const uint key_pitch = HEAD_SIZE;
#endif
-#endif
-#endif
-
+#endif // BEAM_TABLE_TYPE
+#endif // IS_PAGED_ATTENTION
int seq_len_calc_size = min((int)(SOURCE_SEQ_LEN) - (int)seq_len, (int)SUBGROUP_SIZE);
#if !IS_CAUSAL
qk_acc = FUNC_CALL(load_attn_mask)(OPTIONAL_SHAPE_INFO_TENSOR
@@ -1079,8 +1133,9 @@ KERNEL(sdpa_opt)(
KEY_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];
#endif
#endif
+ uint head_idx_index = 0;
__attribute__((opencl_unroll_hint(1)))
- for (uint head_idx_index = 0; head_idx_index < HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
+ for (; head_idx_index + SUBGROUP_SIZE <= HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, 1, ptr, offset);
#define QUERY_VEC MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
@@ -1111,6 +1166,31 @@ KERNEL(sdpa_opt)(
}
}
}
+ #ifdef HEAD_SIZE_LEFTOVER
+ QUERY_VEC queries_vec;
+ uint query_local_offset = (head_idx_index * TARGET_SEQ_LEN_BLOCK_SIZE) + sglid;
+ unroll_for (uint q_row_idx = 0; q_row_idx < HEAD_SIZE_LEFTOVER; q_row_idx++) {
+ queries_vec[q_row_idx] = slm_query[query_local_offset];
+ query_local_offset += TARGET_SEQ_LEN_BLOCK_SIZE;
+ }
+ unroll_for (uint key_row_idx = 0; key_row_idx < TARGET_SEQ_LEN_BLOCK_SIZE; ++key_row_idx) {
+#ifdef BEAM_TABLE_TYPE
+ INPUT1_TYPE key_packed = (sglid < HEAD_SIZE_LEFTOVER) ? key_input[sub_group_broadcast(key_offset, key_row_idx) + head_idx_index + sglid] : INPUT1_VAL_ZERO;
+#else
+ INPUT1_TYPE key_packed = (sglid < HEAD_SIZE_LEFTOVER) ? key_input[key_offset + key_row_idx * key_pitch + head_idx_index + sglid] : INPUT1_VAL_ZERO;
+#endif
+#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
+ KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);j
+#elif IS_KV_COMPRESSED
+ KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
+#else
+ INPUT1_TYPE key_vals = key_packed;
+#endif
+ unroll_for (uint i = 0; i < HEAD_SIZE_LEFTOVER; i++) {
+ qk_acc[key_row_idx] = mad(sub_group_broadcast(key_vals, i), queries_vec[i], qk_acc[key_row_idx]);
+ }
+ }
+ #endif
} else if (seq_len_calc_size > 0) {
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, seq_len + min(sglid, (uint)seq_len_calc_size - 1), 0);
@@ -1120,8 +1200,9 @@ KERNEL(sdpa_opt)(
KEY_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];
#endif
#endif
+ uint head_idx_index = 0;
__attribute__((opencl_unroll_hint(1)))
- for (uint head_idx_index = 0; head_idx_index < HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
+ for (; head_idx_index + SUBGROUP_SIZE <= HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, 1, ptr, offset)
#define QUERY_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
#if IS_KV_COMPRESSED
@@ -1156,7 +1237,7 @@ KERNEL(sdpa_opt)(
key_vec[key_row_idx] *= sub_group_broadcast(comp_scale, key_row_idx);
#endif
}
-#endif
+#endif // LOAD_KEY_LEFTOVERS_IN_CALC_LOOP
unroll_for (uint key_row_idx = 0; key_row_idx < TARGET_SEQ_LEN_BLOCK_SIZE; key_row_idx++) {
#ifdef LOAD_KEY_LEFTOVERS_IN_CALC_LOOP
@@ -1166,21 +1247,46 @@ KERNEL(sdpa_opt)(
key_vals = TO_KEY_UNPACKED_TYPE(KEY_BLOCK_READ(key_input, sub_group_broadcast(key_offset, key_row_idx) + head_idx_index));
#else
key_vals = TO_KEY_UNPACKED_TYPE(KEY_BLOCK_READ(key_input, key_offset + key_row_idx * key_pitch + head_idx_index));
-#endif
+#endif // BEAM_TABLE_TYPE
}
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
key_vals = (key_vals - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
#elif IS_KV_COMPRESSED
key_vals *= sub_group_broadcast(comp_scale, key_row_idx);
#endif
-#else
- #define key_vals key_vec[key_row_idx]
-#endif
+#else // !defined(LOAD_KEY_LEFTOVERS_IN_CALC_LOOP)
+ #define key_vals key_vec[key_row_idx]
+#endif // !defined(LOAD_KEY_LEFTOVERS_IN_CALC_LOOP)
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
qk_acc[key_row_idx] = mad(sub_group_broadcast(key_vals, i), queries_vec[i], qk_acc[key_row_idx]);
}
}
}
+ #ifdef HEAD_SIZE_LEFTOVER
+ QUERY_VEC queries_vec;
+ uint query_local_offset = (head_idx_index * TARGET_SEQ_LEN_BLOCK_SIZE) + sglid;
+ unroll_for (uint q_row_idx = 0; q_row_idx < HEAD_SIZE_LEFTOVER; q_row_idx++) {
+ queries_vec[q_row_idx] = slm_query[query_local_offset];
+ query_local_offset += TARGET_SEQ_LEN_BLOCK_SIZE;
+ }
+ unroll_for (uint key_row_idx = 0; key_row_idx < TARGET_SEQ_LEN_BLOCK_SIZE; ++key_row_idx) {
+#ifdef BEAM_TABLE_TYPE
+ const INPUT1_TYPE key_packed = (sglid < HEAD_SIZE_LEFTOVER) ? key_input[sub_group_broadcast(key_offset, key_row_idx) + head_idx_index + sglid] : INPUT1_VAL_ZERO;
+#else
+ const INPUT1_TYPE key_packed = (sglid < HEAD_SIZE_LEFTOVER) ? key_input[key_offset + key_row_idx * key_pitch + head_idx_index + sglid] : INPUT1_VAL_ZERO;
+#endif
+#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
+ KEY_COMPRESSION_SCALE_TYPE key_val = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);j
+#elif IS_KV_COMPRESSED
+ KEY_COMPRESSION_SCALE_TYPE key_val = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
+#else
+ INPUT1_TYPE key_val = key_packed;
+#endif
+ unroll_for (uint i = 0; i < HEAD_SIZE_LEFTOVER; i++) {
+ qk_acc[key_row_idx] = mad(sub_group_broadcast(key_val, i), queries_vec[i], qk_acc[key_row_idx]);
+ }
+ }
+ #endif // HEAD_SIZE_LEFTOVER
}
// softmax_scale
@@ -1247,7 +1353,6 @@ KERNEL(sdpa_opt)(
// each sg will compute a whole row of query
uint aligned_width = ((SUBGROUPS_PER_WG + (SUBGROUP_SIZE-1)) & ~(SUBGROUP_SIZE-1));
for (uint m = sgid; m < seq_idx_end; m += SUBGROUPS_PER_WG) {
- // rowmax
SOFTMAX_ACCUMULATOR_TYPE qk_max_new, qk_max_cur = SOFTMAX_ACCUMULATOR_VAL_MIN;
for (uint k = sglid; k < aligned_width; k += SUBGROUP_SIZE) {
if (k < SUBGROUPS_PER_WG) {
@@ -1466,11 +1571,19 @@ KERNEL(sdpa_opt)(
#endif
#endif
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
-#ifdef BEAM_TABLE_TYPE
+ #ifdef HEAD_SIZE_LEFTOVER
+ #ifdef BEAM_TABLE_TYPE
+ const INPUT2_TYPE value_packed = (sgid < SUBGROUPS_PER_WG - 1) ? VALUE_BLOCK_READ(value_input, sub_group_broadcast(value_offset, i)) : head_size_idx < HEAD_SIZE ? value_input[sub_group_broadcast(value_offset, i)] : INPUT2_VAL_ZERO;
+ #else
+ const INPUT2_TYPE value_packed = (sgid < SUBGROUPS_PER_WG - 1) ? VALUE_BLOCK_READ(value_input, value_offset) : head_size_idx < HEAD_SIZE ? value_input[value_offset] : INPUT2_VAL_ZERO;
+ #endif
+ #else // !defined(HEAD_SIZE_LEFTOVER)
+ #ifdef BEAM_TABLE_TYPE
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, sub_group_broadcast(value_offset, i));
-#else
+ #else
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, value_offset);
-#endif
+ #endif
+ #endif
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
@@ -1489,12 +1602,11 @@ KERNEL(sdpa_opt)(
#endif
}
}
- } else {
+ } else { // partition_seq_len is less than SEQ_LEN_PARTITION_SIZE
const uint seq_len_start = (sgid / (SUBGROUPS_PER_WG / SG_SCALE_FACTOR)) * (SEQ_LEN_PARTITION_SIZE / SG_SCALE_FACTOR);
uint seq_len_end = 0;
if (seq_len_start < partition_seq_len)
seq_len_end = seq_len_start + min(partition_seq_len - seq_len_start, (uint)(SEQ_LEN_PARTITION_SIZE / SG_SCALE_FACTOR));;
-
for (uint seq_len = seq_len_start / SUBGROUP_SIZE; seq_len < seq_len_end / SUBGROUP_SIZE; seq_len++) {
#if IS_PAGED_ATTENTION
#ifdef BROADCAST_GROUP_SIZE
@@ -1507,7 +1619,7 @@ KERNEL(sdpa_opt)(
value_seq_offset * value_pitch +
heads_dim * HEAD_SIZE +
(start_partition_idx + (seq_len * SUBGROUP_SIZE)) * value_pitch + head_size_idx;
-#else
+#else // !IS_PAGED_ATTENTION
#ifdef BEAM_TABLE_TYPE
const uint b_idx = beam_table[FUNC_CALL(get_bt_index_value)(OPTIONAL_SHAPE_INFO_TENSOR b0_idx, b1_idx, 0, 0, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, sgid * SUBGROUP_SIZE)];
uint value_offset = FUNC_CALL(get_input2_index)(OPTIONAL_SHAPE_INFO_TENSOR b_idx, b1_idx, 0, 0, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, sgid * SUBGROUP_SIZE);
@@ -1527,7 +1639,7 @@ KERNEL(sdpa_opt)(
#if USE_ASYMMETRIC_QUANTIZATION
VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];
#endif
-#endif
+#endif // IS_KV_COMPRESSED
MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
@@ -1535,11 +1647,19 @@ KERNEL(sdpa_opt)(
}
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
-#ifdef BEAM_TABLE_TYPE
+ #ifdef HEAD_SIZE_LEFTOVER
+ #ifdef BEAM_TABLE_TYPE
+ const INPUT2_TYPE value_packed = (sgid < SUBGROUPS_PER_WG - 1) ? VALUE_BLOCK_READ(value_input, sub_group_broadcast(value_offset, i)) : head_size_idx < HEAD_SIZE ? value_input[sub_group_broadcast(value_offset, i)] : INPUT2_VAL_ZERO;
+ #else
+ const INPUT2_TYPE value_packed = (sgid < SUBGROUPS_PER_WG - 1) ? VALUE_BLOCK_READ(value_input, value_offset) : head_size_idx < HEAD_SIZE ? value_input[value_offset] : INPUT2_VAL_ZERO;
+ #endif
+ #else // !defined(HEAD_SIZE_LEFTOVER)
+ #ifdef BEAM_TABLE_TYPE
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, sub_group_broadcast(value_offset, i));
-#else
+ #else
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, value_offset);
-#endif
+ #endif
+ #endif
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
@@ -1576,7 +1696,8 @@ KERNEL(sdpa_opt)(
value_seq_offset * value_pitch +
heads_dim * HEAD_SIZE +
(start_partition_idx + seq_len_leftovers_start) * value_pitch + head_size_idx;
-#else
+
+#else // !IS_PAGED_ATTENTION
#ifdef BEAM_TABLE_TYPE
const uint b_idx = beam_table[FUNC_CALL(get_bt_index_value)(OPTIONAL_SHAPE_INFO_TENSOR b0_idx, b1_idx, 0, 0, start_partition_idx + seq_len_leftovers_start + sglid, sgid * SUBGROUP_SIZE)];
const uint value_offset = FUNC_CALL(get_input2_index)(OPTIONAL_SHAPE_INFO_TENSOR b_idx, b1_idx, 0, 0, start_partition_idx + seq_len_leftovers_start + sglid, sgid * SUBGROUP_SIZE);
@@ -1600,11 +1721,25 @@ KERNEL(sdpa_opt)(
#endif
for (uint seq_len_idx = 0; seq_len_idx < partition_seq_len - seq_len_leftovers_start; seq_len_idx++) {
-#ifdef BEAM_TABLE_TYPE
+ #ifdef HEAD_SIZE_LEFTOVER
+ #ifdef BEAM_TABLE_TYPE
+ const uint value_offset_seq = sub_group_broadcast(value_offset, seq_len_idx);
+ const INPUT2_TYPE value_packed = (head_size_idx <= HEAD_SIZE) ? value_input[value_offset_seq] : INPUT2_VAL_ZERO;
+ #else // !BEAM_TABLE_TYPE
+ INPUT2_TYPE value_packed;
+ if (sgid < SUBGROUPS_PER_WG - 1)
+ value_packed = VALUE_BLOCK_READ(value_input, value_offset);
+ else
+ value_packed = (sglid < HEAD_SIZE_LEFTOVER) ? value_input[value_offset] : INPUT2_VAL_ZERO;
+ #endif // BEAM_TABLE_TYPE
+
+ #else // !HEAD_SIZE_LEFTOVER
+ #ifdef BEAM_TABLE_TYPE
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, sub_group_broadcast(value_offset, seq_len_idx));
-#else
+ #else
const INPUT2_TYPE value_packed = VALUE_BLOCK_READ(value_input, value_offset);
-#endif
+ #endif
+ #endif // HEAD_SIZE_LEFTOVER
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, seq_len_idx)) * sub_group_broadcast(comp_scale, seq_len_idx);
@@ -1698,23 +1833,63 @@ KERNEL(sdpa_opt)(
const uint output_pitch = HEAD_SIZE;
#endif
+ #ifdef HEAD_SIZE_LEFTOVER
if (TARGET_SEQ_LEN_BLOCK_SIZE > seq_idx_end) {
- for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
+ if (sgid < SUBGROUPS_PER_WG - 1) {
+ for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
#if IS_FLASHATTEN_V2
- output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
+ output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
- OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
- output_offset += output_pitch;
+ OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
+ output_offset += output_pitch;
+ }
+ } else if (sglid < HEAD_SIZE_LEFTOVER) {
+ for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
+#if IS_FLASHATTEN_V2
+ output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
+#endif
+ output[output_offset + sglid] = output_acc[seq_idx];
+ output_offset += output_pitch;
+ }
}
} else {
- unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
+ if (sgid < SUBGROUPS_PER_WG - 1) {
+ unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
+#if IS_FLASHATTEN_V2
+ output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
+#endif
+ OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
+ output_offset += output_pitch;
+ }
+ } else if (sglid < HEAD_SIZE_LEFTOVER) {
+ unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
+#if IS_FLASHATTEN_V2
+ output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
+#endif
+ output[output_offset + sglid] = output_acc[seq_idx];
+ output_offset += output_pitch;
+ }
+ }
+ }
+ #else // !defined(HEAD_SIZE_LEFTOVER)
+ if (TARGET_SEQ_LEN_BLOCK_SIZE > seq_idx_end) {
+ for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
#if IS_FLASHATTEN_V2
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
output_offset += output_pitch;
}
+ } else {
+ unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
+#if IS_FLASHATTEN_V2
+ output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
+#endif
+ OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
+ output_offset += output_pitch;
+ }
}
+ #endif // HEAD_SIZE_LEFTOVER
}
}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_opt.cpp
index 7ee342849fb..55733372b0e 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_opt.cpp
@@ -37,6 +37,10 @@ static bool is_prefill_stage(const sdpa_params& sdpa_params) {
return target_seq_len > 1;
}
+static bool unaligned_head_size(const sdpa_params& sdpa_params) {
+ return sdpa_params.conf.head_size % subgroup_size != 0;
+}
+
static size_t get_partitions_num(const sdpa_params& sdpa_params, size_t kernel_type) {
if (sdpa_params.has_dynamic_tensors() || kernel_type == KernelsTypes::MULTI_TOKENS)
return 1;
@@ -106,7 +110,8 @@ static std::string GetKernelName(std::string base_name, KernelsTypes type, const
size_t SDPAKernelOpt::get_sg_number_scale_factor(const Params& params, size_t head_size, size_t kernel_type) {
const size_t optimal_scale_factor = 2;
if (kernel_type == KernelsTypes::MULTI_TOKENS) {
- if (head_size * optimal_scale_factor <= params.engineInfo.maxWorkGroupSize) {
+ size_t group_size = head_size * optimal_scale_factor;
+ if (head_size % subgroup_size == 0 && group_size <= params.engineInfo.maxWorkGroupSize) {
return optimal_scale_factor;
}
} else if (kernel_type == KernelsTypes::SINGLE_TOKEN) {
@@ -122,7 +127,7 @@ size_t SDPAKernelOpt::get_sg_number_scale_factor(const Params& params, size_t he
size_t SDPAKernelOpt::get_seq_len_partition_size(const Params& params, size_t head_size, size_t kernel_type) {
size_t seq_len = 0;
if (kernel_type == KernelsTypes::MULTI_TOKENS) {
- seq_len = head_size * get_sg_number_scale_factor(params, head_size, kernel_type);
+ seq_len = Align(head_size * get_sg_number_scale_factor(params, head_size, kernel_type), 16);
} else {
seq_len = 256;
}
@@ -158,7 +163,10 @@ bool SDPAKernelOpt::Validate(const Params& p) const {
const sdpa_params& params = static_cast<const sdpa_params&>(p);
- if (params.conf.head_size < 1 || params.conf.head_size % subgroup_size != 0)
+ if (params.conf.head_size < 1)
+ return false;
+
+ if (params.conf.is_paged_attention && unaligned_head_size(params))
return false;
if (params.conf.use_asymmetric_quantization && !params.conf.combine_scales_and_zp)
@@ -177,6 +185,8 @@ JitConstants SDPAKernelOpt::GetJitConstants(const sdpa_params& params, size_t ke
jit.AddConstant(MakeJitConstant("SUBGROUP_SIZE", subgroup_size));
jit.AddConstant(MakeJitConstant("HEAD_SIZE", config.head_size));
jit.AddConstant(MakeJitConstant("SEQ_LEN_PARTITION_SIZE", get_seq_len_partition_size(params, config.head_size, kernel_idx)));
+ if (unaligned_head_size(params))
+ jit.AddConstant(MakeJitConstant("HEAD_SIZE_LEFTOVER", config.head_size % subgroup_size));
auto target_seq_len_block_size = kernel_idx == KernelsTypes::SINGLE_TOKEN ? 1 : get_target_seq_len_block_size();
jit.AddConstant(MakeJitConstant("TARGET_SEQ_LEN_BLOCK_SIZE", target_seq_len_block_size));
@@ -269,8 +279,8 @@ CommonDispatchData SDPAKernelOpt::SetDefault(const sdpa_params& params, size_t k
const size_t sg_num_scale = get_sg_number_scale_factor(params, head_size, kernel_idx);
dispatch_data.gws = { batch_size * heads_num,
CeilDiv(target_seq_len, target_seq_len_block_size),
- head_size * sg_num_scale };
- dispatch_data.lws = { 1, 1, head_size * sg_num_scale };
+ Align(head_size * sg_num_scale, subgroup_size) };
+ dispatch_data.lws = { 1, 1, Align(head_size * sg_num_scale, subgroup_size) };
} else if (kernel_idx == KernelsTypes::FINALIZATION) {
dispatch_data.gws = { batch_size * heads_num,
target_seq_len,
@@ -290,7 +300,11 @@ KernelsData SDPAKernelOpt::GetKernelsData(const Params& params) const {
std::vector<KernelsTypes> kernels_type;
const auto& prim_params = static_cast<const sdpa_params&>(params);
- if (prim_params.conf.is_paged_attention) {
+ if (prim_params.conf.is_paged_attention || unaligned_head_size(prim_params)) {
+ // TODO: Unaligned head size is currently supported by only multi tokens kernel.
+ // So far this case was observed only from the non-lm models such as vision embedding model.
+ // If we need to optimize unaligned head size SDPA for 2nd+ token phase of LM model,
+ // we'll need to fix single_token kernel to support unaligned head size.
kernels_type = { KernelsTypes::MULTI_TOKENS };
} else if (params.is_shape_agnostic) {
kernels_type = { KernelsTypes::SINGLE_TOKEN, KernelsTypes::MULTI_TOKENS, KernelsTypes::FINALIZATION };
@@ -399,12 +413,11 @@ void SDPAKernelOpt::GetUpdateDispatchDataFunc(KernelData& kd) const {
const auto& prim_params = static_cast<const sdpa_params&>(params);
const size_t paged_attention_kernels_num = 1;
- const size_t expected_kernels_num = prim_params.conf.is_paged_attention ? paged_attention_kernels_num
- : KernelsTypes::TOTAL_KERNELS_NUM;
- OPENVINO_ASSERT(kernel_data.kernels.size() == expected_kernels_num,
- "[GPU] Invalid kernels size for update dispatch data func of SDPA kernel");
+ const size_t expected_kernels_num =
+ prim_params.conf.is_paged_attention || unaligned_head_size(prim_params) ? paged_attention_kernels_num : KernelsTypes::TOTAL_KERNELS_NUM;
+ OPENVINO_ASSERT(kernel_data.kernels.size() == expected_kernels_num, "[GPU] Invalid kernels size for update dispatch data func of SDPA kernel");
- if (prim_params.conf.is_paged_attention) {
+ if (prim_params.conf.is_paged_attention || unaligned_head_size(prim_params)) {
auto dispatch_data = SetDefault(prim_params, KernelsTypes::MULTI_TOKENS);
kernel_data.kernels[0].params.workGroups.global = dispatch_data.gws;
kernel_data.kernels[0].params.workGroups.local = dispatch_data.lws;
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 662eb7c9eac..ab130d154b1 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -526,13 +526,18 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
// - Head size should be 128 for any model type; or should be in the range of 64 to 256 for stateful LLMs because of performance reasons.
// This limitations is recommended to prevent performance drop in models with small head size, such as SD,
// until the SDPA operation is optimized for these cases
- const auto optimal_subgroup_size = 16;
- bool valid_head_size = head_size % optimal_subgroup_size == 0;
- valid_head_size &= (head_size >= 64 && head_size <= 256);
+ bool valid_head_size = (head_size >= 64 && head_size <= 256);
if (!valid_head_size) {
return false;
}
+ const auto optimal_subgroup_size = 16;
+ // sdpa_opt is not supporting compressed KV yet for unaligned head size
+ if (head_size % optimal_subgroup_size != 0) {
+ if (ov::element::Type(sdpa->get_input_element_type(1)).size() < 2 || ov::element::Type(sdpa->get_input_element_type(2)).size() < 2) {
+ return false;
+ }
+ }
return true;
});
diff --git a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
index 799977f5de3..ef3f4fa9cae 100644
--- a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
+++ b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
@@ -207,22 +207,40 @@ void ScaledAttnLayerGPUTest::transpose_prepare(std::vector<InputShape>& shapes,
}
void ScaledAttnLayerGPUTest::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
+ const auto& model_inputs = function->inputs();
+ inputs.clear();
std::vector<ov::Shape> shapes(3);
- shapes[0] = targetInputStaticShapes[0];
- shapes[1] = targetInputStaticShapes[1];
- shapes[2] = targetInputStaticShapes[2];
+ {
+ // Generate QKV
+ for (int i = 0; i < 3; ++i) {
+ shapes[i] = targetInputStaticShapes[i];
+ ov::test::utils::InputGenerateData data(0, 8, 32);
+ ov::Tensor data_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[i], data);
+ inputs.insert({model_inputs[i].get_node_shared_ptr(), data_tensor});
+ }
+ }
+ ov::test::utils::InputGenerateData attn_data(-1.0f, 2, 1);
+ ov::test::utils::InputGenerateData scale_data(0.1f, 1, 10);
if (!has_attn && has_scale) {
shapes.push_back(ov::Shape{});
shapes.push_back(ov::Shape{1});
+ ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[3], attn_data);
+ inputs.insert({model_inputs[3].get_node_shared_ptr(), attn_tensor});
+ ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[4], scale_data);
+ inputs.insert({model_inputs[4].get_node_shared_ptr(), scale_tensor});
} else {
+ int idx = 3;
if (has_attn) {
shapes.push_back(targetInputStaticShapes[3]);
+ ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[idx], attn_data);
+ inputs.insert({model_inputs[idx++].get_node_shared_ptr(), attn_tensor});
}
if (has_scale) {
shapes.push_back(ov::Shape{1});
+ ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[idx], scale_data);
+ inputs.insert({model_inputs[idx].get_node_shared_ptr(), scale_tensor});
}
}
- SubgraphBaseTest::generate_inputs(shapes);
}
TEST_P(ScaledAttnLayerGPUTest, CompareWithRefs) {
@@ -236,7 +254,7 @@ TEST_P(ScaledAttnLayerGPUTest, CompareWithRefs) {
run();
}
-const std::vector<std::vector<InputShape>> shapes{
+const std::vector<std::vector<InputShape>> shapes {
// normal case, shapes of q,k,v are same
{
// q shape
@@ -309,7 +327,22 @@ const std::vector<std::vector<InputShape>> shapes{
{ov::test::InputShape{ov::PartialShape{-1, 1, -1, -1},
{ov::Shape{1, 1, 7, 7}, ov::Shape{1, 1, 1, 1}, ov::Shape{2, 1, 10, 10}}}
},
- }
+ },
+ // head size not aligned to 16
+ {
+ // q shape
+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 72},
+ {ov::Shape{1, 8, 100, 72}, ov::Shape{1, 8, 32, 72}, ov::Shape{1, 8, 1, 72}}}
+ },
+ // kv shape
+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 72},
+ {ov::Shape{1, 8, 100, 72}, ov::Shape{1, 8, 32, 72}, ov::Shape{1, 8, 1, 72}}}
+ },
+ // attn shape: [B, 1, -1, L0+L1]
+ {ov::test::InputShape{ov::PartialShape{-1, 1, -1, -1},
+ {ov::Shape{1, 1, 100, 100}, ov::Shape{1, 1, 32, 32}, ov::Shape{1, 1, 1, 1}}}
+ },
+ },
};
const std::vector<std::vector<int64_t>> disable_transpose{};
@@ -358,6 +391,34 @@ const std::vector<std::vector<InputShape>> static_shapes{
{ov::Shape{1, 1, 64, 13}}}
},
},
+ {
+ // q shape
+ {ov::test::InputShape{ov::PartialShape{1, 8, 100, 72},
+ {ov::Shape{1, 8, 100, 72}}}
+ },
+ // kv shape
+ {ov::test::InputShape{ov::PartialShape{1, 8, 100, 72},
+ {ov::Shape{1, 8, 100, 72}}}
+ },
+ // attn shape: [B, 1, -1, L0+L1]
+ {ov::test::InputShape{ov::PartialShape{1, 1, 100, 100},
+ {ov::Shape{1, 1, 100, 100}}}
+ },
+ },
+ {
+ // q shape
+ {ov::test::InputShape{ov::PartialShape{1, 8, 1, 72},
+ {ov::Shape{1, 8, 1, 72}}}
+ },
+ // kv shape
+ {ov::test::InputShape{ov::PartialShape{1, 8, 100, 72},
+ {ov::Shape{1, 8, 100, 72}}}
+ },
+ // attn shape: [B, 1, -1, L0+L1]
+ {ov::test::InputShape{ov::PartialShape{1, 1, 1, 100},
+ {ov::Shape{1, 1, 1, 100}}}
+ },
+ },
};
const auto static_shape_params = testing::Combine(testing::Values(ov::element::f16),