Skip to content

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

[CPU] Add gate_up_swapped support for LLM MLP fusion (#32683)

Bo Liu2990b0c9b19f
diff --git a/src/plugins/intel_cpu/src/nodes/llm_mlp.cpp b/src/plugins/intel_cpu/src/nodes/llm_mlp.cpp
index b794dc84068..4654daeb2c7 100644
--- a/src/plugins/intel_cpu/src/nodes/llm_mlp.cpp
+++ b/src/plugins/intel_cpu/src/nodes/llm_mlp.cpp
@@ -379,9 +379,15 @@ struct LLMMLP::Executor : public LLMMLP::ExecutorBase {
auto K = w_gate.size(1);
auto N = w_gate.size(0);
OPENVINO_ASSERT(w_gate.stride_bytes(0) == w_up.stride_bytes(0));
- if (m_config.gate_up_combined) {
+ if (m_config.gate_up_type != LLMMLPNode::GATE_UP_TYPE::SEPARATE) {
N = w_gate.size(0) / 2;
- gate_up.setup(w_gate.ptr_v(), w_up.ptr_v(N, 0), w_up.stride_bytes(0), N * 2, K, config);
+ if (m_config.gate_up_type == LLMMLPNode::GATE_UP_TYPE::COMBINED_UP_GATE) {
+ // COMBINED_UP_GATE: VariadicSplit output[0] connects to up, output[1] connects to gate
+ gate_up.setup(w_gate.ptr_v(N, 0), w_gate.ptr_v(), w_gate.stride_bytes(0), N * 2, K, config);
+ } else {
+ // COMBINED_GATE_UP: VariadicSplit output[0] connects to gate, output[1] connects to up
+ gate_up.setup(w_gate.ptr_v(), w_gate.ptr_v(N, 0), w_gate.stride_bytes(0), N * 2, K, config);
+ }
} else {
gate_up.setup(w_gate.ptr_v(), w_up.ptr_v(), w_up.stride_bytes(0), N * 2, K, config);
}
@@ -392,13 +398,21 @@ struct LLMMLP::Executor : public LLMMLP::ExecutorBase {
auto* w_scale_gate = pnode->getSrcMemoryAtPort(4)->getDataAs<float>();
auto* w_scale_up = pnode->getSrcMemoryAtPort(5)->getDataAs<float>();
auto* dst = m_w_scale_gateup.ptr<float>();
- if (m_config.gate_up_combined) {
+ if (m_config.gate_up_type != LLMMLPNode::GATE_UP_TYPE::SEPARATE) {
w_scale_up = w_scale_gate + N;
}
+
+ // When gate_up_type is COMBINED_UP_GATE, we need to swap the scales
+ // to match the swapped weight layout
+ auto* scale_first = w_scale_gate;
+ auto* scale_second = w_scale_up;
+ if (m_config.gate_up_type == LLMMLPNode::GATE_UP_TYPE::COMBINED_UP_GATE) {
+ std::swap(scale_first, scale_second);
+ }
for (size_t i = 0; i < N; i += 16) {
- memcpy(dst, w_scale_gate + i, 16 * sizeof(float));
+ memcpy(dst, scale_first + i, 16 * sizeof(float));
dst += 16;
- memcpy(dst, w_scale_up + i, 16 * sizeof(float));
+ memcpy(dst, scale_second + i, 16 * sizeof(float));
dst += 16;
}
}
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.cpp
index 652f4019094..05525f5ba03 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.cpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.cpp
@@ -23,10 +23,24 @@ EnumNames<ov::intel_cpu::LLMMLPNode::ACT_FN>& EnumNames<ov::intel_cpu::LLMMLPNod
return enum_names;
}
+template <>
+EnumNames<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>& EnumNames<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>::get() {
+ static auto enum_names = EnumNames<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>(
+ "op::intel_cpu::LLMMLPNode::GATE_UP_TYPE",
+ {{"SEPARATE", ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE::SEPARATE},
+ {"COMBINED_GATE_UP", ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE::COMBINED_GATE_UP},
+ {"COMBINED_UP_GATE", ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE::COMBINED_UP_GATE}});
+ return enum_names;
+}
+
std::ostream& operator<<(std::ostream& os, const ov::intel_cpu::LLMMLPNode::ACT_FN& type) {
return os << as_string(type);
}
+std::ostream& operator<<(std::ostream& os, const ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE& type) {
+ return os << as_string(type);
+}
+
namespace intel_cpu {
bool LLMMLPNode::visit_attributes(ov::AttributeVisitor& visitor) {
@@ -37,7 +51,7 @@ bool LLMMLPNode::visit_attributes(ov::AttributeVisitor& visitor) {
visitor.on_attribute("down_quantized", m_config.down_quantized);
visitor.on_attribute("hidden_size", m_config.hidden_size);
visitor.on_attribute("up_size", m_config.up_size);
- visitor.on_attribute("gate_up_combined", m_config.gate_up_combined);
+ visitor.on_attribute("gate_up_type", m_config.gate_up_type);
visitor.finish_structure();
return true;
}
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.hpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.hpp
index f2a896ad13d..9068d64442c 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.hpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/op/llm_mlp.hpp
@@ -25,13 +25,19 @@ public:
enum class ACT_FN : uint8_t { SILU = 0, GELU = 1 };
+ enum class GATE_UP_TYPE : uint8_t {
+ SEPARATE = 0, // separate gate and up projections
+ COMBINED_GATE_UP = 1, // combined weights, gate first (normal)
+ COMBINED_UP_GATE = 2 // combined weights, up first (swapped)
+ };
+
struct Config {
ACT_FN act;
bool gate_up_quantized;
bool down_quantized;
int hidden_size;
int up_size;
- bool gate_up_combined;
+ GATE_UP_TYPE gate_up_type;
};
// args:
@@ -69,6 +75,17 @@ public:
OPENVINO_RTTI("AttributeAdapter<ov::intel_cpu::LLMMLPNode::ACT_FN>");
};
+template <>
+class AttributeAdapter<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>
+ : public EnumAttributeAdapterBase<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE> {
+public:
+ explicit AttributeAdapter(ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE& value)
+ : EnumAttributeAdapterBase<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>(value) {}
+
+ OPENVINO_RTTI("AttributeAdapter<ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE>");
+};
+
std::ostream& operator<<(std::ostream& s, const ov::intel_cpu::LLMMLPNode::ACT_FN& type);
+std::ostream& operator<<(std::ostream& s, const ov::intel_cpu::LLMMLPNode::GATE_UP_TYPE& type);
} // namespace ov
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/pass/mlp_fusion.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/pass/mlp_fusion.cpp
index 57f5a6dfbb1..193d9b7d935 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/pass/mlp_fusion.cpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/x64/pass/mlp_fusion.cpp
@@ -122,6 +122,24 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
const auto& pattern_map = m.get_pattern_value_map();
auto root = m.get_match_root();
+
+ // Determine gate_up_type based on pattern matching
+ LLMMLPNode::GATE_UP_TYPE gate_up_type = LLMMLPNode::GATE_UP_TYPE::SEPARATE;
+ if (pattern_map.count(gate_up_proj_split)) {
+ auto mlp_gated_up_node = pattern_map.at(mlp_gated_up).get_node_shared_ptr();
+ auto input0 = mlp_gated_up_node->input_value(0);
+ auto input1 = mlp_gated_up_node->input_value(1);
+
+ // Check if VariadicSplit output[0] connects to Multiply (swapped case)
+ // Since pattern matching succeeded, we know one of the outputs connects to Multiply
+ if ((input0.get_node() == pattern_map.at(gate_up_proj_split).get_node() && input0.get_index() == 0) ||
+ (input1.get_node() == pattern_map.at(gate_up_proj_split).get_node() && input1.get_index() == 0)) {
+ gate_up_type = LLMMLPNode::GATE_UP_TYPE::COMBINED_UP_GATE; // swapped case
+ } else {
+ gate_up_type = LLMMLPNode::GATE_UP_TYPE::COMBINED_GATE_UP; // normal combined case
+ }
+ }
+
auto src = pattern_map.at(input);
if (!src.get_element_type().is_real()) {
// FakeQuantize, should skip fusion
@@ -134,17 +152,20 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
// down projection is harder to quantize w/o causing accuracy problem, so it may be un-quantized instead
bool is_gate_up_quantized_int8 = false;
bool is_down_proj_int8 = false;
- bool is_gate_up_combined = false;
if (pattern_map.count(gate_up_proj_weight_const_i8) > 0 && pattern_map.count(down_proj_weight_compressed) > 0) {
// gate-up combined & quantized
is_gate_up_quantized_int8 = true;
- is_gate_up_combined = true;
+ gate_up_type = (gate_up_type == LLMMLPNode::GATE_UP_TYPE::SEPARATE)
+ ? LLMMLPNode::GATE_UP_TYPE::COMBINED_GATE_UP
+ : gate_up_type;
gate_proj_w = pattern_map.at(gate_up_proj_weight_const_i8);
up_proj_w = pattern_map.at(gate_up_proj_weight_const_i8);
down_proj_w = pattern_map.at(down_proj_weight_compressed);
} else if (pattern_map.count(gate_up_proj_weight) > 0 && pattern_map.count(down_proj_weight_compressed) > 0) {
// gate-up combined
- is_gate_up_combined = true;
+ gate_up_type = (gate_up_type == LLMMLPNode::GATE_UP_TYPE::SEPARATE)
+ ? LLMMLPNode::GATE_UP_TYPE::COMBINED_GATE_UP
+ : gate_up_type;
gate_proj_w = pattern_map.at(gate_up_proj_weight);
up_proj_w = pattern_map.at(gate_up_proj_weight);
down_proj_w = pattern_map.at(down_proj_weight_compressed);
@@ -207,7 +228,7 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
return false;
}
- auto up_size = is_gate_up_combined ? (up_shape[0] / 2) : (up_shape[0]);
+ auto up_size = (gate_up_type != LLMMLPNode::GATE_UP_TYPE::SEPARATE) ? (up_shape[0] / 2) : (up_shape[0]);
auto down_size = up_shape[1];
if (down_shape[0] != down_size) {
return false;
@@ -223,7 +244,7 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
cfg.down_quantized = is_down_proj_int8;
cfg.hidden_size = down_size;
cfg.up_size = up_size;
- cfg.gate_up_combined = is_gate_up_combined;
+ cfg.gate_up_type = gate_up_type;
if (pattern_map.count(mlp_silu_gate) > 0) {
cfg.act = LLMMLPNode::ACT_FN::SILU;
@@ -248,7 +269,7 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
new_args.push_back(up_proj_w);
new_args.push_back(down_proj_w);
if (is_gate_up_quantized_int8) {
- if (is_gate_up_combined) {
+ if (gate_up_type != LLMMLPNode::GATE_UP_TYPE::SEPARATE) {
new_args.push_back(pattern_map.at(gate_up_proj_weight_scales_per_OC));
new_args.push_back(pattern_map.at(gate_up_proj_weight_scales_per_OC));
} else {
@@ -266,7 +287,7 @@ ov::intel_cpu::MLPFusionPass::MLPFusionPass() {
ov::copy_runtime_info(
{pattern_map.at(gate_act).get_node_shared_ptr(), pattern_map.at(down_proj).get_node_shared_ptr()},
new_node);
- if (is_gate_up_combined) {
+ if (gate_up_type != LLMMLPNode::GATE_UP_TYPE::SEPARATE) {
ov::copy_runtime_info({pattern_map.at(gate_up_proj).get_node_shared_ptr()}, new_node);
} else {
ov::copy_runtime_info({pattern_map.at(mlp_gate_proj).get_node_shared_ptr(),
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/mlp_fusion.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/mlp_fusion.cpp
index e2b9f9b8317..3e3b5e45864 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/mlp_fusion.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/mlp_fusion.cpp
@@ -6,13 +6,15 @@
#include <vector>
#include "common_test_utils/ov_tensor_utils.hpp"
-#include "openvino/runtime/exec_model_info.hpp"
-#include "shared_test_classes/base/ov_subgraph.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/gelu.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/swish.hpp"
+#include "openvino/op/variadic_split.hpp"
+#include "openvino/runtime/exec_model_info.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "transformations/rt_info/decompression.hpp"
namespace ov {
namespace test {
@@ -23,6 +25,7 @@ struct LLMMLPFusionParams {
size_t up_size;
std::string act_type;
bool use_dynamic_quant;
+ bool use_swapped_outputs; // true = create pattern with swapped VariadicSplit outputs (should still fuse)
};
class LLMMLPFusionTest : public testing::WithParamInterface<LLMMLPFusionParams>, public ov::test::SubgraphBaseTest {
@@ -39,6 +42,7 @@ public:
result << "up_size=" << obj.param.up_size << "_";
result << "act_type=" << obj.param.act_type << "_";
result << "use_dynamic_quant=" << obj.param.use_dynamic_quant << "_";
+ result << "use_swapped_outputs=" << obj.param.use_swapped_outputs << "_";
result << obj.index;
return result.str();
}
@@ -70,7 +74,8 @@ protected:
in_data.start_from = 0;
in_data.range = 1;
in_data.resolution = 128;
- auto tensor_scale_per_oc = ov::test::utils::create_and_fill_tensor(ov::element::f32, ov::Shape{OC, 1}, in_data);
+ auto tensor_scale_per_oc =
+ ov::test::utils::create_and_fill_tensor(ov::element::f32, ov::Shape{OC, 1}, in_data);
auto scale_per_oc = std::make_shared<ov::op::v0::Constant>(tensor_scale_per_oc);
auto weight_deq = std::make_shared<ov::op::v1::Multiply>(weight_const_f32, scale_per_oc);
@@ -85,23 +90,74 @@ protected:
return std::make_shared<ov::op::v0::Constant>(tensor);
};
if (param.use_dynamic_quant)
- configuration.insert({ov::hint::dynamic_quantization_group_size.name(), std::numeric_limits<uint64_t>::max()});
-
- auto gate_weight = create_const(param.up_size, param.down_size, 100);
- auto up_weight = create_const(param.up_size, param.down_size, 100);
- // down_proj has special cache blocking along K dimension requires lower weight resolution
- auto down_weight = create_const(param.down_size, param.up_size, 16);
-
- auto gate_proj = std::make_shared<ov::op::v0::MatMul>(src, gate_weight, false, true);
- auto up_proj = std::make_shared<ov::op::v0::MatMul>(src, up_weight, false, true);
+ configuration.insert(
+ {ov::hint::dynamic_quantization_group_size.name(), std::numeric_limits<uint64_t>::max()});
std::shared_ptr<Node> gate_act;
- if (param.act_type == "Swish")
- gate_act = std::make_shared<ov::op::v4::Swish>(gate_proj);
- if (param.act_type == "Gelu")
- gate_act = std::make_shared<ov::op::v7::Gelu>(gate_proj);
+ ov::Output<ov::Node> up_output;
- auto gate_up = std::make_shared<ov::op::v1::Multiply>(gate_act, up_proj);
+ if (param.use_swapped_outputs) {
+ // Create pattern with swapped VariadicSplit outputs to test COMBINED_UP_GATE type
+ ov::test::utils::InputGenerateData in_data;
+ in_data.start_from = -0.5;
+ in_data.range = 1.0;
+ in_data.resolution = 16;
+
+ // Combined gate_up weight in FP16 format
+ auto tensor_f16 = ov::test::utils::create_and_fill_tensor(ov::element::f16,
+ ov::Shape{param.up_size * 2, param.down_size},
+ in_data);
+ auto gate_up_weight_f16 = std::make_shared<ov::op::v0::Constant>(tensor_f16);
+ auto gate_up_weight_f32 = std::make_shared<ov::op::v0::Convert>(gate_up_weight_f16, ov::element::f32);
+ // Mark as decompression to prevent constant folding optimization and avoid pattern mismatch
+ mark_as_decompression(gate_up_weight_f32);
+
+ auto gate_up_proj = std::make_shared<ov::op::v0::MatMul>(src, gate_up_weight_f32, false, true);
+
+ auto split_lengths = std::make_shared<ov::op::v0::Constant>(
+ ov::element::i32,
+ ov::Shape{2},
+ std::vector<int32_t>{static_cast<int32_t>(param.up_size), static_cast<int32_t>(param.up_size)});
+ auto axis_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, -1);
+ auto gate_up_split = std::make_shared<ov::op::v1::VariadicSplit>(gate_up_proj, axis_const, split_lengths);
+
+ // Swap outputs to test COMBINED_UP_GATE type
+ auto gate_part = gate_up_split->output(1); // activation on output[1]
+ if (param.act_type == "Swish")
+ gate_act = std::make_shared<ov::op::v4::Swish>(gate_part);
+ if (param.act_type == "Gelu")
+ gate_act = std::make_shared<ov::op::v7::Gelu>(gate_part);
+
+ auto up_part = gate_up_split->output(0); // up branch from output[0] (swapped case)
+ up_output = up_part;
+ } else {
+ // Standard separate weights pattern
+ auto gate_weight = create_const(param.up_size, param.down_size, 100);
+ auto up_weight = create_const(param.up_size, param.down_size, 100);
+
+ auto gate_proj = std::make_shared<ov::op::v0::MatMul>(src, gate_weight, false, true);
+ auto up_proj = std::make_shared<ov::op::v0::MatMul>(src, up_weight, false, true);
+
+ if (param.act_type == "Swish")
+ gate_act = std::make_shared<ov::op::v4::Swish>(gate_proj);
+ if (param.act_type == "Gelu")
+ gate_act = std::make_shared<ov::op::v7::Gelu>(gate_proj);
+
+ up_output = up_proj;
+ }
+
+ // Create compressed down projection weight
+ ov::test::utils::InputGenerateData down_data;
+ down_data.start_from = -0.5;
+ down_data.range = 1;
+ down_data.resolution = 16;
+ auto tensor_f16_down = ov::test::utils::create_and_fill_tensor(ov::element::f16,
+ ov::Shape{param.down_size, param.up_size},
+ down_data);
+ auto down_weight_f16 = std::make_shared<ov::op::v0::Constant>(tensor_f16_down);
+ auto down_weight = std::make_shared<ov::op::v0::Convert>(down_weight_f16, ov::element::f32);
+
+ auto gate_up = std::make_shared<ov::op::v1::Multiply>(gate_act, up_output);
auto output = std::make_shared<ov::op::v0::MatMul>(gate_up, down_weight, false, true);
function = std::make_shared<ov::Model>(ov::OutputVector{output}, ov::ParameterVector{src});
@@ -109,14 +165,15 @@ protected:
void check_results() {
auto exec_model = compiledModel.get_runtime_model();
-
int fused_node_found = 0;
for (const auto& n : exec_model->get_ordered_ops()) {
auto layer_type = n->get_rt_info().at(ov::exec_model_info::LAYER_TYPE).as<std::string>();
if (layer_type == "LLMMLP")
fused_node_found++;
}
- ASSERT_EQ(fused_node_found, 1);
+
+ // Both normal and swapped cases should fuse successfully
+ ASSERT_EQ(fused_node_found, 1) << "Fusion should occur with valid MLP patterns (both normal and swapped cases)";
}
};
@@ -129,13 +186,18 @@ TEST_P(LLMMLPFusionTest, CompareWithRefs) {
namespace {
-static ov::test::InputShape ishape{ov::PartialShape{-1, -1, 4096 / 4}, {ov::Shape{1, 8, 4096 / 4}, ov::Shape{5, 37, 4096 / 4}}};
+static ov::test::InputShape ishape{ov::PartialShape{-1, -1, 4096 / 4},
+ {ov::Shape{1, 8, 4096 / 4}, ov::Shape{5, 37, 4096 / 4}}};
const std::vector<LLMMLPFusionParams> mlp_params = {
- {ishape, 4096 / 4, 11008 / 4, "Gelu", false},
- {ishape, 4096 / 4, 11008 / 4, "Gelu", true},
- {ishape, 4096 / 4, 11008 / 4, "Swish", false},
- {ishape, 4096 / 4, 11008 / 4, "Swish", true},
+ // Standard separate weights cases (should all fuse successfully)
+ {ishape, 4096 / 4, 11008 / 4, "Gelu", false, false},
+ {ishape, 4096 / 4, 11008 / 4, "Gelu", true, false},
+ {ishape, 4096 / 4, 11008 / 4, "Swish", false, false},
+ {ishape, 4096 / 4, 11008 / 4, "Swish", true, false},
+
+ // Test case with swapped VariadicSplit outputs (should fuse with COMBINED_UP_GATE type)
+ {ishape, 4096 / 4, 11008 / 4, "Gelu", false, true},
};
INSTANTIATE_TEST_SUITE_P(smoke_LLMMLPFusion,