Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Fix accuracy issue at gemm_tiled_opt kernel with dynamic padding (#25869)

Kelvin Choicab5b2c8a8d2
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gemm_tiled_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gemm_tiled_opt.cl
index 5ad89020ebf..1a1288f5dc8 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gemm_tiled_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gemm_tiled_opt.cl
@@ -181,7 +181,7 @@ KERNEL(gemm_tiled_opt)(
// Start pointers offsets
#if TRANSPOSE_INPUT0 == TRANSPOSE_X_LAST
const __global INPUT0_TYPE* a_ptr = input0 + batch_offset_input0;
- #if HAS_DYNAMIC_K_PADDING || INPUT0_HAS_PADDING
+ #if INPUT0_HAS_DYNAMIC_PADDING || INPUT0_HAS_PADDING
const uint input0_offset = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, (y+1), 0) - batch_offset_input0;
const uint input0_offset1 = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, y, (TILE_K)) - batch_offset_input0;
#else
@@ -190,7 +190,7 @@ KERNEL(gemm_tiled_opt)(
#endif
#elif TRANSPOSE_INPUT0 == TRANSPOSE_Y_LAST
const __global INPUT0_TYPE* a_ptr = input0 + batch_offset_input0;
- #if HAS_DYNAMIC_K_PADDING || INPUT0_HAS_PADDING
+ #if INPUT0_HAS_DYNAMIC_PADDING || INPUT0_HAS_PADDING
const uint input0_offset = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, y, 1) - batch_offset_input0;
const uint input0_offset1 = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, y, (TILE_K)) - batch_offset_input0;
#else
@@ -200,14 +200,14 @@ KERNEL(gemm_tiled_opt)(
#endif // TRANSPOSE_INPUT0
#if TRANSPOSE_INPUT1 == TRANSPOSE_X_LAST
const __global INPUT1_TYPE* b_ptr = input1 + batch_offset_input1;
- #if HAS_DYNAMIC_K_PADDING || INPUT1_HAS_PADDING
+ #if INPUT1_HAS_DYNAMIC_PADDING || INPUT1_HAS_PADDING
const uint input1_offset = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, 1, tile_n_offset) - batch_offset_input1;
#else
const uint input1_offset = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR 0, 0, 0, 0, 1, 0);
#endif
#elif TRANSPOSE_INPUT1 == TRANSPOSE_Y_LAST
const __global INPUT1_TYPE* b_ptr = input1 + batch_offset_input1;
- #if HAS_DYNAMIC_K_PADDING || INPUT1_HAS_PADDING
+ #if INPUT1_HAS_DYNAMIC_PADDING || INPUT1_HAS_PADDING
const uint input1_offset = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, 0, (tile_n_offset + 1)) - batch_offset_input1;
const uint input1_offset1 = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, (TILE_K), tile_n_offset) - batch_offset_input1;
#else
@@ -386,7 +386,7 @@ KERNEL(gemm_tiled_opt)(
#endif // TRANSPOSE_INPUT1 == TRANSPOSE_Y_LAST
// Loading A tile and tile C calculation
-#if IS_DYNAMIC && !INDIRECT_INPUT0 && !HAS_DYNAMIC_K_PADDING && TRANSPOSE_INPUT0 == TRANSPOSE_X_LAST
+#if IS_DYNAMIC && !INDIRECT_INPUT0 && !INPUT0_HAS_DYNAMIC_PADDING && !INPUT1_HAS_DYNAMIC_PADDING && TRANSPOSE_INPUT0 == TRANSPOSE_X_LAST
A_FLOATN a_read = (TILE_K_NOT_DIVISIBLE == 0 || K_IS_ALIGNED_4BYTE) ? BLOCK_READ_A(a_ptr, 0): a_ptr[sglid];
#endif
unroll_for (uint dot_id = 0; dot_id < tile_m_iterations; dot_id++) {
@@ -395,7 +395,7 @@ KERNEL(gemm_tiled_opt)(
#if INDIRECT_INPUT0
uint a_idx = FUNC_CALL(get_input0_indirect_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, (y + dot_id), (k * TILE_K + sglid), beam_table);
A_FLOATN a_read = input0[a_idx];
- #elif HAS_DYNAMIC_K_PADDING || INPUT0_HAS_PADDING
+ #elif INPUT0_HAS_DYNAMIC_PADDING || INPUT1_HAS_DYNAMIC_PADDING || INPUT0_HAS_PADDING
// In case of dynamic padding we can't guarantee memory access alignment for
// block reads (4 bytes), so use scattered read
uint a_idx = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b, f, w, z, (y + dot_id), (k * TILE_K + sglid));
@@ -431,7 +431,7 @@ KERNEL(gemm_tiled_opt)(
#endif // TILE_K > SIMD_WIDTH
}
}
- #if IS_DYNAMIC && !INDIRECT_INPUT0 && !HAS_DYNAMIC_K_PADDING
+ #if IS_DYNAMIC && !INDIRECT_INPUT0 && !INPUT0_HAS_DYNAMIC_PADDING && !INPUT1_HAS_DYNAMIC_PADDING
// Read A for next dot_id
a_read = (dot_id + 1 < tile_m_iterations) ? (TILE_K_NOT_DIVISIBLE == 0 || K_IS_ALIGNED_4BYTE) ? BLOCK_READ_A(a_ptr, 0) : a_ptr[sglid] : 0;
#endif
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_utils.h b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_utils.h
index 2c8256b8551..04e15832b46 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_utils.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_utils.h
@@ -42,6 +42,7 @@ struct DimensionAccessHelperJit : virtual DimensionAccessHelperBase {
: DimensionAccessHelperBase(t) {
size_t dyn_shape_offset = t.get_dynamic_shape_offset();
size_t dyn_pad_offset = dyn_shape_offset + DataTensor::max_rank();
+ has_dynamic_pad = false;
for (auto d : dims) {
dims_sizes.push_back(toCodeString(d, dyn_shape_offset, padded, d.pad.is_dynamic, dyn_pad_offset));
dyn_shape_offset++;
@@ -49,6 +50,7 @@ struct DimensionAccessHelperJit : virtual DimensionAccessHelperBase {
if (d.pad.is_dynamic) {
pad_before_after_sizes.push_back("(shape_info[" + std::to_string(dyn_pad_offset++) + "])");
pad_before_after_sizes.push_back("(shape_info[" + std::to_string(dyn_pad_offset++) + "])");
+ has_dynamic_pad = true;
} else {
pad_before_after_sizes.push_back(toCodeString(d.pad.before));
pad_before_after_sizes.push_back(toCodeString(d.pad.after));
@@ -76,6 +78,7 @@ struct DimensionAccessHelperJit : virtual DimensionAccessHelperBase {
std::vector<std::string> dims_sizes;
std::vector<std::string> pad_before_after_sizes;
+ bool has_dynamic_pad;
};
std::vector<size_t> GetImageSizes(const kernel_selector::WeightsTensor& dimensions, const WeightsLayout layout);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/gemm/gemm_kernel_tiled_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/gemm/gemm_kernel_tiled_opt.cpp
index 43f32f3e8e1..65d4494a6e8 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/gemm/gemm_kernel_tiled_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/gemm/gemm_kernel_tiled_opt.cpp
@@ -205,14 +205,10 @@ JitConstants GemmKernelTiledOpt::GetJitConstants(const gemm_params& params) cons
else
jit.AddConstant(MakeJitConstant("TRANSPOSE_OUTPUT", 0 /* set as TRANSPOSE_X_LAST */));
- bool has_dynamic_k_padding = params.transpose_input0 ? params.inputs[0].Y().pad.is_dynamic
- : params.inputs[0].X().pad.is_dynamic;
- bool has_dynamic_n_padding = params.transpose_input1 ? params.inputs[1].Y().pad.is_dynamic
- : params.inputs[1].X().pad.is_dynamic;
- if (has_dynamic_k_padding)
- jit.AddConstant(MakeJitConstant("HAS_DYNAMIC_K_PADDING", 1));
- if (has_dynamic_n_padding)
- jit.AddConstant(MakeJitConstant("HAS_DYNAMIC_N_PADDING", 1));
+ if (dims0_padded.has_dynamic_pad)
+ jit.AddConstant(MakeJitConstant("INPUT0_HAS_DYNAMIC_PADDING", 1));
+ if (dims1_padded.has_dynamic_pad)
+ jit.AddConstant(MakeJitConstant("INPUT1_HAS_DYNAMIC_PADDING", 1));
} else {
auto get_transposed_dim_size = [](const kernel_selector::DataTensor &data_tensor,
const std::vector<int64_t>& dims_order, const std::string dim) {
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp
index b61d05767fe..adaa572878b 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/gemm_gpu_test.cpp
@@ -522,6 +522,168 @@ public:
}
}
+ void test_dynamic_padding_w_transpose_order(bool is_caching_test, bool n_dim_only) {
+ tests::random_generator rg;
+ rg.set_seed(GET_SUITE_NAME);
+
+ auto& engine = get_test_engine();
+
+ const unsigned long BATCH_SIZE = 128;
+ const unsigned long M_SIZE = 12;
+ const unsigned long K_SIZE = 64;
+ const unsigned long N_SIZE = 12;
+
+ auto fill_mem = [&](cldnn::memory_ptr mem, std::vector<ov::float16>& data) {
+ cldnn::mem_lock<ov::float16> mem_ptr(mem, get_test_stream());
+ auto&& l = mem->get_layout();
+ auto data_idx = 0;
+ for (cldnn::tensor::value_type b = 0; b < l.batch(); ++b) {
+ for (cldnn::tensor::value_type f = 0; f < l.feature(); ++f) {
+ for (cldnn::tensor::value_type y = 0; y < l.spatial(1); ++y) {
+ for (cldnn::tensor::value_type x = 0; x < l.spatial(0); ++x) {
+ auto tensor_coord = cldnn::tensor{{b, f, x, y}, 0};
+ auto buffer_idx = l.get_linear_offset(tensor_coord);
+ mem_ptr[buffer_idx] = data[data_idx++];
+ }
+ }
+ }
+ }
+ };
+
+ const auto align_size_m = 13;
+ const auto align_size_k = 16;
+ const auto align_size_n = 15;
+ const auto align_size_b1 = 3;
+ const auto align_size_b2 = 19;
+
+ const auto aligned_batch1_size = align_to(1ul, align_size_b1);
+ auto padding_size_batch1 = static_cast<int>(aligned_batch1_size - 1);
+
+ const auto aligned_batch2_size = align_to(BATCH_SIZE, align_size_b2);
+ auto padding_size_batch2 = static_cast<int>(aligned_batch2_size - BATCH_SIZE);
+
+ const auto aligned_m_size = align_to(M_SIZE, align_size_m);
+ auto padding_size_m = static_cast<int>(aligned_m_size - M_SIZE);
+ const auto aligned_k_size = align_to(K_SIZE, align_size_k);
+ auto padding_size_k = static_cast<int>(aligned_k_size - K_SIZE);
+ const auto aligned_n_size = align_to(N_SIZE, align_size_n);
+ auto padding_size_n = static_cast<int>(aligned_n_size - N_SIZE);
+
+ ov::Shape in1_shape = { 1, BATCH_SIZE, M_SIZE, K_SIZE };
+ ov::Shape in2_shape = { 1, BATCH_SIZE, N_SIZE, K_SIZE };
+ ov::Shape in1_shape_aligned = { aligned_batch1_size, aligned_batch2_size, aligned_m_size, aligned_k_size };
+ ov::Shape in2_shape_aligned = { aligned_batch1_size, aligned_batch2_size, aligned_n_size, aligned_k_size };
+
+ // Use dynamic padding for all BFYX dimensions
+ padding::DynamicDimsMask dyn_pad_dims_input1;
+ padding::DynamicDimsMask dyn_pad_dims_input2;
+
+ if (n_dim_only) {
+ dyn_pad_dims_input1 = 0ul;
+ dyn_pad_dims_input2 = padding::DynamicDimsMask("1000");
+ } else {
+ dyn_pad_dims_input1 = padding::DynamicDimsMask("1111");
+ dyn_pad_dims_input2 = padding::DynamicDimsMask("1111");
+ }
+
+ auto in1_layout = layout{ {-1, -1, -1, -1}, data_types::f16, format::bfyx, padding({0, 0, 0, 0}, {0, 0, 0, 0}, dyn_pad_dims_input1)};
+ auto in2_layout = layout{ {-1, -1, -1, -1}, data_types::f16, format::bfyx, padding({0, 0, 0, 0}, {0, 0, 0, 0}, dyn_pad_dims_input2)};
+
+ auto aligned_input1_mem = engine.allocate_memory({ov::PartialShape(in1_shape_aligned), data_types::f16, format::bfyx});
+ auto aligned_input2_mem = engine.allocate_memory({ov::PartialShape(in2_shape_aligned), data_types::f16, format::bfyx});
+
+ auto input1_mem = engine.reinterpret_buffer(*aligned_input1_mem, layout{ov::PartialShape(in1_shape),
+ data_types::f16,
+ format::bfyx,
+ n_dim_only ? padding({0, 0, 0, 0 }, {0, 0, 0, 0}, dyn_pad_dims_input1) :
+ padding({padding_size_batch1, 0, 0, 0}, {0, padding_size_batch2, padding_size_m, padding_size_k}, dyn_pad_dims_input1)});
+
+ auto input2_mem = engine.reinterpret_buffer(*aligned_input2_mem, layout{ov::PartialShape(in2_shape),
+ data_types::f16,
+ format::bfyx,
+ n_dim_only ? padding({0, 0, 0, 0}, {0, 0, 0, padding_size_n}, dyn_pad_dims_input2) :
+ padding({0, padding_size_batch2, 0, 0}, {padding_size_batch1, 0, padding_size_n, padding_size_k }, dyn_pad_dims_input2)});
+
+ auto input_1_data = rg.generate_random_1d<ov::float16>(ov::shape_size(in1_shape), -2, 2);
+ auto input_2_data = rg.generate_random_1d<ov::float16>(ov::shape_size(in2_shape), -2, 2);
+
+ fill_mem(input1_mem, input_1_data);
+ fill_mem(input2_mem, input_2_data);
+
+ auto get_ref_results = [&]() {
+ ov::Shape in1_shape = { 1, BATCH_SIZE, M_SIZE, K_SIZE };
+ ov::Shape in2_shape = { 1, BATCH_SIZE, N_SIZE, K_SIZE };
+ auto in1_layout = layout{ {-1, -1, -1, -1}, data_types::f16, format::bfyx};
+ auto in2_layout = layout{ {-1, -1, -1, -1}, data_types::f16, format::bfyx};
+
+ auto input1_mem = engine.allocate_memory(layout{ov::PartialShape(in1_shape), data_types::f16, format::bfyx});
+ auto input2_mem = engine.allocate_memory(layout{ov::PartialShape(in2_shape), data_types::f16, format::bfyx});
+
+ fill_mem(input1_mem, input_1_data);
+ fill_mem(input2_mem, input_2_data);
+
+ topology topology;
+ topology.add(input_layout("input1", in1_layout),
+ input_layout("input2", in2_layout),
+ gemm("gemm_ref", { input_info("input1"), input_info("input2") }, data_types::f16,
+ {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 1, 2, 3})
+ );
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ network network(engine, topology, config);
+ network.set_input_data("input1", input1_mem);
+ network.set_input_data("input2", input2_mem);
+
+ auto outputs = network.execute();
+ OPENVINO_ASSERT(outputs.size() == 1);
+ OPENVINO_ASSERT(outputs.begin()->first == "gemm_ref");
+
+ auto inst = network.get_primitive("gemm_ref");
+
+ auto output_mem = outputs.at("gemm_ref").get_memory();
+ auto output_layout = outputs.at("gemm_ref").get_layout();
+
+ return engine.reinterpret_buffer(*output_mem, output_layout);
+ };
+
+ topology topology;
+ topology.add(input_layout("input1", in1_layout),
+ input_layout("input2", in2_layout),
+ gemm("gemm", { input_info("input1"), input_info("input2") }, data_types::f16,
+ {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 1, 2, 3})
+ );
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network::ptr network = get_network(engine, topology, config, get_test_stream_ptr(), is_caching_test);
+ network->set_input_data("input1", input1_mem);
+ network->set_input_data("input2", input2_mem);
+
+ auto inst = network->get_primitive("gemm");
+ auto impl = inst->get_impl();
+ ASSERT_TRUE(impl != nullptr);
+ ASSERT_TRUE(impl->is_dynamic());
+
+ auto outputs = network->execute();
+
+ auto output_mem = outputs.at("gemm").get_memory();
+ auto output_layout = outputs.at("gemm").get_layout();
+
+ auto res = engine.reinterpret_buffer(*output_mem, output_layout);
+
+ auto ref_res = get_ref_results();
+
+ mem_lock<ov::float16> res_lock(res, get_test_stream());
+ mem_lock<ov::float16> res_ref_lock(ref_res, get_test_stream());
+ for (size_t i = 0; i < res->count(); i++) {
+ ASSERT_EQ(res_lock[i], res_ref_lock[i]) << i;
+ }
+ }
+
void test_dynamic_multi_inference_same_shape(bool is_caching_test) {
auto& engine = get_test_engine();
@@ -1433,6 +1595,16 @@ TEST_F(gemm_gpu_tests, dynamic_padding_n_dim_only) {
this->test_dynamic_padding(false, true);
}
+#ifndef ENABLE_ONEDNN_FOR_GPU
+// Disable onednn test because onednn does not support format_tag::cbda, format_tag::badc.
+TEST_F(gemm_gpu_tests, dynamic_padding_w_transpose_order_all_dim) {
+ this->test_dynamic_padding_w_transpose_order(false, false);
+}
+
+TEST_F(gemm_gpu_tests, dynamic_padding_w_transpose_order_n_dim_only) {
+ this->test_dynamic_padding_w_transpose_order(false, true);
+}
+#endif
TEST_F(gemm_gpu_tests, dynamic_multi_inference_same_shape) {
this->test_dynamic_multi_inference_same_shape(false);