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
Fix for FlashAtten V2 online softmax tricks accuracy issue with … (#29803)
cecilia peng6de6703d55d6
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hppindex 78c3ed845ce..b940a6d2b6e 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/internal_properties.hpp@@ -167,7 +167,7 @@ static constexpr Property<bool, ov::PropertyMutability::RW> asym_dynamic_quantizstatic constexpr Property<ShapePredictor::Settings, ov::PropertyMutability::RW> shape_predictor_settings{"GPU_SHAPE_PREDICTOR_SETTINGS"};static constexpr Property<std::vector<std::string>, ov::PropertyMutability::RW> load_dump_raw_binary{"GPU_LOAD_DUMP_RAW_BINARY"};static constexpr Property<std::vector<std::string>, ov::PropertyMutability::RW> start_after_processes{"GPU_START_AFTER_PROCESSES"};-+static constexpr Property<bool, ov::PropertyMutability::RW> could_use_flashattn_v2{"GPU_COULD_USE_FLASHATTN_V2"};} // namespace ov::intel_gpunamespace cldnn {diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inlindex e55711b6c5e..5c255f87895 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl@@ -48,6 +48,7 @@ OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, use_cm, true, "Enable/Disable COV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, max_kernels_per_batch, 8, "Controls how many kernels we combine into batch for more efficient ocl compilation")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, impls_cache_capacity, 300, "Controls capacity of LRU implementations cache that is created for each program object for dynamic models")OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, asym_dynamic_quantization, false, "Enforce asymmetric mode for dynamically quantized activations")+OV_CONFIG_RELEASE_INTERNAL_OPTION(ov::intel_gpu, could_use_flashattn_v2, true, "Enable/Disable SDPA primitive executing with FlashAttenV2 online softmax tricks.")OV_CONFIG_DEBUG_GLOBAL_OPTION(ov::intel_gpu, help, false, "Print help message for all config options")OV_CONFIG_DEBUG_GLOBAL_OPTION(ov::intel_gpu, verbose, 0, "Enable logging for debugging purposes. The higher value the more verbose output. 0 - Disabled, 4 - Maximum verbosity")diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cppindex 4559a2e16ce..d348ea59313 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/paged_attention.cpp@@ -853,6 +853,8 @@ struct paged_attention_impl : multi_stage_primitive<paged_attention> {params.set_dynamic_shape_offsets(in_tensor_to_offset_map, out_tensor_to_offset_map);+ params.could_use_flashattn_v2 = impl_param.get_program().get_config().get_could_use_flashattn_v2();+return params;}diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/scaled_dot_product_attention.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/scaled_dot_product_attention.cppindex 58db5e9cd61..bf9751f2f6f 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl/scaled_dot_product_attention.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/scaled_dot_product_attention.cpp@@ -384,6 +384,8 @@ public:params.beam_table.SetDynamicShapeOffset(in_offsets_map.at(get_beam_table_id(desc)));}+ params.could_use_flashattn_v2 = impl_param.get_program().get_config().get_could_use_flashattn_v2();+return params;}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.clindex d710827ab8a..7cdf0903dba 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@@ -894,12 +894,22 @@ KERNEL(sdpa_opt)(__local OUTPUT_TYPE slm_qk_vals[TARGET_SEQ_LEN_BLOCK_SIZE][SEQ_LEN_PARTITION_SIZE];// SLM buffers for SoftMax calculation and qk_max/qk_sums results aggregation across all WGs+#if IS_FLASHATTEN_V2__local SOFTMAX_ACCUMULATOR_TYPE slm_qk_max_vals[TARGET_SEQ_LEN_BLOCK_SIZE][SUBGROUPS_PER_WG];+#else+ __local SOFTMAX_ACCUMULATOR_TYPE slm_qk_max_vals[SUBGROUPS_PER_WG][TARGET_SEQ_LEN_BLOCK_SIZE];+ __local SOFTMAX_ACCUMULATOR_TYPE slm_exp_sum_vals[SUBGROUPS_PER_WG * TARGET_SEQ_LEN_BLOCK_SIZE];+#endif// SLM buffers for SoftMax recalculation for current iteration based on the previous results__local SOFTMAX_ACCUMULATOR_TYPE slm_exp_sum_prev[TARGET_SEQ_LEN_BLOCK_SIZE];__local SOFTMAX_ACCUMULATOR_TYPE slm_max_val_prev[TARGET_SEQ_LEN_BLOCK_SIZE];+#if IS_FLASHATTEN_V2__local SOFTMAX_ACCUMULATOR_TYPE slm_update_factor[TARGET_SEQ_LEN_BLOCK_SIZE];+#else+ __local SOFTMAX_ACCUMULATOR_TYPE slm_exp_sum_cur[TARGET_SEQ_LEN_BLOCK_SIZE];+ __local SOFTMAX_ACCUMULATOR_TYPE slm_max_val_cur[TARGET_SEQ_LEN_BLOCK_SIZE];+#endif#if IS_PAGED_ATTENTIONconst uint block_start_pos = blocked_indexes_start[target_seq_dim];@@ -1015,7 +1025,7 @@ KERNEL(sdpa_opt)(const uint partition_seq_len = min((uint)SOURCE_SEQ_LEN - start_partition_idx, (uint)SEQ_LEN_PARTITION_SIZE);#endif-MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;+ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;#if IS_CAUSALif (seq_len <= target_seq_idx) { // keep tril i.e. m >= n#endif@@ -1173,52 +1183,67 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZER}}+ // softmax_scale{SOFTMAX_ACCUMULATOR_TYPE qk_max = SOFTMAX_ACCUMULATOR_VAL_MIN;unroll_for (uint i = 0; i < TARGET_SEQ_LEN_BLOCK_SIZE; i++) {#if IS_CAUSAL- // casual mask: valid only if m >= n+ // casual mask: valid only if m >= n#if defined(IS_PAGED_ATTENTION) && SLIDING_WINDOW_SIZE != 0- if ((seq_len + i <= target_seq_idx + sglid) && (target_seq_idx + sglid < SLIDING_WINDOW_SIZE || seq_len + i >= target_seq_idx + sglid - SLIDING_WINDOW_SIZE)) {+ if ((seq_len + i <= target_seq_idx + sglid) && (target_seq_idx + sglid < SLIDING_WINDOW_SIZE || seq_len + i >= target_seq_idx + sglid - SLIDING_WINDOW_SIZE)) {#else- if (seq_len + i <= target_seq_idx + sglid) {+ if (seq_len + i <= target_seq_idx + sglid) {#endif#endif // IS_CAUSAL#if !APPLY_SCALES_TO_QUERY#if HAS_SCALE_INPUT- const OUTPUT_TYPE scale_val = *scale;+ const OUTPUT_TYPE scale_val = *scale;#else- const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);-#endif- qk_acc[i] *= scale_val;+ const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);#endif+ qk_acc[i] *= scale_val;+#endif // !APPLY_SCALES_TO_QUERY#ifdef HAS_ALIBI- const int alibi_val = (1 - SOURCE_SEQ_LEN) + seq_len + i;- qk_acc[i] += alibi_slopes[num_heads_dim] * alibi_val;+ const int alibi_val = (1 - SOURCE_SEQ_LEN) + seq_len + i;+ qk_acc[i] += alibi_slopes[num_heads_dim] * alibi_val;#endif- qk_acc[i] = INPUT0_MIN_FUNC(INPUT0_MAX_FUNC(qk_acc[i], INPUT0_VAL_MIN), INPUT0_VAL_MAX);+ qk_acc[i] = INPUT0_MIN_FUNC(INPUT0_MAX_FUNC(qk_acc[i], INPUT0_VAL_MIN), INPUT0_VAL_MAX);#if IS_CAUSAL- } else {- qk_acc[i] = INPUT0_VAL_MIN;- }+ } else {+ qk_acc[i] = INPUT0_VAL_MIN;+ }#endif // IS_CAUSALqk_max = SOFTMAX_ACCUMULATOR_MAX_FUNC(qk_max, TO_SOFTMAX_ACCUMULATOR_TYPE(qk_acc[i]));+#if IS_FLASHATTEN_V2slm_qk_vals[sglid][sgid * TARGET_SEQ_LEN_BLOCK_SIZE + i] = qk_acc[i];+#endif+ } // unroll_for+ {+#if IS_FLASHATTEN_V2+ slm_qk_max_vals[sglid][sgid] = qk_max;+#else+ slm_qk_max_vals[sgid][sglid] = qk_max;+ qk_max = SOFTMAX_ACCUMULATOR_VAL_MIN; // sounds no need to reset?+#endif}- slm_qk_max_vals[sglid][sgid] = qk_max;- }+ } // end of softmax_scale#if IS_CAUSAL} else { // skip triu+#if IS_FLASHATTEN_V2slm_qk_max_vals[sglid][sgid] = SOFTMAX_ACCUMULATOR_VAL_MIN;- }+#else+ slm_qk_max_vals[sgid][sglid] = SOFTMAX_ACCUMULATOR_VAL_MIN;#endif+ }+#endif // IS_CAUSALbarrier(CLK_LOCAL_MEM_FENCE);+ // SoftMax calculation+#if IS_FLASHATTEN_V2{- // SoftMax calculation// each sg will compute a whole row of queryuint aligned_width = ((SUBGROUPS_PER_WG + (SUBGROUP_SIZE-1)) & ~(SUBGROUP_SIZE-1));for (uint m = sgid; m < seq_idx_end; m += SUBGROUPS_PER_WG) {@@ -1247,6 +1272,109 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERexp_sum_new = sub_group_reduce_add(exp_sum_new);#if PAGED_ATTENTION_SCORES_OUTPUT+ {+ const uint subsequence_idx = gws_seq_indexes_correspondence[target_seq_dim];+ const uint subsequence_end_pos = subsequence_begins[subsequence_idx + 1];++ // PagedAttention is supposed to save only last "row" of the QK matrix multiplication,+ // so save SEQ_LEN_PARTITION_SIZE elements for each partition+ if (subsequence_end_pos == block_end_pos) {+ const uint last_row_idx = block_end_pos - block_start_pos - 1;+ if (m == last_row_idx) {+ const uint partition_idx = start_partition_idx / SEQ_LEN_PARTITION_SIZE;++ SOFTMAX_ACCUMULATOR_TYPE correction_factor = native_exp(qk_max_new - qk_max_cur);++ if (sglid == 0) {+ const uint max_partitions_num = aligned_max_context_len / SEQ_LEN_PARTITION_SIZE;+ const uint exp_sums_output_offset = subsequence_idx * NUM_HEADS * max_partitions_num ++ num_heads_dim * max_partitions_num ++ partition_idx;+ exp_sums[exp_sums_output_offset] = exp_sum_new * correction_factor;+ max_logits[exp_sums_output_offset] = qk_max_cur;+ }++ const uint output_offset = subsequence_idx * NUM_HEADS * aligned_max_context_len ++ num_heads_dim * aligned_max_context_len ++ partition_idx * SEQ_LEN_PARTITION_SIZE;+ for (uint i = sglid; i < partition_seq_len; i += SUBGROUP_SIZE) {+ softmax_results[output_offset + i] = TO_SOFTMAX_ACCUMULATOR_TYPE(slm_qk_vals[m][i]) / exp_sum_new;+ }+ }+ }+ }+#endif /*end of PAGED_ATTENTION_SCORES_OUTPUT*/++ // update+ if (sglid == 0) {+ SOFTMAX_ACCUMULATOR_TYPE pre_exp_sum = slm_exp_sum_prev[m];+ SOFTMAX_ACCUMULATOR_TYPE correction_factor = native_exp(max_val_prev - qk_max_new);+ SOFTMAX_ACCUMULATOR_TYPE pre_exp_sum_fixed = pre_exp_sum * correction_factor;+ exp_sum_new += pre_exp_sum_fixed;++ slm_update_factor[m] = correction_factor;+ slm_max_val_prev[m] = qk_max_new;+ slm_exp_sum_prev[m] = exp_sum_new;+ }+ }+ }+#else /*!IS_FLASHATTEN_V2*/+ {+ SOFTMAX_ACCUMULATOR_TYPE qk_max_new = SOFTMAX_ACCUMULATOR_VAL_MIN;++ for (uint i = 0; i < SUBGROUPS_PER_WG; i++) {+ SOFTMAX_ACCUMULATOR_TYPE qk_max_val = slm_qk_max_vals[i][sglid];+ qk_max_new = SOFTMAX_ACCUMULATOR_MAX_FUNC(qk_max_new, qk_max_val);+ }++ if (sgid == 0) {+ slm_max_val_cur[sglid] = qk_max_new;+ }++ SOFTMAX_ACCUMULATOR_TYPE exp_sum_new = SOFTMAX_ACCUMULATOR_VAL_ZERO;++ for (uint i = 0; i < TARGET_SEQ_LEN_BLOCK_SIZE; i++) {+ qk_acc[i] = native_exp(TO_SOFTMAX_ACCUMULATOR_TYPE(qk_acc[i]) - qk_max_new);+#if IS_CAUSAL+#if defined(IS_PAGED_ATTENTION) && SLIDING_WINDOW_SIZE != 0+ if ((seq_len + i <= target_seq_idx + sglid) && (target_seq_idx + sglid < SLIDING_WINDOW_SIZE || seq_len + i >= target_seq_idx + sglid - SLIDING_WINDOW_SIZE)) {+#else+ if (seq_len + i <= target_seq_idx + sglid) {+#endif+ exp_sum_new += qk_acc[i];+ }+# else+ exp_sum_new += qk_acc[i];+#endif+ }++ {+ slm_exp_sum_vals[sgid * SUBGROUP_SIZE + sglid] = exp_sum_new;+ }++ exp_sum_new = SOFTMAX_ACCUMULATOR_VAL_ZERO;++ barrier(CLK_LOCAL_MEM_FENCE);++ for (uint i = 0; i < SUBGROUPS_PER_WG; i++) {+ SOFTMAX_ACCUMULATOR_TYPE exp_sum = slm_exp_sum_vals[i * SUBGROUP_SIZE + sglid];+ exp_sum_new += exp_sum;+ }++ for (uint i = 0; i < TARGET_SEQ_LEN_BLOCK_SIZE; i++) {+ qk_acc[i] = qk_acc[i] / exp_sum_new;+ }++ if (sgid == 0) {+ slm_exp_sum_cur[sglid] = exp_sum_new;+ }++ for (uint i = 0; i < TARGET_SEQ_LEN_BLOCK_SIZE; i++) {+ slm_qk_vals[sglid][sgid * TARGET_SEQ_LEN_BLOCK_SIZE + i] = qk_acc[i];+ }++#if PAGED_ATTENTION_SCORES_OUTPUT+ {const uint subsequence_idx = gws_seq_indexes_correspondence[target_seq_dim];const uint subsequence_end_pos = subsequence_begins[subsequence_idx + 1];@@ -1254,48 +1382,36 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZER// so save SEQ_LEN_PARTITION_SIZE elements for each partitionif (subsequence_end_pos == block_end_pos) {const uint last_row_idx = block_end_pos - block_start_pos - 1;- if (m == last_row_idx) {+ if (sglid == last_row_idx) {const uint partition_idx = start_partition_idx / SEQ_LEN_PARTITION_SIZE;- SOFTMAX_ACCUMULATOR_TYPE correction_factor = native_exp(qk_max_new - qk_max_cur);-- if (sglid == 0) {+ if (sgid == 0) {const uint max_partitions_num = aligned_max_context_len / SEQ_LEN_PARTITION_SIZE;const uint exp_sums_output_offset = subsequence_idx * NUM_HEADS * max_partitions_num +num_heads_dim * max_partitions_num +partition_idx;- exp_sums[exp_sums_output_offset] = exp_sum_new * correction_factor;- max_logits[exp_sums_output_offset] = qk_max_cur;+ exp_sums[exp_sums_output_offset] = exp_sum_new;+ max_logits[exp_sums_output_offset] = qk_max_new;}const uint output_offset = subsequence_idx * NUM_HEADS * aligned_max_context_len +num_heads_dim * aligned_max_context_len +- partition_idx * SEQ_LEN_PARTITION_SIZE;- for (uint i = sglid; i < partition_seq_len; i += SUBGROUP_SIZE) {- softmax_results[output_offset + i] = TO_SOFTMAX_ACCUMULATOR_TYPE(slm_qk_vals[m][i]) / exp_sum_new;+ partition_idx * SEQ_LEN_PARTITION_SIZE + sgid * TARGET_SEQ_LEN_BLOCK_SIZE;+ for (uint i = 0; i < TARGET_SEQ_LEN_BLOCK_SIZE; i++) {+ softmax_results[output_offset + i] = qk_acc[i];}- }- }-#endif-- // update- if (sglid == 0) {- SOFTMAX_ACCUMULATOR_TYPE pre_exp_sum = slm_exp_sum_prev[m];- SOFTMAX_ACCUMULATOR_TYPE correction_factor = native_exp(max_val_prev - qk_max_new);- SOFTMAX_ACCUMULATOR_TYPE pre_exp_sum_fixed = pre_exp_sum * correction_factor;- exp_sum_new += pre_exp_sum_fixed;- slm_update_factor[m] = correction_factor;- slm_max_val_prev[m] = qk_max_new;- slm_exp_sum_prev[m] = exp_sum_new;+ }}}-- barrier(CLK_LOCAL_MEM_FENCE);+#endif /*end of PAGED_ATTENTION_SCORES_OUTPUT*/}+#endif /*end of softmax calc*/++ barrier(CLK_LOCAL_MEM_FENCE);+ // QK*V calculation{- // QK*V calculationMAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) acc_output_res = OUTPUT_VAL_ZERO;#if IS_PAGED_ATTENTIONconst uint value_pitch = (HEAD_SIZE * NUM_KV_HEADS + INPUT2_PAD_BEFORE_FEATURE_NUM + INPUT2_PAD_AFTER_FEATURE_NUM);@@ -1510,11 +1626,12 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZER}- // protect slm_qk_vals as it is read in w*v stage and write in next round q*k stage.- barrier(CLK_LOCAL_MEM_FENCE);-+ // Rescale acc_output_res values and save current iter results to global accumulator+#if IS_FLASHATTEN_V2{- // Rescale acc_output_res values and save current iter results to global accumulator+ // protect slm_qk_vals as it is read in w*v stage and write in next round q*k stage.+ barrier(CLK_LOCAL_MEM_FENCE);+for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {if (start_partition_idx > 0) {OUTPUT_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * slm_update_factor[seq_idx];@@ -1523,8 +1640,38 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERoutput_acc[seq_idx] = acc_output_res[seq_idx];}}- }- }+#else /*!IS_FLASHATTEN_V2*/+ {+ SOFTMAX_ACCUMULATOR_TYPE exp_sum_prev = slm_exp_sum_prev[sglid];+ SOFTMAX_ACCUMULATOR_TYPE exp_sum_cur = slm_exp_sum_cur[sglid];+ SOFTMAX_ACCUMULATOR_TYPE max_val_prev = slm_max_val_prev[sglid];+ SOFTMAX_ACCUMULATOR_TYPE max_val_cur = slm_max_val_cur[sglid];++ barrier(CLK_LOCAL_MEM_FENCE);++ for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {+ SOFTMAX_ACCUMULATOR_TYPE total_max = SOFTMAX_ACCUMULATOR_MAX_FUNC(sub_group_broadcast(max_val_prev, seq_idx), sub_group_broadcast(max_val_cur, seq_idx));+ SOFTMAX_ACCUMULATOR_TYPE updated_exp_sum_prev = sub_group_broadcast(exp_sum_prev, seq_idx) * native_exp(sub_group_broadcast(max_val_prev, seq_idx) - total_max);+ SOFTMAX_ACCUMULATOR_TYPE updated_exp_sum_cur = sub_group_broadcast(exp_sum_cur, seq_idx) * native_exp(sub_group_broadcast(max_val_cur, seq_idx) - total_max);+ SOFTMAX_ACCUMULATOR_TYPE updated_total_exp_sum = updated_exp_sum_prev + updated_exp_sum_cur;++ if (start_partition_idx > 0) {+ OUTPUT_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * updated_exp_sum_prev / updated_total_exp_sum;;+ acc_output_res[seq_idx] *= updated_exp_sum_cur / updated_total_exp_sum;+ acc_output_res[seq_idx] += updated_prev_res;+ }++ output_acc[seq_idx] = acc_output_res[seq_idx];++ if (sgid == 0 && sglid == 0) {+ slm_exp_sum_prev[seq_idx] = updated_total_exp_sum;+ slm_max_val_prev[seq_idx] = total_max;+ }+ }+ }+#endif /*!IS_FLASHATTEN_V2*/+ } /* end of QK*V calculation */+ } /* end of iter over source sequence length */// Combine results from multiple SGs and store to output buffer@@ -1553,13 +1700,17 @@ MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERif (TARGET_SEQ_LEN_BLOCK_SIZE > seq_idx_end) {for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {+#if IS_FLASHATTEN_V2output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];+#endifOUTPUT_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_V2output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];+#endifOUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);output_offset += output_pitch;}diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_base.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_base.hindex 668924ba36b..1454f87aa22 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_base.h+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/sdpa/sdpa_kernel_base.h@@ -126,6 +126,7 @@ struct sdpa_params : public base_params {sdpa_configuration conf;bool should_use_sdpa_opt = false;+ bool could_use_flashattn_v2 = true;};////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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.cppindex ca9336ef15b..7ee342849fb 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@@ -218,6 +218,9 @@ JitConstants SDPAKernelOpt::GetJitConstants(const sdpa_params& params, size_t keif (params.conf.is_paged_attention)jit.AddConstant(MakeJitConstant("IS_PAGED_ATTENTION", 1));+ if (params.could_use_flashattn_v2)+ jit.AddConstant(MakeJitConstant("IS_FLASHATTEN_V2", 1));+if (params.engineInfo.supports_immad && params.conf.broadcast_axis == -1 && params.conf.head_size >= 128)jit.AddConstant(MakeJitConstant("LOAD_KEY_LEFTOVERS_IN_CALC_LOOP", 1));diff --git a/src/plugins/intel_gpu/src/runtime/execution_config.cpp b/src/plugins/intel_gpu/src/runtime/execution_config.cppindex 47d95a26c09..e86517c0a51 100644--- a/src/plugins/intel_gpu/src/runtime/execution_config.cpp+++ b/src/plugins/intel_gpu/src/runtime/execution_config.cpp@@ -18,6 +18,7 @@#include "openvino/runtime/plugin_config.hpp"#include "openvino/runtime/properties.hpp"#include "transformations/utils/utils.hpp"+#include "intel_gpu/op/kv_cache.hpp"namespace ov::intel_gpu {@@ -132,7 +133,9 @@ void ExecutionConfig::apply_rt_info(const IRemoteContext* context, const ov::RTM}void ExecutionConfig::apply_model_specific_options(const IRemoteContext* context, const ov::Model& model) {- apply_rt_info(context, get_rt_info(model), ov::op::util::is_large_language_model(model));+ const auto is_LLM = ov::op::util::is_large_language_model(model) ||+ ov::op::util::has_op_with_type<ov::intel_gpu::op::KVCache>(model.shared_from_this());+ apply_rt_info(context, get_rt_info(model), is_LLM);const auto& ops = model.get_ops();@@ -153,7 +156,7 @@ void ExecutionConfig::apply_model_specific_options(const IRemoteContext* contextm_use_onednn = true;}- if (auto multi_subgraph_op = ov::as_type_ptr<op::util::MultiSubGraphOp>(op)) {+ if (auto multi_subgraph_op = ov::as_type_ptr<ov::op::util::MultiSubGraphOp>(op)) {for (const auto& sub_graph : multi_subgraph_op->get_functions()) {for (auto& sub_op : sub_graph->get_ops()) {process_op(sub_op);@@ -182,6 +185,10 @@ void ExecutionConfig::apply_model_specific_options(const IRemoteContext* context}}+ // Disable FlashAttn V2 online softmax tricks by default for non-LLMs.+ if (!is_set_by_user(ov::intel_gpu::could_use_flashattn_v2) && !is_LLM) {+ m_could_use_flashattn_v2 = false;+ }m_optimize_data = true;}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 c16b1bbdb4a..f2b7489985a 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@@ -936,6 +936,8 @@ public:ExecutionConfig config = get_test_default_config(get_test_engine());config.set_property(ov::intel_gpu::optimize_data(true));config.set_property(ov::intel_gpu::allow_new_shape_infer(true));+ // FlashAttn v1 or v2?+ config.set_property(ov::intel_gpu::could_use_flashattn_v2(p.disable_flashattn_v2));network::ptr network = get_network(get_test_engine(), topology, config, get_test_stream_ptr(), false);network->set_input_data("query", query_mem);@@ -1000,6 +1002,7 @@ struct paged_attention_test_params {bool dynamic_paddings;bool scores_output;CacheRotationDescriptor rotation_config;+ bool disable_flashattn_v2;};class paged_attention_test : public PagedAttentionTest<paged_attention_test_params> {};@@ -1018,44 +1021,46 @@ const auto PER_TOKEN_ROTATION = CacheRotationDescriptor{ true, false };const auto DISABLE_ROTATION = CacheRotationDescriptor{ false, false };const auto STATIC_INPUT_PAD = false;const auto DYNAMIC_INPUT_PAD = true;+const auto ENABLE_FA_V2 = false;+const auto DISABLE_FA_V2 = true;INSTANTIATE_TEST_SUITE_P(smoke_paged_attention, paged_attention_test, ::testing::ValuesIn(std::vector<paged_attention_test_params>{/* with scores output */- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token- paged_attention_test_params{ {{36, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token long- paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{1, 10}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 2nd token- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{36, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token long+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{1, 10}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token/* without scores output, dynamic input query paddings */- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token long- paged_attention_test_params{ {{10, 0}, {81, 0}, {129, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 1st token long+ paged_attention_test_params{ {{10, 0}, {81, 0}, {129, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token/* with scores, per_block rotation */- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token- paged_attention_test_params{ {{36, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token long- paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{1, 10}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 2nd token- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, ENABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{36, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, ENABLE_FA_V2 }, // 1st token long+ paged_attention_test_params{ {{10, 0}, {30, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{128, 0}, {256, 0}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, ENABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{1, 10}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token/* with scores, per_token rotation */- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, DISABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token/* without scores output, dynamic input query paddings, KV-cache compression */- paged_attention_test_params{ {{10, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token- paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token long- paged_attention_test_params{ {{10, 0}, {81, 0}, {129, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 1st token + 1st token- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{10, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 1st token+ paged_attention_test_params{ {{1024, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token long+ paged_attention_test_params{ {{10, 0}, {81, 0}, {129, 0}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, ENABLE_FA_V2 }, // 1st token + 1st token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, DYNAMIC_INPUT_PAD, DISABLE_SCORES, DISABLE_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token/* with scores, per_block rotation, KV-cache compression */- paged_attention_test_params{ {{1, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION }, // 2nd token+ paged_attention_test_params{ {{1, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_BLOCK_ROTATION, DISABLE_FA_V2 }, // 2nd token/* with scores, per_token rotation, KV-cache compression */- paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION }, // 2nd token + 2nd token- paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION }, // mixed: 2nd token + 1st token + part of 1st token+ paged_attention_test_params{ {{1, 34}, {1, 515}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // 2nd token + 2nd token+ paged_attention_test_params{ {{1, 34}, {25, 0}, {10, 34}}, 2, 64, 16, ENABLE_CACHE_COMPRESSION, STATIC_INPUT_PAD, ENABLE_SCORES, PER_TOKEN_ROTATION, DISABLE_FA_V2 }, // mixed: 2nd token + 1st token + part of 1st token}));