Skip to content

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

[GPU] NormalizeL2Decomposition fp32 inner nodes to avoid fp16 range overflow from reducesum (#31623)

Wilson Seok10027c8d64ab
diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.hpp b/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.hpp
index 98ca2540b23..1d9d2cbd9e1 100644
--- a/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/onednn/reduce_onednn.hpp
@@ -61,7 +61,8 @@ struct ReduceImplementationManager : public ImplementationManager {
auto in_dt = in_layout.data_type;
auto out_dt = out_layout.data_type;
- if (in_dt == data_types::f32 && out_dt == data_types::f32)
+ // onednn reduction does not support different input/output data types
+ if (in_dt != out_dt)
return false;
static const std::vector<format::type> supported_formats = {
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.cpp b/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.cpp
new file mode 100644
index 00000000000..a7dffe96730
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.cpp
@@ -0,0 +1,108 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "normalize_l2_decomposition.hpp"
+
+#include <memory>
+
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/normalize_l2.hpp"
+#include "openvino/op/power.hpp"
+#include "openvino/op/reduce_sum.hpp"
+#include "openvino/op/sqrt.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
+
+namespace ov::intel_gpu {
+
+namespace {
+ std::shared_ptr<ov::Node> create_eps_node(const std::shared_ptr<ov::Node>& reduce_sum,
+ const std::shared_ptr<ov::op::v0::NormalizeL2>& normalize_l2,
+ ov::element::Type eps_type) {
+ auto eps_const_node = ov::op::v0::Constant::create(eps_type, Shape{}, {normalize_l2->get_eps()});
+
+ switch (normalize_l2->get_eps_mode()) {
+ case ov::op::EpsMode::ADD:
+ return std::make_shared<ov::op::v1::Add>(reduce_sum, eps_const_node);
+ case ov::op::EpsMode::MAX:
+ return std::make_shared<ov::op::v1::Maximum>(reduce_sum, eps_const_node);
+ default:
+ return nullptr;
+ }
+ }
+} // namespace
+
+NormalizeL2Decomposition::NormalizeL2Decomposition(bool use_fp32_reducesum) {
+ auto normalize_l2_pattern = ov::pass::pattern::wrap_type<ov::op::v0::NormalizeL2>();
+
+ matcher_pass_callback callback = [this, use_fp32_reducesum](ov::pass::pattern::Matcher& m) {
+ auto normalize_l2 = std::dynamic_pointer_cast<ov::op::v0::NormalizeL2>(m.get_match_root());
+
+ if (!normalize_l2 || transformation_callback(normalize_l2)) {
+ return false;
+ }
+
+ // Create power operation
+ auto input_type = normalize_l2->get_input_element_type(0);
+
+ std::shared_ptr<ov::Node> input = normalize_l2->input_value(0).get_node_shared_ptr();
+ // Convert to fp32 if requested
+ if (use_fp32_reducesum) {
+ input = std::make_shared<ov::op::v0::Convert>(normalize_l2->input_value(0), ov::element::f32);
+ }
+
+ ov::element::Type decomposition_data_type = use_fp32_reducesum ? ov::element::f32 : input_type;
+
+ auto power = std::make_shared<ov::op::v1::Power>(input,
+ ov::op::v0::Constant::create(decomposition_data_type, Shape{}, {2.0}));
+
+ // Create reduce sum
+ auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
+ power, normalize_l2->input_value(1), true);
+
+
+ auto eps_node = create_eps_node(reduce_sum, normalize_l2, decomposition_data_type);
+ if (!eps_node) {
+ return false;
+ }
+
+ // Create sqrt
+ auto sqrt = std::make_shared<ov::op::v0::Sqrt>(eps_node);
+
+ // Create division
+ std::shared_ptr<ov::Node> dividend = normalize_l2->input_value(0).get_node_shared_ptr();
+ std::shared_ptr<ov::Node> final_convert = nullptr;
+
+ auto div = std::make_shared<ov::op::v1::Divide>(input, sqrt);
+ div->set_friendly_name(normalize_l2->get_friendly_name());
+
+ std::shared_ptr<ov::Node> final_result = div;
+
+ // Convert back to original type if we used fp32
+ if (use_fp32_reducesum && input_type != ov::element::f32) {
+ final_convert = std::make_shared<ov::op::v0::Convert>(div, input_type);
+ final_result = final_convert;
+ }
+
+ // Copy runtime info and replace node
+ std::vector<std::shared_ptr<ov::Node>> new_ops = {power, reduce_sum, eps_node, sqrt, div};
+ if (final_convert) new_ops.push_back(final_convert);
+
+ ov::copy_runtime_info(normalize_l2, new_ops);
+ ov::replace_node(normalize_l2, final_result);
+
+ return true;
+ };
+
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(normalize_l2_pattern, "NormalizeL2DecompositionGPU");
+ register_matcher(m, callback);
+}
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.hpp b/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.hpp
new file mode 100644
index 00000000000..dbb488ee5c6
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/normalize_l2_decomposition.hpp
@@ -0,0 +1,21 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/graph_rewrite.hpp"
+#include "openvino/core/visibility.hpp"
+
+namespace ov::intel_gpu {
+
+/**
+ * @brief Decomposes NormalizeL2 into subgraph
+ */
+class NormalizeL2Decomposition : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("NormalizeL2DecompositionGPU");
+ NormalizeL2Decomposition(bool use_fp32_reducesum = false);
+};
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 5608ae9f3b3..1458219e3ed 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -126,6 +126,7 @@
#include "transformations/fp16_compression/convert_compression_only_to_legacy.hpp"
#include "transformations/fp16_compression/mark_decompression_convert_constant_folding.hpp"
#include "transformations/init_node_info.hpp"
+#include "transformations/normalize_l2_decomposition.hpp"
#include "transformations/low_precision/mark_dequantization_subgraph.hpp"
#include "transformations/op_conversions/bidirectional_sequences_decomposition.hpp"
#include "transformations/op_conversions/convert_batch_to_space.hpp"
@@ -850,8 +851,11 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
return false;
});
- pass_config->enable<ov::pass::NormalizeL2Decomposition>();
- pass_config->set_callback<ov::pass::NormalizeL2Decomposition>(
+ // ReduceSum would causes fp16 out of range.
+ // To avoid the out of range, use fp32 nodes for NormalizeL2Decomposition.
+ bool use_fp32_internal_nodes = (infer_precision == ov::element::f16);
+ manager.register_pass<ov::intel_gpu::NormalizeL2Decomposition>(use_fp32_internal_nodes);
+ pass_config->set_callback<ov::intel_gpu::NormalizeL2Decomposition>(
[](const_node_ptr &node) -> bool {
// Condition to filter out axes such as [0, 1, 2] which is not supported currently.
const auto norm = ov::as_type_ptr<const ov::op::v0::NormalizeL2>(node);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp
index 758fbf5786e..b5368ba3122 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/reduce_gpu_test.cpp
@@ -487,6 +487,8 @@ protected:
public:
void execute(bool is_caching_test) {
+ if (engine.get_device_info().supports_immad)
+ return;
int input_dim = static_cast<int>(input_format.dimension());
cldnn::format layout_format = input_format;
@@ -1720,6 +1722,8 @@ TEST(reduce_gpu, dynamic) {
TEST(reduce_gpu, b_fs_yx_fsv16_min_dynamic) {
auto& engine = get_test_engine();
+ if (engine.get_device_info().supports_immad)
+ return;
auto input = engine.allocate_memory({data_types::f32, format::bfyx, {1, 17, 1, 2}});
set_values(input, {
@@ -1775,6 +1779,8 @@ TEST(reduce_gpu, b_fs_yx_fsv16_min_dynamic) {
TEST(reduce_gpu, b_fs_yx_fsv16_max_dynamic) {
auto& engine = get_test_engine();
+ if (engine.get_device_info().supports_immad)
+ return;
auto input = engine.allocate_memory({data_types::f32, format::bfyx, {1, 17, 1, 2}});
set_values(input, {
@@ -2175,6 +2181,9 @@ TEST_P(onednn_reduce_gpu_i8_f32, base) { execute_onednn(); }
class onednn_reduce_gpu_f16_f16 : public ReduceOnednnTestBase<data_types::f16, data_types::f16> {};
TEST_P(onednn_reduce_gpu_f16_f16, base) { execute_onednn(); }
+class onednn_reduce_gpu_f32_f32 : public ReduceOnednnTestBase<data_types::f32, data_types::f32> {};
+TEST_P(onednn_reduce_gpu_f32_f32, base) { execute_onednn(); }
+
INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_b_fs_yx_fsv16_i8_f32,
onednn_reduce_gpu_i8_f32,
::testing::Values(
@@ -2208,6 +2217,29 @@ INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_b_fs_yx_fsv16_f16_f16,
TestParamType_general_reduce_gpu(17, 3, 1, 1, 14, 11, format::b_fs_yx_fsv16, reduce_mode::mean, {1}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f16, false, data_types::f16)
), general_reduce_gpu::PrintToStringParamName);
+ INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_b_fs_yx_fsv16_f32_f32,
+ onednn_reduce_gpu_f32_f32,
+ ::testing::Values(
+ TestParamType_general_reduce_gpu(3, 3, 1, 1, 3, 2, format::b_fs_yx_fsv16, reduce_mode::sum, {3, 2, 1, 0}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(3, 3, 1, 1, 3, 3, format::b_fs_yx_fsv16, reduce_mode::l1, {3, 2, 1, 0}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(3, 3, 1, 1, 2, 11, format::b_fs_yx_fsv16, reduce_mode::min, {3, 2, 1, 0}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(7, 3, 1, 1, 13, 11, format::b_fs_yx_fsv16, reduce_mode::mean, {3, 2, 1, 0}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(16, 4, 1, 1, 16, 8, format::b_fs_yx_fsv16, reduce_mode::max, {1, 2, 3}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 13, 12, format::b_fs_yx_fsv16, reduce_mode::l2, {0, 2, 3}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(16, 16, 1, 1, 16, 8, format::b_fs_yx_fsv16, reduce_mode::min, {2, 3}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 18, 11, format::b_fs_yx_fsv16, reduce_mode::max, {2, 1}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 16, 11, format::b_fs_yx_fsv16, reduce_mode::l1, {1, 0}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 14, 11, format::b_fs_yx_fsv16, reduce_mode::l2, {1}, "reduce_gpu_b_fs_yx_fsv16", false, data_types::f32, false, data_types::f32),
+
+ TestParamType_general_reduce_gpu(7, 3, 1, 1, 13, 11, format::b_fs_yx_fsv16, reduce_mode::mean, {3, 2, 1, 0}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(16, 4, 1, 1, 16, 8, format::b_fs_yx_fsv16, reduce_mode::max, {1, 2, 3}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 13, 9, format::b_fs_yx_fsv16, reduce_mode::l2, {0, 2, 3}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(16, 16, 1, 1, 16, 8, format::b_fs_yx_fsv16, reduce_mode::min, {2, 3}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 18, 11, format::b_fs_yx_fsv16, reduce_mode::max, {2, 1}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 16, 15, format::b_fs_yx_fsv16, reduce_mode::l1, {1, 0}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 3, 1, 1, 14, 11, format::b_fs_yx_fsv16, reduce_mode::mean, {1}, "reduce_gpu_b_fs_yx_fsv16", true, data_types::f32, false, data_types::f32)
+ ), general_reduce_gpu::PrintToStringParamName);
+
INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_byxf_i8_f32,
onednn_reduce_gpu_i8_f32,
::testing::Values(
@@ -2221,6 +2253,13 @@ INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_byxf_f16_f16,
TestParamType_general_reduce_gpu(17, 34, 1, 1, 16, 15, format::byxf, reduce_mode::l1, {1, 0}, "reduce_gpu_byxf", true, data_types::f16, false, data_types::f16),
TestParamType_general_reduce_gpu(17, 3, 1, 1, 14, 11, format::byxf, reduce_mode::mean, {1}, "reduce_gpu_byxf", true, data_types::f16, false, data_types::f16)
), general_reduce_gpu::PrintToStringParamName);
+
+INSTANTIATE_TEST_SUITE_P(onednn_reduce_gpu_byxf_f32_f32,
+ onednn_reduce_gpu_f32_f32,
+ ::testing::Values(
+ TestParamType_general_reduce_gpu(17, 34, 1, 1, 16, 15, format::byxf, reduce_mode::l1, {1, 0}, "reduce_gpu_byxf", true, data_types::f32, false, data_types::f32),
+ TestParamType_general_reduce_gpu(17, 3, 1, 1, 14, 11, format::byxf, reduce_mode::mean, {1}, "reduce_gpu_byxf", true, data_types::f32, false, data_types::f32)
+ ), general_reduce_gpu::PrintToStringParamName);
#endif // ENABLE_ONEDNN_FOR_GPU
#ifdef RUN_ALL_MODEL_CACHING_TESTS