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] Fixing convolution fsv16 kernels to avoid crashes in Level Zero (#36280)
Mikolaj Roszczykdfe9b3809828
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16.clindex b52ef8a4628..9779e5c63e8 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16.cl@@ -60,6 +60,7 @@ KERNEL(convolution_bfyx_f16)(const int xy = get_global_id(0);const int x = (xy % X_BLOCKS) * OUTPUT_X_BLOCK_SIZE;const int y = (xy / X_BLOCKS);+ const int input_spatial_size_x = INPUT0_SIZE_X;const int lid1 = (int)get_local_id(1);const int feature_per_wg = (int)get_local_size(1) / SLM_DIV_FACTOR;@@ -187,7 +188,8 @@ KERNEL(convolution_bfyx_f16)({for (int xb = 0; xb < INPUT_LINE_SIZE; xb++){- if (icb * FEATURE_SLICE_SIZE + sglid >= FILTER_IFM_NUM)+ const int in_x = input_x + xb;+ if (icb * FEATURE_SLICE_SIZE + sglid >= FILTER_IFM_NUM || in_x < 0 || in_x >= input_spatial_size_x)line_cache[xb] = 0;elseline_cache[xb] = input[grouped_input_offset +@@ -200,38 +202,54 @@ KERNEL(convolution_bfyx_f16)(else#endif // INPUT_LEFTOVERS{- int xb = 0;- for (; xb + 8 <= INPUT_LINE_SIZE; xb += 8) {- INPUT_TYPE8 vv = DT_INPUT_BLOCK_READ8(input, grouped_input_offset +- icb * input_fs_pitch +- kh * DILATION_SIZE_Y * input_y_pitch +- xb * input_x_pitch);-- line_cache[xb + 0] = vv[0];- line_cache[xb + 1] = vv[1];- line_cache[xb + 2] = vv[2];- line_cache[xb + 3] = vv[3];- line_cache[xb + 4] = vv[4];- line_cache[xb + 5] = vv[5];- line_cache[xb + 6] = vv[6];- line_cache[xb + 7] = vv[7];- }- for (; xb + 4 <= INPUT_LINE_SIZE; xb += 4) {- INPUT_TYPE4 vv = DT_INPUT_BLOCK_READ4(input, grouped_input_offset +- icb * input_fs_pitch +- kh * DILATION_SIZE_Y * input_y_pitch +- xb * input_x_pitch);-- line_cache[xb + 0] = vv[0];- line_cache[xb + 1] = vv[1];- line_cache[xb + 2] = vv[2];- line_cache[xb + 3] = vv[3];- }- for (; xb < INPUT_LINE_SIZE; xb++) {- line_cache[xb] = DT_INPUT_BLOCK_READ(input, grouped_input_offset +- icb * input_fs_pitch +- kh * DILATION_SIZE_Y * input_y_pitch +- xb * input_x_pitch);+ int valid_start = max(0, -input_x);+ int valid_end = min(INPUT_LINE_SIZE, input_spatial_size_x - input_x);++ if (valid_start <= 0 && valid_end >= INPUT_LINE_SIZE) {+ int xb = 0;+ for (; xb + 8 <= INPUT_LINE_SIZE; xb += 8) {+ INPUT_TYPE8 vv = DT_INPUT_BLOCK_READ8(input, grouped_input_offset ++ icb * input_fs_pitch ++ kh * DILATION_SIZE_Y * input_y_pitch ++ xb * input_x_pitch);++ line_cache[xb + 0] = vv[0];+ line_cache[xb + 1] = vv[1];+ line_cache[xb + 2] = vv[2];+ line_cache[xb + 3] = vv[3];+ line_cache[xb + 4] = vv[4];+ line_cache[xb + 5] = vv[5];+ line_cache[xb + 6] = vv[6];+ line_cache[xb + 7] = vv[7];+ }+ for (; xb + 4 <= INPUT_LINE_SIZE; xb += 4) {+ INPUT_TYPE4 vv = DT_INPUT_BLOCK_READ4(input, grouped_input_offset ++ icb * input_fs_pitch ++ kh * DILATION_SIZE_Y * input_y_pitch ++ xb * input_x_pitch);++ line_cache[xb + 0] = vv[0];+ line_cache[xb + 1] = vv[1];+ line_cache[xb + 2] = vv[2];+ line_cache[xb + 3] = vv[3];+ }+ for (; xb < INPUT_LINE_SIZE; xb++) {+ line_cache[xb] = DT_INPUT_BLOCK_READ(input, grouped_input_offset ++ icb * input_fs_pitch ++ kh * DILATION_SIZE_Y * input_y_pitch ++ xb * input_x_pitch);+ }+ } else {+ for (int xb = 0; xb < INPUT_LINE_SIZE; xb++) {+ if (xb < valid_start || xb >= valid_end) {+ line_cache[xb] = 0;+ } else {+ line_cache[xb] = DT_INPUT_BLOCK_READ(input, grouped_input_offset ++ icb * input_fs_pitch ++ kh * DILATION_SIZE_Y * input_y_pitch ++ xb * input_x_pitch);+ }+ }}}diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_1x1.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_1x1.clindex bb474e00779..107f1839b4f 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_1x1.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_1x1.cl@@ -41,6 +41,9 @@ KERNEL(convolution_b_fs_yx_fsv16_1x1)(const uint xy = (int)get_global_id(0);const uint x = (xy * X_BLOCK_SIZE) % OUTPUT_SIZE_X;const uint y = (xy * X_BLOCK_SIZE) / OUTPUT_SIZE_X;+ const uint spatial_base = xy * X_BLOCK_SIZE;+ const uint input_spatial_size = INPUT0_SIZE_X * INPUT0_SIZE_Y;+ const uint full_spatial_block = (spatial_base + X_BLOCK_SIZE) <= input_spatial_size;const uint input_x = x;const uint input_y = y;@@ -131,11 +134,16 @@ KERNEL(convolution_b_fs_yx_fsv16_1x1)(__attribute__((opencl_unroll_hint(X_BLOCK_SIZE)))for (int i = 0; i < X_BLOCK_SIZE; i++){- const uint xb = (x + i) % INPUT0_SIZE_X;- const uint yb = y + (x + i) / INPUT0_SIZE_X;- const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;-- src[i] = input[input_idx + sglid];+ const uint spatial_idx = spatial_base + i;+ if (spatial_idx < input_spatial_size) {+ const uint xb = spatial_idx % INPUT0_SIZE_X;+ const uint yb = spatial_idx / INPUT0_SIZE_X;+ const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;++ src[i] = input[input_idx + sglid];+ } else {+ src[i] = UNIT_VAL_ZERO;+ }}#elsesrc = input[input_offset + k * input_fs_pitch + sglid];@@ -146,14 +154,31 @@ KERNEL(convolution_b_fs_yx_fsv16_1x1)({#if PADDED_INPUT#if X_BLOCK_SIZE > 1- __attribute__((opencl_unroll_hint(X_BLOCK_SIZE)))- for (int i = 0; i < X_BLOCK_SIZE; i++)- {- const uint xb = (x + i) % INPUT0_SIZE_X;- const uint yb = y + (x + i) / INPUT0_SIZE_X;- const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;+ if (full_spatial_block) {+ __attribute__((opencl_unroll_hint(X_BLOCK_SIZE)))+ for (int i = 0; i < X_BLOCK_SIZE; i++)+ {+ const uint xb = (x + i) % INPUT0_SIZE_X;+ const uint yb = y + (x + i) / INPUT0_SIZE_X;+ const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;- src[i] = UNIT_BLOCK_READ(input, input_idx);+ src[i] = UNIT_BLOCK_READ(input, input_idx);+ }+ } else {+ __attribute__((opencl_unroll_hint(X_BLOCK_SIZE)))+ for (int i = 0; i < X_BLOCK_SIZE; i++)+ {+ const uint spatial_idx = spatial_base + i;+ if (spatial_idx < input_spatial_size) {+ const uint xb = spatial_idx % INPUT0_SIZE_X;+ const uint yb = spatial_idx / INPUT0_SIZE_X;+ const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;++ src[i] = UNIT_BLOCK_READ(input, input_idx);+ } else {+ src[i] = UNIT_VAL_ZERO;+ }+ }}#elsesrc = UNIT_BLOCK_READ(input, input_offset + k * input_fs_pitch);@@ -162,7 +187,24 @@ KERNEL(convolution_b_fs_yx_fsv16_1x1)(#else // PADDED_INPUT#if X_BLOCK_SIZE > 1- src = UNIT_BLOCK_READ_VEC(input, input_offset + k * input_fs_pitch + input_y * input_y_pitch + input_x * input_x_pitch);+ if (full_spatial_block) {+ src = UNIT_BLOCK_READ_VEC(input, input_offset + k * input_fs_pitch + input_y * input_y_pitch + input_x * input_x_pitch);+ } else {+ __attribute__((opencl_unroll_hint(X_BLOCK_SIZE)))+ for (int i = 0; i < X_BLOCK_SIZE; i++)+ {+ const uint spatial_idx = spatial_base + i;+ if (spatial_idx < input_spatial_size) {+ const uint xb = spatial_idx % INPUT0_SIZE_X;+ const uint yb = spatial_idx / INPUT0_SIZE_X;+ const uint input_idx = input_offset + k * input_fs_pitch + yb * input_y_pitch + xb * input_x_pitch;++ src[i] = UNIT_BLOCK_READ(input, input_idx);+ } else {+ src[i] = UNIT_VAL_ZERO;+ }+ }+ }#elsesrc = UNIT_BLOCK_READ(input, input_offset + k * input_fs_pitch);#endif // X_BLOCK_SIZE > 1diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/convolution/convolution_kernel_b_fs_yx_fsv16_1x1.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/convolution/convolution_kernel_b_fs_yx_fsv16_1x1.cppindex 5f380d97481..6585352900c 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/convolution/convolution_kernel_b_fs_yx_fsv16_1x1.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/convolution/convolution_kernel_b_fs_yx_fsv16_1x1.cpp@@ -2,7 +2,6 @@// SPDX-License-Identifier: Apache-2.0//-#include <iostream>#include "convolution_kernel_b_fs_yx_fsv16_1x1.h"#include "kernel_selector_utils.h"#include <string>@@ -273,10 +272,10 @@ JitConstants ConvolutionKernel_b_fs_yx_fsv16_1x1::GetJitConstants(const convolutDimensionAccessHelperJit output_dims(params.outputs[0]);DimensionAccessHelperJit output_padded_dims(params.outputs[0], true);- const auto padded_input = "(" + input0_padded_dims.x_pad().first + "+" + input0_padded_dims.x_pad().first + ") != 0";+ const auto padded_input = "(" + input0_padded_dims.x_pad().first + "+" + input0_padded_dims.x_pad().second + ") != 0";jit.AddConstant(MakeJitConstant("PADDED_INPUT", padded_input));- const auto padded_output = "(" + output_padded_dims.x_pad().first + "+" + output_padded_dims.x_pad().first + ") != 0";+ const auto padded_output = "(" + output_padded_dims.x_pad().first + "+" + output_padded_dims.x_pad().second + ") != 0";jit.AddConstant(MakeJitConstant("PADDED_OUTPUT", padded_output));// In shape agnostic kernel, the fused shape cannot be specified at build time or run time.diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cppindex d933ec2d433..6e69b5be245 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp@@ -12771,3 +12771,227 @@ TEST(convolution_gpu, onednn_custom_format_3d_weights) {});}#endif // ENABLE_ONEDNN_FOR_GPU++TEST(convolution_gpu_bfyx_f16, dynamic_padded_input_asymmetric_x_padding_1x1) {+ auto& engine = get_test_engine();++ if (!engine.get_device_info().supports_fp16)+ GTEST_SKIP() << "fp16 is not supported on this device.";++ const int batch = 1, in_f = 32, out_f = 32, in_h = 7, in_w = 8;+ const int x_pad_after = 4;++ auto input_mem = engine.allocate_memory({ ov::Shape{batch, in_f, in_h, in_w}, data_types::f16, format::bfyx });+ auto weights = engine.allocate_memory({ ov::Shape{out_f, in_f, 1, 1}, data_types::f16, format::bfyx });++ tests::random_generator rg(GET_SUITE_NAME);+ set_values(input_mem, rg.generate_random_1d<ov::float16>(batch * in_f * in_h * in_w, -5, 5));+ set_values(weights, rg.generate_random_1d<ov::float16>(out_f * in_f, -5, 5));++ padding x_padding({0, 0, 0, 0}, {0, 0, 0, x_pad_after});+ layout padded_fsv16(data_types::f16, format::b_fs_yx_fsv16, tensor{batch, in_f, in_w, in_h}, x_padding);+ layout plain_fsv16 (data_types::f16, format::b_fs_yx_fsv16, tensor{batch, in_f, in_w, in_h});++ layout dyn_input_layout{ov::PartialShape::dynamic(4), data_types::f16, format::bfyx};++ auto make_topology = [&](bool with_x_padding) {+ return topology(+ input_layout("input", dyn_input_layout),+ reorder("reordered", input_info("input"), with_x_padding ? padded_fsv16 : plain_fsv16),+ data("weights", weights),+ convolution("conv", input_info("reordered"), "weights", no_bias, 1,+ ov::Strides{1, 1}, ov::Strides{1, 1},+ ov::CoordinateDiff{0, 0}, ov::CoordinateDiff{0, 0}, false));+ };++ ExecutionConfig cfg_test = get_test_default_config(engine);+ cfg_test.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ { "conv", { format::b_fs_yx_fsv16, "convolution_gpu_bfyx_f16_1x1", impl_types::ocl } }+ }));+ cfg_test.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ ExecutionConfig cfg_ref = get_test_default_config(engine);+ cfg_ref.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ { "conv", { format::b_fs_yx_fsv16, "convolution_gpu_ref", impl_types::ocl } }+ }));+ cfg_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net_test(engine, make_topology(/*with_x_padding=*/true), cfg_test);+ network net_ref (engine, make_topology(/*with_x_padding=*/true), cfg_ref);++ net_test.set_input_data("input", input_mem);+ net_ref .set_input_data("input", input_mem);++ cldnn::mem_lock<ov::float16> out(net_test.execute().at("conv").get_memory(), get_test_stream());+ cldnn::mem_lock<ov::float16> ref(net_ref .execute().at("conv").get_memory(), get_test_stream());++ ASSERT_EQ(out.size(), ref.size());+ for (size_t i = 0; i < out.size(); i++)+ ASSERT_EQ(out[i], ref[i]) << "Mismatch at idx=" << i;+}++TEST(convolution_gpu_bfyx_f16, dynamic_tail_spatial_block_with_output_padding_1x1) {+ auto& engine = get_test_engine();++ if (!engine.get_device_info().supports_fp16)+ GTEST_SKIP() << "fp16 is not supported on this device.";++ const ov::Shape in_shape = {1, 32, 3, 5}; // X*Y = 15 => tail when X_BLOCK_SIZE is 2/4/8+ const ov::Shape wei_shape = {32, 32, 1, 1};++ auto input_mem = engine.allocate_memory({in_shape, data_types::f16, format::bfyx});+ auto weights = engine.allocate_memory({wei_shape, data_types::f16, format::bfyx});++ tests::random_generator rg(GET_SUITE_NAME);+ set_values(input_mem, rg.generate_random_1d<ov::float16>(ov::shape_size(in_shape), -5, 5));+ set_values(weights, rg.generate_random_1d<ov::float16>(ov::shape_size(wei_shape), -5, 5));++ layout dyn_input_layout{ov::PartialShape::dynamic(4), data_types::f16, format::bfyx};+ layout input_fsv16_layout{in_shape, data_types::f16, format::b_fs_yx_fsv16};++ auto make_topo = [&]() {+ auto conv = convolution("conv",+ input_info("input"),+ "weights",+ no_bias,+ 1,+ ov::Strides{1, 1},+ ov::Strides{1, 1},+ ov::CoordinateDiff{0, 0},+ ov::CoordinateDiff{0, 0},+ false,+ ov::op::PadType::EXPLICIT);++ conv.output_paddings = {padding{{0, 0, 1, 2}, 0}};++ return topology(+ input_layout("input", dyn_input_layout),+ reorder("input_fsv16", input_info("input"), input_fsv16_layout),+ data("weights", weights),+ conv);+ };++ // Target kernel+ ExecutionConfig cfg_test = get_test_default_config(engine);+ cfg_test.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ {"conv", {format::b_fs_yx_fsv16, "convolution_gpu_bfyx_f16_1x1", impl_types::ocl}}+ }));+ cfg_test.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ // Reference kernel+ ExecutionConfig cfg_ref = get_test_default_config(engine);+ cfg_ref.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ {"conv", {format::b_fs_yx_fsv16, "convolution_gpu_ref", impl_types::ocl}}+ }));+ cfg_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net_test(engine, make_topo(), cfg_test);+ network net_ref(engine, make_topo(), cfg_ref);++ net_test.set_input_data("input", input_mem);+ net_ref.set_input_data("input", input_mem);++ auto out_test = net_test.execute();+ auto out_ref = net_ref.execute();++ auto mem_test = out_test.at("conv").get_memory();+ auto mem_ref = out_ref.at("conv").get_memory();++ cldnn::mem_lock<ov::float16> ptr_test(mem_test, get_test_stream());+ cldnn::mem_lock<ov::float16> ptr_ref(mem_ref, get_test_stream());++ ASSERT_EQ(mem_test->get_layout(), mem_ref->get_layout());+ ASSERT_EQ(ptr_test.size(), ptr_ref.size());++ const float atol = 0.1f;+ const float rtol = 1e-2f;+ for (size_t i = 0; i < ptr_test.size(); i++) {+ const float test_val = float(ptr_test[i]);+ const float ref_val = float(ptr_ref[i]);+ const float tol = atol + rtol * std::fabs(ref_val);+ ASSERT_NEAR(test_val, ref_val, tol) << "Mismatch at idx=" << i;+ }+}++TEST(convolution_gpu_bfyx_f16, dynamic_tail_spatial_block_with_output_padding) {+ auto& engine = get_test_engine();++ if (!engine.get_device_info().supports_fp16)+ GTEST_SKIP() << "fp16 is not supported on this device.";++ const ov::Shape in_shape = {1, 32, 3, 5};+ const ov::Shape wei_shape = {32, 32, 3, 3};++ auto input_mem = engine.allocate_memory({in_shape, data_types::f16, format::bfyx});+ auto weights = engine.allocate_memory({wei_shape, data_types::f16, format::bfyx});++ tests::random_generator rg(GET_SUITE_NAME);+ set_values(input_mem, rg.generate_random_1d<ov::float16>(ov::shape_size(in_shape), -5, 5));+ set_values(weights, rg.generate_random_1d<ov::float16>(ov::shape_size(wei_shape), -5, 5));++ layout dyn_input_layout{ov::PartialShape::dynamic(4), data_types::f16, format::bfyx};+ layout input_fsv16_layout{in_shape, data_types::f16, format::b_fs_yx_fsv16};++ auto make_topo = [&]() {+ auto conv = convolution("conv",+ input_info("input"),+ "weights",+ no_bias,+ 1,+ ov::Strides{1, 1},+ ov::Strides{1, 1},+ ov::CoordinateDiff{1, 1},+ ov::CoordinateDiff{1, 1},+ false,+ ov::op::PadType::EXPLICIT);++ conv.output_paddings = {padding{{0, 0, 1, 2}, 0}};++ return topology(+ input_layout("input", dyn_input_layout),+ reorder("input_fsv16", input_info("input"), input_fsv16_layout),+ data("weights", weights),+ conv);+ };++ // Target kernel+ ExecutionConfig cfg_test = get_test_default_config(engine);+ cfg_test.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ {"conv", {format::b_fs_yx_fsv16, "convolution_gpu_bfyx_f16", impl_types::ocl}}+ }));+ cfg_test.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ // Reference kernel+ ExecutionConfig cfg_ref = get_test_default_config(engine);+ cfg_ref.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{+ {"conv", {format::b_fs_yx_fsv16, "convolution_gpu_ref", impl_types::ocl}}+ }));+ cfg_ref.set_property(ov::intel_gpu::allow_new_shape_infer(true));++ network net_test(engine, make_topo(), cfg_test);+ network net_ref(engine, make_topo(), cfg_ref);++ net_test.set_input_data("input", input_mem);+ net_ref.set_input_data("input", input_mem);++ auto out_test = net_test.execute();+ auto out_ref = net_ref.execute();++ auto mem_test = out_test.at("conv").get_memory();+ auto mem_ref = out_ref.at("conv").get_memory();++ cldnn::mem_lock<ov::float16> ptr_test(mem_test, get_test_stream());+ cldnn::mem_lock<ov::float16> ptr_ref(mem_ref, get_test_stream());++ ASSERT_EQ(mem_test->get_layout(), mem_ref->get_layout());+ ASSERT_EQ(ptr_test.size(), ptr_ref.size());++ const float atol = 0.5f;+ const float rtol = 1e-2f;+ for (size_t i = 0; i < ptr_test.size(); i++) {+ const float test_val = float(ptr_test[i]);+ const float ref_val = float(ptr_ref[i]);+ const float tol = atol + rtol * std::fabs(ref_val);+ ASSERT_NEAR(test_val, ref_val, tol) << "Mismatch at idx=" << i;+ }+}