Skip to content

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

[NPUW] Relax strictly-required moe pattern ops to optional (#38180)

YingyingMa8ea8c6e400d9
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/moe.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/moe.cpp
index 63832e7e142..14aa7ecbbe5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/moe.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/moe.cpp
@@ -11,6 +11,7 @@
#include "../../logging.hpp"
#include "openvino/op/ops.hpp"
#include "openvino/pass/pattern/op/optional.hpp"
+#include "openvino/pass/pattern/op/or.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "openvino/util/common_util.hpp"
@@ -157,11 +158,26 @@ RouterScorePattern build_shared_router_score_pattern() {
return {matmul, topk, reduce_sum, divide};
}
+// Matches Gemma4's per-expert learned scale as a literal Gather, or the OneHot-expand form
+// some exporters use instead: OneHot->Reshape->Multiply->ReduceSum->Reshape.
+std::shared_ptr<ov::Node> build_gemma4_router_scale() {
+ auto gather = opp::wrap_type<ov::op::v8::Gather>({opp::any_input(), opp::any_input(), opp::any_input()});
+
+ auto onehot =
+ opp::wrap_type<ov::op::v1::OneHot>({opp::any_input(), opp::any_input(), opp::any_input(), opp::any_input()});
+ auto onehot_reshape = opp::wrap_type<ov::op::v1::Reshape>({onehot, opp::any_input()});
+ auto scale_multiply = opp::wrap_type<ov::op::v1::Multiply>({onehot_reshape, opp::any_input()});
+ auto scale_reduce_sum = opp::wrap_type<ov::op::v1::ReduceSum>({scale_multiply, opp::any_input()});
+ auto scale_reshape = opp::wrap_type<ov::op::v1::Reshape>({scale_reduce_sum, opp::any_input()});
+
+ return std::make_shared<opp::op::Or>(ov::OutputVector{gather, scale_reshape});
+}
+
// Builds the shared router tail pattern (Scatter -> Transpose -> Reshape -> Unsqueeze).
// Pattern root is Unsqueeze; this helper encapsulates the tail logic common to all Router implementations.
std::shared_ptr<ov::Node> build_shared_router_tail(const std::shared_ptr<ov::Node>& scatter_input) {
auto transpose = opp::wrap_type<ov::op::v1::Transpose>({scatter_input, opp::any_input()});
- auto reshape = opp::wrap_type<ov::op::v1::Reshape>({transpose, opp::any_input()});
+ auto reshape = opp::optional<ov::op::v1::Reshape>({transpose, opp::any_input()});
auto unsqueeze = opp::wrap_type<ov::op::v0::Unsqueeze>({reshape, opp::any_input()});
return unsqueeze;
}
@@ -172,10 +188,12 @@ std::shared_ptr<ov::Node> build_shared_router_tail(const std::shared_ptr<ov::Nod
//
// Pattern structure (parameterised by activation operator):
// Tile -> Reshape1
-// Reshape1 -> MatMul_gate (dequant: Multiply->Convert) -> ActivationOp
-// Reshape1 -> MatMul_up (dequant: Multiply->Convert)
-// ActivationOp * MatMul_up -> MatMul_down (dequant: Multiply->Convert) -> Reshape2
+// Reshape1 -> MatMul_gate (dequant: Multiply->[Convert]) -> ActivationOp
+// Reshape1 -> MatMul_up (dequant: Multiply->[Convert])
+// ActivationOp * MatMul_up -> MatMul_down (dequant: Multiply->[Convert]) -> Reshape2
// Reshape2 * router_score -> output_multiply [pattern root]
+// Convert after each weight Multiply is optional: some exports feed the fp32
+// Multiply output straight into MatMul with no trailing Convert.
//
// Isolation strategy (applied in callback):
// - Prefill (token_count > 1): Isolate up to output_multiply; ReduceSum stays in downstream
@@ -186,17 +204,17 @@ std::pair<std::shared_ptr<opp::Matcher>, std::function<bool(opp::Matcher&)>> mak
const std::string& isol_tag,
const char* expert_name) {
auto tile = opp::wrap_type<ov::op::v0::Tile>({opp::any_input(), opp::any_input()});
- auto reshape1 = opp::wrap_type<ov::op::v1::Reshape>({tile, opp::any_input()});
+ auto reshape1 = opp::optional<ov::op::v1::Reshape>({tile, opp::any_input()});
// Gate projection: dequantized weights -> MatMul -> activation
auto gate_w_mul = opp::wrap_type<ov::op::v1::Multiply>({opp::any_input(), opp::any_input()});
- auto gate_w_cvt = opp::wrap_type<ov::op::v0::Convert>({gate_w_mul});
+ auto gate_w_cvt = opp::optional<ov::op::v0::Convert>({gate_w_mul});
auto matmul_gate = opp::wrap_type<ov::op::v0::MatMul>({reshape1, gate_w_cvt});
auto activation = opp::wrap_type<ActivationOp>({matmul_gate});
// Up projection: dequantized weights -> MatMul
auto up_w_mul = opp::wrap_type<ov::op::v1::Multiply>({opp::any_input(), opp::any_input()});
- auto up_w_cvt = opp::wrap_type<ov::op::v0::Convert>({up_w_mul});
+ auto up_w_cvt = opp::optional<ov::op::v0::Convert>({up_w_mul});
auto matmul_up = opp::wrap_type<ov::op::v0::MatMul>({reshape1, up_w_cvt});
// SwiGLU merge: activation(gate) * up
@@ -204,9 +222,9 @@ std::pair<std::shared_ptr<opp::Matcher>, std::function<bool(opp::Matcher&)>> mak
// Down projection: dequantized weights -> MatMul -> Reshape
auto down_w_mul = opp::wrap_type<ov::op::v1::Multiply>({opp::any_input(), opp::any_input()});
- auto down_w_cvt = opp::wrap_type<ov::op::v0::Convert>({down_w_mul});
+ auto down_w_cvt = opp::optional<ov::op::v0::Convert>({down_w_mul});
auto matmul_down = opp::wrap_type<ov::op::v0::MatMul>({merge, down_w_cvt});
- auto reshape2 = opp::wrap_type<ov::op::v1::Reshape>({matmul_down, opp::any_input()});
+ auto reshape2 = opp::optional<ov::op::v1::Reshape>({matmul_down, opp::any_input()});
// Pattern root: expert output * scattered router scores
auto output_multiply = opp::wrap_type<ov::op::v1::Multiply>({reshape2, opp::any_input()});
@@ -220,6 +238,10 @@ std::pair<std::shared_ptr<opp::Matcher>, std::function<bool(opp::Matcher&)>> mak
LOG_DEBUG(expert_name << " Expert pattern matched (" << (is_decoding ? "Decoding" : "Prefill") << " stage)");
auto isolate = [&](const std::shared_ptr<ov::Node>& pattern_node) {
+ // A branch that didn't match has no entry in the map; skip rather than assert.
+ if (node_to_output.count(pattern_node) == 0) {
+ return;
+ }
isolate_node(node_to_output.at(pattern_node).get_node_shared_ptr(), isol_tag, node_to_gptr);
};
@@ -446,16 +468,16 @@ GPTOSSRouter::GPTOSSRouter([[maybe_unused]] const std::shared_ptr<ov::npuw::onli
Tile -> Reshape1
Gate projection (SwiGLU gate branch):
- Reshape1 -> MatMul_gate (with weights: Multiply -> Convert) -> Swish
+ Reshape1 -> MatMul_gate (with weights: Multiply -> [Convert]) -> Swish
Up projection (SwiGLU up branch):
- Reshape1 -> MatMul_up (with weights: Multiply -> Convert)
+ Reshape1 -> MatMul_up (with weights: Multiply -> [Convert])
SwiGLU merge:
Swish + MatMul_up -> Multiply_swiglu
Down projection:
- Multiply_swiglu -> MatMul_down (with weights: Multiply -> Convert) -> Reshape2
+ Multiply_swiglu -> MatMul_down (with weights: Multiply -> [Convert]) -> Reshape2
Output (scaled by router scores):
Reshape2 * router_score -> Multiply_output <-- pattern root
@@ -515,16 +537,16 @@ Qwen3Router::Qwen3Router([[maybe_unused]] const std::shared_ptr<ov::npuw::online
Tile -> Reshape1
Gate projection (with weights Convert1) -> Gelu:
- Reshape1 -> MatMul1 (Multiply -> Convert) -> Gelu
+ Reshape1 -> MatMul1 (Multiply -> [Convert]) -> Gelu
Up projection (with weights Convert2):
- Reshape1 -> MatMul2 (Multiply -> Convert)
+ Reshape1 -> MatMul2 (Multiply -> [Convert])
SwiGLU merge:
Gelu + MatMul2 -> Multiply1
Down projection (with weights Convert3) -> Reshape2:
- Multiply1 -> MatMul3 (Multiply -> Convert) -> Reshape2
+ Multiply1 -> MatMul3 (Multiply -> [Convert]) -> Reshape2
Output:
Reshape2 -> Multiply2 (with scattered router scores) <-- pattern root
@@ -548,26 +570,27 @@ Gemma4Expert::Gemma4Expert(const std::shared_ptr<ov::npuw::online::Snapshot>& sn
MatMul inputs may be quantized or non-quantized; pattern uses any_input() for flexibility
Gemma4-specific scores path (per-expert learned scale):
- Gather(per_expert_scale, topk_indices) -> Multiply(Divide, Gather) -> Slice
- per-expert scale is combined with renormalized scores before scatter
+ Gather(per_expert_scale, topk_indices) -- or, equivalently, an OneHot-expand form some
+ exports use instead (see build_gemma4_router_scale()) -- combined with the renormalized
+ scores via Multiply(Divide, ^) before an optional Slice and scatter.
Scatter to full expert dimension:
- ScatterElementsUpdate -> Transpose -> Reshape -> Unsqueeze <-- pattern root
+ ScatterElementsUpdate -> Transpose -> [Reshape] -> Unsqueeze <-- pattern root
Key difference from Qwen3Router:
- - Per-expert learned scale applied via Gather+Multiply before scatter (mandatory in Gemma4)
+ - Per-expert learned scale applied via Gather (or an equivalent OneHot+Multiply+ReduceSum
+ expansion) before scatter (mandatory in Gemma4)
*/
Gemma4Router::Gemma4Router([[maybe_unused]] const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot,
[[maybe_unused]] const std::string& isol_tag) {
// Use shared score computation: MatMul -> Softmax -> TopK -> ReduceSum -> Divide
auto score_nodes = build_shared_router_score_pattern();
- // Gemma4-specific: Per-expert learned scale via Gather, then Multiply and Slice
- // TopK output(1) (indices) goes through Convert before Gather port 1;
- // wrap_type can only express output(0), so both use any_input().
- auto gather = opp::wrap_type<ov::op::v8::Gather>({opp::any_input(), opp::any_input(), opp::any_input()});
- auto scores_multiply = opp::wrap_type<ov::op::v1::Multiply>({score_nodes.divide, gather});
- auto slice = opp::wrap_type<ov::op::v8::Slice>(
+ // Gemma4-specific: per-expert learned scale, either a literal Gather or the OneHot-expanded
+ // equivalent (see build_gemma4_router_scale()).
+ auto per_expert_scale = build_gemma4_router_scale();
+ auto scores_multiply = opp::wrap_type<ov::op::v1::Multiply>({score_nodes.divide, per_expert_scale});
+ auto slice = opp::optional<ov::op::v8::Slice>(
{scores_multiply, opp::any_input(), opp::any_input(), opp::any_input(), opp::any_input()});
// Scatter inputs: any_input() for indices and zero_broadcast, slice for scores
diff --git a/src/plugins/intel_npu/tests/unit/npuw/moe_k_tag_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/moe_k_tag_test.cpp
index edf2c07d397..997bee7b7ae 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/moe_k_tag_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/moe_k_tag_test.cpp
@@ -394,12 +394,20 @@ TEST_F(Qwen3RouterTest, RouterNodesNotIsolated_WithConvertAndSlice) {
// Unsqueeze output (the pattern root).
// Gemma4 differences vs Qwen3:
// - FP32 plain-weight MatMul (no dequantisation chain)
-// - Per-expert learned scale via Gather before scatter
-// - Extra Slice between scores and ScatterElementsUpdate
+// - Per-expert learned scale via Gather (or an OneHot-expand equivalent) before scatter
+// - Optional Slice between scores and ScatterElementsUpdate
+// use_onehot_scale: build the OneHot->Reshape->Multiply->ReduceSum->Reshape alternative
+// for the per-expert scale instead of a literal Gather (the other Or-branch in
+// build_gemma4_router_scale()).
+// with_slice / with_tail_reshape: control presence of the two optional structural ops
+// that some Gemma4 export variants omit.
std::shared_ptr<Node> build_gemma4_router_layer(const std::shared_ptr<op::v0::Parameter>& router_input,
int64_t k_value,
int layer_idx,
- size_t num_experts) {
+ size_t num_experts,
+ bool use_onehot_scale = false,
+ bool with_slice = true,
+ bool with_tail_reshape = true) {
const size_t hidden_dim = router_input->get_shape()[1];
const std::string prefix = "__module.model.layer" + std::to_string(layer_idx) + ".router/";
@@ -423,40 +431,77 @@ std::shared_ptr<Node> build_gemma4_router_layer(const std::shared_ptr<op::v0::Pa
auto reduce_sum = std::make_shared<op::v1::ReduceSum>(topk->output(0), reduce_axes, /*keep_dims=*/true);
auto divide = std::make_shared<op::v1::Divide>(topk->output(0), reduce_sum);
- // Per-expert learned scale: Gather(per_expert_scale, Convert(topk_indices), axis=0)
- auto per_expert_scale =
- op::v0::Constant::create(element::f32, Shape{num_experts}, std::vector<float>(num_experts, 1.0f));
+ // Per-expert learned scale: either Gather(per_expert_scale, Convert(topk_indices), axis=0),
+ // or the OneHot-expand equivalent some exporters use instead.
auto topk_indices_convert = std::make_shared<op::v0::Convert>(topk->output(1), element::i64);
- auto gather_axis = op::v0::Constant::create(element::i64, Shape{}, {0LL});
- auto gather = std::make_shared<op::v8::Gather>(per_expert_scale, topk_indices_convert, gather_axis);
+ Output<Node> per_expert_scale_out;
+ if (!use_onehot_scale) {
+ auto per_expert_scale =
+ op::v0::Constant::create(element::f32, Shape{num_experts}, std::vector<float>(num_experts, 1.0f));
+ auto gather_axis = op::v0::Constant::create(element::i64, Shape{}, {0LL});
+ auto gather = std::make_shared<op::v8::Gather>(per_expert_scale, topk_indices_convert, gather_axis);
+ per_expert_scale_out = gather->output(0);
+ } else {
+ // OneHot(topk_indices, depth=num_experts) -> Reshape -> Multiply(per_expert_scale)
+ // -> ReduceSum(over experts) -> Reshape(back to [1, k]).
+ auto per_expert_scale =
+ op::v0::Constant::create(element::f32, Shape{num_experts}, std::vector<float>(num_experts, 1.0f));
+ auto onehot_depth = op::v0::Constant::create(element::i64, Shape{}, {static_cast<int64_t>(num_experts)});
+ auto on_value = op::v0::Constant::create(element::f32, Shape{}, {1.0f});
+ auto off_value = op::v0::Constant::create(element::f32, Shape{}, {0.0f});
+ auto onehot =
+ std::make_shared<op::v1::OneHot>(topk_indices_convert, onehot_depth, on_value, off_value, /*axis=*/-1);
+ auto onehot_reshape_shape =
+ op::v0::Constant::create(element::i64,
+ Shape{2},
+ std::vector<int64_t>{k_value, static_cast<int64_t>(num_experts)});
+ auto onehot_reshape = std::make_shared<op::v1::Reshape>(onehot, onehot_reshape_shape, false);
+ auto scale_multiply = std::make_shared<op::v1::Multiply>(onehot_reshape, per_expert_scale);
+ auto reduce_axis = op::v0::Constant::create(element::i64, Shape{1}, {1LL});
+ auto scale_reduce_sum = std::make_shared<op::v1::ReduceSum>(scale_multiply, reduce_axis, /*keep_dims=*/false);
+ auto scale_reshape_shape = op::v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{1LL, k_value});
+ auto scale_reshape = std::make_shared<op::v1::Reshape>(scale_reduce_sum, scale_reshape_shape, false);
+ per_expert_scale_out = scale_reshape->output(0);
+ }
// Combine renormalised scores with per-expert scale
- auto scores_multiply = std::make_shared<op::v1::Multiply>(divide, gather);
-
- // Slice(scores_multiply, 0, k_value, step=1, axis=1) — Gemma4-specific trim
- auto sl_begin = op::v0::Constant::create(element::i64, Shape{1}, {0LL});
- auto sl_end = op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{k_value});
- auto sl_step = op::v0::Constant::create(element::i64, Shape{1}, {1LL});
- auto sl_axes = op::v0::Constant::create(element::i64, Shape{1}, {1LL});
- auto slice = std::make_shared<op::v8::Slice>(scores_multiply, sl_begin, sl_end, sl_step, sl_axes);
+ auto scores_multiply = std::make_shared<op::v1::Multiply>(divide, per_expert_scale_out);
+
+ // Slice(scores_multiply, 0, k_value, step=1, axis=1) — Gemma4-specific trim, optional
+ Output<Node> scatter_scores = scores_multiply->output(0);
+ if (with_slice) {
+ auto sl_begin = op::v0::Constant::create(element::i64, Shape{1}, {0LL});
+ auto sl_end = op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{k_value});
+ auto sl_step = op::v0::Constant::create(element::i64, Shape{1}, {1LL});
+ auto sl_axes = op::v0::Constant::create(element::i64, Shape{1}, {1LL});
+ auto slice = std::make_shared<op::v8::Slice>(scores_multiply, sl_begin, sl_end, sl_step, sl_axes);
+ scatter_scores = slice->output(0);
+ }
// Scatter selected scores to full-expert dimension
auto zero_base =
op::v0::Constant::create(element::f32, Shape{1, num_experts}, std::vector<float>(num_experts, 0.0f));
auto scatter_axis = op::v0::Constant::create(element::i64, Shape{}, {1LL});
auto scatter_indices = std::make_shared<op::v0::Convert>(topk->output(1), element::i64);
- auto scatter = std::make_shared<op::v12::ScatterElementsUpdate>(zero_base, scatter_indices, slice, scatter_axis);
+ auto scatter =
+ std::make_shared<op::v12::ScatterElementsUpdate>(zero_base, scatter_indices, scatter_scores, scatter_axis);
- // Tail: Transpose -> Reshape -> Unsqueeze (pattern root)
+ // Tail: Transpose -> [Reshape] -> Unsqueeze (pattern root, Reshape optional)
auto t_order = op::v0::Constant::create(element::i32, Shape{2}, std::vector<int32_t>{1, 0});
auto transpose = std::make_shared<op::v1::Transpose>(scatter, t_order);
- auto reshape_shape =
- op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{static_cast<int64_t>(num_experts), 1, 1});
- auto reshape = std::make_shared<op::v1::Reshape>(transpose, reshape_shape, false);
-
- auto unsqueeze_axis = op::v0::Constant::create(element::i64, Shape{}, {3LL});
- return std::make_shared<op::v0::Unsqueeze>(reshape, unsqueeze_axis);
+ if (with_tail_reshape) {
+ auto reshape_shape = op::v0::Constant::create(element::i64,
+ Shape{3},
+ std::vector<int64_t>{static_cast<int64_t>(num_experts), 1, 1});
+ auto reshape = std::make_shared<op::v1::Reshape>(transpose, reshape_shape, false);
+ auto unsqueeze_axis = op::v0::Constant::create(element::i64, Shape{}, {3LL});
+ return std::make_shared<op::v0::Unsqueeze>(reshape, unsqueeze_axis);
+ }
+ // Without the tail Reshape, Transpose's own rank leaves room for the same final
+ // unsqueeze axis one position earlier ([num_experts, 1] -> [num_experts, 1, 1]).
+ auto unsqueeze_axis = op::v0::Constant::create(element::i64, Shape{}, {2LL});
+ return std::make_shared<op::v0::Unsqueeze>(transpose, unsqueeze_axis);
}
std::shared_ptr<Model> build_gemma4_router_graph(int64_t k_value, size_t hidden_dim = 16, size_t num_experts = 8) {
@@ -466,6 +511,26 @@ std::shared_ptr<Model> build_gemma4_router_graph(int64_t k_value, size_t hidden_
return std::make_shared<Model>(ResultVector{std::make_shared<op::v0::Result>(out)}, ParameterVector{router_input});
}
+// Builds a Gemma4 Router graph exercising the newly-optional matcher variants:
+// the OneHot-expand per-expert scale, and/or the absent pre-scatter Slice / tail Reshape.
+std::shared_ptr<Model> build_gemma4_router_graph_variant(int64_t k_value,
+ bool use_onehot_scale,
+ bool with_slice,
+ bool with_tail_reshape,
+ size_t hidden_dim = 16,
+ size_t num_experts = 8) {
+ auto router_input = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, hidden_dim});
+ router_input->set_friendly_name("router_input");
+ auto out = build_gemma4_router_layer(router_input,
+ k_value,
+ 0,
+ num_experts,
+ use_onehot_scale,
+ with_slice,
+ with_tail_reshape);
+ return std::make_shared<Model>(ResultVector{std::make_shared<op::v0::Result>(out)}, ParameterVector{router_input});
+}
+
std::shared_ptr<Model> build_two_gemma4_router_model(int64_t k0,
int64_t k1,
size_t hidden_dim = 16,
@@ -558,4 +623,77 @@ TEST_F(Gemma4RouterTest, InconsistentKAcrossLayersThrows) {
EXPECT_THROW(run_pass(model), ov::Exception) << "Inconsistent K values across MoE layers must throw";
}
+// Verifies the OneHot-expand alternative for the per-expert scale (the other Or-branch
+// in build_gemma4_router_scale()) matches and tags K, not just the literal Gather form.
+TEST_F(Gemma4RouterTest, OneHotScaleTagsTopKWithCorrectK) {
+ constexpr int64_t K = 4;
+ auto model = build_gemma4_router_graph_variant(K,
+ /*use_onehot_scale=*/true,
+ /*with_slice=*/true,
+ /*with_tail_reshape=*/true);
+ run_pass(model);
+
+ auto topks = find_all_topk(model);
+ ASSERT_EQ(topks.size(), 1u);
+ const auto& rt = topks[0]->get_rt_info();
+ ASSERT_NE(rt.find(ov::npuw::patterns::moe::RT_INFO_MOE_K), rt.end())
+ << "K must be tagged when the per-expert scale uses the OneHot-expand form";
+ EXPECT_EQ(rt.at(ov::npuw::patterns::moe::RT_INFO_MOE_K).as<size_t>(), static_cast<size_t>(K));
+}
+
+// Verifies the absent-optional path: no pre-scatter Slice and no router-tail Reshape.
+TEST_F(Gemma4RouterTest, MissingSliceAndTailReshapeTagsTopKWithCorrectK) {
+ constexpr int64_t K = 4;
+ auto model = build_gemma4_router_graph_variant(K,
+ /*use_onehot_scale=*/false,
+ /*with_slice=*/false,
+ /*with_tail_reshape=*/false);
+ run_pass(model);
+
+ auto topks = find_all_topk(model);
+ ASSERT_EQ(topks.size(), 1u);
+ const auto& rt = topks[0]->get_rt_info();
+ ASSERT_NE(rt.find(ov::npuw::patterns::moe::RT_INFO_MOE_K), rt.end())
+ << "K must be tagged when the optional Slice and tail Reshape are both absent";
+ EXPECT_EQ(rt.at(ov::npuw::patterns::moe::RT_INFO_MOE_K).as<size_t>(), static_cast<size_t>(K));
+}
+
+// Combines all three newly-optional matcher variants at once: OneHot scale, no Slice,
+// no tail Reshape.
+TEST_F(Gemma4RouterTest, OneHotScaleWithoutSliceOrTailReshapeTagsTopKWithCorrectK) {
+ constexpr int64_t K = 4;
+ auto model = build_gemma4_router_graph_variant(K,
+ /*use_onehot_scale=*/true,
+ /*with_slice=*/false,
+ /*with_tail_reshape=*/false);
+ run_pass(model);
+
+ auto topks = find_all_topk(model);
+ ASSERT_EQ(topks.size(), 1u);
+ const auto& rt = topks[0]->get_rt_info();
+ ASSERT_NE(rt.find(ov::npuw::patterns::moe::RT_INFO_MOE_K), rt.end())
+ << "K must be tagged when OneHot scale, missing Slice, and missing tail Reshape combine";
+ EXPECT_EQ(rt.at(ov::npuw::patterns::moe::RT_INFO_MOE_K).as<size_t>(), static_cast<size_t>(K));
+}
+
+// Callback must still return false (no isolation) for the fully-absent-optional variant.
+TEST_F(Gemma4RouterTest, RouterNodesNotIsolated_OneHotWithoutSliceOrTailReshape) {
+ constexpr int64_t K = 4;
+ auto model = build_gemma4_router_graph_variant(K,
+ /*use_onehot_scale=*/true,
+ /*with_slice=*/false,
+ /*with_tail_reshape=*/false);
+ auto snapshot = std::make_shared<ov::npuw::online::Snapshot>(model);
+ snapshot->buildGraph();
+
+ ov::pass::GraphRewrite rewr;
+ rewr.add_matcher<ov::npuw::patterns::moe::Gemma4Router>(snapshot, "router");
+ rewr.run_on_model(model);
+
+ for (const auto& [node, group] : *snapshot->getNodeToGroupMap()) {
+ EXPECT_NE(group->isolatedTag(), "router")
+ << "Node \"" << node->get_friendly_name() << "\" was unexpectedly isolated";
+ }
+}
+
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/moe_transformation_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/moe_transformation_test.cpp
index 95b3b6b3e8c..5522808c80e 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/moe_transformation_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/moe_transformation_test.cpp
@@ -962,6 +962,122 @@ static std::shared_ptr<Model> create_gemma4_expert_graph(size_t num_experts,
return model;
}
+// Build a Gemma4-style expert subgraph with all newly-optional structural ops absent:
+// - no Reshape1 (Tile's input/repeats are already 3D, so Tile itself emits
+// [num_experts, token_count, hidden_dim] with no reshape needed)
+// - no per-weight Convert after each dequant Multiply (the Multiply output is already fp32)
+// - no Reshape2 (MatMul_down's output already has the router-score-compatible shape)
+// Mirrors create_gemma4_expert_graph() above but exercises the absent-optional path added
+// to make_swiglu_expert_matcher().
+static std::shared_ptr<Model> create_gemma4_expert_graph_no_optional(size_t num_experts,
+ size_t hidden_dim = 64,
+ size_t intermediate_dim = 128,
+ size_t token_count = 1) {
+ ov::ParameterVector params;
+
+ // Expert input already 3D: [1, token_count, hidden_dim]
+ auto expert_input = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, token_count, hidden_dim});
+ expert_input->set_friendly_name("expert_input");
+ params.push_back(expert_input);
+
+ // Tile directly produces [num_experts, token_count, hidden_dim]; no Reshape1 needed.
+ auto repeats =
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{static_cast<int64_t>(num_experts), 1, 1});
+ auto tile = std::make_shared<op::v0::Tile>(expert_input, repeats);
+ tile->set_friendly_name("expert_tile");
+
+ // Dequant weight chain feeding MatMul directly (Multiply already fp32; no trailing Convert).
+ auto make_dq_weight_no_convert = [&](const std::string& name, const Shape& weight_shape) -> std::shared_ptr<Node> {
+ auto w_nf4 = std::make_shared<op::v0::Parameter>(element::nf4, weight_shape);
+ w_nf4->set_friendly_name(name + "_nf4");
+ params.push_back(w_nf4);
+ auto w_fp32 = std::make_shared<op::v0::Convert>(w_nf4, element::f32);
+ w_fp32->set_friendly_name(name + "_fp32");
+ Shape scale_shape{weight_shape[0], weight_shape[1], 1};
+ auto scale = std::make_shared<op::v0::Parameter>(element::f32, scale_shape);
+ scale->set_friendly_name(name + "_scale");
+ params.push_back(scale);
+ auto w_scaled = std::make_shared<op::v1::Multiply>(w_fp32, scale);
+ w_scaled->set_friendly_name(name + "_multiply");
+ return w_scaled;
+ };
+
+ // Gate projection: MatMul1(tile, DQ_gate) -> Gelu
+ auto gate_dq = make_dq_weight_no_convert("gate_weights", Shape{num_experts, intermediate_dim, hidden_dim});
+ auto matmul1 = std::make_shared<op::v0::MatMul>(tile, gate_dq, false, true);
+ matmul1->set_friendly_name("expert_matmul1_gate");
+ auto gelu = std::make_shared<op::v7::Gelu>(matmul1);
+ gelu->set_friendly_name("expert_gelu");
+
+ // Up projection: MatMul2(tile, DQ_up)
+ auto up_dq = make_dq_weight_no_convert("up_weights", Shape{num_experts, intermediate_dim, hidden_dim});
+ auto matmul2 = std::make_shared<op::v0::MatMul>(tile, up_dq, false, true);
+ matmul2->set_friendly_name("expert_matmul2_up");
+
+ // SwiGLU merge: Gelu(gate) * up
+ auto multiply1 = std::make_shared<op::v1::Multiply>(gelu, matmul2);
+ multiply1->set_friendly_name("expert_multiply1_swiglu");
+
+ // Down projection: MatMul3(merge, DQ_down); output is already [num_experts, token_count,
+ // hidden_dim], so no Reshape2 is needed before the router-score Multiply.
+ auto down_dq = make_dq_weight_no_convert("down_weights", Shape{num_experts, hidden_dim, intermediate_dim});
+ auto matmul3 = std::make_shared<op::v0::MatMul>(multiply1, down_dq, false, true);
+ matmul3->set_friendly_name("expert_matmul3_down");
+
+ // Pattern root: matmul3 output * scattered router scores
+ auto router_scores = std::make_shared<op::v0::Parameter>(element::f32, Shape{num_experts, token_count, 1});
+ router_scores->set_friendly_name("router_scores");
+ params.push_back(router_scores);
+ auto output_multiply = std::make_shared<op::v1::Multiply>(matmul3, router_scores);
+ output_multiply->set_friendly_name("output_multiply");
+
+ auto result = std::make_shared<op::v0::Result>(output_multiply);
+ auto model = std::make_shared<Model>(ResultVector{result}, params);
+ model->set_friendly_name("gemma4_expert_no_optional_" + std::to_string(num_experts));
+ return model;
+}
+
+// Verify that Gemma4Expert still isolates all matched nodes with the "expert" tag when
+// Reshape1, Reshape2, and the three weight Converts are all absent from the graph.
+TEST_F(MoETransformationTest, Gemma4Expert_NoOptionalReshapesOrConverts_IsolatesNodes) {
+ constexpr size_t num_experts = 8;
+ auto model = create_gemma4_expert_graph_no_optional(num_experts,
+ /*hidden_dim=*/64,
+ /*intermediate_dim=*/128,
+ /*token_count=*/1);
+
+ auto snapshot = std::make_shared<ov::npuw::online::Snapshot>(model);
+ snapshot->buildGraph();
+
+ ov::pass::GraphRewrite rewr;
+ rewr.add_matcher<ov::npuw::patterns::moe::Gemma4Expert>(snapshot, "expert");
+ ASSERT_NO_THROW(rewr.run_on_model(model));
+
+ const auto& node_map = *snapshot->getNodeToGroupMap();
+ auto is_isolated = [&](const std::string& name) {
+ for (const auto& [node, group] : node_map) {
+ if (node->get_friendly_name() == name) {
+ return group->isolatedTag() == "expert";
+ }
+ }
+ return false;
+ };
+
+ // The core compute chain must still be isolated even without the optional ops.
+ for (const auto& name : {"expert_tile",
+ "expert_matmul1_gate",
+ "expert_gelu",
+ "gate_weights_multiply",
+ "expert_matmul2_up",
+ "up_weights_multiply",
+ "expert_multiply1_swiglu",
+ "expert_matmul3_down",
+ "down_weights_multiply",
+ "output_multiply"}) {
+ EXPECT_TRUE(is_isolated(name)) << "Expected node \"" << name << "\" to be isolated into the expert group";
+ }
+}
+
// Verify that Gemma4Expert isolates all matched nodes with the "expert" tag.
TEST_F(MoETransformationTest, Gemma4Expert_IsolatesNodes) {
constexpr size_t num_experts = 8;