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] Process head size larger than 256 (#33496)
Maksim Kutakov0d3c90684d96
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cppindex 6100be7cf3b..48039b8e235 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp@@ -209,7 +209,7 @@ bool SDPAOpt::supports_micro_sdpa(const RuntimeParams& params) {auto K_head_size = get_head_size(k_layout, extended_input_k_transpose_order);auto V_head_size = get_head_size(v_layout, extended_input_v_transpose_order);- if (K_head_size != V_head_size || K_head_size > 256 || V_head_size > 256) {+ if (K_head_size != V_head_size || K_head_size > 512 || V_head_size > 512) {return false;}@@ -220,7 +220,7 @@ bool SDPAOpt::supports_micro_sdpa(const RuntimeParams& params) {params.get_input_layout(mask_idx).count() == 1) {return false;}- if (q_layout.get_partial_shape()[mask_idx].get_length() > 256) {+ if (q_layout.get_partial_shape()[mask_idx].get_length() > 512) {return false;}diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.clindex 0fab490d1a6..ac2e2cb1009 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl@@ -182,9 +182,6 @@ KERNEL(sdpa_opt)(#error "sdpa_opt.cl: Unsupported scale factor"#endif-#if SUBGROUPS_PER_WG > SUBGROUP_SIZE- #error "sdpa_opt.cl: Number of subgroups per work group should be no more than subgroup_size"-#endifconst uint sgid = get_sub_group_id();const uint sglid = get_sub_group_local_id();@@ -473,6 +470,8 @@ KERNEL(sdpa_opt)(barrier(CLK_LOCAL_MEM_FENCE);+#if SUBGROUPS_PER_WG <= SUBGROUP_SIZE+ // Fast path: original single-subgroup reduction (when SUBGROUPS_PER_WG <= SUBGROUP_SIZE)for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {qk_max[seq_idx] = SOFTMAX_ACCUMULATOR_VAL_MIN;@@ -485,6 +484,40 @@ KERNEL(sdpa_opt)(qk_max[seq_idx] = qk_max[seq_idx] > sink_ptr[b1_idx] ? qk_max[seq_idx] : sink_ptr[b1_idx];#endif}+#else+ // Slow path: hierarchical reduction for large head sizes (when SUBGROUPS_PER_WG > SUBGROUP_SIZE)+ // Use parallel SIMD reduction within first subgroup to reduce across all subgroups+ if (sgid == 0) {+ for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {+ SOFTMAX_ACCUMULATOR_TYPE wg_max = SOFTMAX_ACCUMULATOR_VAL_MIN;++ // Parallel reduction: each lane processes SUBGROUPS_PER_WG/SUBGROUP_SIZE elements+ const uint num_sg_per_lane = CEIL_DIV(SUBGROUPS_PER_WG, SUBGROUP_SIZE);+ for (uint i = 0; i < num_sg_per_lane; i++) {+ uint sg_idx = sglid + i * SUBGROUP_SIZE;+ if (sg_idx < SUBGROUPS_PER_WG) {+ wg_max = SOFTMAX_ACCUMULATOR_MAX_FUNC(wg_max, qk_max_vals[seq_idx * SUBGROUPS_PER_WG + sg_idx]);+ }+ }++ // Horizontal reduction across subgroup lanes+ wg_max = sub_group_reduce_max(wg_max);++ if (sglid == 0) {+ qk_max_vals[seq_idx * SUBGROUPS_PER_WG] = wg_max;+ }+ }+ }++ barrier(CLK_LOCAL_MEM_FENCE);++ for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {+ qk_max[seq_idx] = qk_max_vals[seq_idx * SUBGROUPS_PER_WG];+ #ifdef HAS_SINK_INPUT+ qk_max[seq_idx] = qk_max[seq_idx] > sink_ptr[b1_idx] ? qk_max[seq_idx] : sink_ptr[b1_idx];+ #endif+ }+#endifSOFTMAX_ACCUMULATOR_TYPE exp_sum[TARGET_SEQ_LEN_BLOCK_SIZE] = {SOFTMAX_ACCUMULATOR_VAL_ZERO};const uint qk_num_per_wi = CEIL_DIV(partition_seq_len, SUBGROUPS_PER_WG * SUBGROUP_SIZE);@@ -509,6 +542,8 @@ KERNEL(sdpa_opt)(barrier(CLK_LOCAL_MEM_FENCE);+#if SUBGROUPS_PER_WG <= SUBGROUP_SIZE+ // Fast path: original single-subgroup reduction (when SUBGROUPS_PER_WG <= SUBGROUP_SIZE)unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {exp_sum[seq_idx] = SOFTMAX_ACCUMULATOR_VAL_ZERO;@@ -521,6 +556,40 @@ KERNEL(sdpa_opt)(exp_sum[seq_idx] += (native_exp(TO_SOFTMAX_ACCUMULATOR_TYPE(sink_ptr[b1_idx] - qk_max[seq_idx])));#endif}+#else+ // Slow path: hierarchical reduction for large head sizes (when SUBGROUPS_PER_WG > SUBGROUP_SIZE)+ // Use parallel SIMD reduction within first subgroup to reduce across all subgroups+ if (sgid == 0) {+ for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {+ SOFTMAX_ACCUMULATOR_TYPE wg_sum = SOFTMAX_ACCUMULATOR_VAL_ZERO;++ // Parallel reduction: each lane processes SUBGROUPS_PER_WG/SUBGROUP_SIZE elements+ const uint num_sg_per_lane = CEIL_DIV(SUBGROUPS_PER_WG, SUBGROUP_SIZE);+ for (uint i = 0; i < num_sg_per_lane; i++) {+ uint sg_idx = sglid + i * SUBGROUP_SIZE;+ if (sg_idx < SUBGROUPS_PER_WG) {+ wg_sum += qk_sum_vals[seq_idx * SUBGROUPS_PER_WG + sg_idx];+ }+ }++ // Horizontal reduction across subgroup lanes+ wg_sum = sub_group_reduce_add(wg_sum);++ if (sglid == 0) {+ qk_sum_vals[seq_idx * SUBGROUPS_PER_WG] = wg_sum;+ }+ }+ }++ barrier(CLK_LOCAL_MEM_FENCE);++ unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {+ exp_sum[seq_idx] = qk_sum_vals[seq_idx * SUBGROUPS_PER_WG];+ #ifdef HAS_SINK_INPUT+ exp_sum[seq_idx] += (native_exp(TO_SOFTMAX_ACCUMULATOR_TYPE(sink_ptr[b1_idx] - qk_max[seq_idx])));+ #endif+ }+#endif// const SOFTMAX_ACCUMULATOR_TYPE inv_exp_sum = SOFTMAX_ACCUMULATOR_VAL_ONE / exp_sum[seq_idx];for (uint qk_idx = 0; qk_idx < qk_num_per_wi; qk_idx++) {@@ -1828,7 +1897,7 @@ KERNEL(sdpa_opt)(#ifdef V_HEAD_SIZE_LEFTOVER#ifdef BEAM_TABLE_TYPEconst uint value_offset_seq = sub_group_broadcast(value_offset, seq_len_idx);- const INPUT2_TYPE value_packed = (head_size_idx <= V_HEAD_SIZE) ? value_input[value_offset_seq] : INPUT2_VAL_ZERO;+ const INPUT2_TYPE value_packed = (head_size_idx < V_HEAD_SIZE) ? value_input[value_offset_seq] : INPUT2_VAL_ZERO;#else // !BEAM_TABLE_TYPEINPUT2_TYPE value_packed;if (sgid < SUBGROUPS_PER_WG - 1)@@ -1943,7 +2012,8 @@ KERNEL(sdpa_opt)(#if IS_FLASHATTEN_V2output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];#endif- OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);+ // Use scalar writes since block writes require alignment, but we process leftovers here+ output[output_offset + sglid] = output_acc[seq_idx];output_offset += output_pitch;}} else if (sglid < V_HEAD_SIZE_LEFTOVER) {@@ -1961,7 +2031,8 @@ KERNEL(sdpa_opt)(#if IS_FLASHATTEN_V2output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];#endif- OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);+ // Use scalar writes since block writes require alignment, but we process leftovers here+ output[output_offset + sglid] = output_acc[seq_idx];output_offset += output_pitch;}} else if (sglid < V_HEAD_SIZE_LEFTOVER) {diff --git a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cppindex 64cb8b8c703..65174d00523 100644--- a/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp+++ b/src/plugins/intel_gpu/src/plugin/sync_infer_request.cpp@@ -608,7 +608,7 @@ void SyncInferRequest::allocate_inputs() {for (const auto& it : m_input_ports_map) {size_t input_idx = it.first;const auto& port = it.second;- GPU_DEBUG_LOG << "[init input blob with index: " << input_idx << "]" << std::endl;+ GPU_DEBUG_LOG << "[init input blob with index: " << input_idx << "]" << " shape: " << port.get_partial_shape() << " type: " << port.get_element_type() << std::endl;bool is_nv12_input = false;if (port.get_rt_info().count(ov::preprocess::TensorInfoMemoryType::get_type_info_static())) {@@ -633,7 +633,7 @@ void SyncInferRequest::allocate_outputs() {for (const auto& it : m_output_ports_map) {size_t output_idx = it.first;const auto& port = it.second;- GPU_DEBUG_LOG << "[init output blob with index: " << output_idx << "]" << std::endl;+ GPU_DEBUG_LOG << "[init output blob with index: " << output_idx << "]" << " shape: " << port.get_partial_shape() << " type: " << port.get_element_type() << std::endl;allocate_output(port, output_idx);total_output_bytes += ov::ISyncInferRequest::get_tensor(port)->get_byte_size();diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cppindex 316118a52a1..e8fc7a0c50c 100644--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp@@ -742,15 +742,16 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {return false;}- const auto head_size = query_ps[query_ps.size() - 1].get_length();+ const auto head_size = static_cast<uint64_t>(query_ps[query_ps.size() - 1].get_length());if (device_info.supports_immad && cldnn::query_microkernels_supported(m_context->get_engine(), config) && head_size <= 256)return true;- // - 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.+ // - Head size should be 128 for any model type; or should be in the range of 64 to 512 for stateful LLMs because of performance+ // reasons and implementation limitations (see sdpa micro and sdpa opt kernels).// 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- bool valid_head_size = (head_size >= 64 && head_size <= 256);- if (!valid_head_size) {+ bool valid_head_size = (head_size >= 64 && head_size <= std::min(device_info.max_work_group_size, static_cast<uint64_t>(512)));+ if (!valid_head_size || head_size % 2 != 0) { // head_size should be an even number (until the SDPA opt kernel is fixed for odd head size)return false;}diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cppindex d159b79306b..d674cd513fe 100644--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp@@ -251,7 +251,6 @@ std::vector<std::string> disabledTestPatterns() {returnVal.push_back(R"(.*smoke_ConvolutionLayerGPUTest_3D_tensor_basic/ConvolutionLayerGPUTest..*)");returnVal.push_back(R"(.*smoke_MatmulWeightsDecompressionQuantizeConvolution_basic.*)");returnVal.push_back(R"(.*smoke_Nms9LayerTest/Nms9LayerTest.Inference/num_batches=2_num_boxes=50.*)");- returnVal.push_back(R"(.*smoke_ScaledAttnDynamic4D_GPU/ScaledAttnLayerGPUTest.CompareWithRefs/n.*)");} else {// CVS-172342returnVal.push_back(R"(.*smoke_MatMulCompressedWeights_3D_weight.*)");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.cppindex a9a6a9afdfc..73c96df0f70 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@@ -519,6 +519,26 @@ const std::vector<std::vector<InputShape>> dynamic_shapes_4D {{ov::Shape{1, 1, 7, 7}, ov::Shape{1, 1, 1, 1}, ov::Shape{2, 1, 10, 10}}}},},+ {+ // q shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 512}, {ov::Shape{1, 8, 7, 512}, ov::Shape{1, 8, 1, 512}, ov::Shape{2, 8, 10, 512}}}},+ // k shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 512}, {ov::Shape{1, 8, 7, 512}, ov::Shape{1, 8, 1, 512}, ov::Shape{2, 8, 10, 512}}}},+ // v shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 512}, {ov::Shape{1, 8, 7, 512}, ov::Shape{1, 8, 1, 512}, ov::Shape{2, 8, 10, 512}}}},+ // attn shape: [B, 1, -1, L0+L1]+ {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}}}},+ },+ {+ // q shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 486}, {ov::Shape{1, 8, 7, 486}, ov::Shape{1, 8, 1, 486}, ov::Shape{2, 8, 10, 486}}}},+ // k shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 486}, {ov::Shape{1, 8, 7, 486}, ov::Shape{1, 8, 1, 486}, ov::Shape{2, 8, 10, 486}}}},+ // v shape+ {ov::test::InputShape{ov::PartialShape{-1, 8, -1, 486}, {ov::Shape{1, 8, 7, 486}, ov::Shape{1, 8, 1, 486}, ov::Shape{2, 8, 10, 486}}}},+ // attn shape: [B, 1, -1, L0+L1]+ {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