Skip to content

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

[MOE] Revert changes to MatMulConstTransposesExtraction, add specialized pass to postponed transpose MOE weights only (#32662)

Mateusz Mikolajczyk444c036a0493
diff --git a/src/common/transformations/include/transformations/common_optimizations/moe_transpose_weights.hpp b/src/common/transformations/include/transformations/common_optimizations/moe_transpose_weights.hpp
new file mode 100644
index 00000000000..91df26f5761
--- /dev/null
+++ b/src/common/transformations/include/transformations/common_optimizations/moe_transpose_weights.hpp
@@ -0,0 +1,20 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/matcher_pass.hpp"
+#include "transformations_visibility.hpp"
+
+namespace ov {
+namespace pass {
+
+class TRANSFORMATIONS_API VectorizedMOE2GEMMTransposeWeights : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("VectorizedMOE2GEMMTransposeWeights");
+ VectorizedMOE2GEMMTransposeWeights();
+};
+
+} // namespace pass
+} // namespace ov
diff --git a/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp b/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
index ce61706c8cc..62522eeeb0b 100644
--- a/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/matmul_const_transposes_extraction.cpp
@@ -10,9 +10,7 @@
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/transpose.hpp"
-#include "openvino/pass/constant_folding.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
-#include "transformations/rt_info/disable_fp16_compression.hpp"
ov::pass::MatMulConstTransposesExtraction::MatMulConstTransposesExtraction() {
auto data_pattern = pattern::any_input();
@@ -20,7 +18,8 @@ ov::pass::MatMulConstTransposesExtraction::MatMulConstTransposesExtraction() {
pattern::wrap_type<ov::op::v0::Constant, ov::op::v0::FakeQuantize>([](Output<Node> node) -> bool {
const auto& pshape = node.get_partial_shape();
const auto& rank = pshape.rank();
- return rank.is_static() && rank.get_length() >= 2;
+ return rank.is_static() && rank.get_length() >= 2 &&
+ std::count(pshape.begin(), pshape.end(), 1) >= rank.get_length() - 2;
});
auto matmul_pattern = pattern::wrap_type<ov::op::v0::MatMul>({data_pattern, weights_pattern});
matcher_pass_callback callback = [=](pattern::Matcher& m) {
@@ -38,15 +37,6 @@ ov::pass::MatMulConstTransposesExtraction::MatMulConstTransposesExtraction() {
std::shared_ptr<Node> transpose = std::make_shared<ov::op::v1::Transpose>(
weights,
ov::op::v0::Constant::create(element::i32, {transpose_order.size()}, transpose_order));
- if (ov::is_type<ov::op::v0::Constant>(weights.get_node_shared_ptr())) {
- // postponed_constant attribute is needed to perform constant folding on serialization step
- transpose->get_rt_info()["postponed_constant"] = true;
- // disable constant folding here to postpone it to serialization step
- ov::pass::disable_constant_folding(transpose);
- // disable fp16 compression. Otherwise an additional conversion will be added after the constant, which
- // breaks postponed_constant serialization
- ov::disable_fp16_compression(weights.get_node_shared_ptr());
- }
auto new_matmul = std::make_shared<ov::op::v0::MatMul>(pattern_value_map.at(data_pattern),
transpose,
matmul->get_transpose_a(),
diff --git a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
index 585a7d0a747..3688a4536ae 100644
--- a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
@@ -45,6 +45,7 @@
#include "transformations/common_optimizations/lstm_cell_fusion.hpp"
#include "transformations/common_optimizations/matmul_const_transposes_extraction.hpp"
#include "transformations/common_optimizations/matmul_multiply_fusion.hpp"
+#include "transformations/common_optimizations/moe_transpose_weights.hpp"
#include "transformations/common_optimizations/mul_conv_fusion.hpp"
#include "transformations/common_optimizations/mul_fake_quantize_fusion.hpp"
#include "transformations/common_optimizations/mvn_fusion.hpp"
@@ -294,6 +295,7 @@ bool ov::pass::MOCTransformations::run_on_model(const std::shared_ptr<ov::Model>
// Remove pytestmark to enable e2e test:
// tests/model_hub_tests/transformation_tests/test_moe_transformation.py
// REGISTER_PASS(manager, FuseMOE)
+ REGISTER_PASS(manager, VectorizedMOE2GEMMTransposeWeights)
manager.run_passes(f);
diff --git a/src/common/transformations/src/transformations/common_optimizations/moe_transpose_weights.cpp b/src/common/transformations/src/transformations/common_optimizations/moe_transpose_weights.cpp
new file mode 100644
index 00000000000..38d3479db0e
--- /dev/null
+++ b/src/common/transformations/src/transformations/common_optimizations/moe_transpose_weights.cpp
@@ -0,0 +1,149 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/common_optimizations/moe_transpose_weights.hpp"
+
+#include <algorithm>
+#include <numeric>
+#include <vector>
+
+#include "itt.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/clamp.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/minimum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/reduce_sum.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/scatter_elements_update.hpp"
+#include "openvino/op/slice.hpp"
+#include "openvino/op/swish.hpp"
+#include "openvino/op/tile.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/constant_folding.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/rt_info/decompression.hpp"
+#include "transformations/rt_info/disable_fp16_compression.hpp"
+#include "transformations/utils/utils.hpp"
+
+using namespace ov::pass;
+
+ov::pass::VectorizedMOE2GEMMTransposeWeights::VectorizedMOE2GEMMTransposeWeights() {
+ MATCHER_SCOPE(VectorizedMOE2GEMMTransposeWeights);
+
+ auto experts_input = pattern::wrap_type<ov::op::v1::Reshape>({pattern::any_input(), pattern::any_input()});
+ auto tile = pattern::wrap_type<ov::op::v0::Tile>({experts_input, pattern::any_input()});
+ auto after_tile_reshape = pattern::wrap_type<ov::op::v1::Reshape>({tile, pattern::any_input()});
+ auto gate_up_matmul = pattern::wrap_type<ov::op::v0::MatMul>({after_tile_reshape, pattern::any_input()},
+ {{"transpose_a", false}, {"transpose_b", false}});
+ auto gate_up_add = pattern::wrap_type<ov::op::v1::Add>({gate_up_matmul, pattern::any_input()});
+
+ auto slice1 = pattern::wrap_type<ov::op::v8::Slice>(
+ {gate_up_add, pattern::any_input(), pattern::any_input(), pattern::any_input(), pattern::any_input()});
+ auto clamp = pattern::wrap_type<ov::op::v0::Clamp>({slice1});
+ auto add1 = pattern::wrap_type<ov::op::v1::Add>({clamp, pattern::wrap_const()});
+
+ auto slice2 = pattern::wrap_type<ov::op::v8::Slice>(
+ {gate_up_add, pattern::any_input(), pattern::any_input(), pattern::any_input(), pattern::any_input()});
+ auto minimum1 = pattern::wrap_type<ov::op::v1::Minimum>({slice2, pattern::wrap_const()});
+ auto swish_beta = pattern::wrap_const();
+ auto swish = pattern::wrap_type<ov::op::v4::Swish>({minimum1, swish_beta});
+
+ auto multiply2 = pattern::wrap_type<ov::op::v1::Multiply>({add1, swish});
+
+ auto down_proj_matmul = pattern::wrap_type<ov::op::v0::MatMul>({multiply2, pattern::any_input()},
+ {{"transpose_a", false}, {"transpose_b", false}});
+ auto down_proj_add = pattern::wrap_type<ov::op::v1::Add>({down_proj_matmul, pattern::wrap_const()});
+ auto end_reshape = pattern::wrap_type<ov::op::v1::Reshape>({down_proj_add, pattern::any_input()});
+
+ auto router_topk_indices = pattern::any_input();
+ auto scatter_elements_update = pattern::wrap_type<ov::op::v12::ScatterElementsUpdate>(
+ {pattern::any_input(), router_topk_indices, pattern::any_input(), pattern::any_input()});
+
+ auto router_transpose = pattern::wrap_type<ov::op::v1::Transpose>({scatter_elements_update, pattern::any_input()});
+ auto router_reshape = pattern::wrap_type<ov::op::v1::Reshape>({router_transpose, pattern::any_input()});
+ auto unsqueeze_routing_weights = pattern::wrap_type<ov::op::v0::Unsqueeze>({router_reshape, pattern::any_input()});
+
+ auto mul3 = pattern::wrap_type<ov::op::v1::Multiply>({end_reshape, unsqueeze_routing_weights});
+ auto reduce_sum = pattern::wrap_type<ov::op::v1::ReduceSum>({mul3, pattern::any_input()}, {{"keep_dims", false}});
+ auto moe_pattern = reduce_sum;
+
+ matcher_pass_callback callback = [=](pattern::Matcher& m) {
+ auto& pm = m.get_pattern_value_map();
+ if (transformation_callback(m.get_match_root())) {
+ return false;
+ }
+ auto gate_matmul = ov::as_type_ptr<ov::op::v0::MatMul>(pm.at(gate_up_matmul).get_node_shared_ptr());
+ auto down_matmul = ov::as_type_ptr<ov::op::v0::MatMul>(pm.at(down_proj_matmul).get_node_shared_ptr());
+ if (!gate_matmul || !down_matmul)
+ return false;
+
+ auto insert_transpose = [&](const std::shared_ptr<ov::op::v0::MatMul>& matmul) -> bool {
+ auto weight_output = matmul->input_value(1);
+ auto transpose_input = weight_output;
+ std::shared_ptr<ov::op::v0::Convert> decompress_convert;
+
+ if (auto convert = ov::as_type_ptr<ov::op::v0::Convert>(weight_output.get_node_shared_ptr())) {
+ if (ov::is_decompression(convert) && convert->output(0).get_target_inputs().size() == 1) {
+ decompress_convert = convert;
+ transpose_input = convert->input_value(0);
+ }
+ }
+
+ const auto& pshape = weight_output.get_partial_shape();
+ const auto rank = pshape.rank();
+ if (!rank.is_static() || rank.get_length() < 2)
+ return false;
+
+ std::vector<int64_t> transpose_order(rank.get_length());
+ std::iota(transpose_order.begin(), transpose_order.end(), 0);
+ std::reverse(transpose_order.end() - 2, transpose_order.end());
+
+ auto order_const =
+ ov::op::v0::Constant::create(ov::element::i64, {transpose_order.size()}, transpose_order);
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(transpose_input, order_const);
+
+ if (ov::is_type<ov::op::v0::Constant>(transpose_input.get_node_shared_ptr())) {
+ transpose->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(transpose);
+ ov::disable_fp16_compression(transpose_input.get_node_shared_ptr());
+ }
+
+ ov::NodeVector rt_sources{transpose_input.get_node_shared_ptr()};
+ if (auto weight_node = weight_output.get_node_shared_ptr()) {
+ rt_sources.push_back(weight_node);
+ }
+ ov::copy_runtime_info(rt_sources, transpose);
+ register_new_node(transpose);
+
+ transpose->validate_and_infer_types();
+
+ Output<Node> matmul_weight_input = transpose;
+
+ if (decompress_convert) {
+ decompress_convert->input(0).replace_source_output(transpose);
+ decompress_convert->validate_and_infer_types();
+ ov::mark_as_decompression(decompress_convert);
+ matmul_weight_input = decompress_convert;
+ }
+
+ matmul->input(1).replace_source_output(matmul_weight_input);
+ matmul->set_transpose_b(true);
+ matmul->validate_and_infer_types();
+
+ return true;
+ };
+
+ if (!insert_transpose(gate_matmul) || !insert_transpose(down_matmul))
+ return false;
+
+ ov::copy_runtime_info(m.get_matched_nodes(), {gate_matmul, down_matmul});
+ return true;
+ };
+
+ auto matcher = std::make_shared<pattern::Matcher>(moe_pattern, matcher_name);
+ this->register_matcher(matcher, callback);
+}
diff --git a/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp b/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
index 43fd93f4f58..f38a5272edb 100644
--- a/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
+++ b/src/common/transformations/tests/common_optimizations/matmul_const_transposes_extraction.cpp
@@ -30,10 +30,8 @@ TEST_F(TransformationTestsF, MatMulConstTransposesExtractionConstantWeights) {
{
auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
- auto weights = opset8::Constant::create(element::f32, Shape{1, 3, 2}, {1, 2, 3, 4, 5, 6});
- auto transpose =
- std::make_shared<opset8::Transpose>(weights, op::v0::Constant::create(element::i32, Shape{3}, {0, 2, 1}));
- auto matmul = std::make_shared<opset8::MatMul>(data, transpose, true, true);
+ auto weights = opset8::Constant::create(element::f32, Shape{1, 2, 3}, {1, 3, 5, 2, 4, 6});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, true, true);
model_ref = std::make_shared<Model>(matmul, ParameterVector{data});
}
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
@@ -87,46 +85,11 @@ TEST_F(TransformationTestsF, NegativeMatMulConstTransposesExtractionTransposeBSe
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
-TEST_F(TransformationTestsF, MatMulConstTransposesExtractionNonUnitDims_transpose_a_true) {
- {
- auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
- auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
- auto matmul = std::make_shared<opset8::MatMul>(data, weights, true, false);
- model = std::make_shared<Model>(matmul, ParameterVector{data});
-
- manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
- manager.register_pass<ov::pass::ConstantFolding>();
- }
- {
- auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
- auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
- auto transpose =
- std::make_shared<opset8::Transpose>(weights, op::v0::Constant::create(element::i32, Shape{3}, {0, 2, 1}));
- auto matmul = std::make_shared<opset8::MatMul>(data, transpose, true, true);
- model_ref = std::make_shared<Model>(matmul, ParameterVector{data});
- }
- comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
- comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
-}
-
-TEST_F(TransformationTestsF, MatMulConstTransposesExtractionNonUnitDims_transpose_b_false) {
- {
- auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4, 3});
- auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
- auto matmul = std::make_shared<opset8::MatMul>(data, weights, false, false);
- model = std::make_shared<Model>(matmul, ParameterVector{data});
-
- manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
- manager.register_pass<ov::pass::ConstantFolding>();
- }
- {
- auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 4, 3});
- auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6});
- auto transpose =
- std::make_shared<opset8::Transpose>(weights, op::v0::Constant::create(element::i32, Shape{3}, {0, 2, 1}));
- auto matmul = std::make_shared<opset8::MatMul>(data, transpose, false, true);
- model_ref = std::make_shared<Model>(matmul, ParameterVector{data});
- }
+TEST_F(TransformationTestsF, NegativeMatMulConstTransposesExtractionNonUnitDims) {
+ auto data = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 4});
+ auto weights = opset8::Constant::create(element::f32, Shape{2, 3, 2}, {1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 7});
+ auto matmul = std::make_shared<opset8::MatMul>(data, weights, true);
+ model = std::make_shared<Model>(matmul, ParameterVector{data});
+ manager.register_pass<ov::pass::MatMulConstTransposesExtraction>();
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
- comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
}
diff --git a/src/common/transformations/tests/common_optimizations/moe_transpose_weights_test.cpp b/src/common/transformations/tests/common_optimizations/moe_transpose_weights_test.cpp
new file mode 100644
index 00000000000..6f96188f8f2
--- /dev/null
+++ b/src/common/transformations/tests/common_optimizations/moe_transpose_weights_test.cpp
@@ -0,0 +1,264 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/common_optimizations/moe_transpose_weights.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <vector>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/clamp.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/minimum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/reduce_sum.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/scatter_elements_update.hpp"
+#include "openvino/op/slice.hpp"
+#include "openvino/op/swish.hpp"
+#include "openvino/op/tile.hpp"
+#include "openvino/op/topk.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/constant_folding.hpp"
+#include "transformations/rt_info/decompression.hpp"
+#include "transformations/rt_info/disable_fp16_compression.hpp"
+
+using namespace ov;
+
+namespace {
+
+std::shared_ptr<ov::Model> build_moe_2gemm_model(bool use_decompression, bool with_transpose) {
+ using namespace ov;
+
+ const size_t batch = 2;
+ const Dimension in_dim = Dimension::dynamic();
+ const size_t hidden_size = 2048;
+ const size_t intermediate_size = 4096;
+ const size_t fusion_factor = 2;
+ const size_t topk = 2;
+ const size_t number_of_experts = 3;
+
+ auto input_shape = PartialShape{batch, in_dim, 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, hidden_size}),
+ false);
+
+ auto tile = std::make_shared<op::v0::Tile>(
+ experts_reshape,
+ op::v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{number_of_experts, 1}));
+ auto after_tile_reshape = std::make_shared<op::v1::Reshape>(
+ tile,
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{number_of_experts, batch, hidden_size}),
+ false);
+
+ // Pattern model (before transform): weights shape matches non-transposed MatMul
+ // Reference model (after transform): weights + Transpose node, MatMul with transpose_b=true
+ Output<Node> gate_weight_output;
+ auto gate_weights = op::v0::Constant::create(
+ element::f32,
+ Shape{number_of_experts, hidden_size, intermediate_size * fusion_factor},
+ std::vector<float>(number_of_experts * hidden_size * intermediate_size * fusion_factor, 1.0f));
+ gate_weights->set_friendly_name("gate_weights");
+ gate_weight_output = gate_weights;
+
+ if (use_decompression) {
+ auto gate_const = op::v0::Constant::create(
+ element::f16,
+ Shape{number_of_experts, hidden_size, intermediate_size * fusion_factor},
+ std::vector<float16>(number_of_experts * hidden_size * intermediate_size * fusion_factor, 1.0f));
+ gate_const->set_friendly_name("gate_weights_storage");
+
+ Output<Node> convert_input = gate_const;
+ if (with_transpose) {
+ auto order = op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 2, 1});
+ auto gate_transpose = std::make_shared<op::v1::Transpose>(convert_input, order);
+ gate_transpose->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(gate_transpose);
+ ov::disable_fp16_compression(gate_const);
+ convert_input = gate_transpose;
+ }
+
+ auto convert = std::make_shared<op::v0::Convert>(convert_input, element::f32);
+ convert->set_friendly_name("gate_convert");
+ ov::mark_as_decompression(convert);
+ gate_weight_output = convert;
+ } else {
+ if (with_transpose) {
+ auto order = op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 2, 1});
+ auto gate_transpose = std::make_shared<op::v1::Transpose>(gate_weight_output, order);
+ gate_transpose->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(gate_transpose);
+ ov::disable_fp16_compression(gate_weights);
+ gate_weight_output = gate_transpose;
+ }
+ }
+
+ auto gate_matmul = std::make_shared<op::v0::MatMul>(after_tile_reshape, gate_weight_output, false, with_transpose);
+
+ auto gate_bias = std::make_shared<op::v1::Add>(
+ gate_matmul,
+ op::v0::Constant::create(element::f32,
+ Shape{number_of_experts, 1, intermediate_size * fusion_factor},
+ std::vector<float>(number_of_experts * intermediate_size * fusion_factor, 0.0f)));
+
+ auto slice1 = std::make_shared<op::v8::Slice>(
+ gate_bias,
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 0, 0}),
+ op::v0::Constant::create(element::i64,
+ Shape{3},
+ std::vector<int64_t>{static_cast<int64_t>(number_of_experts),
+ static_cast<int64_t>(batch),
+ static_cast<int64_t>(intermediate_size * fusion_factor)}),
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{1, 1, 2}),
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 1, 2}));
+ auto clamp = std::make_shared<op::v0::Clamp>(slice1, -5.0f, 5.0f);
+ auto add1 = std::make_shared<op::v1::Add>(clamp, op::v0::Constant::create(element::f32, Shape{1}, {1.0f}));
+
+ auto slice2 = std::make_shared<op::v8::Slice>(
+ gate_bias,
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 0, 0}),
+ op::v0::Constant::create(element::i64,
+ Shape{3},
+ std::vector<int64_t>{static_cast<int64_t>(number_of_experts),
+ static_cast<int64_t>(batch),
+ static_cast<int64_t>(intermediate_size * fusion_factor)}),
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{1, 1, 2}),
+ op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 1, 2}));
+ auto minimum1 =
+ std::make_shared<op::v1::Minimum>(slice2, op::v0::Constant::create(element::f32, Shape{1}, {10.0f}));
+ auto swish_beta = op::v0::Constant::create(element::f32, Shape{}, std::vector<float>{1.7f});
+ auto swish = std::make_shared<op::v4::Swish>(minimum1, swish_beta);
+
+ auto multiply2 = std::make_shared<op::v1::Multiply>(add1, swish);
+
+ Output<Node> down_weight_output;
+ if (use_decompression) {
+ auto down_const =
+ op::v0::Constant::create(element::f16,
+ Shape{number_of_experts, intermediate_size, hidden_size},
+ std::vector<float16>(number_of_experts * intermediate_size * hidden_size, 2));
+ down_const->set_friendly_name("down_weights_storage");
+
+ Output<Node> convert_input = down_const;
+ if (with_transpose) {
+ auto order = op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 2, 1});
+ auto down_transpose = std::make_shared<op::v1::Transpose>(convert_input, order);
+ down_transpose->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(down_transpose);
+ ov::disable_fp16_compression(down_const);
+ convert_input = down_transpose;
+ }
+
+ auto convert = std::make_shared<op::v0::Convert>(convert_input, element::f32);
+ convert->set_friendly_name("down_convert");
+ ov::mark_as_decompression(convert);
+ down_weight_output = convert;
+ } else {
+ auto down_const =
+ op::v0::Constant::create(element::f32,
+ Shape{number_of_experts, intermediate_size, hidden_size},
+ std::vector<float>(number_of_experts * intermediate_size * hidden_size, 0.5f));
+ down_const->set_friendly_name("down_weights");
+
+ down_weight_output = down_const;
+ if (with_transpose) {
+ auto order = op::v0::Constant::create(element::i64, Shape{3}, std::vector<int64_t>{0, 2, 1});
+ auto down_transpose = std::make_shared<op::v1::Transpose>(down_weight_output, order);
+ down_transpose->get_rt_info()["postponed_constant"] = true;
+ ov::pass::disable_constant_folding(down_transpose);
+ ov::disable_fp16_compression(down_const);
+ down_weight_output = down_transpose;
+ }
+ }
+
+ auto down_matmul = std::make_shared<op::v0::MatMul>(multiply2, down_weight_output, false, with_transpose);
+
+ auto down_bias = std::make_shared<op::v1::Add>(
+ down_matmul,
+ op::v0::Constant::create(element::f32,
+ Shape{number_of_experts, 1, hidden_size},
+ std::vector<float>(number_of_experts * hidden_size, 0.0f)));
+ auto end_reshape = std::make_shared<op::v1::Reshape>(
+ down_bias,
+ op::v0::Constant::create(element::i64,
+ Shape{4},
+ std::vector<int64_t>{static_cast<int64_t>(number_of_experts),
+ static_cast<int64_t>(batch),
+ -1,
+ static_cast<int64_t>(hidden_size)}),
+ false);
+
+ auto router_matmul = std::make_shared<op::v0::MatMul>(
+ experts_reshape,
+ op::v0::Constant::create(element::f32, Shape{number_of_experts, hidden_size}, {1.0f}),
+ false,
+ true);
+
+ auto router_topk = std::make_shared<op::v11::TopK>(router_matmul,
+ op::v0::Constant::create(element::i64, Shape{}, {topk}),
+ -1,
+ op::v11::TopK::Mode::MAX,
+ op::v11::TopK::SortType::SORT_VALUES,
+ element::i64);
+ auto router_topk_values = router_topk->output(0);
+ auto router_topk_indices = router_topk->output(1);
+
+ auto scatter = std::make_shared<op::v12::ScatterElementsUpdate>(
+ router_topk_values,
+ router_topk_indices,
+ op::v0::Constant::create(element::f32, Shape{batch, topk}, {0.0f}),
+ op::v0::Constant::create(element::i64, Shape{1}, {1}));
+ auto router_transpose = std::make_shared<op::v1::Transpose>(
+ scatter,
+ op::v0::Constant::create(element::i64, Shape{2}, std::vector<int64_t>{1, 0}));
+ auto router_reshape = std::make_shared<op::v1::Reshape>(
+ router_transpose,
+ op::v0::Constant::create(element::i64,
+ Shape{3},
+ std::vector<int64_t>{static_cast<int64_t>(number_of_experts), 1, -1}),
+ true);
+ auto unsqueeze =
+ std::make_shared<op::v0::Unsqueeze>(router_reshape,
+ op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{-1}));
+
+ auto mul3 = std::make_shared<op::v1::Multiply>(end_reshape, unsqueeze);
+ auto reduce_sum =
+ std::make_shared<op::v1::ReduceSum>(mul3,
+ op::v0::Constant::create(element::i64, Shape{1}, std::vector<int64_t>{0}),
+ false);
+
+ return std::make_shared<ov::Model>(OutputVector{reduce_sum}, ParameterVector{input});
+}
+
+std::shared_ptr<ov::Model> build_moe_2gemm_pattern_model(bool use_decompression) {
+ return build_moe_2gemm_model(use_decompression, false);
+}
+
+std::shared_ptr<ov::Model> build_moe_2gemm_reference_model(bool use_decompression) {
+ return build_moe_2gemm_model(use_decompression, true);
+}
+
+} // namespace
+
+TEST_F(TransformationTestsF, VectorizedMOE2GEMMTransposeWeightsConstantWeights) {
+ model = build_moe_2gemm_pattern_model(/*use_decompression=*/false);
+ model_ref = build_moe_2gemm_reference_model(/*use_decompression=*/false);
+ manager.register_pass<ov::pass::VectorizedMOE2GEMMTransposeWeights>();
+}
+
+TEST_F(TransformationTestsF, VectorizedMOE2GEMMTransposeWeightsDecompressionWeights) {
+ model = build_moe_2gemm_pattern_model(/*use_decompression=*/true);
+ model_ref = build_moe_2gemm_reference_model(/*use_decompression=*/true);
+ manager.register_pass<ov::pass::VectorizedMOE2GEMMTransposeWeights>();
+}
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
index e0aadc2139e..1851fd30789 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/subgraph_tests/matmul_const_transposes_extraction.cpp
@@ -19,11 +19,6 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> shape_params = {
{{2, 3, 5, 10}, {1, 10, 1}, false},
{{2, 3, 5, 10}, {1, 1, 10, 7}, false},
{{2, 3, 5, 10}, {1, 1, 10, 1}, false},
- {{5, 10}, {2, 10, 7}, false},
- {{5, 10}, {2, 3, 10, 7}, false},
- {{2, 3, 5, 10}, {3, 10, 7}, false},
- {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
- {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_MatMulConstTransposesExtractionTest,
@@ -38,12 +33,17 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> negative_shape_param
{{5}, {3, 5}, true},
{{5, 5}, {5, 5}, true},
{{5, 10}, {7, 10}, true},
+ {{5, 10}, {2, 10, 7}, false},
+ {{5, 10}, {2, 3, 10, 7}, false},
{{1, 1, 5, 10}, {10}, false},
{{1, 1, 5, 10}, {7, 10}, true},
{{1, 1, 5, 10}, {1, 1, 7, 10}, true},
{{2, 3, 5, 10}, {7, 10}, true},
{{2, 3, 5, 10}, {3, 7, 10}, true},
{{2, 3, 5, 10}, {2, 3, 7, 10}, true},
+ {{2, 3, 5, 10}, {3, 10, 7}, false},
+ {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
+ {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_NegativeMatMulConstTransposesExtractionTest,
@@ -61,11 +61,6 @@ std::vector<MatMulConstTransposesExtractionTestShapeParams> shape_params2 = {
{{2, 3, 5, 10}, {10, 7}, false},
{{2, 3, 5, 10}, {1, 10, 7}, false},
{{2, 3, 5, 10}, {1, 1, 10, 7}, false},
- {{5, 10}, {2, 10, 7}, false},
- {{5, 10}, {2, 3, 10, 7}, false},
- {{2, 3, 5, 10}, {3, 10, 7}, false},
- {{2, 3, 5, 10}, {1, 3, 10, 7}, false},
- {{2, 3, 5, 10}, {2, 3, 10, 7}, false},
};
INSTANTIATE_TEST_SUITE_P(smoke_QuantizedMatMulConstTransposesExtractionTest,