Skip to content

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

[GPU] Keep the gated MLP down projection input in the FP16 range (#39814)

Kamil Wieloch6070aaf20328
diff --git a/src/plugins/intel_gpu/sources.cmake b/src/plugins/intel_gpu/sources.cmake
index ea51365eaf7..ecb2379638e 100644
--- a/src/plugins/intel_gpu/sources.cmake
+++ b/src/plugins/intel_gpu/sources.cmake
@@ -204,6 +204,7 @@ set(GPU_PLUGIN_TRANSFORMATIONS_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/print_model_statistics.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/reduce_fc_dimensions.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/remove_fq_before_dw_conv.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/scale_down_gated_mlp_input.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/sdpa_transpose_fusion.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/sink_reshape.cpp
${CMAKE_CURRENT_LIST_DIR}/src/plugin/transformations/stateless_kv_fusion.cpp
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.cpp b/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.cpp
new file mode 100644
index 00000000000..21cd0c201ec
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.cpp
@@ -0,0 +1,193 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "scale_down_gated_mlp_input.hpp"
+
+#include <algorithm>
+#include <cmath>
+#include <cstddef>
+#include <memory>
+#include <numeric>
+#include <utility>
+#include <vector>
+
+#include "intel_gpu/op/fully_connected_compressed.hpp"
+#include "intel_gpu/op/placeholder.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/swish.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
+
+namespace ov::intel_gpu {
+
+namespace {
+// `value` multiplied by `factor` and folded into a Constant, or nullptr if it does not fold or an FP16 element
+// would leave the normal range (then the scaling would not be exact).
+std::shared_ptr<ov::Node> fold_scaled(const ov::Output<ov::Node>& value, const std::shared_ptr<ov::Node>& factor) {
+ std::shared_ptr<ov::op::v0::Constant> folded = ov::util::get_constant_from_source(std::make_shared<ov::op::v1::Multiply>(value, factor));
+ if (!folded || folded->get_output_partial_shape(0) != value.get_partial_shape()) {
+ return nullptr;
+ }
+ if (folded->get_element_type() == ov::element::f16) {
+ constexpr float min_normal_f16 = 6.103515625e-05F;
+ for (const float v : folded->cast_vector<float>()) {
+ if (std::isinf(v) || (v != 0.F && std::abs(v) < min_normal_f16)) {
+ return nullptr;
+ }
+ }
+ }
+ folded->set_friendly_name(value.get_node()->get_friendly_name() + "_gated_mlp_scaled");
+ ov::copy_runtime_info(value.get_node_shared_ptr(), std::static_pointer_cast<ov::Node>(folded));
+ return folded;
+}
+
+std::shared_ptr<ov::Node> scalar(const ov::element::Type& type, float value) {
+ return ov::op::v0::Constant::create(type, ov::Shape{}, {value});
+}
+
+// Factor for a per-output-channel tensor of `shape`: `value` on channels [begin, end) of `n_axis`, 1 on the others.
+std::shared_ptr<ov::Node> channel_factor(const ov::element::Type& type, const ov::PartialShape& shape, size_t n_axis, size_t begin, size_t end, float value) {
+ if (!shape.is_static() || n_axis >= shape.size()) {
+ return nullptr;
+ }
+ const auto n = static_cast<size_t>(shape[n_axis].get_length());
+ if (begin >= end || end > n) {
+ return nullptr;
+ }
+ ov::Shape factor_shape(shape.size(), 1);
+ factor_shape[n_axis] = n;
+ std::vector<float> values(n, 1.F);
+ std::fill(values.begin() + static_cast<std::ptrdiff_t>(begin), values.begin() + static_cast<std::ptrdiff_t>(end), value);
+ return ov::op::v0::Constant::create(type, factor_shape, values);
+}
+
+// Ops between the up projection and the product that a scalar factor passes through unchanged.
+bool is_transparent(const ov::Node* node) {
+ if (ov::is_type<ov::op::v1::Reshape>(node) || ov::is_type<ov::op::v0::Squeeze>(node) || ov::is_type<ov::op::v0::Unsqueeze>(node)) {
+ return true;
+ }
+ return ov::is_type<ov::op::v0::Convert>(node) && node->get_input_element_type(0).is_real() && node->get_output_element_type(0).is_real();
+}
+} // namespace
+
+ScaleDownGatedMLPInput::ScaleDownGatedMLPInput(float scale_factor) {
+ using namespace ov::pass::pattern;
+
+ // FullyConnectedCompressed takes a varying number of inputs, so the gated product on input 0 is checked in the callback.
+ auto down_m = wrap_type<op::FullyConnectedCompressed>();
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) {
+ const auto down = ov::as_type_ptr<op::FullyConnectedCompressed>(m.get_match_root());
+ if (!down || transformation_callback(down)) {
+ return false;
+ }
+
+ const auto product = ov::as_type_ptr<ov::op::v1::Multiply>(down->get_input_node_shared_ptr(0));
+ if (!product || product->get_output_element_type(0) != ov::element::f16 || product->get_output_target_inputs(0).size() != 1) {
+ return false;
+ }
+ // The up branch is the operand that is not the activated gate.
+ const bool gate_first = ov::is_type<ov::op::v4::Swish>(product->get_input_node_ptr(0));
+ if (!gate_first && !ov::is_type<ov::op::v4::Swish>(product->get_input_node_ptr(1))) {
+ return false;
+ }
+
+ // Every input that the factor 1/scale_factor goes to, with its factor: the constant of each Add on the up
+ // branch, then the decompression scale and the bias of the up projection.
+ std::vector<std::pair<ov::Input<ov::Node>, std::shared_ptr<ov::Node>>> targets;
+ auto up = product->input_value(gate_first ? 1 : 0);
+ while (true) {
+ if (up.get_target_inputs().size() != 1) {
+ return false;
+ }
+ auto* const node = up.get_node();
+ if (is_transparent(node)) {
+ up = node->input_value(0);
+ } else if (ov::is_type<ov::op::v1::Add>(node)) {
+ const bool const_0 = ov::util::get_constant_from_source(node->input_value(0)) != nullptr;
+ const bool const_1 = ov::util::get_constant_from_source(node->input_value(1)) != nullptr;
+ if (const_0 == const_1) {
+ return false;
+ }
+ const size_t c = const_1 ? 1 : 0;
+ targets.emplace_back(node->input(c), scalar(node->get_input_element_type(c), 1.F / scale_factor));
+ up = node->input_value(1 - c);
+ } else {
+ break;
+ }
+ }
+
+ // The up projection is its own FC, or one output of the VariadicSplit after FullyConnectedHorizontalFusion merged
+ // the gate/up pair; then only the up channels of the merged FC are scaled, and GLUFusion still matches.
+ std::shared_ptr<op::FullyConnectedCompressed> up_fc;
+ size_t begin = 0, end = 0;
+ if (const auto split = ov::as_type_ptr<ov::op::v1::VariadicSplit>(up.get_node_shared_ptr())) {
+ up_fc = ov::as_type_ptr<op::FullyConnectedCompressed>(split->get_input_node_shared_ptr(0));
+ const auto axis = ov::util::get_constant_from_source(split->input_value(1));
+ const auto lengths = ov::util::get_constant_from_source(split->input_value(2));
+ if (!up_fc || up_fc->get_output_target_inputs(0).size() != 1 || !axis || !lengths) {
+ return false;
+ }
+ const auto rank = up_fc->get_output_partial_shape(0).rank();
+ const auto axis_value = axis->cast_vector<int64_t>()[0];
+ const auto sizes = lengths->cast_vector<int64_t>();
+ const bool negative_size = std::any_of(sizes.begin(), sizes.end(), [](int64_t s) {
+ return s < 0;
+ });
+ if (rank.is_dynamic() || (axis_value != -1 && axis_value != rank.get_length() - 1) || negative_size) {
+ return false;
+ }
+ begin = static_cast<size_t>(std::accumulate(sizes.begin(), sizes.begin() + static_cast<std::ptrdiff_t>(up.get_index()), int64_t{0}));
+ end = begin + static_cast<size_t>(sizes[up.get_index()]);
+ } else {
+ up_fc = ov::as_type_ptr<op::FullyConnectedCompressed>(up.get_node_shared_ptr());
+ }
+ if (!up_fc) {
+ return false;
+ }
+ // A merged FC needs per-channel factors; an FC of its own takes a scalar one.
+ auto up_factor = [&](size_t input, size_t n_axis) -> std::shared_ptr<ov::Node> {
+ const auto& type = up_fc->get_input_element_type(input);
+ if (end == 0) {
+ return scalar(type, 1.F / scale_factor);
+ }
+ return channel_factor(type, up_fc->get_input_partial_shape(input), n_axis, begin, end, 1.F / scale_factor);
+ };
+ const auto& scale_shape = up_fc->get_input_partial_shape(3);
+ const size_t scale_n_axis = up_fc->get_transpose_b_scale() ? 0 : std::max<size_t>(scale_shape.size(), 1) - 1;
+ targets.emplace_back(up_fc->input(3), up_factor(3, scale_n_axis));
+ if (!ov::is_type<op::Placeholder>(up_fc->get_input_node_ptr(2))) {
+ targets.emplace_back(up_fc->input(2), up_factor(2, std::max<size_t>(up_fc->get_input_partial_shape(2).size(), 1) - 1));
+ }
+ // The down projection takes the factor back, so its output and its bias stay as they are.
+ targets.emplace_back(down->input(3), scalar(down->get_input_element_type(3), scale_factor));
+
+ // Fold everything before changing the graph, so that a failed fold leaves the model intact.
+ std::vector<std::shared_ptr<ov::Node>> folded;
+ for (const auto& [input, factor] : targets) {
+ folded.push_back(factor ? fold_scaled(input.get_source_output(), factor) : nullptr);
+ if (!folded.back()) {
+ return false;
+ }
+ }
+ for (size_t i = 0; i < targets.size(); ++i) {
+ targets[i].first.replace_source_output(folded[i]);
+ }
+ return true;
+ };
+
+ auto m = std::make_shared<Matcher>(down_m, "ScaleDownGatedMLPInput");
+ this->register_matcher(m, callback);
+}
+
+} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.hpp b/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.hpp
new file mode 100644
index 00000000000..5488f52608f
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.hpp
@@ -0,0 +1,30 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/graph_rewrite.hpp"
+
+namespace ov::intel_gpu {
+
+/**
+ * @brief Keeps the input of a gated MLP down projection inside the FP16 range.
+ *
+ * In FC(down)(Multiply(Swish(FC(gate)), FC(up))) the product is stored in FP16. A single element
+ * can exceed the FP16 range while the rest of the tensor stays far below it, and the down
+ * projection then turns the whole row into NaN. The pass divides the weight decompression scale
+ * of the up projection by a power of two and multiplies that of the down projection by the same
+ * factor, so no node is added and the output is unchanged. The up branch may pass through
+ * Reshape, Squeeze, Unsqueeze, Convert and Add with a constant (scaled too), and the bias of the
+ * up projection is scaled as well. For a gate/up pair merged by FullyConnectedHorizontalFusion
+ * only the up channels are scaled. Scaling by a power of two is exact for normal FP16 values, so
+ * the pass is skipped when a scaled FP16 constant would leave that range.
+ */
+class ScaleDownGatedMLPInput : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("ScaleDownGatedMLPInput");
+ explicit ScaleDownGatedMLPInput(float scale_factor = 8.F);
+};
+
+} // 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 dc0f08b4a44..3293536d666 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -138,6 +138,7 @@
#include "plugin/transformations/expand_broadcast_reshape_sdpa_fusion.hpp"
#include "plugin/transformations/disable_fp16_comp_direct_multiply_sin_cos.hpp"
#include "plugin/transformations/disable_fp16_comp_gated_residual.hpp"
+#include "plugin/transformations/scale_down_gated_mlp_input.hpp"
#include "plugin/transformations/disable_fp16_comp_rms.hpp"
#include "plugin/transformations/swiglu_fusion_with_clamp.hpp"
#include "plugin/transformations/disable_fp16_comp_cumsum_sin_gen.hpp"
@@ -1969,6 +1970,12 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.register_pass<ov::intel_gpu::LoRAHorizontalFusion>();
}
}
+ // After the horizontal fusion, so that the scales of a merged gate/up FC are the ones scaled.
+ // ActivationsScaling, when enabled, already scales every MatMul.
+ if (infer_precision == ov::element::f16 && !(config.get_activations_scale_factor() > 0.F)) {
+ manager.register_pass<ov::intel_gpu::ScaleDownGatedMLPInput>();
+ }
+
// After the horizontal fusion so a merged gate/up FC absorbs the shared
// rotation once. Only the TernOCL impl applies a fused rotation, so without it
// the rotation stays in the graph.
diff --git a/src/plugins/intel_gpu/tests/unit/sources.cmake b/src/plugins/intel_gpu/tests/unit/sources.cmake
index a4fd957180e..e8d8a1f8bf0 100644
--- a/src/plugins/intel_gpu/tests/unit/sources.cmake
+++ b/src/plugins/intel_gpu/tests/unit/sources.cmake
@@ -360,6 +360,7 @@ set(GPU_UNIT_TESTS_SRCS
${CMAKE_CURRENT_LIST_DIR}/transformations/preserve_single_selective_ssm_output_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/reduce_fc_dimensions_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/remove_fq_before_dw_conv_test.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/transformations/scale_down_gated_mlp_input_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/sdpa_opt_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/sdpa_transpose_fusion_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/sink_reshape_test.cpp
@@ -484,6 +485,7 @@ set(GPU_UNIT_TESTS_TRANSFORMATIONS_SRCS
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/reduce_fc_dimensions.hpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/remove_fq_before_dw_conv.cpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/remove_fq_before_dw_conv.hpp
+ ${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/scale_down_gated_mlp_input.cpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/sdpa_transpose_fusion.cpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/sdpa_transpose_fusion.hpp
${CMAKE_HOME_DIRECTORY}/src/plugins/intel_gpu/src/plugin/transformations/sink_reshape.cpp
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/scale_down_gated_mlp_input_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/scale_down_gated_mlp_input_test.cpp
new file mode 100644
index 00000000000..61bc0708c37
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/transformations/scale_down_gated_mlp_input_test.cpp
@@ -0,0 +1,205 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "plugin/transformations/scale_down_gated_mlp_input.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <vector>
+
+#include "common_test_utils/graph_comparator.hpp"
+#include "common_test_utils/ov_test_utils.hpp"
+#include "intel_gpu/op/fully_connected_compressed.hpp"
+#include "intel_gpu/op/placeholder.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/swish.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "openvino/pass/manager.hpp"
+#include "ov_ops/glu.hpp"
+#include "transformations/common_optimizations/glu_fusion.hpp"
+
+using namespace testing;
+using namespace ov::intel_gpu;
+
+namespace ov {
+namespace test {
+namespace intel_gpu {
+
+namespace {
+// FullyConnectedCompressed with N output channels whose decompression scales are `scales` (one per channel).
+std::shared_ptr<ov::Node> fc(const ov::Output<ov::Node>& in, size_t n, size_t k, const std::vector<float>& scales, float bias = 0.f) {
+ auto weights = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{n, k}, {1});
+ auto scale = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{n, 1}, scales);
+ auto zp = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{n, 1}, {1});
+ std::shared_ptr<ov::Node> b = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ if (bias != 0.f)
+ b = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, n}, {bias});
+ return std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(in, weights, b, scale, zp);
+}
+
+std::vector<float> filled(size_t n, float value) {
+ return std::vector<float>(n, value);
+}
+
+std::shared_ptr<ov::op::v0::Parameter> input() {
+ return std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{-1, 16});
+}
+
+std::shared_ptr<ov::Model> model_of(const std::shared_ptr<ov::Node>& out, const std::shared_ptr<ov::op::v0::Parameter>& in) {
+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(out)}, ov::ParameterVector{in});
+}
+
+// in -> FC(gate) -> Swish -> Multiply(., FC(up)) -> FC(down); `fixed` builds the expected scales.
+std::shared_ptr<ov::Model> gated_mlp(bool fixed, bool swap, bool down_bias = false) {
+ auto in = input();
+ auto gate = std::make_shared<ov::op::v4::Swish>(fc(in, 64, 16, filled(64, 1.f)));
+ auto up = fc(in, 64, 16, filled(64, fixed ? 0.125f : 1.f));
+ auto product = swap ? std::make_shared<ov::op::v1::Multiply>(up, gate) : std::make_shared<ov::op::v1::Multiply>(gate, up);
+ return model_of(fc(product, 16, 64, filled(16, fixed ? 16.f : 2.f), down_bias ? 0.5f : 0.f), in);
+}
+
+// in -> FC(gate|up merged) -> VariadicSplit -> Multiply(Swish(gate), up) -> FC(down), as after FullyConnectedHorizontalFusion.
+std::shared_ptr<ov::Model> merged_gated_mlp(bool fixed, bool up_first) {
+ auto in = input();
+ std::vector<float> scales = filled(128, 1.f);
+ if (fixed)
+ std::fill(scales.begin() + (up_first ? 0 : 64), scales.begin() + (up_first ? 64 : 128), 0.125f);
+ auto split = std::make_shared<ov::op::v1::VariadicSplit>(fc(in, 128, 16, scales),
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1}),
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {64, 64}));
+ auto gate = std::make_shared<ov::op::v4::Swish>(split->output(up_first ? 1 : 0));
+ auto product = std::make_shared<ov::op::v1::Multiply>(gate, split->output(up_first ? 0 : 1));
+ return model_of(fc(product, 16, 64, filled(16, fixed ? 16.f : 2.f)), in);
+}
+} // namespace
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput) {
+ model = gated_mlp(false, false);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = gated_mlp(true, false);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_SwappedOperands) {
+ model = gated_mlp(false, true);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = gated_mlp(true, true);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+// The down projection output keeps its magnitude, so its bias is left as is.
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_BiasOnDown) {
+ model = gated_mlp(false, false, true);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = gated_mlp(true, false, true);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_MergedGateUp) {
+ model = merged_gated_mlp(false, false);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = merged_gated_mlp(true, false);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_MergedUpFirst) {
+ model = merged_gated_mlp(false, true);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = merged_gated_mlp(true, true);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+// The pass adds no node, so the merged gate/up pair still fuses into GLU.
+TEST(ScaleDownGatedMLPInputTest, MergedGateUpKeepsGLUFusion) {
+ auto model = merged_gated_mlp(false, false);
+ ov::pass::Manager manager;
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ manager.register_pass<ov::pass::GLUFusion>();
+ manager.run_passes(model);
+ size_t glu_count = 0;
+ for (const auto& op : model->get_ops())
+ glu_count += ov::is_type<ov::op::internal::GLU>(op) ? 1 : 0;
+ ASSERT_EQ(glu_count, 1u);
+}
+
+// A bias on the up projection is scaled together with its decompression scales.
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_BiasOnUp) {
+ auto build = [](bool fixed) {
+ auto in = input();
+ auto gate = std::make_shared<ov::op::v4::Swish>(fc(in, 64, 16, filled(64, 1.f)));
+ auto up = fc(in, 64, 16, filled(64, fixed ? 0.125f : 1.f), fixed ? 0.0625f : 0.5f);
+ return model_of(fc(std::make_shared<ov::op::v1::Multiply>(gate, up), 16, 64, filled(16, fixed ? 16.f : 2.f)), in);
+ };
+ model = build(false);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = build(true);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+// The factor passes through a Reshape and scales the constant of an unfused bias Add on the up branch.
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_UpThroughReshapeAndAdd) {
+ auto build = [](bool fixed) {
+ auto in = input();
+ auto gate = std::make_shared<ov::op::v4::Swish>(fc(in, 64, 16, filled(64, 1.f)));
+ auto reshape = std::make_shared<ov::op::v1::Reshape>(fc(in, 64, 16, filled(64, fixed ? 0.125f : 1.f)),
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {0, 64}),
+ true);
+ auto up = std::make_shared<ov::op::v1::Add>(reshape, ov::op::v0::Constant::create(ov::element::f16, ov::Shape{64}, {fixed ? 0.25f : 2.f}));
+ return model_of(fc(std::make_shared<ov::op::v1::Multiply>(gate, up), 16, 64, filled(16, fixed ? 16.f : 2.f)), in);
+ };
+ model = build(false);
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = build(true);
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+// 1e-4 / 8 is below the smallest normal FP16 value, so the scaling would not be exact.
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_SubnormalScale_NotApplied) {
+ auto build = [] {
+ auto in = input();
+ auto gate = std::make_shared<ov::op::v4::Swish>(fc(in, 64, 16, filled(64, 1.f)));
+ auto product = std::make_shared<ov::op::v1::Multiply>(gate, fc(in, 64, 16, filled(64, 1e-4f)));
+ return model_of(fc(product, 16, 64, filled(16, 2.f)), in);
+ };
+ model = build();
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = build();
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_NoSwish_NotApplied) {
+ auto build = [] {
+ auto in = input();
+ auto product = std::make_shared<ov::op::v1::Multiply>(fc(in, 64, 16, filled(64, 1.f)), fc(in, 64, 16, filled(64, 1.f)));
+ return model_of(fc(product, 16, 64, filled(16, 2.f)), in);
+ };
+ model = build();
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = build();
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+TEST_F(TransformationTestsF, ScaleDownGatedMLPInput_ProductWithTwoConsumers_NotApplied) {
+ auto build = [] {
+ auto in = input();
+ auto gate = std::make_shared<ov::op::v4::Swish>(fc(in, 64, 16, filled(64, 1.f)));
+ auto product = std::make_shared<ov::op::v1::Multiply>(gate, fc(in, 64, 16, filled(64, 1.f)));
+ return std::make_shared<ov::Model>(
+ ov::ResultVector{std::make_shared<ov::op::v0::Result>(fc(product, 16, 64, filled(16, 2.f))), std::make_shared<ov::op::v0::Result>(product)},
+ ov::ParameterVector{in});
+ };
+ model = build();
+ manager.register_pass<ScaleDownGatedMLPInput>();
+ model_ref = build();
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+}
+
+} // namespace intel_gpu
+} // namespace test
+} // namespace ov