Skip to content

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

[GPU] Fix incorrect MVN result for last-axis LayerNorm in byxf layout (#36649)

Maksim Kutakovdef82ab881ea
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/mvn.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/mvn.hpp
index fbc5aa8922d..552382d12a7 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/mvn.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/mvn.hpp
@@ -102,5 +102,10 @@ struct mvn : public primitive_base<mvn> {
return false;
}
+
+ // Returns false when the given layout needs a reorder to planar before an aligned MVN (see requires_alignment):
+ // the normalized-axes flattening in mvn_impl::static_canonicalize_shapes is only valid for planar / single-fsv
+ // layouts.
+ bool is_aligned_layout_supported(const layout& input_layout) const;
};
} // namespace cldnn
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
index a9eab68a70a..e23a34f2adb 100644
--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
@@ -137,6 +137,15 @@ bool layout_optimizer::is_format_supported(program_node& node, format::type fmt)
if (node.is_type<fully_connected>() && fmt == format::byxf)
return false;
+ // Aligned MVN flattens the normalized axes into the innermost dimension, which is only valid for planar /
+ // single feature-blocked layouts; reject other layouts (e.g. byxf) so a reorder to planar is inserted instead.
+ if (node.is_type<mvn>()) {
+ const auto& input_layout = node.get_input_layout(0);
+ const layout candidate{input_layout.get_partial_shape(), input_layout.data_type, fmt};
+ if (!node.as<mvn>().get_primitive()->is_aligned_layout_supported(candidate))
+ return false;
+ }
+
if (node.is_type<input_layout>())
return node.get_output_layout().format == fmt;
diff --git a/src/plugins/intel_gpu/src/graph/mvn.cpp b/src/plugins/intel_gpu/src/graph/mvn.cpp
index f896aec3131..5ff96e4b187 100644
--- a/src/plugins/intel_gpu/src/graph/mvn.cpp
+++ b/src/plugins/intel_gpu/src/graph/mvn.cpp
@@ -5,11 +5,37 @@
#include "mvn_inst.h"
#include "primitive_type_base.h"
#include "json_object.h"
+#include <algorithm>
#include <string>
namespace cldnn {
GPU_DEFINE_PRIMITIVE_TYPE_ID(mvn)
+bool mvn::is_aligned_layout_supported(const layout& input_layout) const {
+ const auto& input_pshape = input_layout.get_partial_shape();
+ if (!requires_alignment(input_pshape))
+ return true;
+
+ const auto& fmt = input_layout.format;
+ if (format::is_default_format(fmt))
+ return true;
+
+ //defer to dyn_formats
+ if (input_pshape.is_dynamic())
+ return true;
+
+ // Mirror the single feature-blocked case handled by mvn_impl::static_canonicalize_shapes.
+ const auto& block_sizes = format::block_sizes(fmt);
+ auto axes = reduction_axes;
+ const auto rank = static_cast<int64_t>(input_pshape.size());
+ std::for_each(axes.begin(), axes.end(), [rank](int64_t& v) {
+ v = (v < 0) ? v + rank : v;
+ });
+ return block_sizes.size() == 1 && block_sizes[0].first == 1 &&
+ (input_pshape[block_sizes[0].first].get_length() % block_sizes[0].second == 0) &&
+ (std::count(axes.begin(), axes.end(), static_cast<int64_t>(block_sizes[0].first)) == 0);
+}
+
layout mvn_inst::calc_output_layout(mvn_node const& node, kernel_impl_params const& impl_param) {
auto input_node_layout = impl_param.get_non_padded_input_layout();
auto output_type = impl_param.desc->output_data_types[0].value_or(input_node_layout.data_type);
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/mvn_layernorm_byxf.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/mvn_layernorm_byxf.cpp
new file mode 100644
index 00000000000..865fc0f9bd0
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/mvn_layernorm_byxf.cpp
@@ -0,0 +1,123 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "common_test_utils/node_builders/constant.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+
+#include "openvino/core/coordinate_diff.hpp"
+#include "openvino/core/strides.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convolution.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/mvn.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/transpose.hpp"
+
+namespace {
+
+// Regression: in f16 the convolutions + residual pin the MVN input to a non-planar byxf layout, where the GPU
+// normalized a last-axis (axes=[-1]) LayerNorm over the wrong axis (in f32 it stays planar bfyx and is correct).
+//
+// ┌───────┐
+// │ Param │
+// └───┬───┘
+// ┌───┴───┐
+// ┌───────┤ Conv1 │
+// │ └───┬───┘
+// │ ┌─────┴─────┐
+// │ │ Transpose │ (bfyx->byxf)
+// │ └─────┬─────┘
+// │ ┌───┴───┐
+// │ │ MVN │ (axes=[-1])
+// │ └───┬───┘
+// │ ┌─────┴────┐
+// │ │ Multiply │ (gamma)
+// │ └─────┬────┘
+// │ ┌──┴──┐
+// │ │ Add │ (beta)
+// │ └──┬──┘
+// │ ┌─────┴─────┐
+// │ │ Transpose │ (byxf->bfyx)
+// │ └─────┬─────┘
+// │ ┌───┴───┐
+// │ │ Conv2 │
+// │ └───┬───┘
+// │ ┌──┴──┐
+// └────────┤ Add │ (residual)
+// └──┬──┘
+// ┌──────┴─────┐
+// │ Result │
+// └────────────┘
+class MVNLayerNormByxf : virtual public ov::test::SubgraphBaseStaticTest {
+protected:
+ void SetUp() override {
+ targetDevice = ov::test::utils::DEVICE_GPU;
+ const auto type = ov::element::f16;
+
+ // Channel count is kept below 16 so the GPU layout optimizer selects the byxf layout for the MVN input.
+ const ov::Shape input_shape{1, 8, 64, 64};
+ const size_t channels = input_shape[1];
+
+ // The wrong-axis MVN corrupts the majority of the output elements (relative error ~0.1), while a correct f16
+ // result stays well within these thresholds at the output magnitude produced by the bounded weights below.
+ abs_threshold = 0.1f;
+ rel_threshold = 0.05f;
+
+ // Deterministic constant values in [-1, 1) keep activations bounded so the f16 reference comparison is clean.
+ auto bounded_const_data = [](int32_t seed) {
+ return ov::test::utils::InputGenerateData(-1, 2, 32, seed);
+ };
+
+ auto input = std::make_shared<ov::op::v0::Parameter>(type, input_shape);
+
+ // First convolution: keeps the activation in a channels-last (byxf) GPU layout.
+ auto weights1 = ov::test::utils::make_constant(type, ov::Shape{channels, channels, 3, 3}, bounded_const_data(1));
+ auto conv1 = std::make_shared<ov::op::v1::Convolution>(input,
+ weights1,
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::Strides{1, 1});
+
+ // LayerNorm over the channel (f) axis, expressed as Transpose -> MVN(axes=[-1]) -> scale/shift -> Transpose.
+ auto order_to_byxf = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 3, 1});
+ auto transpose_in = std::make_shared<ov::op::v1::Transpose>(conv1, order_to_byxf);
+
+ auto axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});
+ auto mvn = std::make_shared<ov::op::v6::MVN>(transpose_in,
+ axes,
+ true,
+ 1e-6f,
+ ov::op::MVNEpsMode::INSIDE_SQRT);
+
+ auto gamma = ov::test::utils::make_constant(type, ov::Shape{1, 1, 1, channels}, bounded_const_data(2));
+ auto scale = std::make_shared<ov::op::v1::Multiply>(mvn, gamma);
+ auto beta = ov::test::utils::make_constant(type, ov::Shape{1, 1, 1, channels}, bounded_const_data(3));
+ auto shift = std::make_shared<ov::op::v1::Add>(scale, beta);
+
+ auto order_to_bfyx = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 3, 1, 2});
+ auto transpose_out = std::make_shared<ov::op::v1::Transpose>(shift, order_to_bfyx);
+
+ // Second convolution + residual connection that pins the byxf layout on the LayerNorm block.
+ auto weights2 = ov::test::utils::make_constant(type, ov::Shape{channels, channels, 3, 3}, bounded_const_data(4));
+ auto conv2 = std::make_shared<ov::op::v1::Convolution>(transpose_out,
+ weights2,
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::Strides{1, 1});
+ auto residual = std::make_shared<ov::op::v1::Add>(conv2, conv1);
+
+ auto result = std::make_shared<ov::op::v0::Result>(residual);
+ function = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "MVNLayerNormByxf");
+ }
+};
+
+TEST_F(MVNLayerNormByxf, smoke_GPU_MVNLayerNormByxf) {
+ run();
+}
+
+} // namespace