Skip to content

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

[GPU] Fix MoE fusion accuracy for flattened PagedAttention inputs (#38631)

Andrew Kwangwoong Parkffbe6c39fb19
diff --git a/src/common/transformations/include/transformations/common_optimizations/moe_op_fusion.hpp b/src/common/transformations/include/transformations/common_optimizations/moe_op_fusion.hpp
index c9353e2287c..51ab97c49d9 100644
--- a/src/common/transformations/include/transformations/common_optimizations/moe_op_fusion.hpp
+++ b/src/common/transformations/include/transformations/common_optimizations/moe_op_fusion.hpp
@@ -22,20 +22,20 @@ class TRANSFORMATIONS_API MoeOpFusion;
class ov::pass::Convert2GatherMatmulMoeBlockToMoeOp : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("Convert2GatherMatmulMoeBlockToMoeOp");
- Convert2GatherMatmulMoeBlockToMoeOp(bool has_batch_dim = true);
+ Convert2GatherMatmulMoeBlockToMoeOp();
};
class ov::pass::Convert3GatherMatmulMoeBlockToMoeOp : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("Convert3GatherMatmulMoeBlockToMoeOp");
- Convert3GatherMatmulMoeBlockToMoeOp(bool has_batch_dim = true);
+ Convert3GatherMatmulMoeBlockToMoeOp();
};
class ov::pass::MoeOpFusion : public ov::pass::GraphRewrite {
public:
OPENVINO_GRAPH_REWRITE_RTTI("MoeOpFusion");
- MoeOpFusion(bool has_batch_dim = true) {
- add_matcher<ov::pass::Convert2GatherMatmulMoeBlockToMoeOp>(has_batch_dim);
- add_matcher<ov::pass::Convert3GatherMatmulMoeBlockToMoeOp>(has_batch_dim);
+ MoeOpFusion() {
+ add_matcher<ov::pass::Convert2GatherMatmulMoeBlockToMoeOp>();
+ add_matcher<ov::pass::Convert3GatherMatmulMoeBlockToMoeOp>();
}
};
diff --git a/src/common/transformations/src/transformations/common_optimizations/moe_op_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/moe_op_fusion.cpp
index badb4bee920..d74ecd66539 100644
--- a/src/common/transformations/src/transformations/common_optimizations/moe_op_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/moe_op_fusion.cpp
@@ -50,10 +50,10 @@ static size_t weight_logical_K(const ov::Shape& shape) {
return shape.size() == 4 ? shape[2] * shape[3] : shape[2];
}
-Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool has_batch_dim) {
+Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp() {
MATCHER_SCOPE(Convert3GatherMatmulMoeBlockToMoeOp);
- auto hidden_states_m = pattern::any_input();
+ auto hidden_states_m = pattern::any_input(pattern::rank_equals(2) || pattern::rank_equals(3));
auto hidden_state_reshape = pattern::optional<v1::Reshape>({hidden_states_m, pattern::any_input()});
auto unsqueeze_m = pattern::wrap_type<v0::Unsqueeze>({hidden_state_reshape, pattern::any_input()});
@@ -68,7 +68,7 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
auto bgm_gate_6_m = pattern::wrap_type<GatherMatmulCompressed>(
{unsqueeze_m, gate_w_m, topk_indices_m, pattern::any_input(), gate_scale_m, gate_zp_m});
// Or-pattern
- auto bgm_gate_m = std::make_shared<pattern::op::Or>(OutputVector{bgm_gate_4_m, bgm_gate_6_m});
+ auto bgm_gate_m = bgm_gate_4_m | bgm_gate_6_m;
// Gate activation: Swish (SwiGLU) or Gelu (GeGLU) with TANH or ERF approximation.
auto swish_m = pattern::wrap_type<v4::Swish, v7::Gelu>({bgm_gate_m});
@@ -79,7 +79,7 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
auto up_zp_m = pattern::any_input();
auto bgm_up_6_m = pattern::wrap_type<GatherMatmulCompressed>(
{unsqueeze_m, up_w_m, topk_indices_m, pattern::any_input(), up_scale_m, up_zp_m});
- auto bgm_up_m = std::make_shared<pattern::op::Or>(OutputVector{bgm_up_4_m, bgm_up_6_m});
+ auto bgm_up_m = bgm_up_4_m | bgm_up_6_m;
auto swiglu_m = pattern::wrap_type<v1::Multiply>({swish_m, bgm_up_m});
@@ -89,7 +89,7 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
auto down_zp_m = pattern::any_input();
auto bgm_down_6_m = pattern::wrap_type<GatherMatmulCompressed>(
{swiglu_m, down_w_m, topk_indices_m, pattern::any_input(), down_scale_m, down_zp_m});
- auto bgm_down_m = std::make_shared<pattern::op::Or>(OutputVector{bgm_down_4_m, bgm_down_6_m});
+ auto bgm_down_m = bgm_down_4_m | bgm_down_6_m;
auto routing_m = pattern::any_input();
auto routing_slice_m = pattern::optional<v8::Slice>(
@@ -102,7 +102,7 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
auto end_reshape_shape_m = pattern::any_input();
auto end_reshape_m = pattern::wrap_type<v1::Reshape>({reduce_sum_m, end_reshape_shape_m});
- matcher_pass_callback callback = [=](pattern::Matcher& m) {
+ matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](pattern::Matcher& m) {
auto& pm = m.get_pattern_value_map();
if (transformation_callback(m.get_match_root())) {
@@ -110,6 +110,9 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
}
auto hidden_states = pm.at(hidden_states_m);
+ const auto& hidden_states_shape = hidden_states.get_partial_shape();
+ const bool has_batch_dim = hidden_states_shape.rank().get_length() == 3 &&
+ !(hidden_states_shape[1].is_static() && hidden_states_shape[1].get_length() == 1);
auto routing = pm.at(routing_m);
auto topk_indices = pm.at(topk_indices_m);
@@ -226,10 +229,17 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
moe_node = std::make_shared<ov::op::internal::MOE>(moe_inputs, config);
}
- moe_node->set_friendly_name(m.get_match_root()->get_friendly_name());
- ov::copy_runtime_info(m.get_matched_nodes(), moe_node);
- ov::replace_node(m.get_match_root(), moe_node);
-
+ auto replacement = moe_node;
+ if (!hidden_states.get_partial_shape().same_scheme(pm.at(end_reshape_m).get_partial_shape())) {
+ moe_node->set_friendly_name(m.get_match_root()->get_friendly_name() + "/MOE");
+ replacement = pm.at(end_reshape_m)
+ .get_node_shared_ptr()
+ ->clone_with_new_inputs({moe_node, pm.at(end_reshape_shape_m)});
+ register_new_node(replacement);
+ }
+ replacement->set_friendly_name(m.get_match_root()->get_friendly_name());
+ ov::copy_runtime_info(m.get_matched_nodes(), {moe_node, replacement});
+ ov::replace_node(m.get_match_root(), replacement);
register_new_node(moe_node);
return true;
};
@@ -238,10 +248,10 @@ Convert3GatherMatmulMoeBlockToMoeOp::Convert3GatherMatmulMoeBlockToMoeOp(bool ha
this->register_matcher(matcher, callback);
}
-Convert2GatherMatmulMoeBlockToMoeOp::Convert2GatherMatmulMoeBlockToMoeOp(bool has_batch_dim) {
+Convert2GatherMatmulMoeBlockToMoeOp::Convert2GatherMatmulMoeBlockToMoeOp() {
MATCHER_SCOPE(Convert2GatherMatmulMoeBlockToMoeOp);
- auto hidden_states_m = pattern::any_input();
+ auto hidden_states_m = pattern::any_input(pattern::rank_equals(2) || pattern::rank_equals(3));
auto hidden_state_reshape = pattern::optional<v1::Reshape>({hidden_states_m, pattern::any_input()});
auto unsqueeze_m = pattern::wrap_type<v0::Unsqueeze>({hidden_state_reshape, pattern::any_input()});
@@ -303,6 +313,9 @@ Convert2GatherMatmulMoeBlockToMoeOp::Convert2GatherMatmulMoeBlockToMoeOp(bool ha
}
auto hidden_states = pm.at(hidden_states_m);
+ const auto& hidden_states_shape = hidden_states.get_partial_shape();
+ const bool has_batch_dim = hidden_states_shape.rank().get_length() == 3 &&
+ !(hidden_states_shape[1].is_static() && hidden_states_shape[1].get_length() == 1);
// Bypass the [1,0] Transpose: moe_scatter_reduction expects tokens-major routing.
// Order is enforced by the pattern (value_matches("1, 0")).
@@ -427,10 +440,17 @@ Convert2GatherMatmulMoeBlockToMoeOp::Convert2GatherMatmulMoeBlockToMoeOp(bool ha
moe_node = std::make_shared<ov::op::internal::MOE>(moe_inputs, config);
}
- moe_node->set_friendly_name(m.get_match_root()->get_friendly_name());
- ov::copy_runtime_info(m.get_matched_nodes(), moe_node);
- ov::replace_node(m.get_match_root(), moe_node);
-
+ auto replacement = moe_node;
+ if (!hidden_states.get_partial_shape().same_scheme(pm.at(end_reshape_m).get_partial_shape())) {
+ moe_node->set_friendly_name(m.get_match_root()->get_friendly_name() + "/MOE");
+ replacement = pm.at(end_reshape_m)
+ .get_node_shared_ptr()
+ ->clone_with_new_inputs({moe_node, pm.at(end_reshape_shape_m)});
+ register_new_node(replacement);
+ }
+ replacement->set_friendly_name(m.get_match_root()->get_friendly_name());
+ ov::copy_runtime_info(m.get_matched_nodes(), {moe_node, replacement});
+ ov::replace_node(m.get_match_root(), replacement);
register_new_node(moe_node);
return true;
};
diff --git a/src/common/transformations/tests/common_optimizations/convert_gather_matmuls_moe_block_to_moe_op.cpp b/src/common/transformations/tests/common_optimizations/convert_gather_matmuls_moe_block_to_moe_op.cpp
index f2a47fc9727..115a0cbb28f 100644
--- a/src/common/transformations/tests/common_optimizations/convert_gather_matmuls_moe_block_to_moe_op.cpp
+++ b/src/common/transformations/tests/common_optimizations/convert_gather_matmuls_moe_block_to_moe_op.cpp
@@ -280,7 +280,9 @@ inline std::shared_ptr<ov::Model> build_3gemm_moe_pattern_model() {
// ============================================================================
inline std::shared_ptr<ov::Model> build_3gemm_bgm_model(
- ov::op::internal::MOE::Activation_type activation_type = ov::op::internal::MOE::Activation_type::SWIGLU) {
+ ov::op::internal::MOE::Activation_type activation_type = ov::op::internal::MOE::Activation_type::SWIGLU,
+ bool has_batch_dim = true,
+ bool restore_output_shape = false) {
using namespace ov;
const size_t batch = 2;
@@ -290,7 +292,8 @@ inline std::shared_ptr<ov::Model> build_3gemm_bgm_model(
const size_t number_of_experts = 3;
const size_t topk = 2;
- auto input = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{batch, in_dim, hidden_size});
+ auto input_shape = has_batch_dim ? PartialShape{batch, in_dim, hidden_size} : PartialShape{in_dim, 1, hidden_size};
+ auto input = std::make_shared<op::v0::Parameter>(element::f32, input_shape);
auto experts_reshape = std::make_shared<op::v1::Reshape>(
input,
op::v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)}),
@@ -353,19 +356,22 @@ inline std::shared_ptr<ov::Model> build_3gemm_bgm_model(
op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{0}),
false);
+ const std::vector<int64_t> output_shape =
+ restore_output_shape ? std::vector<int64_t>{-1, static_cast<int64_t>(batch), static_cast<int64_t>(hidden_size)}
+ : has_batch_dim ? std::vector<int64_t>{static_cast<int64_t>(batch), -1, static_cast<int64_t>(hidden_size)}
+ : std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)};
auto end_reshape = std::make_shared<op::v1::Reshape>(
reduce_sum,
- op::v0::Constant::create(
- element::i64,
- Shape{3},
- std::vector<int64_t>{static_cast<int64_t>(batch), -1, static_cast<int64_t>(hidden_size)}),
+ op::v0::Constant::create(element::i64, Shape{output_shape.size()}, output_shape),
true);
return std::make_shared<ov::Model>(ov::OutputVector{end_reshape}, ov::ParameterVector{input});
}
inline std::shared_ptr<ov::Model> build_3gemm_bgm_to_moe_reference_model(
- ov::op::internal::MOE::Activation_type activation_type = ov::op::internal::MOE::Activation_type::SWIGLU) {
+ ov::op::internal::MOE::Activation_type activation_type = ov::op::internal::MOE::Activation_type::SWIGLU,
+ bool has_batch_dim = true,
+ bool restore_output_shape = false) {
using namespace ov;
const size_t batch = 2;
@@ -375,7 +381,8 @@ inline std::shared_ptr<ov::Model> build_3gemm_bgm_to_moe_reference_model(
const size_t number_of_experts = 3;
const size_t topk = 2;
- auto input = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{batch, in_dim, hidden_size});
+ auto input_shape = has_batch_dim ? PartialShape{batch, in_dim, hidden_size} : PartialShape{in_dim, 1, hidden_size};
+ auto input = std::make_shared<op::v0::Parameter>(element::f32, input_shape);
// Router subgraph (not fused, remains in the graph)
auto experts_reshape = std::make_shared<op::v1::Reshape>(
@@ -406,16 +413,28 @@ inline std::shared_ptr<ov::Model> build_3gemm_bgm_to_moe_reference_model(
op::v0::Constant::create(element::f32, Shape{number_of_experts, hidden_size, intermediate_size}, {1.0f});
// MOE op with compact routing
- ov::OutputVector moe_inputs = {input, routing, topk_indices, gate_w, up_w, down_w};
+ auto hidden_states = input->output(0);
+ ov::OutputVector moe_inputs = {hidden_states, routing, topk_indices, gate_w, up_w, down_w};
ov::op::internal::MOE::Config config;
config.expert_type = ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU;
config.activation_type = activation_type;
auto moe = std::make_shared<ov::op::internal::MOE>(moe_inputs, config);
- return std::make_shared<ov::Model>(ov::OutputVector{moe}, ov::ParameterVector{input});
+ ov::Output<ov::Node> output = moe;
+ if (!has_batch_dim || restore_output_shape) {
+ const std::vector<int64_t> output_shape =
+ restore_output_shape
+ ? std::vector<int64_t>{-1, static_cast<int64_t>(batch), static_cast<int64_t>(hidden_size)}
+ : std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)};
+ output = std::make_shared<op::v1::Reshape>(
+ moe,
+ op::v0::Constant::create(element::i64, Shape{output_shape.size()}, output_shape),
+ true);
+ }
+ return std::make_shared<ov::Model>(ov::OutputVector{output}, ov::ParameterVector{input});
}
-inline std::shared_ptr<ov::Model> build_2gemm_bgm_model() {
+inline std::shared_ptr<ov::Model> build_2gemm_bgm_model(bool has_batch_dim = true, bool restore_output_shape = false) {
using namespace ov;
const size_t batch = 2;
@@ -428,7 +447,8 @@ inline std::shared_ptr<ov::Model> build_2gemm_bgm_model() {
const auto expert_alpha = 1.702f;
const auto expert_beta = 7.0f;
- auto input = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{batch, in_dim, hidden_size});
+ auto input_shape = has_batch_dim ? PartialShape{batch, in_dim, hidden_size} : PartialShape{in_dim, 1, hidden_size};
+ auto input = std::make_shared<op::v0::Parameter>(element::f32, input_shape);
auto experts_reshape = std::make_shared<op::v1::Reshape>(
input,
op::v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)}),
@@ -513,18 +533,20 @@ inline std::shared_ptr<ov::Model> build_2gemm_bgm_model() {
std::make_shared<op::v1::ReduceSum>(final_mul,
op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{0}),
false);
+ const std::vector<int64_t> output_shape =
+ restore_output_shape ? std::vector<int64_t>{-1, static_cast<int64_t>(batch), static_cast<int64_t>(hidden_size)}
+ : has_batch_dim ? std::vector<int64_t>{static_cast<int64_t>(batch), -1, static_cast<int64_t>(hidden_size)}
+ : std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)};
auto end_reshape = std::make_shared<op::v1::Reshape>(
reduce_sum,
- op::v0::Constant::create(
- element::i64,
- Shape{3},
- std::vector<int64_t>{static_cast<int64_t>(batch), -1, static_cast<int64_t>(hidden_size)}),
+ op::v0::Constant::create(element::i64, Shape{output_shape.size()}, output_shape),
true);
return std::make_shared<ov::Model>(ov::OutputVector{end_reshape}, ov::ParameterVector{input});
}
-inline std::shared_ptr<ov::Model> build_2gemm_bgm_to_moe_reference_model() {
+inline std::shared_ptr<ov::Model> build_2gemm_bgm_to_moe_reference_model(bool has_batch_dim = true,
+ bool restore_output_shape = false) {
using namespace ov;
const size_t batch = 2;
@@ -537,7 +559,8 @@ inline std::shared_ptr<ov::Model> build_2gemm_bgm_to_moe_reference_model() {
const auto expert_alpha = 1.702f;
const auto expert_beta = 7.0f;
- auto input = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{batch, in_dim, hidden_size});
+ auto input_shape = has_batch_dim ? PartialShape{batch, in_dim, hidden_size} : PartialShape{in_dim, 1, hidden_size};
+ auto input = std::make_shared<op::v0::Parameter>(element::f32, input_shape);
auto experts_reshape = std::make_shared<op::v1::Reshape>(
input,
@@ -575,7 +598,8 @@ inline std::shared_ptr<ov::Model> build_2gemm_bgm_to_moe_reference_model() {
op::v0::Constant::create(element::f32, Shape{number_of_experts, hidden_size, intermediate_size}, {1.0f});
auto down_bias = op::v0::Constant::create(element::f32, Shape{number_of_experts, 1, hidden_size}, {1.0f});
- ov::OutputVector moe_inputs = {input, routing, topk_indices, gate_up_w, gate_up_bias, down_w, down_bias};
+ auto hidden_states = input->output(0);
+ ov::OutputVector moe_inputs = {hidden_states, routing, topk_indices, gate_up_w, gate_up_bias, down_w, down_bias};
ov::op::internal::MOE::Config config;
config.expert_type = ov::op::internal::MOE::Expert_type::GEMM2_BIAS_SWIGLU_CLAMP;
@@ -583,7 +607,18 @@ inline std::shared_ptr<ov::Model> build_2gemm_bgm_to_moe_reference_model() {
config.expert_beta = expert_beta;
auto moe = std::make_shared<ov::op::internal::MOE>(moe_inputs, config);
- return std::make_shared<ov::Model>(ov::OutputVector{moe}, ov::ParameterVector{input});
+ ov::Output<ov::Node> output = moe;
+ if (!has_batch_dim || restore_output_shape) {
+ const std::vector<int64_t> output_shape =
+ restore_output_shape
+ ? std::vector<int64_t>{-1, static_cast<int64_t>(batch), static_cast<int64_t>(hidden_size)}
+ : std::vector<int64_t>{-1, static_cast<int64_t>(hidden_size)};
+ output = std::make_shared<op::v1::Reshape>(
+ moe,
+ op::v0::Constant::create(element::i64, Shape{output_shape.size()}, output_shape),
+ true);
+ }
+ return std::make_shared<ov::Model>(ov::OutputVector{output}, ov::ParameterVector{input});
}
// ============================================================================
@@ -746,6 +781,18 @@ TEST_F(TransformationTestsF, Convert3GatherMatmulMoeBlockToMoeOp_basic) {
model_ref = build_3gemm_bgm_to_moe_reference_model();
}
+TEST_F(TransformationTestsF, Convert3GatherMatmulMoeBlockToMoeOp_without_batch_dim) {
+ model = build_3gemm_bgm_model(ov::op::internal::MOE::Activation_type::SWIGLU, false);
+ manager.register_pass<ov::pass::Convert3GatherMatmulMoeBlockToMoeOp>();
+ model_ref = build_3gemm_bgm_to_moe_reference_model(ov::op::internal::MOE::Activation_type::SWIGLU, false);
+}
+
+TEST_F(TransformationTestsF, Convert3GatherMatmulMoeBlockToMoeOp_restores_output_shape) {
+ model = build_3gemm_bgm_model(ov::op::internal::MOE::Activation_type::SWIGLU, true, true);
+ manager.register_pass<ov::pass::Convert3GatherMatmulMoeBlockToMoeOp>();
+ model_ref = build_3gemm_bgm_to_moe_reference_model(ov::op::internal::MOE::Activation_type::SWIGLU, true, true);
+}
+
TEST_F(TransformationTestsF, Convert3GatherMatmulMoeBlockToMoeOp_gelu_tanh) {
using AT = ov::op::internal::MOE::Activation_type;
model = build_3gemm_bgm_model(AT::GEGLU_TANH);
@@ -778,3 +825,15 @@ TEST_F(TransformationTestsF, Convert2GatherMatmulMoeBlockToMoeOp_basic) {
manager.register_pass<ov::pass::Convert2GatherMatmulMoeBlockToMoeOp>();
model_ref = build_2gemm_bgm_to_moe_reference_model();
}
+
+TEST_F(TransformationTestsF, Convert2GatherMatmulMoeBlockToMoeOp_without_batch_dim) {
+ model = build_2gemm_bgm_model(false);
+ manager.register_pass<ov::pass::Convert2GatherMatmulMoeBlockToMoeOp>();
+ model_ref = build_2gemm_bgm_to_moe_reference_model(false);
+}
+
+TEST_F(TransformationTestsF, Convert2GatherMatmulMoeBlockToMoeOp_restores_output_shape) {
+ model = build_2gemm_bgm_model(true, true);
+ manager.register_pass<ov::pass::Convert2GatherMatmulMoeBlockToMoeOp>();
+ model_ref = build_2gemm_bgm_to_moe_reference_model(true, true);
+}
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 1e9019091c7..b6f8f680a1c 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -688,16 +688,6 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
false);
}
- const bool is_pa = [&func]() {
- for (const auto& op : func->get_ops()) {
- if (ov::is_type<ov::op::PagedAttentionExtension>(op)) {
- return true;
- }
- }
-
- return false;
- }();
-
// MOE: TiledMoeBlock -> GatherMatmuls(compressed) -> MoeOp(compressed) -> MoeOpWithRouting(compressed).
// Gated on supports_immad (systolic-only) and oneDNN (required for expert GEMM dispatch).
// Note: even though we are already inside `if (supports_immad)`, oneDNN can still be explicitly disabled by the user.
@@ -717,17 +707,13 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
supported_compressed_weights_types);
manager.register_pass<ov::intel_gpu::FuseMoERouter>();
- {
- // PA models flatten batch into seq.
- const bool has_batch_dim = !is_pa;
- // MOE3GemmCompressed kernel dispatches expert GEMMs through
- // oneDNN, which requires an in-order OCL queue. If oneDNN is
- // disabled (e.g. via OV_GPU_USE_ONEDNN=0 on an IMMAD GPU), the
- // queue stays out-of-order and the oneDNN stream creation may assert.
- manager.register_pass<ov::pass::MoeOpFusion>(has_batch_dim);
- manager.register_pass<ov::intel_gpu::FuseMoERouterScale>();
- manager.register_pass<ov::intel_gpu::FuseMOESharedExpert>();
- }
+ // MOE3GemmCompressed kernel dispatches expert GEMMs through
+ // oneDNN, which requires an in-order OCL queue. If oneDNN is
+ // disabled (e.g. via OV_GPU_USE_ONEDNN=0 on an IMMAD GPU), the
+ // queue stays out-of-order and the oneDNN stream creation may assert.
+ manager.register_pass<ov::pass::MoeOpFusion>();
+ manager.register_pass<ov::intel_gpu::FuseMoERouterScale>();
+ manager.register_pass<ov::intel_gpu::FuseMOESharedExpert>();
}
manager.register_pass<ov::pass::GatedDeltaNetFusion>();
manager.register_pass<ov::pass::InitNodeInfo>();
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_router_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_router_test.cpp
index 492f7aa03c8..81108d4d599 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_router_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_router_test.cpp
@@ -31,15 +31,13 @@ namespace intel_gpu {
using namespace ov::test;
-using TestParams = std::tuple<MoERoutingType, bool, size_t, size_t>; // routing_type, with_convert_on_indices, num_expert, top_k
+using TestParams =
+ std::tuple<MoERoutingType, ov::element::Type, bool, size_t, size_t>; // routing_type, data_precision, with_convert_on_indices, num_expert, top_k
class FuseMoERouterTest : public TransformationTestsF, public ::testing::WithParamInterface<TestParams> {
public:
static std::string get_test_case_name(const ::testing::TestParamInfo<TestParams>& info) {
- const auto routing_type = std::get<0>(info.param);
- const bool with_convert = std::get<1>(info.param);
- const size_t num_expert = std::get<2>(info.param);
- const size_t top_k = std::get<3>(info.param);
+ const auto& [routing_type, data_precision, with_convert, num_expert, top_k] = info.param;
std::string name;
switch (routing_type) {
case MoERoutingType::SOFTMAX:
@@ -51,6 +49,7 @@ public:
default:
OPENVINO_THROW("Unsupported routing type");
}
+ name += "_" + data_precision.get_type_name();
name += with_convert ? "_WithConvert" : "_NoConvert";
name += "_E" + std::to_string(num_expert) + "_K" + std::to_string(top_k);
return name;
@@ -97,18 +96,18 @@ static std::pair<ov::Output<ov::Node>, ov::Output<ov::Node>> build_sigmoid_routi
}
TEST_P(FuseMoERouterTest, CompareFunctions) {
- const auto [routing_type, with_convert_on_indices, num_expert, top_k] = GetParam();
+ const auto [routing_type, data_precision, with_convert_on_indices, num_expert, top_k] = GetParam();
{
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{4, 8, 2048});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(data_precision, Shape{4, 8, 2048});
auto flatten_shape = op::v0::Constant::create(element::i32, Shape{2}, {32, 2048});
auto hidden_states_reshape = std::make_shared<ov::op::v1::Reshape>(hidden_states, flatten_shape, false);
- auto routers = op::v0::Constant::create(element::f16, Shape{2048, num_expert}, {0.2});
+ auto routers = op::v0::Constant::create(data_precision, Shape{2048, num_expert}, {0.2});
auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
const auto [routing_out, topk_indices] =
routing_type == MoERoutingType::SOFTMAX
? build_softmax_routing_for_fuse_test(routing_weights, top_k, with_convert_on_indices)
- : build_sigmoid_routing_for_fuse_test(routing_weights, element::f16, num_expert, top_k, with_convert_on_indices);
+ : build_sigmoid_routing_for_fuse_test(routing_weights, data_precision, num_expert, top_k, with_convert_on_indices);
// Wrap outputs to avoid feeding Result nodes directly (required by replace_output_update_name)
auto weights_out = std::make_shared<ov::op::v0::Unsqueeze>(routing_out, ov::op::v0::Constant::create(element::i32, Shape{1}, {0}));
@@ -117,10 +116,10 @@ TEST_P(FuseMoERouterTest, CompareFunctions) {
}
manager.register_pass<FuseMoERouter>();
{
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{4, 8, 2048});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(data_precision, Shape{4, 8, 2048});
auto flatten_shape = op::v0::Constant::create(element::i32, Shape{2}, {32, 2048});
auto hidden_states_reshape = std::make_shared<ov::op::v1::Reshape>(hidden_states, flatten_shape, false);
- auto routers = op::v0::Constant::create(element::f16, Shape{2048, num_expert}, {0.2});
+ auto routers = op::v0::Constant::create(data_precision, Shape{2048, num_expert}, {0.2});
auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
ov::intel_gpu::op::MoERouterFused::Config router_config;
@@ -129,8 +128,8 @@ TEST_P(FuseMoERouterTest, CompareFunctions) {
ov::OutputVector router_args{routing_weights};
if (routing_type == MoERoutingType::SIGMOID_BIAS) {
router_config.routing_type = ov::intel_gpu::op::MoERouterFused::RoutingType::SIGMOID_BIAS;
- router_args.push_back(op::v0::Constant::create(element::f16, Shape{1, num_expert}, {0.1f}));
- router_args.push_back(op::v0::Constant::create(element::f16, Shape{1, 1}, {1e-6f}));
+ router_args.push_back(op::v0::Constant::create(data_precision, Shape{1, num_expert}, {0.1f}));
+ router_args.push_back(op::v0::Constant::create(data_precision, Shape{1, 1}, {1e-6f}));
}
auto router_node = std::make_shared<ov::intel_gpu::op::MoERouterFused>(router_args, router_config);
@@ -143,6 +142,7 @@ TEST_P(FuseMoERouterTest, CompareFunctions) {
INSTANTIATE_TEST_SUITE_P(smoke,
FuseMoERouterTest,
::testing::Combine(::testing::Values(MoERoutingType::SOFTMAX, MoERoutingType::SIGMOID_BIAS),
+ ::testing::Values(element::f16, element::f32),
::testing::Values(true, false),
::testing::Values(128, 512),
::testing::Values(8, 10)),