Skip to content

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

[GPU] Fix RMS bfyx_opt kernel producing wrong results with static padding (#35350)

zlma26c3c8f94d41
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rms_gpu_bfyx_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rms_gpu_bfyx_opt.cl
index 53efd93775e..771df857aeb 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rms_gpu_bfyx_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rms_gpu_bfyx_opt.cl
@@ -43,7 +43,7 @@ KERNEL(rms_gpu_bfyx_opt)(
const uint items_num = data_size / workers_per_data;
const uint leftovers = data_size % workers_per_data;
- #if HAS_DYNAMIC_PADDING
+ #if HAS_PADDING
uint b_idx = 0;
uint f_idx = 0;
uint z_idx = 0;
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/rms/rms_kernel_bfyx_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/rms/rms_kernel_bfyx_opt.cpp
index 56513be1772..ffb52b9a8fe 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/rms/rms_kernel_bfyx_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/rms/rms_kernel_bfyx_opt.cpp
@@ -54,12 +54,20 @@ DeviceFeaturesKey RMSKernelBfyxOpt::get_required_device_features_key(const Param
JitConstants RMSKernelBfyxOpt::GetJitConstants(const rms_params& params, DispatchData dispatchData) const {
auto jit = Parent::GetJitConstants(params, dispatchData);
- bool has_dynamic_padding = false;
- for (const auto& dim : params.inputs[0].GetDims())
- has_dynamic_padding |= dim.pad.is_dynamic;
+ // Check for any padding (dynamic or static) on input dimensions.
+ // The flat addressing path (data_idx * data_size) assumes contiguous memory,
+ // which breaks when padding introduces gaps between slices (e.g., from in-place crop).
+ // Switch to per-dimension indexed addressing via get_input_index() in that case.
+ bool has_padding = false;
+ for (const auto& dim : params.inputs[0].GetDims()) {
+ if (dim.pad.is_dynamic || dim.pad.before != 0 || dim.pad.after != 0) {
+ has_padding = true;
+ break;
+ }
+ }
- if (has_dynamic_padding)
- jit.AddConstant(MakeJitConstant("HAS_DYNAMIC_PADDING", 1));
+ if (has_padding)
+ jit.AddConstant(MakeJitConstant("HAS_PADDING", 1));
if (params.has_dynamic_tensors()) {
const auto& input = params.inputs[0];
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/rms_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/rms_gpu_test.cpp
index 490db4b4a79..d2cac945299 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/rms_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/rms_gpu_test.cpp
@@ -4,7 +4,9 @@
#include "test_utils.h"
+#include <intel_gpu/primitives/crop.hpp>
#include <intel_gpu/primitives/input_layout.hpp>
+#include <intel_gpu/primitives/reorder.hpp>
#include <intel_gpu/primitives/rms.hpp>
#include "rms_inst.h"
@@ -30,36 +32,36 @@ void rms_ref(const memory::ptr input, const memory::ptr gamma, memory::ptr outpu
weight = std::make_unique<cldnn::mem_lock<T>>(gamma, get_test_stream());
}
+ // RMS normalization across the last (innermost populated) dimension.
+ // For bfyx with x_size > 1 (rank 4): normalize across X per (b, f, y).
+ // For bfyx with x_size == 1 (rank 3 mapped to bfyx): normalize across Y per (b, f).
+ // This matches the kernel behavior based on ov_input_rank.
+ bool norm_over_x = (x_size > 1);
+ uint32_t outer_y = norm_over_x ? y_size : 1;
+ uint32_t norm_size = norm_over_x ? x_size : y_size;
+
for (uint32_t b = 0; b < batch_size; ++b) {
for (uint32_t f = 0; f < feature_size; ++f) {
- float rms = 0.f;
- for (uint32_t y = 0; y < y_size; ++y) {
- for (uint32_t x = 0; x < x_size; ++x) {
- auto tensor_src = tensor(batch(b), feature(f), spatial(x, y, 0, 0));
- size_t src_offset = input_layout.get_linear_offset(tensor_src);
- rms += std::pow(static_cast<float>(src[src_offset]), 2);
+ for (uint32_t oy = 0; oy < outer_y; ++oy) {
+ float rms = 0.f;
+ for (uint32_t n = 0; n < norm_size; ++n) {
+ uint32_t y = norm_over_x ? oy : n;
+ uint32_t x = norm_over_x ? n : 0;
+ auto t = tensor(batch(b), feature(f), spatial(x, y, 0, 0));
+ rms += std::pow(static_cast<float>(src[input_layout.get_linear_offset(t)]), 2);
}
- }
- rms /= y_size * x_size;
- rms += epsilon;
- rms = std::pow(std::sqrt(rms), -1);
-
- for (uint32_t y = 0; y < y_size; ++y) {
- for (uint32_t x = 0; x < x_size; ++x) {
- auto tensor_src = tensor(batch(b), feature(f), spatial(x, y, 0, 0));
- auto tensor_dst = tensor(batch(b), feature(f), spatial(x, y, 0, 0));
- size_t src_offset = input_layout.get_linear_offset(tensor_src);
- size_t dst_offset = input_layout.get_linear_offset(tensor_dst);
-
- float gamma_val = 1.0f;
- if (weight) {
- auto tensor_weight = tensor(batch(0), feature(0), spatial(x, y, 0, 0));
- size_t weight_offset = input_layout.get_linear_offset(tensor_weight);
- gamma_val = static_cast<float>((*weight)[weight_offset]);
- }
-
- float result = rms * static_cast<float>(src[src_offset]) * gamma_val;
- dst[dst_offset] = static_cast<T>(result);
+ rms /= norm_size;
+ rms += epsilon;
+ rms = std::pow(std::sqrt(rms), -1);
+
+ for (uint32_t n = 0; n < norm_size; ++n) {
+ uint32_t y = norm_over_x ? oy : n;
+ uint32_t x = norm_over_x ? n : 0;
+ auto t = tensor(batch(b), feature(f), spatial(x, y, 0, 0));
+ size_t offset = input_layout.get_linear_offset(t);
+
+ float gamma_val = weight ? static_cast<float>((*weight)[n]) : 1.0f;
+ dst[offset] = static_cast<T>(rms * static_cast<float>(src[offset]) * gamma_val);
}
}
}
@@ -538,3 +540,108 @@ TEST(rms_gpu_test, rms_test_without_gamma_dyn) {
EXPECT_NEAR(output_ptr[i], output_ref_ptr[i], 1e-3);
}
}
+
+// Regression test for in-place crop on spatial axis followed by RMS normalization.
+// RMS kernels may not correctly handle spatial padding introduced by in-place crop.
+TEST(rms_gpu_test, in_place_crop_rms_spatial_split) {
+ auto& engine = get_test_engine();
+
+ // Input [1,2,3,16] bfyx. VariadicSplit on axis 2 (spatial Y) into [2, 1].
+ // crop_0 covers y=[0..1] (size 2), crop_1 covers y=2 (size 1).
+ // Each crop feeds RMS normalization.
+ // dim_x = 16 is the minimum for the bfyx_opt kernel (requires gamma >= subgroup_size(16)).
+ // force_implementations ensures bfyx_opt is selected regardless of other heuristics.
+ const int64_t dim_b = 1, dim_f = 2, dim_y = 3, dim_x = 16;
+ const int64_t split0 = 2, split1 = 1;
+ const int64_t off_y0 = 0, off_y1 = 2;
+ const float epsilon = 1e-6f;
+
+ auto input_mem = engine.allocate_memory({ov::PartialShape{dim_b, dim_f, dim_y, dim_x},
+ data_types::f32, format::bfyx});
+ auto axis_mem = engine.allocate_memory({ov::PartialShape{}, data_types::i64, format::bfyx});
+ auto splits_length_mem = engine.allocate_memory({ov::PartialShape{2}, data_types::i64, format::bfyx});
+ // Gamma weights for RMS: shape [1, 1, 1, dim_x] — per-element scale on X axis
+ auto gamma_mem = engine.allocate_memory({ov::PartialShape{1, 1, 1, dim_x}, data_types::f32, format::bfyx});
+
+ const int64_t axis = 2;
+ const size_t total = static_cast<size_t>(dim_b * dim_f * dim_y * dim_x);
+
+ // Deterministic input with small values
+ std::vector<float> input_data(total);
+ for (size_t i = 0; i < total; i++)
+ input_data[i] = static_cast<float>(i + 1);
+ set_values(input_mem, input_data);
+ set_values<int64_t>(axis_mem, {axis});
+ set_values<int64_t>(splits_length_mem, {split0, split1});
+ // Gamma = 1.0 (identity scale) for easy reference computation
+ std::vector<float> gamma_data(dim_x, 1.0f);
+ set_values(gamma_mem, gamma_data);
+
+ // Prepare crop slices and compute reference outputs using rms_ref
+ auto make_crop_ref = [&](int64_t split_size, int64_t y_offset) {
+ auto crop_mem = engine.allocate_memory({ov::PartialShape{dim_b, dim_f, split_size, dim_x}, data_types::f32, format::bfyx});
+ auto out_mem = engine.allocate_memory({ov::PartialShape{dim_b, dim_f, split_size, dim_x}, data_types::f32, format::bfyx});
+ std::vector<float> crop_data;
+ for (int64_t f = 0; f < dim_f; f++)
+ for (int64_t y = 0; y < split_size; y++)
+ for (int64_t x = 0; x < dim_x; x++)
+ crop_data.push_back(input_data[static_cast<size_t>(f * dim_y * dim_x + (y_offset + y) * dim_x + x)]);
+ set_values(crop_mem, crop_data);
+ rms_ref<float>(crop_mem, gamma_mem, out_mem, epsilon);
+ return out_mem;
+ };
+ auto output_ref_0 = make_crop_ref(split0, off_y0);
+ auto output_ref_1 = make_crop_ref(split1, off_y1);
+
+ cldnn::crop_ngraph_op_mode op_mode = cldnn::crop_ngraph_op_mode::variadic_split;
+ topology topology;
+ topology.add(input_layout("input", input_mem->get_layout()));
+ topology.add(data("axis", axis_mem));
+ topology.add(data("splits_length", splits_length_mem));
+ topology.add(data("gamma", gamma_mem));
+ // Branch 0: crop [1,2,2,16] -> RMS normalization
+ topology.add(crop("crop_0", {input_info("input"), input_info("axis"), input_info("splits_length")},
+ tensor(1), tensor(0, 0, 0, off_y0), op_mode, 0, axis));
+ topology.add(rms("rms_0", input_info("crop_0"), input_info("gamma"), epsilon));
+ topology.add(reorder("output_0", input_info("rms_0"), format::bfyx, data_types::f32,
+ std::vector<float>(), reorder_mean_mode::subtract, padding(), true));
+ // Branch 1: crop [1,2,1,16] -> RMS normalization
+ topology.add(crop("crop_1", {input_info("input"), input_info("axis"), input_info("splits_length")},
+ tensor(1), tensor(0, 0, 0, off_y1), op_mode, 1, axis));
+ topology.add(rms("rms_1", input_info("crop_1"), input_info("gamma"), epsilon));
+ topology.add(reorder("output_1", input_info("rms_1"), format::bfyx, data_types::f32,
+ std::vector<float>(), reorder_mean_mode::subtract, padding(), true));
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{
+ {"rms_0", {format::bfyx, "rms_gpu_bfyx_opt"}},
+ {"rms_1", {format::bfyx, "rms_gpu_bfyx_opt"}}
+ }));
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+
+ auto outputs = network.execute();
+
+ ASSERT_TRUE(network.get_primitive("crop_0")->can_be_optimized());
+ ASSERT_TRUE(network.get_primitive("crop_1")->can_be_optimized());
+
+ // Verify branch 0: crop [1,2,2,16], y-offset = 0
+ auto out0_mem = outputs.at("output_0").get_memory();
+ cldnn::mem_lock<float> out0(out0_mem, get_test_stream());
+ cldnn::mem_lock<float> ref0(output_ref_0, get_test_stream());
+ ASSERT_EQ(out0.size(), ref0.size());
+ for (size_t i = 0; i < ref0.size(); i++) {
+ ASSERT_NEAR(out0[i], ref0[i], 1e-4f) << "Branch 0 mismatch at index=" << i;
+ }
+
+ // Verify branch 1: crop [1,2,1,16], y-offset = 2
+ auto out1_mem = outputs.at("output_1").get_memory();
+ cldnn::mem_lock<float> out1(out1_mem, get_test_stream());
+ cldnn::mem_lock<float> ref1(output_ref_1, get_test_stream());
+ ASSERT_EQ(out1.size(), ref1.size());
+ for (size_t i = 0; i < ref1.size(); i++) {
+ ASSERT_NEAR(out1[i], ref1[i], 1e-4f) << "Branch 1 mismatch at index=" << i;
+ }
+}