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] Optimize sliding window attention for large-input decoding (#37064)
hyunback kimf84db88dc3fb
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_attention_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_attention_opt.clindex b60a34f0c9a..710f36356de 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_attention_opt.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_attention_opt.cl@@ -179,12 +179,22 @@ KERNEL(pa_sdpa_opt)(const uint partition_idx = get_group_id(2);- if (partition_idx * SEQ_LEN_PARTITION_SIZE >= seq_len) {+ const uint total_blocks_num = CEIL_DIV(seq_len, PAGED_ATTENTION_BLOCK_SIZE);++#if SWA_BLOCK_SKIP_ENABLED && !MULTI_TOKENS_PROCESSING+ const uint swa_start_block = (seq_len > SLIDING_WINDOW_SIZE) ? ((seq_len - SLIDING_WINDOW_SIZE) / PAGED_ATTENTION_BLOCK_SIZE) : 0;+ const uint swa_start_token = swa_start_block * PAGED_ATTENTION_BLOCK_SIZE;+ const uint effective_blocks_num = total_blocks_num - swa_start_block;+#else+ const uint swa_start_block = 0;+ const uint swa_start_token = 0;+ const uint effective_blocks_num = total_blocks_num;+#endif++ if (partition_idx * SEQ_LEN_PARTITION_SIZE >= effective_blocks_num * PAGED_ATTENTION_BLOCK_SIZE) {return;}- const uint total_blocks_num = CEIL_DIV(seq_len, PAGED_ATTENTION_BLOCK_SIZE);-#ifdef STORE_QUERY_TO_SLM// SLM buffer for query inputs__local INPUT0_TYPE slm_query[K_HEAD_SIZE * QUERIES_PER_WI];@@ -247,17 +257,18 @@ KERNEL(pa_sdpa_opt)(}#endif // STORE_QUERY_TO_SLM- const uint blocks_num_per_partition = min(total_blocks_num - partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION, (uint)PAGED_ATTENTION_BLOCKS_PER_PARTITION);+ const uint blocks_num_per_partition = min(effective_blocks_num - partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION, (uint)PAGED_ATTENTION_BLOCKS_PER_PARTITION);uint blocks_num = blocks_num_per_partition / SUBGROUPS_PER_WG;if (sgid < blocks_num_per_partition % SUBGROUPS_PER_WG)blocks_num++;- const uint start_block_idx = block_indices_begins[subsequence_idx] + partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION + sgid;+ const uint start_block_idx = block_indices_begins[subsequence_idx] + swa_start_block + partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION + sgid;for (uint block_num = 0; block_num < blocks_num; block_num++) {const uint head_idx = head_num_idx / KV_HEADS_GROUP_SIZE;const uint block_indice = block_indices[start_block_idx + block_num * SUBGROUPS_PER_WG];+SOFTMAX_ACCUMULATOR_VEC_TYPE qk_acc = SOFTMAX_ACCUMULATOR_VAL_ZERO;#define KEY_VEC_SIZE SUBGROUP_SIZE@@ -420,7 +431,7 @@ KERNEL(pa_sdpa_opt)(}#endif // !(IS_KV_COMPRESSED && IS_INT4_COMPRESSED)- const uint token_idx = partition_idx * SEQ_LEN_PARTITION_SIZE + block_num * SUBGROUPS_PER_WG * SUBGROUP_SIZE + sgid * SUBGROUP_SIZE + sglid;+ const uint token_idx = swa_start_token + partition_idx * SEQ_LEN_PARTITION_SIZE + block_num * SUBGROUPS_PER_WG * SUBGROUP_SIZE + sgid * SUBGROUP_SIZE + sglid;#ifdef HAS_ALIBIconst int alibi_val = (1 - seq_len) + token_idx;@@ -503,8 +514,8 @@ KERNEL(pa_sdpa_opt)(const uint qk_iters_num = CEIL_DIV(SEQ_LEN_PARTITION_SIZE, SUBGROUPS_PER_WG * SUBGROUP_SIZE);for (uint qk_idx = 0; qk_idx < qk_iters_num; qk_idx++) {const uint local_data_idx = qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;- // TODO: const uint global_data_idx = partition_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx- const uint global_data_idx = partition_idx * SEQ_LEN_PARTITION_SIZE + qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;+ // TODO: const uint global_data_idx = swa_start_token + partition_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx+ const uint global_data_idx = swa_start_token + partition_idx * SEQ_LEN_PARTITION_SIZE + qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;#if (SEQ_LEN_PARTITION_SIZE % (SUBGROUPS_PER_WG * SUBGROUP_SIZE)) == 0if (global_data_idx < seq_len) {@@ -552,7 +563,7 @@ KERNEL(pa_sdpa_opt)(for (uint qk_idx = 0; qk_idx < qk_iters_num; qk_idx++) {const uint local_data_idx = qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;- const uint global_data_idx = partition_idx * SEQ_LEN_PARTITION_SIZE + qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;+ const uint global_data_idx = swa_start_token + partition_idx * SEQ_LEN_PARTITION_SIZE + qk_idx * (SUBGROUPS_PER_WG * SUBGROUP_SIZE) + sgid * SUBGROUP_SIZE + sglid;#if (SEQ_LEN_PARTITION_SIZE % (SUBGROUPS_PER_WG * SUBGROUP_SIZE)) == 0if (global_data_idx < seq_len) {@@ -564,6 +575,10 @@ KERNEL(pa_sdpa_opt)(SOFTMAX_ACCUMULATOR_TYPE qk_new = TO_SOFTMAX_ACCUMULATOR_TYPE(slm_qk_vals[slm_idx]) / GET_VECTOR_ELEMENT(exp_sum, q_idx);slm_qk_vals[slm_idx] = qk_new;}+ } else if (local_data_idx < SEQ_LEN_PARTITION_SIZE) {+ unroll_for (uint q_idx = 0; q_idx < QUERIES_PER_WI; q_idx++) {+ slm_qk_vals[q_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx] = 0;+ }}}@@ -643,7 +658,12 @@ KERNEL(pa_sdpa_opt)(MAKE_VECTOR_TYPE(OUTPUT_TYPE, QUERIES_PER_WI) acc = OUTPUT_VAL_ZERO;#endif- const uint partition_seq_len = min(seq_len - partition_idx * SEQ_LEN_PARTITION_SIZE, (uint)SEQ_LEN_PARTITION_SIZE);+#if SWA_BLOCK_SKIP_ENABLED && !MULTI_TOKENS_PROCESSING+ const uint effective_seq_len = min(effective_blocks_num * PAGED_ATTENTION_BLOCK_SIZE, seq_len);+#else+ const uint effective_seq_len = seq_len;+#endif+ const uint partition_seq_len = min(effective_seq_len - partition_idx * SEQ_LEN_PARTITION_SIZE, (uint)SEQ_LEN_PARTITION_SIZE);#if SG_SCALE_FACTOR > 1const uint block_start_idx = (sgid / (SUBGROUPS_PER_WG / SG_SCALE_FACTOR)) * (SEQ_LEN_PARTITION_SIZE / SG_SCALE_FACTOR / SUBGROUP_SIZE);@@ -653,7 +673,7 @@ KERNEL(pa_sdpa_opt)(const uint block_end_idx = partition_seq_len / SUBGROUP_SIZE;#endif- uint blocks_num_per_partition = min(total_blocks_num - partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION, (uint)PAGED_ATTENTION_BLOCKS_PER_PARTITION);+ uint blocks_num_per_partition = min(effective_blocks_num - partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION, (uint)PAGED_ATTENTION_BLOCKS_PER_PARTITION);uint leftovers = blocks_num_per_partition * PAGED_ATTENTION_BLOCK_SIZE - partition_seq_len;if (leftovers != 0) {@@ -661,7 +681,7 @@ KERNEL(pa_sdpa_opt)(blocks_num_per_partition = blocks_num_per_partition - 1;}- const uint start_block_idx = block_indices_begins[subsequence_idx] + partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION;+ const uint start_block_idx = block_indices_begins[subsequence_idx] + swa_start_block + partition_idx * PAGED_ATTENTION_BLOCKS_PER_PARTITION;for (uint block_num = block_start_idx; block_num < block_end_idx; block_num++) {const uint head_idx = head_num_idx / KV_HEADS_GROUP_SIZE;@@ -669,6 +689,7 @@ KERNEL(pa_sdpa_opt)(const uint value_offset = block_offset + head_size_idx;+#if IS_KV_COMPRESSEDconst uint packed_block_offset = block_indices[start_block_idx + block_num] * KV_HEADS_NUM * phys_adjusted_v_head_size * PAGED_ATTENTION_BLOCK_SIZE+ head_idx * phys_adjusted_v_head_size * PAGED_ATTENTION_BLOCK_SIZE;@@ -898,7 +919,7 @@ KERNEL(pa_sdpa_opt)(#endif#ifdef USE_DUAL_NIBBLE_V_OPT- if (seq_len > SEQ_LEN_PARTITION_SIZE) {+ if (seq_len > SEQ_LEN_PARTITION_SIZE && total_partitions_num > 1) {unroll_for (uint q_idx = 0; q_idx < HEADS_PER_WI; q_idx++) {#if HEADS_LEFTOVERS_NUM > 0if (q_idx >= iter_heads_num)@@ -925,7 +946,7 @@ KERNEL(pa_sdpa_opt)(}}#else // !USE_DUAL_NIBBLE_V_OPT- if (seq_len > SEQ_LEN_PARTITION_SIZE) {+ if (seq_len > SEQ_LEN_PARTITION_SIZE && total_partitions_num > 1) {unroll_for (uint q_idx = 0; q_idx < HEADS_PER_WI; q_idx++) {#if HEADS_LEFTOVERS_NUM > 0if (q_idx >= iter_heads_num)@@ -1007,9 +1028,9 @@ KERNEL(pa_sdpa_finalization_stage)(const uint seq_len = past_lens[seq_idx] + 1;#endif- const uint num_of_partitions = CEIL_DIV(seq_len, SEQ_LEN_PARTITION_SIZE);+ const uint num_of_partitions = min((uint)CEIL_DIV(seq_len, SEQ_LEN_PARTITION_SIZE), total_partitions_num);- if (seq_len <= SEQ_LEN_PARTITION_SIZE) {+ if (num_of_partitions <= 1) {/* Short path, no need any actions for currently processing sequence */return;} else if (num_of_partitions <= SUBGROUP_SIZE * REG_VERSION_MAX_VALUES_PER_WI) {@@ -1150,7 +1171,7 @@ KERNEL(pa_sdpa_scores_calculation)(const int subsequence_end = subsequence_begins[subsequence_idx + 1];const uint seq_len = (subsequence_end - subsequence_begin) + past_lens[subsequence_idx];- const uint num_of_partitions = CEIL_DIV(seq_len, partition_size);+ const uint num_of_partitions = min((uint)CEIL_DIV(seq_len, partition_size), max_partitions_num);if (partition_idx >= num_of_partitions)return;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cppindex 1004eeb79f0..35478c18c0c 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/paged_attention_opt.cpp@@ -297,6 +297,7 @@ public:jit.make("PAGED_ATTENTION_BLOCK_SIZE", paged_attention_block_size);jit.make("SUBGROUP_SIZE", subgroup_size);jit.make("SLIDING_WINDOW_SIZE", desc->sliding_window);+ jit.make("SWA_BLOCK_SKIP_ENABLED", desc->sliding_window > 0 && !desc->has_scores_output());const auto kv_cache_dt = params.get_program().get_config().get_kv_cache_precision();const bool is_kv_compressed = get_kv_compressed(params);@@ -1475,7 +1476,17 @@ public:rt_params->max_context_len = get_max_context_len(params);rt_params->stage = stage;rt_params->partition_size = get_partitioning_size(params, desc->v_head_size, rt_params->stage);- rt_params->num_of_partitions = ceil_div(rt_params->max_context_len, rt_params->partition_size);++ auto effective_context_len = rt_params->max_context_len;+ // scores_output is only used in SnapKV path, and it doesn't yet handle the SWA block skip offset+ if (desc->sliding_window > 0 && rt_params->stage == PagedAttentionStage::GENERATE && !desc->has_scores_output()) {+ auto total_blocks = ceil_div(rt_params->max_context_len, paged_attention_block_size);+ auto swa_start_block =+ rt_params->max_context_len > desc->sliding_window ? (rt_params->max_context_len - desc->sliding_window) / paged_attention_block_size : 0;+ auto effective_blocks = total_blocks - swa_start_block;+ effective_context_len = effective_blocks * paged_attention_block_size;+ }+ rt_params->num_of_partitions = ceil_div(effective_context_len, rt_params->partition_size);if ((rt_params->stage == PagedAttentionStage::PREFILL || rt_params->stage == PagedAttentionStage::MIXED) && !params.is_dynamic())rt_params->paged_attention_aligned_seq_len = static_cast<size_t>(get_aligned_seq_len(params, rt_params->stage));diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cppindex 20957895997..d6c16b2a032 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_attention_gpu_test.cpp@@ -386,6 +386,13 @@ INSTANTIATE_TEST_SUITE_P(smoke_paged_attention, paged_attention_test, ::testing:paged_attention_test_params{ {{1, 4200}}, 8, 2, 64, 64, 16, 8, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA 8/2 + scores + sw=8, kv_group_size=4 -> QUERIES_PER_WI=4paged_attention_test_params{ {{1, 4200}}, 4, 2, 64, 64, 16, 8, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA 4/2 + scores + sw=8, kv_group_size=2 -> QUERIES_PER_WI=2paged_attention_test_params{ {{1, 5000}}, 16, 4, 128, 128, 16, 16, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA 16/4 + scores + sw=16, k_head_size=128, kv_group_size=4++ /* SWA block skip: decode with past_len >> sliding_window */+ paged_attention_test_params{ {{1, 1024}}, 2, 2, 64, 64, 16, 32, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // decode, past_len=1024 >> sw=32+ paged_attention_test_params{ {{1, 2048}}, 8, 2, 128, 128, 16, 512, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA decode, Gemma4-like config (sw=512)+ paged_attention_test_params{ {{1, 4096}}, 8, 2, 128, 128, 16, 512, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA decode, triggers GQA kernel path (context>=4096)+ paged_attention_test_params{ {{1, 2048}}, 8, 2, 128, 128, 16, 512, ENABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // GQA decode + KV compression + SWA+ paged_attention_test_params{ {{1, 1024}, {1, 2048}}, 8, 2, 128, 128, 16, 512, DISABLE_CACHE_COMPRESSION, ov::internal::CacheQuantMode::BY_TOKEN, STATIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2, false, 0, {}, false }, // multi-seq GQA decode with SWA}));INSTANTIATE_TEST_SUITE_P(smoke_cm_xattention, xattention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{