Skip to content

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

[GPU] Qwen3_next_MOE_enable (#33829)

Chenhu Wange7b3a553bec3
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/fuse_moe_3gemm_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/fuse_moe_3gemm_compressed.cpp
index 9cdc5fe9c42..0ac2fe79f53 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/fuse_moe_3gemm_compressed.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/fuse_moe_3gemm_compressed.cpp
@@ -9,7 +9,10 @@
#include "intel_gpu/op/moe_compressed.hpp"
#include "intel_gpu/op/moe_3gemm_fused_compressed.hpp"
#include "openvino/core/graph_util.hpp"
+#include "openvino/core/node.hpp"
#include "openvino/core/rt_info.hpp"
+#include "openvino/core/type.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
@@ -22,42 +25,52 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_elements_update.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/softmax.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/topk.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/pattern/op/optional.hpp"
+#include "openvino/pass/pattern/op/or.hpp"
#include "openvino/pass/pattern/op/pattern.hpp"
+#include "openvino/pass/pattern/op/label.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/utils/utils.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/label.hpp"
+#include "openvino/pass/pattern/op/op.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+
namespace ov::intel_gpu {
+using namespace ov::pass;
using namespace ov::pass::pattern;
FuseMOE3GemmCompressed::FuseMOE3GemmCompressed() {
auto hidden_state_m = any_input();
auto routers_m = any_input();
- auto router_matmul_m = wrap_type<ov::op::v0::MatMul>({hidden_state_m, routers_m}, consumers_count(1));
+ auto hidden_state_reshape_m = wrap_type<ov::op::v1::Reshape>({hidden_state_m, any_input()});
+ auto router_matmul_m = wrap_type<ov::op::v0::MatMul>({hidden_state_reshape_m, routers_m}, consumers_count(1));
auto softmax_m = wrap_type<ov::op::v8::Softmax>({router_matmul_m}, consumers_count(1));
auto topk_m = wrap_type<ov::op::v11::TopK>({softmax_m, any_input()});
topk_m->set_output_size(2);
+ // idx output
+ auto topk_idx_m = optional<ov::op::v0::Convert>({topk_m->output(1)});
// weight output
auto reduce_sum_m = wrap_type<ov::op::v1::ReduceSum>({topk_m->output(0), any_input()}, consumers_count(1));
auto norm_m = wrap_type<ov::op::v1::Divide>({topk_m->output(0), reduce_sum_m->output(0)}, consumers_count(1));
- // idx output
- auto shape_of_m = wrap_type<ov::op::v3::ShapeOf>({topk_m->output(1)}, consumers_count(1));
- auto gather_m = wrap_type<ov::op::v8::Gather>({shape_of_m, any_input(), any_input()}, consumers_count(1));
- auto unsqueeze_m = wrap_type<ov::op::v0::Unsqueeze>({gather_m, any_input()});
-
- auto unsqueeze_const_m = wrap_type<ov::op::v0::Unsqueeze>({any_input(), any_input()});
- auto concat_m = wrap_type<ov::op::v0::Concat>({unsqueeze_m, unsqueeze_const_m}, consumers_count(1));
- auto concat1_m = wrap_type<ov::op::v0::Concat>({unsqueeze_const_m, unsqueeze_m, any_input()}, consumers_count(1));
- auto bc_m = wrap_type<ov::op::v3::Broadcast>({any_input(), concat_m}, consumers_count(1));
- auto scatter_m = wrap_type<ov::op::v12::ScatterElementsUpdate>({bc_m->output(0), topk_m->output(1), norm_m->output(0), any_input()}, consumers_count(1));
+ auto bc_m = wrap_type<ov::op::v3::Broadcast>({any_input(), any_input()});
+ auto shape_of_m = wrap_type<ov::op::v3::ShapeOf>({topk_idx_m}, consumers_count(1));
+ auto norm_slice = optional<ov::op::v8::Slice>({norm_m, any_input(), shape_of_m, any_input(), any_input()}, consumers_count(1));
+
+ auto scatter_m = wrap_type<ov::op::v12::ScatterElementsUpdate>({bc_m, topk_idx_m, norm_m | norm_slice, any_input()}, consumers_count(1));
+
auto transpose_m = wrap_type<ov::op::v1::Transpose>({scatter_m, any_input()}, consumers_count(1));
- auto reshape_m = wrap_type<ov::op::v1::Reshape>({transpose_m, concat1_m}, consumers_count(1));
+ auto reshape_m = wrap_type<ov::op::v1::Reshape>({transpose_m, any_input()}, consumers_count(1));
auto unsqueeze_moe_m = wrap_type<ov::op::v0::Unsqueeze>({reshape_m, any_input()}, consumers_count(1));
auto gate_wei_m = wrap_type<ov::op::v0::Constant>();
@@ -71,9 +84,9 @@ FuseMOE3GemmCompressed::FuseMOE3GemmCompressed() {
auto down_zp_m = any_input();
// moe compressed
- auto moe_compressed_m = wrap_type<ov::intel_gpu::op::MOECompressed>({hidden_state_m->output(0),
+ auto moe_compressed_m = wrap_type<ov::intel_gpu::op::MOECompressed>({hidden_state_reshape_m | hidden_state_m,
unsqueeze_moe_m->output(0),
- topk_m->output(1),
+ topk_idx_m->output(0),
gate_wei_m->output(0),
gate_scale_m->output(0),
gate_zp_m->output(0),
@@ -91,8 +104,9 @@ FuseMOE3GemmCompressed::FuseMOE3GemmCompressed() {
if (!moe_compressed || transformation_callback(moe_compressed)) {
return false;
}
+
OutputVector args(11);
- args[0] = pattern_map.at(hidden_state_m);
+ args[0] = pattern_map.at(hidden_state_reshape_m);
args[1] = pattern_map.at(router_matmul_m);
args[2] = pattern_map.at(gate_wei_m);
args[3] = pattern_map.at(gate_scale_m);
@@ -104,10 +118,16 @@ FuseMOE3GemmCompressed::FuseMOE3GemmCompressed() {
args[9] = pattern_map.at(down_scale_m);
args[10] = pattern_map.at(down_zp_m);
- auto moe_3gemm_fused_compressed = std::make_shared<ov::intel_gpu::op::MOE3GemmFusedCompressed>(args, moe_compressed->get_config());
- moe_3gemm_fused_compressed->set_friendly_name(moe_compressed->get_friendly_name());
- ov::copy_runtime_info(moe_compressed, moe_3gemm_fused_compressed);
- ov::replace_node(moe_compressed, moe_3gemm_fused_compressed);
+ std::shared_ptr<ov::op::Op> moe_router_fused = std::make_shared<ov::intel_gpu::op::MOE3GemmFusedCompressed>(args, moe_compressed->get_config());
+ ov::copy_runtime_info(moe_compressed, moe_router_fused);
+ if (moe_compressed->input_value(0) == pattern_map.at(hidden_state_m)) {
+ auto hidden_state_shape = std::make_shared<ov::op::v3::ShapeOf>(pattern_map.at(hidden_state_m));
+ moe_router_fused = std::make_shared<ov::op::v1::Reshape>(moe_router_fused, hidden_state_shape, false);
+ ov::copy_runtime_info(moe_compressed, {hidden_state_shape, moe_router_fused});
+ }
+
+ moe_router_fused->set_friendly_name(moe_compressed->get_friendly_name());
+ ov::replace_node(moe_compressed, moe_router_fused);
return true;
};
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_3gemm_compressed_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_3gemm_compressed_test.cpp
index 05aec44e0d6..a4deed264de 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_3gemm_compressed_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/fuse_moe_3gemm_compressed_test.cpp
@@ -19,6 +19,7 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_elements_update.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/softmax.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/topk.hpp"
@@ -35,9 +36,11 @@ namespace intel_gpu {
TEST_F(TransformationTestsF, FuseMOE3GemmCompressedTest) {
{
// tokens:32, hidden_size:2048, iter_size:768, experts:128, topk:8
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{32, 2048});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, 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, 128}, {0.2});
- auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states, routers);
+ auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
auto softmax = std::make_shared<ov::op::v8::Softmax>(routing_weights, 1);
auto k = op::v0::Constant::create(element::i32, Shape{}, {8});
@@ -99,16 +102,18 @@ TEST_F(TransformationTestsF, FuseMOE3GemmCompressedTest) {
config.top_k = 8;
config.out_type = ov::element::f16;
auto moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(
- ov::OutputVector{hidden_states, unsqueeze_moe, topk->output(1),
+ ov::OutputVector{hidden_states_reshape, unsqueeze_moe, topk->output(1),
wei_gate, scale_gate, zp_gate, wei_up, scale_up, zp_up, wei_down, scale_down, zp_down}, config);
model = std::make_shared<ov::Model>(moe_compressed, ov::ParameterVector{hidden_states});
manager.register_pass<FuseMOE3GemmCompressed>();
}
{
// tokens:32, hidden_size:2048, iter_size:768, experts:128, topk:8
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{32, 2048});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, 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, 128}, {0.2});
- auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states, routers);
+ auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
// weight
auto wei_gate = op::v0::Constant::create(element::u4, Shape{128, 768, 16, 128}, {1});
@@ -129,12 +134,121 @@ TEST_F(TransformationTestsF, FuseMOE3GemmCompressedTest) {
config.top_k = 8;
config.out_type = ov::element::f16;
auto moe_3gemm_fused_compressed = std::make_shared<ov::intel_gpu::op::MOE3GemmFusedCompressed>(
- ov::OutputVector{hidden_states, routing_weights,
+ ov::OutputVector{hidden_states_reshape, routing_weights,
wei_gate, scale_gate, zp_gate, wei_up, scale_up, zp_up, wei_down, scale_down, zp_down}, config);
model_ref = std::make_shared<ov::Model>(moe_3gemm_fused_compressed, ov::ParameterVector{hidden_states});
}
}
+
+TEST_F(TransformationTestsF, FuseMOE3GemmCompressedTest1) {
+ {
+ // tokens:32, hidden_size:2048, iter_size:512, experts:512, topk:10
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, 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, 512}, {0.2});
+ // [32, 512]
+ auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
+
+ auto softmax = std::make_shared<ov::op::v8::Softmax>(routing_weights, 1);
+ auto k = op::v0::Constant::create(element::i64, Shape{}, {10});
+ // [32, 10]
+ auto topk = std::make_shared<ov::op::v11::TopK>(softmax, k, 1,
+ ov::op::v11::TopK::Mode::MAX, ov::op::v11::TopK::SortType::SORT_VALUES);
+
+ // weight output
+ auto reduce_axis = op::v0::Constant::create(element::i64, Shape{1}, {1});
+ auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(topk->output(0), reduce_axis->output(0), true);
+ auto norm = std::make_shared<ov::op::v1::Divide>(topk->output(0), reduce_sum->output(0));
+
+ // index
+ auto topk_indices_i32 = std::make_shared<ov::op::v0::Convert>(topk->output(1), ov::element::i32);
+ auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(topk_indices_i32);
+
+ auto slice = std::make_shared<ov::op::v8::Slice>(norm,
+ op::v0::Constant::create(element::i32, Shape{2}, {0, 0}),
+ shape_of,
+ op::v0::Constant::create(element::i32, Shape{2}, {1, 1}),
+ op::v0::Constant::create(element::i32, Shape{2}, {0, 1}));
+
+ auto zero = op::v0::Constant::create(element::f16, Shape{1}, {0});
+ auto router_shape = op::v0::Constant::create(element::i64, Shape{2}, {32, 512});
+ auto bc = std::make_shared<ov::op::v3::Broadcast>(zero, router_shape);
+ auto scatter_axis = op::v0::Constant::create(element::i64, Shape{1}, {1});
+ auto scatter = std::make_shared<ov::op::v12::ScatterElementsUpdate>(bc,
+ topk_indices_i32,
+ slice,
+ scatter_axis);
+ // [512, 32]
+ auto transpose_shape = op::v0::Constant::create(element::i64, Shape{2}, {1, 0});
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(scatter, transpose_shape);
+ // [512, 4, 8]
+ auto router_shape_transpose = op::v0::Constant::create(element::i64, Shape{3}, {512, 4, 8});
+ auto reshape = std::make_shared<ov::op::v1::Reshape>(transpose, router_shape_transpose, false);
+ // [512, 4, 8, 1]
+ auto unsqueeze_const = op::v0::Constant::create(element::i64, Shape{1}, {3});
+ auto unsqueeze_moe = std::make_shared<ov::op::v0::Unsqueeze>(reshape, unsqueeze_const);
+
+ // weight
+ auto wei_gate = op::v0::Constant::create(element::u4, Shape{512, 512, 16, 128}, {1});
+ auto scale_gate = op::v0::Constant::create(element::f16, Shape{512, 16, 512}, {0.01f});
+ auto zp_gate = op::v0::Constant::create(element::u4, Shape{512, 16, 512}, {0});
+ auto wei_up = op::v0::Constant::create(element::u4, Shape{512, 512, 16, 128}, {1});
+ auto scale_up = op::v0::Constant::create(element::f16, Shape{512, 16, 512}, {0.01f});
+ auto zp_up = op::v0::Constant::create(element::u4, Shape{512, 16, 512}, {0});
+ auto wei_down = op::v0::Constant::create(element::u4, Shape{512, 2048, 4, 128}, {1});
+ auto scale_down = op::v0::Constant::create(element::f16, Shape{512, 4, 2048}, {0.01f});
+ auto zp_down = op::v0::Constant::create(element::u4, Shape{512, 4, 2048}, {0});
+
+ ov::intel_gpu::op::MOECompressed::Config config;
+ config.hidden_size = 2048;
+ config.inter_size = 512;
+ config.num_expert = 512;
+ config.group_size = 128;
+ config.top_k = 10;
+ config.out_type = ov::element::f16;
+ auto moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(
+ ov::OutputVector{hidden_states, unsqueeze_moe, topk->output(1),
+ wei_gate, scale_gate, zp_gate, wei_up, scale_up, zp_up, wei_down, scale_down, zp_down}, config);
+ model = std::make_shared<ov::Model>(moe_compressed, ov::ParameterVector{hidden_states});
+ manager.register_pass<FuseMOE3GemmCompressed>();
+ }
+ {
+ // tokens:32, hidden_size:2048, inter_size:512, experts:512, topk:10
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, 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, 512}, {0.2});
+ auto routing_weights = std::make_shared<ov::op::v0::MatMul>(hidden_states_reshape, routers);
+
+ // weight
+ auto wei_gate = op::v0::Constant::create(element::u4, Shape{512, 512, 16, 128}, {1});
+ auto scale_gate = op::v0::Constant::create(element::f16, Shape{512, 16, 512}, {0.01f});
+ auto zp_gate = op::v0::Constant::create(element::u4, Shape{512, 16, 512}, {0});
+ auto wei_up = op::v0::Constant::create(element::u4, Shape{512, 512, 16, 128}, {1});
+ auto scale_up = op::v0::Constant::create(element::f16, Shape{512, 16, 512}, {0.01f});
+ auto zp_up = op::v0::Constant::create(element::u4, Shape{512, 16, 512}, {0});
+ auto wei_down = op::v0::Constant::create(element::u4, Shape{512, 2048, 4, 128}, {1});
+ auto scale_down = op::v0::Constant::create(element::f16, Shape{512, 4, 2048}, {0.01f});
+ auto zp_down = op::v0::Constant::create(element::u4, Shape{512, 4, 2048}, {0});
+
+ ov::intel_gpu::op::MOECompressed::Config config;
+ config.hidden_size = 2048;
+ config.inter_size = 512;
+ config.num_expert = 512;
+ config.group_size = 128;
+ config.top_k = 10;
+ config.out_type = ov::element::f16;
+ auto moe_3gemm_fused_compressed = std::make_shared<ov::intel_gpu::op::MOE3GemmFusedCompressed>(
+ ov::OutputVector{hidden_states_reshape, routing_weights,
+ wei_gate, scale_gate, zp_gate, wei_up, scale_up, zp_up, wei_down, scale_down, zp_down}, config);
+ auto hidden_state_shape = std::make_shared<ov::op::v3::ShapeOf>(hidden_states);
+ auto reshape_back = std::make_shared<ov::op::v1::Reshape>(moe_3gemm_fused_compressed->output(0), hidden_state_shape, false);
+
+ model_ref = std::make_shared<ov::Model>(reshape_back, ov::ParameterVector{hidden_states});
+ }
+}
} // namespace intel_gpu
} // namespace test
} // namespace ov