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] Fix sdpa_opt single token kernel to support scalar input for scale (#31074)
Andrew Kwangwoong Park217e5b4b3f68
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 d37a456daea..22ebc3b5452 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@@ -205,6 +205,8 @@ KERNEL(sdpa_opt)(// Gemm1 calculation#if HAS_SCALE_INPUTconst OUTPUT_TYPE scale_val = *scale;+#elif defined(STATIC_SCALE_VALUE)+ const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);#elseconst OUTPUT_TYPE scale_val = OUTPUT_VAL_ONE / sqrt(TO_OUTPUT_TYPE(V_HEAD_SIZE));#endifdiff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_ref.clindex be1f8763a2a..1d5be718600 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_ref.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/sdpa_ref.cl@@ -157,7 +157,7 @@ KERNEL(sdpa_ref)(#if HAS_SCALE_INPUTconst OUTPUT_TYPE scale_val = *scale;#elif defined(STATIC_SCALE_VALUE)- const OUTPUT_TYPE scale_val = STATIC_SCALE_VALUE;+ const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);#elseconst OUTPUT_TYPE scale_val = OUTPUT_VAL_ONE / sqrt(TO_OUTPUT_TYPE(INPUT1_SIZE_X));#endifdiff --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 82db77af5af..d9bafdbb28c 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@@ -25,6 +25,7 @@ typedef std::tuple<ov::element::Type, // netPrecisionbool, // is_causalbool, // has_attnbool, // has_scale+ bool, // is_scale_conststd::vector<std::vector<int64_t>> // input_transpose> ScaledAttnGPUTestParams;@@ -40,6 +41,7 @@ protected:bool is_causal;bool has_attn;bool has_scale;+ bool is_scale_const;};std::string ScaledAttnLayerGPUTest::getTestCaseName(const testing::TestParamInfo<ScaledAttnGPUTestParams>& obj) {@@ -49,8 +51,9 @@ std::string ScaledAttnLayerGPUTest::getTestCaseName(const testing::TestParamInfobool is_causal;bool has_attn;bool has_scale;+ bool is_scale_const;bool transpose_enable;- std::tie(inType, inputShapes, is_causal, has_attn, has_scale, input_transpose) = obj.param;+ std::tie(inType, inputShapes, is_causal, has_attn, has_scale, is_scale_const, input_transpose) = obj.param;transpose_enable = (input_transpose.size() != 0);std::ostringstream result;@@ -69,6 +72,7 @@ std::string ScaledAttnLayerGPUTest::getTestCaseName(const testing::TestParamInforesult << "is_causal=" << is_causal << "_";result << "has_attn=" << has_attn << "_";result << "has_scale=" << has_scale << "_";+ result << "is_scale_const=" << is_scale_const << "_";result << "with_transpose" << transpose_enable << "_";return result.str();@@ -81,7 +85,7 @@ void ScaledAttnLayerGPUTest::SetUp() {targetDevice = ov::test::utils::DEVICE_GPU;- std::tie(inType, inputShapes, is_causal, has_attn, has_scale, input_transpose) = this->GetParam();+ std::tie(inType, inputShapes, is_causal, has_attn, has_scale, is_scale_const, input_transpose) = this->GetParam();transpose_prepare(inputShapes, input_transpose);init_input_shapes(inputShapes);@@ -94,30 +98,25 @@ void ScaledAttnLayerGPUTest::SetUp() {inputParams[0]->set_friendly_name("q");inputParams[1]->set_friendly_name("k");inputParams[2]->set_friendly_name("v");- // special case: only scale but no attn- if (!has_attn && has_scale) {- // attention_mask:[1]- inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, ov::PartialShape{}));+ if (has_attn || has_scale) {+ inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(+ inType, has_attn ? inputDynamicShapes[3] : ov::PartialShape{}));inputParams.back()->set_friendly_name("attention_mask");- // scale:[1]+ }+ if (has_scale && !is_scale_const) {inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, ov::PartialShape{1}));inputParams.back()->set_friendly_name("scale");- } else {- if (has_attn) {- inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, inputDynamicShapes[3]));- inputParams.back()->set_friendly_name("attention_mask");- }- if (has_scale) {- // scale:[1]- inputParams.push_back(std::make_shared<ov::op::v0::Parameter>(inType, ov::PartialShape{1}));- inputParams.back()->set_friendly_name("scale");- }}ov::OutputVector inputParams_transpose;for (size_t i = 0; i < inputParams.size(); i++) {inputParams_transpose.push_back(inputParams[i]);}+ if (has_scale && is_scale_const) {+ auto scale_const = std::make_shared<ov::op::v0::Constant>(inType, ov::Shape({1}), 0.35f);+ scale_const->set_friendly_name("scale");+ inputParams_transpose.push_back(scale_const);+ }if (input_transpose.size() != 0) {auto rank = input_transpose[0].size();// deal with transpose.@@ -230,11 +229,13 @@ void ScaledAttnLayerGPUTest::generate_inputs(const std::vector<ov::Shape>& targeov::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});+ if (!is_scale_const) {+ shapes.push_back(ov::Shape{1});+ 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) {@@ -242,7 +243,7 @@ void ScaledAttnLayerGPUTest::generate_inputs(const std::vector<ov::Shape>& targeov::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) {+ if (has_scale && !is_scale_const) {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});@@ -257,7 +258,8 @@ TEST_P(ScaledAttnLayerGPUTest, CompareWithRefs) {bool is_causal;bool has_attn;bool has_scale;- std::tie(inType, inputShapes, is_causal, has_attn, has_scale, input_transpose) = this->GetParam();+ bool is_scale_const;+ std::tie(inType, inputShapes, is_causal, has_attn, has_scale, is_scale_const, input_transpose) = this->GetParam();run();}@@ -289,6 +291,7 @@ const auto dynamic_shape_params_3D = testing::Combine(testing::Values(ov::elementesting::Values(false),testing::Values(true, false),testing::Values(true, false),+ testing::Values(false),testing::ValuesIn({disable_transpose, transpose_all_3D}));INSTANTIATE_TEST_SUITE_P(smoke_ScaledAttnDynamic3D_GPU,@@ -482,6 +485,25 @@ const std::vector<std::vector<InputShape>> dynamic_shapes_4D {{ov::test::InputShape{ov::PartialShape{-1, 1, -1, -1},{ov::Shape{1, 1, 100, 100}, ov::Shape{1, 1, 1, 1}, ov::Shape{2, 1, 10, 10}}}},+ },+ // single token+ {+ // q shape+ {ov::test::InputShape{ov::PartialShape{-1, 20, -1, 64},+ {ov::Shape{1, 20, 1, 64}, ov::Shape{1, 20, 1, 64}, ov::Shape{2, 20, 1, 64}}}+ },+ // k shape+ {ov::test::InputShape{ov::PartialShape{-1, 20, -1, 64},+ {ov::Shape{1, 20, 2, 64}, ov::Shape{1, 20, 10, 64}, ov::Shape{2, 20, 2, 64}}}+ },+ // v shape+ {ov::test::InputShape{ov::PartialShape{-1, 20, -1, 64},+ {ov::Shape{1, 20, 2, 64}, ov::Shape{1, 20, 10, 64}, ov::Shape{2, 20, 2, 64}}}+ },+ // attn shape: [B, 1, -1, L0+L1]+ {ov::test::InputShape{ov::PartialShape{-1, 1, -1, -1},+ {ov::Shape{1, 1, 1, 2}, ov::Shape{1, 1, 1, 10}, ov::Shape{2, 1, 1, 2}}}+ },}};@@ -493,6 +515,7 @@ const auto dynamic_shape_params_4D = testing::Combine(testing::Values(ov::elementesting::Values(true, false),testing::Values(true, false),testing::Values(true, false),+ testing::Values(true, false),testing::ValuesIn({disable_transpose, transpose_value}));INSTANTIATE_TEST_SUITE_P(smoke_ScaledAttnDynamic4D_GPU,@@ -581,6 +604,7 @@ const auto static_shape_params = testing::Combine(testing::Values(ov::element::ftesting::Values(true, false),testing::Values(true, false),testing::Values(true, false),+ testing::Values(false),testing::ValuesIn({disable_transpose, transpose_all_4D}));INSTANTIATE_TEST_SUITE_P(smoke_ScaledAttnStatic_GPU,