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 to embedding_bag_ref kernel (#37371)
Mikolaj Roszczyk2c1ebb6a89b7
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/embedding_bag_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/embedding_bag_ref.clindex 13ec9f19094..8bc2730e5c2 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/embedding_bag_ref.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/embedding_bag_ref.cl@@ -53,10 +53,10 @@ KERNEL(embedding_bag_ref)(const uint emb_dim3 = (uint)get_global_id(2) % OUTPUT_SIZE_X;uint offsets_ind = INPUT2_OFFSET + batch;- uint start_indices = INPUT1_OFFSET + offsets[offsets_ind];+ uint start_indices = offsets[offsets_ind];offsets_ind = INPUT2_OFFSET + batch + 1;uint end_indices = (batch < OUTPUT_BATCH_NUM - 1) ?- INPUT1_OFFSET + offsets[offsets_ind] :+ offsets[offsets_ind] :INPUT1_LENGTH;OUTPUT_TYPE res = OUTPUT_VAL_ZERO;@@ -119,7 +119,7 @@ KERNEL(embedding_bag_ref)(OUTPUT_TYPE val = emb_table[emb_index];#ifdef INPUT4_TYPE{- uint weight_index = INPUT3_OFFSET + i;+ uint weight_index = INPUT4_OFFSET + i;val *= weights[weight_index];}#endifdiff --git a/src/plugins/intel_gpu/tests/unit/test_cases/embedding_bag_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/embedding_bag_gpu_test.cppindex 7d767667165..91a9018327c 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/embedding_bag_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/embedding_bag_gpu_test.cpp@@ -426,6 +426,71 @@ TEST(embedding_bag_fp16_gpu, offsets_sum_basic) {}}+TEST(embedding_bag_fp16_gpu, offsets_sum_indices_with_non_zero_physical_offset) {+ // Same logical values as offsets_sum_basic, but indices memory has explicit lower padding.+ // This guarantees non-zero INPUT1_OFFSET and catches regressions in indices addressing.+ auto& engine = get_test_engine();++ auto emb_table = engine.allocate_memory({ data_types::f16, format::bfyx, { 5, 2, 1, 1 } });+ auto indices_layout = layout{ data_types::i32, format::bfyx, { 4, 1, 1, 1 }, padding({ 1, 0, 0, 0 }, { 0, 0, 0, 0 }) };+ auto indices = engine.allocate_memory(indices_layout);+ auto offsets = engine.allocate_memory({ data_types::i32, format::bfyx, { 3, 1, 1, 1 } });+ auto per_sample_weights = engine.allocate_memory({ data_types::f16, format::bfyx, { 4, 1, 1, 1 } });+ tensor output_shape = {3, 2, 1, 1};++ set_values(emb_table, {+ ov::float16(-0.2f), ov::float16(-0.6f),+ ov::float16(-0.1f), ov::float16(-0.4f),+ ov::float16(-1.9f), ov::float16(-1.8f),+ ov::float16(-1.0f), ov::float16(1.5f),+ ov::float16(0.8f), ov::float16(-0.7f)+ });+ {+ cldnn::mem_lock<int32_t, mem_lock_type::write> indices_ptr(indices, get_test_stream());+ auto indices_l = indices->get_layout();+ indices_ptr[indices_l.get_linear_offset(tensor(batch(0), feature(0), spatial(0, 0, 0, 0)))] = 0;+ indices_ptr[indices_l.get_linear_offset(tensor(batch(1), feature(0), spatial(0, 0, 0, 0)))] = 2;+ indices_ptr[indices_l.get_linear_offset(tensor(batch(2), feature(0), spatial(0, 0, 0, 0)))] = 3;+ indices_ptr[indices_l.get_linear_offset(tensor(batch(3), feature(0), spatial(0, 0, 0, 0)))] = 4;+ }+ set_values<int32_t>(offsets, {+ 0, 2, 2+ });+ set_values(per_sample_weights, {+ ov::float16(0.5f), ov::float16(0.5f), ov::float16(0.5f), ov::float16(0.5f)+ });++ auto type = embedding_bag::offsets_sum;+ topology topology;+ topology.add(input_layout("Input0", emb_table->get_layout()));+ topology.add(input_layout("Input1", indices->get_layout()));+ topology.add(input_layout("Input2", offsets->get_layout()));+ topology.add(data("Input3", per_sample_weights));+ topology.add(+ embedding_bag("embedding_bag", { input_info("Input0"), input_info("Input1"), input_info("Input2"), input_info("Input3") }, type, output_shape, 0)+ );+ network network(engine, topology, get_test_default_config(engine));++ network.set_input_data("Input0", emb_table);+ network.set_input_data("Input1", indices);+ network.set_input_data("Input2", offsets);++ auto outputs = network.execute();++ auto output = outputs.at("embedding_bag").get_memory();+ cldnn::mem_lock<uint16_t, mem_lock_type::read> output_ptr(output, get_test_stream());++ std::vector<float> expected_results = {+ -1.05f, -1.2f,+ -0.2f, -0.6f,+ -0.1f, 0.4f+ };++ for (size_t i = 0; i < expected_results.size(); ++i) {+ ASSERT_TRUE(are_equal(expected_results[i], half_to_float(output_ptr[i]))) << i;+ }+}+TEST(embedding_bag_fp16_gpu, offsets_sum_basic_first_empty) {// emb_table : 5x2// indices : 4x1@@ -830,6 +895,78 @@ TEST(embedding_bag_fp16_gpu, segments_sum_basic) {}}+TEST(embedding_bag_fp16_gpu, segments_sum_weighted_with_non_zero_weights_physical_offset) {+ // Keep Input3 (segments_num) dense while giving Input4 (weights) a non-zero physical offset.+ // This catches regressions where INPUT3_OFFSET is accidentally used for weights access.+ auto& engine = get_test_engine();++ auto emb_table = engine.allocate_memory({ data_types::f16, format::bfyx, { 5, 2, 1, 1 } });+ auto indices = engine.allocate_memory({ data_types::i32, format::bfyx, { 4, 1, 1, 1 } });+ auto segment_ids = engine.allocate_memory({ data_types::i32, format::bfyx, { 4, 1, 1, 1 } });+ auto segments_num = engine.allocate_memory({ data_types::i32, format::bfyx, { 1, 1, 1, 1 } });+ auto weights_layout = layout{ data_types::f16, format::bfyx, { 4, 1, 1, 1 }, padding({ 1, 0, 0, 0 }, { 0, 0, 0, 0 }) };+ auto per_sample_weights = engine.allocate_memory(weights_layout);+ tensor output_shape = {3, 2, 1, 1};++ set_values(emb_table, {+ ov::float16(-0.2f), ov::float16(-0.6f),+ ov::float16(-0.1f), ov::float16(-0.4f),+ ov::float16(-1.9f), ov::float16(-1.8f),+ ov::float16(-1.0f), ov::float16(1.5f),+ ov::float16(0.8f), ov::float16(-0.7f)+ });+ set_values<int32_t>(indices, {+ 0, 2, 3, 4+ });+ set_values<int32_t>(segment_ids, {+ 0, 0, 2, 2+ });+ set_values<int32_t>(segments_num, { 4 });+ {+ cldnn::mem_lock<ov::float16, mem_lock_type::write> weights_ptr(per_sample_weights, get_test_stream());+ std::fill(weights_ptr.begin(), weights_ptr.end(), ov::float16(10.0f));+ auto weights_l = per_sample_weights->get_layout();+ weights_ptr[weights_l.get_linear_offset(tensor(batch(0), feature(0), spatial(0, 0, 0, 0)))] = ov::float16(0.5f);+ weights_ptr[weights_l.get_linear_offset(tensor(batch(1), feature(0), spatial(0, 0, 0, 0)))] = ov::float16(0.5f);+ weights_ptr[weights_l.get_linear_offset(tensor(batch(2), feature(0), spatial(0, 0, 0, 0)))] = ov::float16(0.5f);+ weights_ptr[weights_l.get_linear_offset(tensor(batch(3), feature(0), spatial(0, 0, 0, 0)))] = ov::float16(0.5f);+ }++ auto type = embedding_bag::segments_sum;+ topology topology;+ topology.add(input_layout("Input0", emb_table->get_layout()));+ topology.add(input_layout("Input1", indices->get_layout()));+ topology.add(input_layout("Input2", segment_ids->get_layout()));+ topology.add(input_layout("Input3", segments_num->get_layout()));+ topology.add(input_layout("Input4", per_sample_weights->get_layout()));+ topology.add(+ embedding_bag("embedding_bag", { input_info("Input0"), input_info("Input1"), input_info("Input2"), input_info("Input3"), input_info("Input4") }, type, output_shape, 0)+ );++ network network(engine, topology, get_test_default_config(engine));++ network.set_input_data("Input0", emb_table);+ network.set_input_data("Input1", indices);+ network.set_input_data("Input2", segment_ids);+ network.set_input_data("Input3", segments_num);+ network.set_input_data("Input4", per_sample_weights);++ auto outputs = network.execute();++ auto output = outputs.at("embedding_bag").get_memory();+ cldnn::mem_lock<uint16_t, mem_lock_type::read> output_ptr(output, get_test_stream());++ std::vector<float> expected_results = {+ -1.05f, -1.2f,+ -0.2f, -0.6f,+ -0.1f, 0.4f+ };++ for (size_t i = 0; i < expected_results.size(); ++i) {+ ASSERT_TRUE(are_equal(expected_results[i], half_to_float(output_ptr[i]))) << i;+ }+}+TEST(embedding_bag_fp16_gpu, segments_sum_basic_first_empty) {// emb_table : 5x2// indices : 4x1