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
Resolve invalid argument index error for SDPA backend execution (#35021)
Min, Byungilb5b4f0816169
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 ac2e2cb1009..7b9f768fa75 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@@ -123,6 +123,13 @@ inline uint FUNC(get_bt_index_value)(OPTIONAL_SHAPE_INFO_ARG uint b, uint f, uin#define GET_COMPRESSION_INDEX(INPUT, b, f, y, x) GET_DATA_INDEX(INPUT, (b), (f), (y), (0));#else#define GET_COMPRESSION_INDEX(INPUT, b, f, y, x) GET_DATA_INDEX(INPUT, (b), (0), (y), (0));+#endif+ #define HAS_KV_CACHE_ZP_INPUT (USE_ASYMMETRIC_QUANTIZATION && !COMBINE_SCALES_AND_ZP)+ #define GET_SCALE(zp, scale, comp_offset) ((scale)[(comp_offset)])+#if HAS_KV_CACHE_ZP_INPUT+ #define GET_ZP(zp, scale, comp_offset) ((zp)[(comp_offset)])+#else+ #define GET_ZP(zp, scale, comp_offset) ((scale)[(comp_offset) + 1])#endif#endif@@ -159,6 +166,10 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst __global KEY_COMPRESSION_SCALE_TYPE* key_scale,const __global VALUE_COMPRESSION_SCALE_TYPE* val_scale,+#if HAS_KV_CACHE_ZP_INPUT+ const __global KEY_COMPRESSION_ZP_TYPE* key_zp,+ const __global VALUE_COMPRESSION_ZP_TYPE* val_zp,+#endif#endif#ifdef BEAM_TABLE_TYPEconst __global BEAM_TABLE_TYPE* beam_table,@@ -277,9 +288,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len, 0);- KEY_COMPRESSION_SCALE_TYPE comp_scale = key_scale[comp_offset];+ KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- KEY_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];+ KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);#endif#endifuint head_idx_index = 0;@@ -659,9 +670,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, 0);- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];+ VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];+ VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);#endif#endif@@ -714,9 +725,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len, 0);- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];+ VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];+ VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);#endif#endif@@ -941,6 +952,10 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst __global KEY_COMPRESSION_SCALE_TYPE* key_scale,const __global VALUE_COMPRESSION_SCALE_TYPE* val_scale,+#if HAS_KV_CACHE_ZP_INPUT+ const __global KEY_COMPRESSION_ZP_TYPE* key_zp,+ const __global VALUE_COMPRESSION_ZP_TYPE* val_zp,+#endif#endif#ifdef BEAM_TABLE_TYPEconst __global BEAM_TABLE_TYPE* beam_table,@@ -1231,9 +1246,9 @@ KERNEL(sdpa_opt)(if (seq_len_calc_size >= SUBGROUP_SIZE) {#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, seq_len + sglid, 0);- KEY_COMPRESSION_SCALE_TYPE comp_scale = key_scale[comp_offset];+ KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- KEY_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];+ KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);#endif#endifuint head_idx_index = 0;@@ -1298,9 +1313,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst 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);// const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, seq_len + sglid, 0);- KEY_COMPRESSION_SCALE_TYPE comp_scale = key_scale[comp_offset];+ KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- KEY_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];+ KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);#endif#endifuint head_idx_index = 0;@@ -1698,9 +1713,9 @@ KERNEL(sdpa_opt)(}#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len + sglid, 0);- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];+ VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];+ VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);#endif#endif#ifdef V_HEAD_SIZE_LEFTOVER@@ -1788,9 +1803,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, 0);- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];+ VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];+ VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);#endif#endif // IS_KV_COMPRESSED@@ -1887,9 +1902,9 @@ KERNEL(sdpa_opt)(#if IS_KV_COMPRESSEDconst uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + min(seq_len_leftovers_start + sglid, seq_len_end - 1), 0);// const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len_leftovers_start + sglid, 0);- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];+ VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);#if USE_ASYMMETRIC_QUANTIZATION- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];+ VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);#endif#endifdiff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cppindex 102accd4418..0f11c1ceb8d 100644--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp@@ -380,6 +380,9 @@ std::vector<Params> get_test_params() {p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::f16, 10, 4, 64, 64, 1, {1, 2, 0, 3}});p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::f16, 10, 4, 128, 96, 1, {1, 2, 0, 3}});+ // Compressed beam search (batch > 1 exercises indirect sdpa_opt path on IMMAD)+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 2, ov::element::Type_t::f16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});+/* -- causal mask -- */p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::f16, 10, 1, 64, 64, 1, {0, 1, 2, 3}});@@ -403,6 +406,9 @@ std::vector<Params> get_test_params() {p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::f16, 10, 4, 128, 64, 1, {0, 1, 2, 3}});p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::f16, 10, 4, 64, 64, 1, {1, 2, 0, 3}});+ // Compressed beam search (batch > 1 exercises indirect sdpa_opt path on IMMAD)+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 2, ov::element::Type_t::f16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});+return p;}diff --git a/src/plugins/intel_gpu/tests/unit/transformations/kv_cache_compression.cpp b/src/plugins/intel_gpu/tests/unit/transformations/kv_cache_compression.cppindex 2c026103372..3ed945e13f0 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/kv_cache_compression.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/kv_cache_compression.cpp@@ -619,6 +619,169 @@ TEST_F(TransformationTestsF, NewTestKVCacheCompression) {}}+// Test KV cache compression with Planar output storage (supports_immad=true),+// where scales and zero points are stored as separate buffers.+// This exercises the code path fixed for separate key_zp/value_zp kernel arguments in sdpa_opt.cl.+TEST_F(TransformationTestsF, KVCacheCompressionPlanar) {+ bool causal = false;+ bool with_mask = true;+ bool with_scale = true;+ size_t concat_axis = 2;+ size_t gather_axis = 0;+ ov::element::Type_t element_type = ov::element::f16;+ std::vector<int64_t> qkv_order = {0, 1, 2, 3};+ std::shared_ptr<ov::Node> mask = nullptr;+ std::shared_ptr<ov::Node> scale = nullptr;+ ov::PartialShape input_shape = ov::PartialShape{1, 32, -1, 80};++ {+ auto query = std::make_shared<ov::op::v0::Parameter>(element_type, input_shape);+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{1});++ auto key_variable = std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::f16, "v0"});+ auto key_current = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, input_shape);+ auto key_past = std::make_shared<ov::intel_gpu::op::ReadValue>(key_variable);+ auto key_cache = std::make_shared<ov::intel_gpu::op::KVCache>(key_past, key_current, beam_idx, key_variable, concat_axis, gather_axis);++ auto value_variable = std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::f16, "v1"});+ auto value_current = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, input_shape);+ auto value_past = std::make_shared<ov::intel_gpu::op::ReadValue>(value_variable);+ auto value_cache = std::make_shared<ov::intel_gpu::op::KVCache>(value_past, value_current, beam_idx, value_variable, concat_axis, gather_axis);++ ov::ParameterVector params{ beam_idx, query, key_current, value_current };++ if (with_mask) {+ auto attn_mask = std::make_shared<ov::op::v0::Parameter>(element_type, ov::PartialShape::dynamic(4));+ mask = attn_mask;+ params.push_back(attn_mask);+ }++ if (with_mask && with_scale) {+ auto scale_input = std::make_shared<ov::op::v0::Parameter>(element_type, ov::PartialShape{1});+ scale = scale_input;+ params.push_back(scale_input);+ }++ ov::OutputVector sdpa_inputs = { query, key_cache->output(0), value_cache->output(0) };++ if (mask) {+ sdpa_inputs.push_back(mask);+ }++ if (scale) {+ sdpa_inputs.push_back(scale);+ }++ std::shared_ptr<ov::Node> sdpa = nullptr;+ sdpa = std::make_shared<ov::intel_gpu::op::IndirectSDPA>(sdpa_inputs,+ key_cache->output(1),+ causal,+ gather_axis,+ qkv_order,+ qkv_order,+ qkv_order,+ ov::intel_gpu::op::SDPA::default_order(4));++ auto result = std::make_shared<ov::op::v0::Result>(sdpa);++ ov::ResultVector results{ result };++ model = std::make_shared<ov::Model>(results, params);+ // supports_immad=true triggers Planar output storage with separate ZP buffers+ manager.register_pass<KVCacheCompression>(ov::element::i8, true);+ }+ {+ ov::op::internal::DynamicQuantize::Attributes dq_config;+ dq_config.quantization_type = ov::op::internal::DynamicQuantize::QuantizationType::Asymmetric;+ dq_config.quantization_dt = ov::element::i8;+ dq_config.scale_dt = ov::element::f16;+ dq_config.zp_dt = ov::element::i8;+ dq_config.group_sizes = { 1, 1, 1, UINT64_MAX };+ dq_config.scales_zp_output_order = { 0, 1, 2, 3 };+ dq_config.output_storage_type = ov::op::internal::DynamicQuantize::OutputStorageType::Planar;++ auto query = std::make_shared<ov::op::v0::Parameter>(element_type, input_shape);+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{1});++ // For Planar mode, ReadValues produces 3 outputs: data(i8), scale(f16), zp(i8)+ auto key_variable = std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::f16, "v0"});+ auto key_current = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, input_shape);+ auto key_past_variable_infos = { ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::i8, "v0"},+ ov::op::util::VariableInfo{{1, 32, -1, 1}, ov::element::f16, "v0"},+ ov::op::util::VariableInfo{{1, 32, -1, 1}, ov::element::i8, "v0"} };+ auto key_past_compressed = std::make_shared<ov::intel_gpu::op::ReadValues>(key_variable, key_past_variable_infos);+ auto key_cache_inputs = ov::OutputVector{ key_past_compressed->output(0), key_current, beam_idx,+ key_past_compressed->output(1), key_past_compressed->output(2) };+ auto key_cache = std::make_shared<ov::intel_gpu::op::KVCacheCompressed>(key_cache_inputs,+ key_variable,+ false,+ concat_axis,+ gather_axis,+ dq_config);++ auto value_variable = std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::f16, "v1"});+ auto value_current = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, input_shape);+ auto value_past_variable_infos = { ov::op::util::VariableInfo{{1, 32, -1, 80}, ov::element::i8, "v1"},+ ov::op::util::VariableInfo{{1, 32, -1, 1}, ov::element::f16, "v1"},+ ov::op::util::VariableInfo{{1, 32, -1, 1}, ov::element::i8, "v1"} };+ auto value_past_compressed = std::make_shared<ov::intel_gpu::op::ReadValues>(value_variable, value_past_variable_infos);+ auto value_cache_inputs = ov::OutputVector{ value_past_compressed->output(0), value_current, beam_idx,+ value_past_compressed->output(1), value_past_compressed->output(2) };+ auto value_cache = std::make_shared<ov::intel_gpu::op::KVCacheCompressed>(value_cache_inputs,+ value_variable,+ false,+ concat_axis,+ gather_axis,+ dq_config);++ ov::ParameterVector params{ beam_idx, query, key_current, value_current };++ if (with_mask) {+ auto attn_input = std::make_shared<ov::op::v0::Parameter>(element_type, ov::PartialShape::dynamic(4));+ mask = attn_input;+ params.push_back(attn_input);+ }++ if (with_mask && with_scale) {+ auto scale_input = std::make_shared<ov::op::v0::Parameter>(element_type, ov::PartialShape{1});+ scale = scale_input;+ params.push_back(scale_input);+ }++ ov::OutputVector sdpa_inputs = { query, key_cache->output(0), value_cache->output(0) };+ if (mask) {+ sdpa_inputs.push_back(mask);+ }++ if (scale) {+ sdpa_inputs.push_back(scale);+ }++ // For Planar mode: key_scale, value_scale, key_zp, value_zp+ sdpa_inputs.push_back(key_cache->output(2));+ sdpa_inputs.push_back(value_cache->output(2));+ sdpa_inputs.push_back(key_cache->output(3));+ sdpa_inputs.push_back(value_cache->output(3));++ std::shared_ptr<ov::Node> sdpa = nullptr;+ sdpa = std::make_shared<ov::intel_gpu::op::IndirectSDPA>(sdpa_inputs,+ key_cache->output(1),+ causal,+ gather_axis,+ qkv_order,+ qkv_order,+ qkv_order,+ ov::intel_gpu::op::SDPA::default_order(4),+ dq_config);++ auto result = std::make_shared<ov::op::v0::Result>(sdpa);++ ov::ResultVector results{ result };++ model_ref = std::make_shared<ov::Model>(results, params);+ }+}+} // namespace intel_gpu} // namespace test} // namespace ov