Skip to content

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

[Arm] Enable Gather MatMul with KleidiAI Microkernels (#34303)

abhijain1204fujitsu007aab5a990b
diff --git a/src/common/transformations/src/transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.cpp b/src/common/transformations/src/transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.cpp
index 96867c048e7..c3276ccdd99 100644
--- a/src/common/transformations/src/transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/convert_tiled_moe_block_to_gather_matmuls.cpp
@@ -20,6 +20,7 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_elements_update.hpp"
#include "openvino/op/slice.hpp"
+#include "openvino/op/squeeze.hpp"
#include "openvino/op/swish.hpp"
#include "openvino/op/tile.hpp"
#include "openvino/op/transpose.hpp"
@@ -74,7 +75,8 @@ struct MOE2GEMMPatternNodes {
std::shared_ptr<ov::Node> end_reshape_target_shape, end_reshape;
std::shared_ptr<ov::Node> router_topk_indices, chosen_experts, scatter_elements_update;
std::shared_ptr<ov::Node> router_transpose, router_reshape, optional_unsqueeze;
- std::shared_ptr<ov::Node> mul3, reduce_sum;
+ std::shared_ptr<ov::Node> mul3, reduceSum_keepDims, reduceSum_squeeze, reduceSum_noKeepDims;
+ std::shared_ptr<ov::Node> reduce_sum;
};
MOE2GEMMPatternNodes build_2gemm_pattern(const std::vector<ov::element::Type>& supported_weights_types) {
@@ -127,10 +129,16 @@ MOE2GEMMPatternNodes build_2gemm_pattern(const std::vector<ov::element::Type>& s
p.router_reshape = pattern::wrap_type<v1::Reshape>({p.router_transpose, pattern::any_input()});
p.optional_unsqueeze = pattern::optional<v0::Unsqueeze>({p.router_reshape, pattern::any_input()});
- p.mul3 = pattern::wrap_type<v1::Multiply>({p.end_reshape, p.optional_unsqueeze}, pattern::consumers_count(1));
- p.reduce_sum = pattern::wrap_type<v1::ReduceSum>({p.mul3, pattern::any_input()},
- pattern::consumers_count(1),
- {{"keep_dims", false}});
+ p.mul3 = pattern::wrap_type<v1::Multiply>({p.end_reshape, p.optional_unsqueeze});
+ // For ARM, Reduce ops are implemented with keep_dims=true followed by Squeeze operation
+ p.reduceSum_keepDims = pattern::wrap_type<ov::op::v1::ReduceSum>({p.mul3, pattern::any_input()},
+ pattern::consumers_count(1),
+ {{"keep_dims", true}});
+ p.reduceSum_squeeze = pattern::wrap_type<ov::op::v0::Squeeze>({p.reduceSum_keepDims, pattern::any_input()});
+ p.reduceSum_noKeepDims = pattern::wrap_type<ov::op::v1::ReduceSum>({p.mul3, pattern::any_input()},
+ pattern::consumers_count(1),
+ {{"keep_dims", false}});
+ p.reduce_sum = p.reduceSum_squeeze | p.reduceSum_noKeepDims;
return p;
}
@@ -142,7 +150,8 @@ struct MOE3GEMMPatternNodes {
std::shared_ptr<ov::Node> end_reshape_target_shape, end_reshape;
std::shared_ptr<ov::Node> router_topk_indices, chosen_experts, scatter_elements_update;
std::shared_ptr<ov::Node> router_transpose, router_reshape, optional_unsqueeze;
- std::shared_ptr<ov::Node> mul3, reduce_sum;
+ std::shared_ptr<ov::Node> mul3, reduceSum_keepDims, reduceSum_squeeze, reduceSum_noKeepDims;
+ std::shared_ptr<ov::Node> reduce_sum;
};
MOE3GEMMPatternNodes build_3gemm_pattern(const std::vector<ov::element::Type>& supported_weights_types) {
@@ -181,10 +190,16 @@ MOE3GEMMPatternNodes build_3gemm_pattern(const std::vector<ov::element::Type>& s
p.router_reshape = pattern::wrap_type<v1::Reshape>({p.router_transpose, pattern::any_input()});
p.optional_unsqueeze = pattern::optional<v0::Unsqueeze>({p.router_reshape, pattern::any_input()});
- p.mul3 = pattern::wrap_type<v1::Multiply>({p.end_reshape, p.optional_unsqueeze}, pattern::consumers_count(1));
- p.reduce_sum = pattern::wrap_type<v1::ReduceSum>({p.mul3, pattern::any_input()},
- pattern::consumers_count(1),
- {{"keep_dims", false}});
+ p.mul3 = pattern::wrap_type<v1::Multiply>({p.end_reshape, p.optional_unsqueeze});
+ // For ARM, Reduce ops are implemented with keep_dims=true followed by Squeeze operation
+ p.reduceSum_keepDims = pattern::wrap_type<ov::op::v1::ReduceSum>({p.mul3, pattern::any_input()},
+ pattern::consumers_count(1),
+ {{"keep_dims", true}});
+ p.reduceSum_squeeze = pattern::wrap_type<ov::op::v0::Squeeze>({p.reduceSum_keepDims, pattern::any_input()});
+ p.reduceSum_noKeepDims = pattern::wrap_type<ov::op::v1::ReduceSum>({p.mul3, pattern::any_input()},
+ pattern::consumers_count(1),
+ {{"keep_dims", false}});
+ p.reduce_sum = p.reduceSum_squeeze | p.reduceSum_noKeepDims;
return p;
}
@@ -257,11 +272,26 @@ ConvertTiledMoeBlockTo2GatherMatmuls::ConvertTiledMoeBlockTo2GatherMatmuls(
ov::copy_runtime_info(final_mul_node, new_final_mul);
new_final_mul->set_friendly_name(final_mul_node->get_friendly_name());
- const auto reduce_sum_node = pm.at(p.reduce_sum).get_node_shared_ptr();
- const auto new_reduce_sum =
- reduce_sum_node->clone_with_new_inputs({new_final_mul->output(0), reduce_sum_node->input_value(1)});
- ov::copy_runtime_info(reduce_sum_node, new_reduce_sum);
- new_reduce_sum->set_friendly_name(reduce_sum_node->get_friendly_name());
+ std::shared_ptr<ov::Node> new_reduce_sum = nullptr;
+ std::string new_reduce_sum_friendly_name;
+ if (pm.find(p.reduceSum_squeeze) != pm.end()) {
+ const auto reduce_node = pm.at(p.reduceSum_keepDims).get_node_shared_ptr();
+ const auto squeeze_node = pm.at(p.reduceSum_squeeze).get_node_shared_ptr();
+ const auto new_reduce_node =
+ reduce_node->clone_with_new_inputs({new_final_mul->output(0), reduce_node->input_value(1)});
+ ov::copy_runtime_info(reduce_node, new_reduce_node);
+ new_reduce_sum =
+ squeeze_node->clone_with_new_inputs({new_reduce_node->output(0), squeeze_node->input_value(1)});
+ ov::copy_runtime_info(squeeze_node, new_reduce_sum);
+ new_reduce_sum_friendly_name = squeeze_node->get_friendly_name();
+ } else {
+ const auto reduce_node = pm.at(p.reduceSum_noKeepDims).get_node_shared_ptr();
+ new_reduce_sum =
+ reduce_node->clone_with_new_inputs({new_final_mul->output(0), reduce_node->input_value(1)});
+ ov::copy_runtime_info(reduce_node, new_reduce_sum);
+ new_reduce_sum_friendly_name = reduce_node->get_friendly_name();
+ }
+ new_reduce_sum->set_friendly_name(new_reduce_sum_friendly_name);
const auto& end_reshape_out = pm.at(p.end_reshape);
const auto end_reshape_rank = end_reshape_out.get_partial_shape().rank();
@@ -344,11 +374,27 @@ ConvertTiledMoeBlockTo3GatherMatmuls::ConvertTiledMoeBlockTo3GatherMatmuls(
final_mul_node->clone_with_new_inputs({down_gathered_mm->output(0), router_unsqueeze->output(0)});
ov::copy_runtime_info(final_mul_node, new_final_mul);
new_final_mul->set_friendly_name(final_mul_node->get_friendly_name());
- const auto reduce_sum_node = pm.at(p.reduce_sum).get_node_shared_ptr();
- const auto new_reduce_sum =
- reduce_sum_node->clone_with_new_inputs({new_final_mul->output(0), reduce_sum_node->input_value(1)});
- ov::copy_runtime_info(reduce_sum_node, new_reduce_sum);
- new_reduce_sum->set_friendly_name(reduce_sum_node->get_friendly_name());
+
+ std::shared_ptr<ov::Node> new_reduce_sum = nullptr;
+ std::string new_reduce_sum_friendly_name;
+ if (pm.find(p.reduceSum_squeeze) != pm.end()) {
+ const auto reduce_node = pm.at(p.reduceSum_keepDims).get_node_shared_ptr();
+ const auto squeeze_node = pm.at(p.reduceSum_squeeze).get_node_shared_ptr();
+ const auto new_reduce_node =
+ reduce_node->clone_with_new_inputs({new_final_mul->output(0), reduce_node->input_value(1)});
+ ov::copy_runtime_info(reduce_node, new_reduce_node);
+ new_reduce_sum =
+ squeeze_node->clone_with_new_inputs({new_reduce_node->output(0), squeeze_node->input_value(1)});
+ ov::copy_runtime_info(squeeze_node, new_reduce_sum);
+ new_reduce_sum_friendly_name = squeeze_node->get_friendly_name();
+ } else {
+ const auto reduce_node = pm.at(p.reduceSum_noKeepDims).get_node_shared_ptr();
+ new_reduce_sum =
+ reduce_node->clone_with_new_inputs({new_final_mul->output(0), reduce_node->input_value(1)});
+ ov::copy_runtime_info(reduce_node, new_reduce_sum);
+ new_reduce_sum_friendly_name = reduce_node->get_friendly_name();
+ }
+ new_reduce_sum->set_friendly_name(new_reduce_sum_friendly_name);
const auto& end_reshape_out = pm.at(p.end_reshape);
const auto end_reshape_rank = end_reshape_out.get_partial_shape().rank();
diff --git a/src/common/transformations/tests/common_optimizations/convert_tiled_moe_block_to_gather_matmuls_test.cpp b/src/common/transformations/tests/common_optimizations/convert_tiled_moe_block_to_gather_matmuls_test.cpp
index 6565590cc60..d10ae94308a 100644
--- a/src/common/transformations/tests/common_optimizations/convert_tiled_moe_block_to_gather_matmuls_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/convert_tiled_moe_block_to_gather_matmuls_test.cpp
@@ -31,6 +31,7 @@
#include "openvino/op/shape_of.hpp"
#include "openvino/op/slice.hpp"
#include "openvino/op/softmax.hpp"
+#include "openvino/op/squeeze.hpp"
#include "openvino/op/swish.hpp"
#include "openvino/op/tile.hpp"
#include "openvino/op/topk.hpp"
@@ -85,6 +86,7 @@ using ConvertTiledMoeBlockToGatherMatmulsParams = std::tuple<MoEType,
AdditionalConsumersMode, // additional_consumers_mode
bool, // use_gelu_gate (3GeMM only)
bool, // norm_before_tile (3GeMM only)
+ bool, // reduce_sum_keep_dims
bool>; // transformation_should_be_applied
inline std::shared_ptr<ov::Node> build_matmul_weights(const ov::Shape& weights_shape,
@@ -106,6 +108,7 @@ inline std::shared_ptr<ov::Model> initMoE2GeMMSubgraph(bool use_scatter_v12,
bool use_broadcast_v3,
bool skip_unsqueeze,
bool matmul_transpose_b,
+ bool reduce_sum_keep_dims,
AdditionalConsumersMode additional_consumers_mode) {
// Fixed values that don't affect pass behavior
const auto expert_alpha = 1.625f;
@@ -294,10 +297,18 @@ inline std::shared_ptr<ov::Model> initMoE2GeMMSubgraph(bool use_scatter_v12,
auto mul3 = std::make_shared<ov::op::v1::Multiply>(end_reshape, routing_weights_final);
- auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
- mul3,
- ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
- false);
+ std::shared_ptr<ov::Node> reduce_sum = nullptr;
+ if (reduce_sum_keep_dims) {
+ auto reduce_dim_constant =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0});
+ auto reduce_sum_w_keep_dims = std::make_shared<ov::op::v1::ReduceSum>(mul3, reduce_dim_constant, true);
+ reduce_sum = std::make_shared<ov::op::v0::Squeeze>(reduce_sum_w_keep_dims, reduce_dim_constant);
+ } else {
+ reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
+ mul3,
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
+ false);
+ }
ov::ParameterVector params = {input};
ov::ResultVector results = {std::make_shared<ov::op::v0::Result>(reduce_sum)};
@@ -317,7 +328,8 @@ inline std::shared_ptr<ov::Model> initMoE2GeMMSubgraph(bool use_scatter_v12,
inline std::shared_ptr<ov::Model> initMoE2GeMMSubgraphRef(bool use_scatter_v12,
bool use_broadcast_v3,
bool skip_unsqueeze,
- bool matmul_transpose_b) {
+ bool matmul_transpose_b,
+ bool reduce_sum_keep_dims) {
// Fixed values that don't affect pass behavior
const auto expert_alpha = 1.625f;
const auto expert_beta = 7.0f;
@@ -434,10 +446,19 @@ inline std::shared_ptr<ov::Model> initMoE2GeMMSubgraphRef(bool use_scatter_v12,
auto mul3 = std::make_shared<ov::op::v1::Multiply>(down_gathered_mm, router_unsqueeze);
- auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
- mul3,
- ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
- false);
+ std::shared_ptr<ov::Node> reduce_sum = nullptr;
+ if (reduce_sum_keep_dims) {
+ auto reduce_dim_constant =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0});
+ auto reduce_sum_w_keep_dims = std::make_shared<ov::op::v1::ReduceSum>(mul3, reduce_dim_constant, true);
+ reduce_sum = std::make_shared<ov::op::v0::Squeeze>(reduce_sum_w_keep_dims, reduce_dim_constant);
+
+ } else {
+ reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
+ mul3,
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
+ false);
+ }
const auto number_of_experts_const =
ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(number_of_experts)});
@@ -469,7 +490,8 @@ inline std::shared_ptr<ov::Model> initMoE3GeMMSubgraph(bool use_scatter_v12,
AdditionalConsumersMode additional_consumers_mode,
GateActivationType gate_activation = GateActivationType::SWISH,
bool norm_before_tile = false,
- bool use_normalized_router = true) {
+ bool use_normalized_router = true,
+ bool reduce_sum_keep_dims = false) {
// Fixed values that don't affect pass behavior
const ov::PartialShape input_shape = {-1, -1, 256};
const size_t topk = 4;
@@ -656,10 +678,18 @@ inline std::shared_ptr<ov::Model> initMoE3GeMMSubgraph(bool use_scatter_v12,
auto mul3 = std::make_shared<ov::op::v1::Multiply>(end_reshape, routing_weights_final);
- auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
- mul3,
- ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
- false);
+ std::shared_ptr<ov::Node> reduce_sum = nullptr;
+ if (reduce_sum_keep_dims) {
+ auto reduce_dim_constant =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0});
+ auto reduce_sum_w_keep_dims = std::make_shared<ov::op::v1::ReduceSum>(mul3, reduce_dim_constant, true);
+ reduce_sum = std::make_shared<ov::op::v0::Squeeze>(reduce_sum_w_keep_dims, reduce_dim_constant);
+ } else {
+ reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
+ mul3,
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
+ false);
+ }
auto final_reshape_const = ov::op::v0::Constant::create(ov::element::i64,
ov::Shape{2},
@@ -695,7 +725,8 @@ inline std::shared_ptr<ov::Model> initMoE3GeMMSubgraphRef(
bool matmul_transpose_b,
GateActivationType gate_activation = GateActivationType::SWISH,
bool norm_before_tile = false,
- bool use_normalized_router = true) {
+ bool use_normalized_router = true,
+ bool reduce_sum_keep_dims = false) {
// Fixed values that don't affect pass behavior
const ov::PartialShape input_shape = {-1, -1, 256};
const size_t topk = 4;
@@ -812,10 +843,19 @@ inline std::shared_ptr<ov::Model> initMoE3GeMMSubgraphRef(
auto mul3 = std::make_shared<ov::op::v1::Multiply>(down_gathered_mm, router_unsqueeze);
- auto reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
- mul3,
- ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
- false);
+ std::shared_ptr<ov::Node> reduce_sum = nullptr;
+ if (reduce_sum_keep_dims) {
+ auto reduce_dim_constant =
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0});
+ auto reduce_sum_w_keep_dims = std::make_shared<ov::op::v1::ReduceSum>(mul3, reduce_dim_constant, true);
+ reduce_sum = std::make_shared<ov::op::v0::Squeeze>(reduce_sum_w_keep_dims, reduce_dim_constant);
+
+ } else {
+ reduce_sum = std::make_shared<ov::op::v1::ReduceSum>(
+ mul3,
+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{0}),
+ false);
+ }
const auto number_of_experts_const =
ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(number_of_experts)});
@@ -861,13 +901,15 @@ public:
additional_consumers_mode,
use_gelu_gate,
norm_before_tile,
+ reduce_sum_keep_dims,
should_be_applied] = obj.param;
std::ostringstream result;
result << "MoEType_" << moe_type << "_ScatterV" << (use_scatter_v12 ? "12" : "3") << "_BroadcastV"
<< (use_broadcast_v3 ? "3" : "1") << "_SkipUnsqueeze_" << (skip_unsqueeze ? "true" : "false")
<< "_MatMulTransposeB_" << (matmul_transpose_b ? "true" : "false") << "_AdditionalConsumers_"
<< additional_consumers_mode << "_GeluGate_" << (use_gelu_gate ? "true" : "false") << "_NormBeforeTile_"
- << (norm_before_tile ? "true" : "false") << "_shouldBeApplied_"
+ << (norm_before_tile ? "true" : "false") << "_reduce_sum_keep_dims_"
+ << (reduce_sum_keep_dims ? "true" : "false") << "_shouldBeApplied_"
<< (should_be_applied ? "true" : "false");
return result.str();
}
@@ -883,6 +925,7 @@ protected:
additional_consumers_mode,
use_gelu_gate,
norm_before_tile,
+ reduce_sum_keep_dims,
should_be_applied] = this->GetParam();
// 3GeMM-only params must be false for MoE2GeMM
@@ -899,6 +942,7 @@ protected:
use_broadcast_v3,
skip_unsqueeze,
matmul_transpose_b,
+ reduce_sum_keep_dims,
additional_consumers_mode);
break;
case MoEType::MoE3GeMM:
@@ -909,7 +953,8 @@ protected:
additional_consumers_mode,
gate_act,
norm_before_tile,
- use_normalized_router);
+ use_normalized_router,
+ reduce_sum_keep_dims);
break;
default:
OPENVINO_THROW("Unexpected MoEType value");
@@ -920,8 +965,11 @@ protected:
if (should_be_applied) {
switch (moe_type) {
case MoEType::MoE2GeMM:
- model_ref =
- initMoE2GeMMSubgraphRef(use_scatter_v12, use_broadcast_v3, skip_unsqueeze, matmul_transpose_b);
+ model_ref = initMoE2GeMMSubgraphRef(use_scatter_v12,
+ use_broadcast_v3,
+ skip_unsqueeze,
+ matmul_transpose_b,
+ reduce_sum_keep_dims);
break;
case MoEType::MoE3GeMM:
model_ref = initMoE3GeMMSubgraphRef(use_scatter_v12,
@@ -930,7 +978,8 @@ protected:
matmul_transpose_b,
gate_act,
norm_before_tile,
- use_normalized_router);
+ use_normalized_router,
+ reduce_sum_keep_dims);
break;
default:
OPENVINO_THROW("Unexpected MoEType value");
@@ -945,6 +994,7 @@ const std::vector<MoEType> moe_types = {MoEType::MoE2GeMM, MoEType::MoE3GeMM};
const std::vector<bool> scatter_versions = {false, true}; // false = v3, true = v12
const std::vector<bool> broadcast_versions = {false, true}; // false = v1, true = v3
const std::vector<bool> skip_unsqueeze_versions = {false, true};
+const std::vector<bool> reduce_sum_keep_dims = {false, true}; // if true, reduce_sum is followed by squeeze.
INSTANTIATE_TEST_SUITE_P(ConvertTiledMoeBlockToGatherMatmulsTest_positive_cases,
ConvertTiledMoeBlockToGatherMatmulsTest,
@@ -956,6 +1006,7 @@ INSTANTIATE_TEST_SUITE_P(ConvertTiledMoeBlockToGatherMatmulsTest_positive_cases,
::testing::Values(AdditionalConsumersMode::NO),
::testing::Values(false),
::testing::Values(false),
+ ::testing::ValuesIn(reduce_sum_keep_dims),
::testing::Values(true)),
ConvertTiledMoeBlockToGatherMatmulsTest::getTestCaseName);
@@ -969,6 +1020,7 @@ INSTANTIATE_TEST_SUITE_P(ConvertTiledMoeBlockToGatherMatmulsTest_3gemm_specific_
::testing::Values(AdditionalConsumersMode::NO),
::testing::Values(false, true),
::testing::Values(false, true),
+ ::testing::ValuesIn(reduce_sum_keep_dims),
::testing::Values(true)),
ConvertTiledMoeBlockToGatherMatmulsTest::getTestCaseName);
@@ -982,6 +1034,7 @@ INSTANTIATE_TEST_SUITE_P(ConvertTiledMoeBlockToGatherMatmulsTest_negative_cases_
::testing::Values(AdditionalConsumersMode::NO),
::testing::Values(false),
::testing::Values(false),
+ ::testing::ValuesIn(reduce_sum_keep_dims),
::testing::Values(false)),
ConvertTiledMoeBlockToGatherMatmulsTest::getTestCaseName);
@@ -997,6 +1050,7 @@ INSTANTIATE_TEST_SUITE_P(ConvertTiledMoeBlockToGatherMatmulsTest_negative_cases_
AdditionalConsumersMode::REDUCE),
::testing::Values(false),
::testing::Values(false),
+ ::testing::Values(false),
::testing::Values(false)),
ConvertTiledMoeBlockToGatherMatmulsTest::getTestCaseName);
-} // namespace
+} // namespace
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/executors/common/offset_helper.hpp b/src/plugins/intel_cpu/src/nodes/executors/common/offset_helper.hpp
new file mode 100644
index 00000000000..6b7d74ce172
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/common/offset_helper.hpp
@@ -0,0 +1,82 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <bitset>
+
+#include "cpu_memory.h"
+#include "memory_desc/blocked_memory_desc.h"
+
+namespace ov::intel_cpu {
+class OffsetHelper {
+public:
+ static OffsetHelper createOffsetHelper(const MemoryPtr& mem) {
+ static const VectorDims empty_dims;
+ std::bitset<2> broadcast_mask;
+ if (nullptr == mem || mem->getDesc().empty()) {
+ return {nullptr, empty_dims, broadcast_mask, 0};
+ }
+ return createOffsetHelper(*mem);
+ }
+
+ static OffsetHelper createOffsetHelper(const IMemory& mem) {
+ std::bitset<2> broadcast_mask;
+ auto* base_ptr = static_cast<uint8_t*>(mem.getData());
+ auto desc = mem.getDescWithType<BlockedMemoryDesc>();
+ const auto& strides = desc->getStrides();
+ const auto prc = desc->getPrecision();
+ const auto& shape = desc->getShape().getStaticDims();
+ for (size_t i = 0; i < shape.size() && i < 2; i++) {
+ if (shape[i] == 1) {
+ broadcast_mask.set(i);
+ }
+ }
+ return {base_ptr, strides, broadcast_mask, prc.bitwidth()};
+ }
+
+ void* operator()(size_t i0) const {
+ if (!m_base_ptr) {
+ return nullptr;
+ }
+ if (m_broadcast_mask.test(0)) {
+ i0 = 0;
+ }
+ const size_t offset_bits = i0 * m_strides[0] * m_num_bits;
+ const size_t offset = div_up(offset_bits, 8); // 8 bits in byte
+ return m_base_ptr + offset;
+ }
+
+ void* operator()(size_t i0, size_t i1) const {
+ if (!m_base_ptr) {
+ return nullptr;
+ }
+ if (m_broadcast_mask.test(0)) {
+ i0 = 0;
+ }
+ if (m_broadcast_mask.test(1)) {
+ i1 = 0;
+ }
+ const size_t offset_bits = i0 * m_strides[0] * m_num_bits + i1 * m_strides[1] * m_num_bits;
+ const size_t offset = div_up(offset_bits, 8); // 8 bits in byte
+ return m_base_ptr + offset;
+ }
+
+ [[nodiscard]] void* get_base() const {
+ return m_base_ptr;
+ }
+
+private:
+ OffsetHelper(uint8_t* base_ptr, const VectorDims& strides, std::bitset<2> broadcast_mask, size_t num_bits)
+ : m_base_ptr(base_ptr),
+ m_strides(strides),
+ m_num_bits(num_bits),
+ m_broadcast_mask(broadcast_mask) {}
+
+ uint8_t* m_base_ptr = nullptr;
+ const VectorDims& m_strides;
+ size_t m_num_bits;
+ std::bitset<2> m_broadcast_mask;
+};
+}; // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_gathermatmul_executor.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_gathermatmul_executor.cpp
index 68bc9488dec..46496ac05ca 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_gathermatmul_executor.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_gathermatmul_executor.cpp
@@ -7,7 +7,6 @@
#include <oneapi/dnnl/dnnl_common_types.h>
#include <oneapi/dnnl/dnnl_types.h>
-#include <bitset>
#include <common/primitive_hashing_utils.hpp>
#include <common/utils.hpp>
#include <cstddef>
@@ -32,6 +31,7 @@
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/cpu_memory_desc_utils.h"
#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/executors/common/offset_helper.hpp"
#include "nodes/executors/dnnl/dnnl_utils.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/gathermatmul_config.hpp"
@@ -224,78 +224,7 @@ private:
impl_desc_type m_impl_type = impl_desc_type::unknown;
};
-// ---- OffsetHelper ------------------------------------------------------------
-
namespace {
-class OffsetHelper {
-public:
- static OffsetHelper createOffsetHelper(const MemoryPtr& mem) {
- static const VectorDims empty_dims;
- std::bitset<2> broadcast_mask;
- if (nullptr == mem || mem->getDesc().empty()) {
- return {nullptr, empty_dims, broadcast_mask, 0};
- }
- return createOffsetHelper(*mem);
- }
-
- static OffsetHelper createOffsetHelper(const IMemory& mem) {
- std::bitset<2> broadcast_mask;
- auto* base_ptr = static_cast<uint8_t*>(mem.getData());
- auto desc = mem.getDescWithType<BlockedMemoryDesc>();
- const auto& strides = desc->getStrides();
- const auto prc = desc->getPrecision();
- const auto& shape = desc->getShape().getStaticDims();
- for (size_t i = 0; i < shape.size() && i < 2; i++) {
- if (shape[i] == 1) {
- broadcast_mask.set(i);
- }
- }
- return {base_ptr, strides, broadcast_mask, prc.bitwidth()};
- }
-
- void* operator()(size_t i0) const {
- if (!m_base_ptr) {
- return nullptr;
- }
- if (m_broadcast_mask.test(0)) {
- i0 = 0;
- }
- const size_t offset_bits = i0 * m_strides[0] * m_num_bits;
- const size_t offset = div_up(offset_bits, 8);
- return m_base_ptr + offset;
- }
-
- void* operator()(size_t i0, size_t i1) const {
- if (!m_base_ptr) {
- return nullptr;
- }
- if (m_broadcast_mask.test(0)) {
- i0 = 0;
- }
- if (m_broadcast_mask.test(1)) {
- i1 = 0;
- }
- const size_t offset_bits = i0 * m_strides[0] * m_num_bits + i1 * m_strides[1] * m_num_bits;
- const size_t offset = div_up(offset_bits, 8);
- return m_base_ptr + offset;
- }
-
- [[nodiscard]] void* get_base() const {
- return m_base_ptr;
- }
-
-private:
- OffsetHelper(uint8_t* base_ptr, const VectorDims& strides, std::bitset<2> broadcast_mask, size_t num_bits)
- : m_base_ptr(base_ptr),
- m_strides(strides),
- m_num_bits(num_bits),
- m_broadcast_mask(broadcast_mask) {}
-
- uint8_t* m_base_ptr = nullptr;
- const VectorDims& m_strides;
- size_t m_num_bits;
- std::bitset<2> m_broadcast_mask;
-};
// ---- normalizeM helper -------------------------------------------------------
diff --git a/src/plugins/intel_cpu/src/nodes/executors/gathermatmul_implementations.cpp b/src/plugins/intel_cpu/src/nodes/executors/gathermatmul_implementations.cpp
index 78794bcbc02..9015614a27d 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/gathermatmul_implementations.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/gathermatmul_implementations.cpp
@@ -24,11 +24,20 @@
# include "nodes/executors/type_mask.hpp"
# include "openvino/core/type/element_type.hpp"
#endif
+
+#if defined(OV_CPU_WITH_KLEIDIAI)
+# include "implementation_utils.hpp"
+# include "nodes/executors/debug_messages.hpp"
+# include "nodes/executors/executor.hpp"
+# include "nodes/executors/kleidiai/kleidiai_gathermatmul.hpp"
+# include "utils/general_utils.h"
+#endif
namespace ov::intel_cpu {
+using namespace ov::element;
+
#if defined(OV_CPU_WITH_DNNL) && defined(OPENVINO_ARCH_X86_64)
-using namespace ov::element;
using namespace TypeMaskAlias;
using namespace executor;
@@ -80,6 +89,7 @@ static const MappingNotation gatherMatmulMappingNotation{
template <>
const std::vector<ExecutorImplementation<GatherMatmulAttrs>>& getImplementations() {
static const std::vector<ExecutorImplementation<GatherMatmulAttrs>> gathermatmulImplementations{
+
OV_CPU_INSTANCE_DNNL_X64(
"gathermatmul_dnnl",
ExecutorType::Dnnl,
@@ -99,6 +109,23 @@ const std::vector<ExecutorImplementation<GatherMatmulAttrs>>& getImplementations
AcceptsAnyShape<GatherMatmulAttrs>,
CreateDefault<GatherMatmulDnnlExecutor, GatherMatmulAttrs>{}
)
+ OV_CPU_INSTANCE_KLEIDIAI(
+ "gathermatmul_kleidiai",
+ ExecutorType::Kleidiai,
+ OperationType::GatherMatmul,
+ // supports
+ [](const GatherMatmulConfig& config) -> bool {
+ VERIFY(all_of(f32, srcType(config), dstType(config)), UNSUPPORTED_SRC_PRECISIONS);
+ VERIFY(any_of(weiType(config), i8, i4, f32), UNSUPPORTED_WEI_PRECISIONS);
+ VERIFY(implication(hasBias(config), biaType(config) == f32), UNSUPPORTED_SRC_PRECISIONS);
+ VERIFY(weiRank(config) == 3U, UNSUPPORTED_WEI_RANK);
+ VERIFY(GatherMatMulKleidiAIExecutor::supports(config), UNSUPPORTED_BY_EXECUTOR);
+ return true;
+ },
+ HasNoOptimalConfig<GatherMatmulAttrs>{},
+ AcceptsAnyShape<GatherMatmulAttrs>,
+ CreateDefault<GatherMatMulKleidiAIExecutor, GatherMatmulAttrs>{}
+ )
};
return gathermatmulImplementations;
}
diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.cpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.cpp
new file mode 100644
index 00000000000..ed5e1573b84
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.cpp
@@ -0,0 +1,172 @@
+// Copyright (C) 2026 FUJITSU LIMITED
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "kleidiai_gathermatmul.hpp"
+
+#include <cstddef>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "cpu_memory.h"
+#include "memory_desc/cpu_memory_desc.h"
+#include "memory_desc/cpu_memory_desc_utils.h"
+#include "nodes/common/blocked_desc_creator.h"
+#include "nodes/executors/common/offset_helper.hpp"
+#include "nodes/executors/executor.hpp"
+#include "nodes/executors/fullyconnected_config.hpp"
+#include "nodes/executors/gathermatmul_config.hpp"
+#include "nodes/executors/kleidiai/kleidiai_mm.hpp"
+#include "nodes/executors/memory_arguments.hpp"
+#include "openvino/core/except.hpp"
+#include "openvino/core/type/element_type.hpp"
+#include "utils/precision_support.h"
+
+namespace ov::intel_cpu {
+
+static bool useDynamicQuantizationImpl(const MemoryDescPtr& weightDesc) {
+ if (!hasIntDotProductSupport() && !hasInt8MMSupport()) {
+ return false;
+ }
+ return weightDesc->getPrecision() == element::i8 || weightDesc->getPrecision() == element::i4;
+}
+
+bool GatherMatMulKleidiAIExecutor::supports([[maybe_unused]] const GatherMatmulConfig& config) {
+ return config.descs.at(ARG_WEI)->getPrecision() == element::f32 ||
+ useDynamicQuantizationImpl(config.descs.at(ARG_WEI));
+}
+
+GatherMatMulKleidiAIExecutor::GatherMatMulKleidiAIExecutor([[maybe_unused]] const GatherMatmulAttrs& attrs,
+ const MemoryArgs& memory,
+ const ExecutorContext::CPtr& context)
+ : m_context(context) {
+ auto srcMemoryDesc = memory.at(ARG_SRC)->getDescPtr();
+ auto weiMemoryDesc = memory.at(ARG_WEI)->getDescPtr();
+ auto biasMemoryDesc = memory.at(ARG_BIAS)->getDescPtr();
+
+ const auto& weiDims = weiMemoryDesc->getShape().getStaticDims();
+ auto weiPrec = weiMemoryDesc->getPrecision();
+ auto SrcPrec = srcMemoryDesc->getPrecision();
+ const size_t N = weiDims[weiDims.size() - 2];
+ const size_t K = weiDims[weiDims.size() - 1];
+ gather_axis_size = weiDims[0];
+
+ MemoryPtr m_weightsMemory = memory.at(ARG_WEI);
+ MemoryPtr m_scalesMemory = nullptr;
+
+ OPENVINO_ASSERT(weiMemoryDesc->isDefined(), "Weights memory descriptor is not defined");
+ OPENVINO_ASSERT(SrcPrec == ov::element::f32, "Activation currently supported only in f32");
+
+ const auto& creatorsMap = BlockedDescCreator::getCommonCreators();
+ bool isCompressedOp = weiMemoryDesc->getPrecision().is_integral();
+ if (isCompressedOp) {
+ const auto& scales = memory.at(ARG_SRC_3);
+ if (scales && !scales->getDesc().empty()) {
+ const auto& fullScalesShape = scales->getDesc().getShape().getStaticDims();
+ if (1 == fullScalesShape.size()) {
+ OPENVINO_THROW(" broadcastable scales shape not supported ");
+ } else {
+ m_scalesMemory = scales;
+ }
+ } else {
+ OPENVINO_THROW(" Scales cannot be empty for GatherMatmulCompressed op ");
+ }
+ }
+ MemoryDescArgs memDescArgs;
+ memDescArgs[ARG_SRC] = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(SrcPrec, Shape({1, K}));
+ memDescArgs[ARG_DST] = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(SrcPrec, Shape({1, N}));
+ memDescArgs[ARG_WEI] = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(weiPrec, Shape({N, K}));
+ if (biasMemoryDesc && !biasMemoryDesc->empty()) {
+ memDescArgs[ARG_BIAS] = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(ov::element::f32, Shape({N}));
+ } else {
+ memDescArgs[ARG_BIAS] = MemoryDescUtils::makeEmptyDesc();
+ }
+
+ FCAttrs fcArgs;
+ if (isCompressedOp) {
+ // set as uint64_max to enable dynamic quantization by default
+ fcArgs.dynamicQuantizationGroupSize = std::numeric_limits<uint64_t>::max();
+ memDescArgs[ARG_WEI | ARG_ATTR_SCALES] =
+ creatorsMap.at(LayoutType::ncsp)->createSharedDesc(ov::element::f32, Shape({N}));
+ }
+
+ // As we call executors sequentially, execution-context can be shared
+ memArgs.reserve(gather_axis_size);
+ for (size_t i = 0; i < gather_axis_size; ++i) {
+ MemoryArgs mArgs;
+ mArgs[ARG_WEI] = split_horizontal(context->getEngine(), m_weightsMemory, 0, i, gather_axis_size, true);
+ // wei_shape shape after split becomes: [1, N, K] --> redefine desc to [N, K]
+ mArgs[ARG_WEI]->redefineDesc(memDescArgs[ARG_WEI]);
+ if (biasMemoryDesc && !biasMemoryDesc->empty()) {
+ auto bias = memory.at(ARG_BIAS);
+ mArgs[ARG_BIAS] = split_horizontal(context->getEngine(), bias, 0, i, gather_axis_size, true);
+ mArgs[ARG_BIAS]->redefineDesc(memDescArgs[ARG_BIAS]);
+ } else {
+ mArgs[ARG_BIAS] = std::make_shared<Memory>(context->getEngine(), MemoryDescUtils::makeEmptyDesc());
+ }
+ if (isCompressedOp) {
+ mArgs[ARG_WEI | ARG_ATTR_SCALES] =
+ split_horizontal(context->getEngine(), m_scalesMemory, 0, i, gather_axis_size, true);
+ mArgs[ARG_WEI | ARG_ATTR_SCALES]->redefineDesc(memDescArgs[ARG_WEI | ARG_ATTR_SCALES]);
+ }
+ mArgs[ARG_SRC] = std::make_shared<Memory>(context->getEngine(), memDescArgs[ARG_SRC]);
+ memArgs.emplace_back(mArgs);
+ auto kai_executor = std::make_shared<MatMulKleidiAIExecutor>(fcArgs, mArgs, context);
+ kai_executor->setKaiExecutorImplAsGatherMatmul();
+ executor.push_back(kai_executor);
+ }
+}
+
+bool GatherMatMulKleidiAIExecutor::update(const MemoryArgs& memory) {
+ for (size_t i = 0; i < gather_axis_size; ++i) {
+ memArgs[i][ARG_SRC] = memory.at(ARG_SRC);
+ memArgs[i][ARG_DST] = memory.at(ARG_DST);
+ executor[i]->update(memArgs[i]);
+ }
+ return true;
+}
+
+void GatherMatMulKleidiAIExecutor::execute(const MemoryArgs& memory) {
+ const auto& indexMem = memory.at(ARG_SRC_1);
+ auto index_offset = OffsetHelper::createOffsetHelper(indexMem);
+
+ const auto& indexShape = indexMem->getStaticDims();
+ size_t M = indexShape[0];
+ size_t B = indexShape[1];
+
+ // all the gather idx for corresponding m index
+ std::vector<std::pair<int32_t, int32_t>> gather_idx_map(gather_axis_size * M);
+ std::vector<int32_t> elements_per_gather_indx(gather_axis_size, 0);
+ for (size_t m = 0; m < M; m++) {
+ const auto* gather_ids = static_cast<const int32_t*>(index_offset(m));
+ for (size_t i = 0; i < B; i++) {
+ int32_t gather_axis_index = gather_ids[i];
+ OPENVINO_ASSERT(gather_axis_index >= 0 && static_cast<size_t>(gather_axis_index) < gather_axis_size,
+ "Invalid gather_id ",
+ gather_axis_index,
+ " for m ",
+ m);
+ auto& index = elements_per_gather_indx[gather_axis_index];
+ gather_idx_map[gather_axis_index * M + index] = {m, i};
+ index++;
+ }
+ }
+
+ for (size_t gather_axis_index = 0; gather_axis_index < gather_axis_size; gather_axis_index++) {
+ const size_t num_valid_rows = elements_per_gather_indx[gather_axis_index];
+ if (0 == num_valid_rows) {
+ continue;
+ }
+ memArgs[gather_axis_index][ARG_SRC] = memory.at(ARG_SRC);
+ memArgs[gather_axis_index][ARG_DST] = memory.at(ARG_DST);
+ auto gather_idx_Offset = gather_idx_map.begin() + gather_axis_index * M;
+ std::vector<std::pair<int32_t, int32_t>> kai_gather_idx(gather_idx_Offset, gather_idx_Offset + num_valid_rows);
+ executor[gather_axis_index]->set_gather_idx(kai_gather_idx);
+ executor[gather_axis_index]->execute(memArgs[gather_axis_index]);
+ }
+}
+
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.hpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.hpp
new file mode 100644
index 00000000000..8b3d8c0f855
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_gathermatmul.hpp
@@ -0,0 +1,39 @@
+// Copyright (C) 2026 FUJITSU LIMITED
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "cpu_memory.h"
+#include "memory_desc/cpu_memory_desc.h"
+#include "nodes/executors/executor.hpp"
+#include "nodes/executors/executor_factory.hpp"
+#include "nodes/executors/kleidiai/kleidiai_mm.hpp"
+#include "nodes/executors/memory_arguments.hpp"
+
+namespace ov::intel_cpu {
+class GatherMatMulKleidiAIExecutor : public Executor {
+public:
+ static bool supports(const GatherMatmulConfig& config);
+
+ GatherMatMulKleidiAIExecutor(const GatherMatmulAttrs& attrs,
+ const MemoryArgs& memory,
+ const ExecutorContext::CPtr& context);
+
+ bool update(const MemoryArgs& memory) override;
+ void execute(const MemoryArgs& memory) override;
+ [[nodiscard]] impl_desc_type implType() const override {
+ return impl_desc_type::kleidiai;
+ }
+
+private:
+ ExecutorContext::CPtr m_context;
+ size_t gather_axis_size = 0;
+ std::vector<MatMulKleidiAIExecutorPtr> executor;
+ std::vector<MemoryArgs> memArgs;
+};
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp
index d5054011a3a..a3b719da723 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.cpp
@@ -7,9 +7,11 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
+#include <cstring>
#include <functional>
#include <limits>
#include <memory>
+#include <utility>
#include <vector>
#include "cpu_memory.h"
@@ -23,7 +25,9 @@
#include "memory_desc/cpu_blocked_memory_desc.h"
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/cpu_memory_desc_utils.h"
+#include "nodes/common/blocked_desc_creator.h"
#include "nodes/executors/acl/acl_fullyconnected_utils.hpp"
+#include "nodes/executors/common/offset_helper.hpp"
#include "nodes/executors/executor.hpp"
#include "nodes/executors/fullyconnected_config.hpp"
#include "nodes/executors/memory_arguments.hpp"
@@ -31,6 +35,8 @@
#include "openvino/core/parallel.hpp"
#include "openvino/core/type/element_type.hpp"
#include "utils/cpu_utils.hpp"
+#include "utils/debug_capabilities.h"
+#include "utils/general_utils.h"
#include "utils/precision_support.h"
#define FLOAT_MAX 3.4028235e38f
@@ -99,7 +105,12 @@ MatMulKleidiAIExecutor::MatMulKleidiAIExecutor(const FCAttrs& attrs,
if (!useDynamicQuant) {
auto dstDesc = originalWeightsDesc->cloneWithNewPrecision(memory.at(ARG_SRC)->getDescPtr()->getPrecision());
auto dnnlDstDesc = MemoryDescUtils::convertToDnnlMemoryDesc(dstDesc);
- packedWeights = acl_fc_executor::reorderWeights(memory, context, aclfcAttrs, dnnlSrcDesc, dnnlDstDesc);
+ if (!attrs.weightsNonTransposed) {
+ dnnlDstDesc = acl_fc_executor::makeTransposedWeightDescriptor(dnnlDstDesc, dnnlSrcDesc);
+ aclfcAttrs.isWeightsRepacked = true;
+ }
+ MemoryCPtr packedWeights =
+ acl_fc_executor::reorderWeights(memory, context, aclfcAttrs, dnnlSrcDesc, dnnlDstDesc);
const size_t rhsPackedSize = kai_get_rhs_packed_size_rhs_pack_kxn_f32p8x1biasf32_f32_f32_neon(N, K);
auto rhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(u8, Shape({rhsPackedSize}));
@@ -218,33 +229,62 @@ MatMulKleidiAIExecutor::MatMulKleidiAIExecutor(const FCAttrs& attrs,
0,
&params);
}
- // Create scratchpad to initialize memory for LHS in update()
- scratchPad = context->getScratchPad();
}
+ // Create scratchpad to initialize memory for LHS in update()
+ scratchPad = context->getScratchPad();
+}
+
+void MatMulKleidiAIExecutor::setKaiExecutorImplAsGatherMatmul() {
+ KaiExecutorImpl = IMPL_TYPE::GatherMatmul;
+}
+
+void MatMulKleidiAIExecutor::set_gather_idx(const std::vector<std::pair<int32_t, int32_t>>& idxMap) {
+ OPENVINO_ASSERT(KaiExecutorImpl == IMPL_TYPE::GatherMatmul,
+ "gather_idx is supported only for GatherMatmul Implementation");
+ gather_idx = idxMap;
}
bool MatMulKleidiAIExecutor::update(const MemoryArgs& memory) {
const auto& weiDesc = memory.at(ARG_WEI)->getDescPtr();
+ const auto& srcDesc = memory.at(ARG_SRC)->getDescPtr();
const auto& dstDesc = memory.at(ARG_DST)->getDescPtr();
const auto& wgtDims = weiDesc->getShape().getStaticDims();
// Weights are transposed by MatMulConstTransposesExtraction
// K is the IC of weight
// the weight is reshaped to [-1, K] in ConvertMatMulToFC
+ OPENVINO_ASSERT(wgtDims.size() == 2, "Weights Shape must be 2D");
K = wgtDims[1];
N = wgtDims[0];
const auto& outDims = dstDesc->getShape().getStaticDims();
- if (outDims.size() > 2) {
+ if (KaiExecutorImpl == IMPL_TYPE::GatherMatmul && outDims.size() == 3) {
+ M = outDims[1];
+ } else if (outDims.size() > 2) {
M = std::accumulate(outDims.begin(), outDims.end() - 1, 1, std::multiplies<>());
} else {
M = outDims[0];
}
// Assign LHS memory
+ size_t totalScratchpadSize = 0;
+ if (KaiExecutorImpl == IMPL_TYPE::GatherMatmul) {
+ const auto& creatorsMap = BlockedDescCreator::getCommonCreators();
+ const auto srcPrc = srcDesc->getPrecision();
+ const auto dstPrc = dstDesc->getPrecision();
+ m_tmpInputDesc = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(srcPrc, Shape({M, K}));
+ m_tmpOutputDesc = creatorsMap.at(LayoutType::ncsp)->createSharedDesc(dstPrc, Shape({M, N}));
+ auto srcSize = rnd_up(m_tmpInputDesc->getCurrentMemSize(), 64); // 64 bytes is the cache line size
+ auto dstSize = rnd_up(m_tmpOutputDesc->getCurrentMemSize(), 64);
+ totalScratchpadSize = srcSize + dstSize;
+ }
if (useDynamicQuant) {
const size_t _m_blocks = (M + BLOCK_SIZE_M_LOWP - 1) / BLOCK_SIZE_M_LOWP;
packedlhs_block_in_bytes = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32(BLOCK_SIZE_M_LOWP, K, mr, kr, sr);
- const size_t lhsPackedSize = packedlhs_block_in_bytes * _m_blocks;
- auto lhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(i8, Shape({lhsPackedSize}));
+ lhsPackedSize = packedlhs_block_in_bytes * _m_blocks;
+ lhsPackedSize = rnd_up(lhsPackedSize, 64);
+ totalScratchpadSize += lhsPackedSize;
+ }
+ if (totalScratchpadSize > 0) {
+ auto lhsPackedDesc = std::make_shared<CpuBlockedMemoryDesc>(i8, Shape({totalScratchpadSize}));
lhsPackedMem = scratchPad->createScratchPadMem(lhsPackedDesc);
}
return true;
@@ -255,12 +295,39 @@ void MatMulKleidiAIExecutor::execute(const MemoryArgs& memory) {
auto srcMem = memory.at(ARG_SRC);
auto weiMem = memory.at(ARG_WEI);
auto dstMem = memory.at(ARG_DST);
- auto srcDims = normalizeDimsTo2D(srcMem->getDesc().getShape().getDims());
+ auto srcDims = srcMem->getDesc().getShape().getDims();
auto weiDims = weiMem->getDesc().getShape().getDims();
- auto M = srcDims[0];
- auto K = srcDims[1];
- auto N = weiDims[0];
-
+ size_t M = 0, K = 0, N = 0;
+ if (KaiExecutorImpl == IMPL_TYPE::GatherMatmul) {
+ OPENVINO_ASSERT(!gather_idx.empty(), "gather_idx is not set");
+ M = srcDims[1];
+ K = srcDims[2];
+ N = weiDims[0];
+ const auto element_size = m_tmpInputDesc->getPrecision().size();
+ auto* input_ptr = lhsPackedMem->getDataAs<uint8_t>() + lhsPackedSize;
+ auto* output_ptr =
+ input_ptr + rnd_up(m_tmpInputDesc->getCurrentMemSize(), 64); // 64 bytes is the cache line size
+ auto tmpInput = std::make_shared<Memory>(executorContext->getEngine(), m_tmpInputDesc, input_ptr);
+ auto tmpOutput = std::make_shared<Memory>(executorContext->getEngine(), m_tmpOutputDesc, output_ptr);
+ auto src_offset = OffsetHelper::createOffsetHelper(*srcMem);
+ auto tmp_input_offset = OffsetHelper::createOffsetHelper(*tmpInput);
+ cpu_parallel->parallel_for(gather_idx.size(), [&](size_t m) {
+ auto* dst_row = tmp_input_offset(m);
+ const auto row_id = gather_idx[m].first;
+ const auto batch_index = gather_idx[m].second;
+ const auto* src_data = src_offset(batch_index, row_id);
+ std::memcpy(dst_row, src_data, K * element_size);
+ });
+ // update M
+ M = gather_idx.size();
+ srcMem = tmpInput;
+ dstMem = tmpOutput;
+ } else {
+ srcDims = normalizeDimsTo2D(srcDims);
+ M = srcDims[0];
+ K = srcDims[1];
+ N = weiDims[0];
+ }
const size_t lhs_stride = K * sizeof(float);
const size_t dst_stride_row = N * sizeof(float);
const size_t dst_stride_col = sizeof(float);
@@ -384,6 +451,25 @@ void MatMulKleidiAIExecutor::execute(const MemoryArgs& memory) {
});
}
}
+
+ if (KaiExecutorImpl == IMPL_TYPE::GatherMatmul) {
+ dstMem = memory.at(ARG_DST);
+ const auto element_size = m_tmpInputDesc->getPrecision().size();
+ auto* input_ptr = lhsPackedMem->getDataAs<uint8_t>() + lhsPackedSize;
+ auto* output_ptr =
+ input_ptr + rnd_up(m_tmpInputDesc->getCurrentMemSize(), 64); // 64 bytes is the cache line size
+ auto tmpInput = std::make_shared<Memory>(executorContext->getEngine(), m_tmpInputDesc, input_ptr);
+ auto tmpOutput = std::make_shared<Memory>(executorContext->getEngine(), m_tmpOutputDesc, output_ptr);
+ auto dst_offset = OffsetHelper::createOffsetHelper(dstMem);
+ auto tmp_dst_offset = OffsetHelper::createOffsetHelper(tmpOutput);
+ cpu_parallel->parallel_for(gather_idx.size(), [&](size_t m) {
+ const auto* src_row = tmp_dst_offset(m);
+ const auto row_id = gather_idx[m].first;
+ const auto batch_index = gather_idx[m].second;
+ auto* dst_row = dst_offset(batch_index, row_id);
+ std::memcpy(dst_row, src_row, N * element_size);
+ });
+ }
}
void MatMulKleidiAIExecutor::moveMemToNumaNode([[maybe_unused]] int numaNodeID) {
diff --git a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp
index ea87d94b61c..f4ccb288360 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/kleidiai/kleidiai_mm.hpp
@@ -30,7 +30,6 @@
#include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4cxp_qs4cxs1s0.h"
namespace ov::intel_cpu {
-
class MatMulKleidiAIExecutor : public Executor {
public:
MatMulKleidiAIExecutor(const FCAttrs& attrs, const MemoryArgs& memory, const ExecutorContext::CPtr& context);
@@ -48,6 +47,9 @@ public:
void moveMemToNumaNode(int numaNodeID) override;
+ void setKaiExecutorImplAsGatherMatmul();
+ void set_gather_idx(const std::vector<std::pair<int32_t, int32_t>>& idxMap);
+
private:
static constexpr kai_matmul_clamp_f32_f32_f32p_ukernel ukernel_f32{
kai_get_m_step_matmul_clamp_f32_f32_f32p8x1biasf32_6x8x4_neon_mla,
@@ -109,12 +111,21 @@ private:
kai_get_dst_size_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm,
kai_run_matmul_clamp_f32_qai8dxp4x8_qsi4cxp8x8_8x8x32_neon_i8mm};
+ // IMPL_TYPE :: Default
+ // [M, K] * [N, K] -> [M, N]
+ // IMPL_TYPE :: GatherMatmul
+ // [B, M, K] -> gather -> [M', K] * [N', K] -> scatter -> [B, N, K]
+ enum class IMPL_TYPE : uint8_t { Default, GatherMatmul };
DnnlScratchPadPtr scratchPad;
+ IMPL_TYPE KaiExecutorImpl = IMPL_TYPE::Default;
+ std::vector<std::pair<int32_t, int32_t>> gather_idx;
+ MemoryDescPtr m_tmpInputDesc = nullptr;
+ MemoryDescPtr m_tmpOutputDesc = nullptr;
+ size_t lhsPackedSize = 0;
ACLFCAttrs aclfcAttrs;
MemoryPtr biasMem;
MemoryPtr rhsPackedMem;
MemoryPtr lhsPackedMem;
- MemoryCPtr packedWeights;
size_t M = 0UL, N = 0UL, K = 0UL;
size_t mr, nr, kr, sr;
// F32 Kernel block size
diff --git a/src/plugins/intel_cpu/src/nodes/gathermatmul.cpp b/src/plugins/intel_cpu/src/nodes/gathermatmul.cpp
index e764b9784bb..44cebf243fd 100644
--- a/src/plugins/intel_cpu/src/nodes/gathermatmul.cpp
+++ b/src/plugins/intel_cpu/src/nodes/gathermatmul.cpp
@@ -16,6 +16,12 @@
#if defined(OPENVINO_ARCH_X86) || defined(OPENVINO_ARCH_X86_64)
# include "cpu/x64/cpu_isa_traits.hpp"
#endif
+#if defined(OPENVINO_ARCH_ARM64)
+# include <cstdint>
+# include <limits>
+
+# include "utils/precision_support.h"
+#endif
#include "graph_context.h"
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/cpu_memory_desc_utils.h"
@@ -130,6 +136,31 @@ bool GatherMatmul::isSupportedCompressedOperation([[maybe_unused]] const std::sh
return false;
}
return true;
+#elif defined(OPENVINO_ARCH_ARM64)
+ try {
+ std::string errorMessage;
+ if (!isSupportedOperation(op, errorMessage)) {
+ return false;
+ }
+ if (!hasIntDotProductSupport() && !hasInt8MMSupport()) {
+ return false;
+ }
+ if (config.fcDynamicQuantizationGroupSize != std::numeric_limits<uint64_t>::max()) {
+ return false;
+ }
+ // supports only symmetric quantization, only scales should be present.
+ if (op->get_input_size() > WEIGHT_SCALES &&
+ op->input(WEIGHT_SCALES).get_element_type() == ov::element::dynamic) {
+ return false;
+ }
+ if (op->get_input_size() > WEIGHT_ZERO_POINTS &&
+ op->input(WEIGHT_ZERO_POINTS).get_element_type() != ov::element::dynamic) {
+ return false;
+ }
+ } catch (...) {
+ return false;
+ }
+ return true;
#else
return false;
#endif
@@ -139,6 +170,8 @@ ov::element::TypeVector GatherMatmul::getSupportedCompressedWeightsTypes([[maybe
using ov::element::Type_t;
#ifdef OPENVINO_ARCH_X86_64
return {Type_t::u8, Type_t::i8, Type_t::u4, Type_t::i4};
+#elif defined(OPENVINO_ARCH_ARM64)
+ return {Type_t::i8, Type_t::i4};
#else
return {};
#endif
@@ -262,4 +295,4 @@ bool GatherMatmul::created() const {
return getType() == Type::GatherMatmul;
}
-} // namespace ov::intel_cpu::node
+} // namespace ov::intel_cpu::node
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/gathermatmul.h b/src/plugins/intel_cpu/src/nodes/gathermatmul.h
index cf941e89cef..1513ee600b0 100644
--- a/src/plugins/intel_cpu/src/nodes/gathermatmul.h
+++ b/src/plugins/intel_cpu/src/nodes/gathermatmul.h
@@ -65,4 +65,4 @@ private:
std::unordered_map<int, int> m_atoi; // executor arg-id → input port mapping
};
-} // namespace ov::intel_cpu::node
+} // namespace ov::intel_cpu::node
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
index c50d333be81..41ddec85f22 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/convert_to_cpu_specific_opset.hpp
@@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
//
+#pragma once
+
#include <cstddef>
#include <memory>
@@ -57,6 +59,19 @@ inline void ConvertToCPUSpecificOpset(std::shared_ptr<ov::Model>& model, const C
size_t G) {
return ov::intel_cpu::node::GatherMatmul::isSupportedCompressedOperation(gather_matmul, IC, OC, G, config);
});
+ CPU_REGISTER_PASS_ARM64(manager, ov::pass::ConvertTiledMoeBlockToGatherMatmuls);
+ CPU_REGISTER_PASS_ARM64(manager, ov::pass::Validate);
+ CPU_REGISTER_PASS_ARM64(
+ manager,
+ ov::pass::ConvertGatherMatmulToGatherMatmulCompressed,
+ ov::intel_cpu::node::GatherMatmul::getSupportedCompressedActivationsTypes(),
+ ov::intel_cpu::node::GatherMatmul::getSupportedCompressedWeightsTypes(),
+ [&](const std::shared_ptr<ov::op::internal::GatherMatmulCompressed>& gather_matmul,
+ size_t IC,
+ size_t OC,
+ size_t G) {
+ return ov::intel_cpu::node::GatherMatmul::isSupportedCompressedOperation(gather_matmul, IC, OC, G, config);
+ });
CPU_REGISTER_PASS_COMMON(manager, ConvertMatMulToFC);
CPU_REGISTER_PASS_COMMON(manager, FullyConnectedBiasFusion);
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/gathermatmul.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/gathermatmul.cpp
new file mode 100644
index 00000000000..cfdc4c5028b
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/arm/gathermatmul.cpp
@@ -0,0 +1,88 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "custom/subgraph_tests/src/classes/moe.hpp"
+#include "utils/cpu_test_utils.hpp"
+using namespace CPUTestUtils;
+namespace ov {
+namespace test {
+namespace {
+const std::vector<MoEType> moe_types = {MoEType::MoE2GeMM, MoEType::MoE3GeMM};
+
+const std::vector<MoeTestShapeParams> moe_params_smoke = {
+ {
+ {{-1, -1, 256}, {{2, 15, 256}, {2, 1, 256}, {3, 8, 256}}}, // data_shape,
+ // seq_len=dynamic, hidden_size=256
+ 4, // topk
+ 8, // number_of_experts
+ 512 // intermediate_size
+ },
+ {
+ {{-1, -1, 128}, {{1, 32, 128}, {1, 1, 128}, {1, 16, 128}}}, // Different seq length
+ 2, // topk
+ 4, // number_of_experts
+ 256 // intermediate_size
+ },
+};
+
+
+std::vector<ov::AnyMap> generate_additional_config() {
+ std::vector<ov::AnyMap> additional_config = {{{ov::hint::dynamic_quantization_group_size.name(), std::numeric_limits<uint64_t>::max()}}};
+ return additional_config;
+}
+} // namespace
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_basic,
+ MoESubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::ValuesIn(moe_types),
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::ValuesIn(generate_additional_config())),
+ MoESubgraphTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_3gemm_gelu,
+ MoESubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::Values(MoEType::MoE3GeMM),
+ ::testing::Values(MoEActivationType::GELU),
+ ::testing::ValuesIn(generate_additional_config())),
+ MoESubgraphTest::getTestCaseName);
+
+const std::vector<ov::test::ElementType> decompression_precisions = {ov::element::f32};
+const std::vector<ov::test::ElementType> weights_precisions = {ov::element::i8, ov::element::i4};
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights,
+ MoECompressedWeightsSubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::ValuesIn(moe_types),
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::empty),
+ ::testing::Values(false), // reshape on decompression
+ ::testing::Values(-1), // decompression group size
+ ::testing::ValuesIn(generate_additional_config()),
+ ::testing::Values(true)), // use_matmul_decompression_impl
+ MoECompressedWeightsSubgraphTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights_3gemm_gelu,
+ MoECompressedWeightsSubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::Values(MoEType::MoE3GeMM),
+ ::testing::Values(MoEActivationType::GELU),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::empty),
+ ::testing::Values(false), // reshape on decompression
+ ::testing::Values(-1),
+ ::testing::ValuesIn(generate_additional_config()),
+ ::testing::Values(true)), // use_matmul_decompression_impl
+ MoECompressedWeightsSubgraphTest::getTestCaseName);
+
+} // namespace test
+} // namespace ov
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.cpp
new file mode 100644
index 00000000000..77a8ae3e09a
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.cpp
@@ -0,0 +1,293 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <memory>
+#include <sstream>
+#include <vector>
+
+#include "common_test_utils/node_builders/moe_builders.hpp"
+#include "common_test_utils/subgraph_builders/weights_decompression_builders.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "shared_test_classes/subgraph/weights_decompression_params.hpp"
+#include "utils/cpu_test_utils.hpp"
+#include "utils/fusing_test_utils.hpp"
+#include "moe.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov::test {
+
+inline std::ostream& operator<<(std::ostream& os, const MoEType& type) {
+ switch (type) {
+ case MoEType::MoE2GeMM:
+ return os << "MoE2GeMM";
+ case MoEType::MoE3GeMM:
+ return os << "MoE3GeMM";
+ default:
+ OPENVINO_THROW("Unsupported MoEType");
+ }
+}
+
+inline std::ostream& operator<<(std::ostream& os, const MoEActivationType& type) {
+ switch (type) {
+ case MoEActivationType::SWISH:
+ return os << "SWISH";
+ case MoEActivationType::GELU:
+ return os << "GELU";
+ default:
+ OPENVINO_THROW("Unsupported MoEActivationType");
+ }
+}
+
+std::string MoESubgraphTest::generateBaseMoeTestName(const MoeTestShapeParams& moe_params,
+ const MoEType& moe_type,
+ const ov::AnyMap& additional_config) {
+ std::ostringstream result;
+ result << "IS=" << ov::test::utils::partialShape2str({moe_params.data_shape.first}) << "_";
+ result << "TS=";
+ for (const auto& static_shape : moe_params.data_shape.second) {
+ result << ov::test::utils::vec2str(static_shape) << ",";
+ }
+ result << "top_k_experts=" << moe_params.topk << "_";
+ result << "total_experts=" << moe_params.number_of_experts << "_";
+ result << "intermediate_size=" << moe_params.intermediate_size << "_";
+ result << "moe_type=" << moe_type << "_";
+
+ result << "config=(";
+ for (const auto& configEntry : additional_config) {
+ result << configEntry.first << "=" << configEntry.second.as<std::string>() << "_";
+ }
+ result << ")";
+
+ return result.str();
+}
+
+std::string MoESubgraphTest::getTestCaseName(const testing::TestParamInfo<MoeTestParams>& obj) {
+ const auto& [moe_params, moe_type, activation_type, additional_config] = obj.param;
+ std::ostringstream result;
+ result << generateBaseMoeTestName(moe_params, moe_type, additional_config);
+ result << "_act=" << activation_type;
+ return result.str();
+}
+
+size_t MoESubgraphTest::get_expected_gather_mm_count(MoEType moe_type) {
+ switch (moe_type) {
+ case MoEType::MoE2GeMM:
+ return 2;
+ case MoEType::MoE3GeMM:
+ return 3;
+ default:
+ OPENVINO_THROW("Unsupported MoEType");
+ }
+}
+
+std::set<std::shared_ptr<ov::Node>> MoESubgraphTest::get_gather_mm_nodes(const std::shared_ptr<const ov::Model>& model,
+ MoEType moe_type) {
+ const std::string expected_gather_mm_type = "GatherMatmul";
+ std::set<std::shared_ptr<ov::Node>> gather_mm_nodes;
+ for (const auto& node : model->get_ordered_ops()) {
+ if (node->get_rt_info().at(ov::exec_model_info::LAYER_TYPE).as<std::string>() == expected_gather_mm_type) {
+ gather_mm_nodes.insert(node);
+ }
+ }
+ return gather_mm_nodes;
+}
+
+void MoESubgraphTest::SetUp() {
+ targetDevice = ov::test::utils::DEVICE_CPU;
+ const auto& [moe_params, moe_type, activation_type, additional_config] = GetParam();
+
+ configuration.insert(additional_config.begin(), additional_config.end());
+ init_input_shapes({moe_params.data_shape});
+ const MoePatternParams shape_params{moe_params.data_shape.first,
+ moe_params.topk,
+ moe_params.number_of_experts,
+ moe_params.intermediate_size};
+ inType = outType = ov::element::f32;
+
+ auto itr = configuration.find(ov::hint::inference_precision.name());
+ if (itr != configuration.end() && itr->second == ov::element::bf16) {
+ rel_threshold = 0.1f;
+ abs_threshold = 0.1f;
+ inType = outType = ov::element::bf16;
+ }
+
+ if (moe_type == MoEType::MoE2GeMM) {
+ ASSERT_TRUE(activation_type == MoEActivationType::SWISH) << "MoE2GeMM only supports SWISH activation";
+ function = initMoE2GeMMSubgraph(shape_params, ov::element::f32, ov::element::f32);
+ } else if (moe_type == MoEType::MoE3GeMM) {
+ function = initMoE3GeMMSubgraph(shape_params,
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ std::nullopt,
+ std::nullopt,
+ std::nullopt,
+ std::nullopt,
+ std::nullopt,
+ std::nullopt,
+ MoERoutingType::SOFTMAX,
+ activation_type);
+ } else {
+ OPENVINO_THROW("Unsupported MoEType");
+ }
+}
+
+void MoESubgraphTest::check_results() {
+ const auto& moe_type = std::get<1>(GetParam());
+ const auto& gather_mm_nodes = get_gather_mm_nodes(compiledModel.get_runtime_model(), moe_type);
+ const size_t expected_gather_mm_count = get_expected_gather_mm_count(moe_type);
+ EXPECT_EQ(gather_mm_nodes.size(), expected_gather_mm_count);
+}
+
+std::string MoECompressedWeightsSubgraphTest::getTestCaseName(const testing::TestParamInfo<MoeCompressedWeightsTestParams>& obj) {
+ const auto& [moe_params,
+ moe_type,
+ activation_type,
+ weights_precision,
+ decompression_precision,
+ scale_precision,
+ decompression_multiply_type,
+ decompression_subtract_type,
+ reshape_on_decompression,
+ decompression_group_size,
+ additional_config,
+ use_matmul_decompression_impl] = obj.param;
+ std::ostringstream result;
+ result << MoESubgraphTest::generateBaseMoeTestName(moe_params, moe_type, additional_config) << "_";
+ result << "act=" << activation_type << "_";
+ result << "_WP=" << weights_precision << "_";
+ result << "DP=" << decompression_precision << "_";
+ result << "SP=" << scale_precision << "_";
+ result << "DM=" << decompression_multiply_type << "_";
+ result << "DS=" << decompression_subtract_type << "_";
+ result << "RD=" << reshape_on_decompression << "_";
+ result << "GS=" << decompression_group_size << "_";
+ result << "use_matmul_decompression_impl=" << use_matmul_decompression_impl << "_";
+ return result.str();
+}
+
+void MoECompressedWeightsSubgraphTest::SetUp() {
+ targetDevice = ov::test::utils::DEVICE_CPU;
+
+ rel_threshold = 1e-3f;
+ abs_threshold = 1e-3f;
+
+ const auto& [moe_params,
+ moe_type,
+ activation_type,
+ weights_precision,
+ decompression_precision,
+ scale_precision,
+ decompression_multiply_type,
+ decompression_subtract_type,
+ reshape_on_decompression,
+ decompression_group_size,
+ additional_config,
+ use_matmul_decompression_impl] = GetParam();
+
+ configuration.insert(additional_config.begin(), additional_config.end());
+ init_input_shapes({moe_params.data_shape});
+ const MoePatternParams shape_params{moe_params.data_shape.first,
+ moe_params.topk,
+ moe_params.number_of_experts,
+ moe_params.intermediate_size};
+ inType = outType = ov::element::f32;
+
+ auto itr = configuration.find(ov::hint::inference_precision.name());
+ if (itr != configuration.end() && itr->second == ov::element::bf16) {
+ rel_threshold = 0.1f;
+ abs_threshold = 0.1f;
+ inType = outType = ov::element::bf16;
+ }
+ if(ov::with_cpu_arm_dotprod() || ov::with_cpu_arm_i8mm()){
+ rel_threshold = 0.05f;
+ abs_threshold = 0.05f;
+ }
+
+ if (moe_type == MoEType::MoE2GeMM) {
+ ASSERT_TRUE(activation_type == MoEActivationType::SWISH) << "MoE2GeMM only supports SWISH activation";
+ function = initMoE2GeMMSubgraph(shape_params,
+ ov::element::f32,
+ weights_precision,
+ true,
+ decompression_precision,
+ scale_precision,
+ decompression_multiply_type,
+ decompression_subtract_type,
+ reshape_on_decompression,
+ decompression_group_size);
+ } else if (moe_type == MoEType::MoE3GeMM) {
+ function = initMoE3GeMMSubgraph(shape_params,
+ ov::element::f32,
+ weights_precision,
+ true,
+ decompression_precision,
+ scale_precision,
+ decompression_multiply_type,
+ decompression_subtract_type,
+ reshape_on_decompression,
+ decompression_group_size,
+ MoERoutingType::SOFTMAX,
+ activation_type);
+ } else {
+ OPENVINO_THROW("Unsupported MoEType");
+ }
+}
+
+void MoECompressedWeightsSubgraphTest::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
+ inputs.clear();
+ const auto& itTargetShape = targetInputStaticShapes.front();
+ const auto& params = function->get_parameters();
+ ASSERT_EQ(params.size(), 1);
+ auto param = params.front();
+ auto type = param->get_element_type();
+
+ auto input_tensor =
+ ov::test::utils::create_and_fill_tensor(type,
+ itTargetShape,
+ ov::test::utils::InputGenerateData(0.125, 2, 8, 1234));
+
+ inputs.insert({param, input_tensor});
+}
+
+void MoECompressedWeightsSubgraphTest::check_results() {
+ const auto& test_param = GetParam();
+ const auto& moe_type = std::get<1>(GetParam());
+ const auto& gather_mm_nodes = MoESubgraphTest::get_gather_mm_nodes(compiledModel.get_runtime_model(), moe_type);
+ const size_t expected_gather_mm_count = MoESubgraphTest::get_expected_gather_mm_count(moe_type);
+ EXPECT_EQ(gather_mm_nodes.size(), expected_gather_mm_count);
+
+ const ov::element::Type compressed_weights_precision = std::get<3>(test_param);
+ const bool use_matmul_decompression_impl = std::get<11>(test_param);
+ for (const auto& gather_mm_node : gather_mm_nodes) {
+ const auto& expected_weights_precision = use_matmul_decompression_impl
+ ? compressed_weights_precision
+ : gather_mm_node->get_input_element_type(0);
+ EXPECT_EQ(gather_mm_node->get_input_element_type(1), expected_weights_precision);
+ }
+}
+
+TEST_P(MoESubgraphTest, CompareWithRefs) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ #if defined(OPENVINO_ARCH_ARM)
+ GTEST_SKIP();
+ #endif
+ run();
+ check_results();
+}
+
+TEST_P(MoECompressedWeightsSubgraphTest, CompareWithRefs) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ #if defined(OPENVINO_ARCH_ARM)
+ GTEST_SKIP();
+ #elif defined(OPENVINO_ARCH_ARM64)
+ if (!ov::with_cpu_arm_dotprod() && !ov::with_cpu_arm_i8mm()){
+ GTEST_SKIP();
+ }
+ #endif
+ run();
+ check_results();
+}
+} // namespace ov::test
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.hpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.hpp
new file mode 100644
index 00000000000..8d02bb0d193
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/classes/moe.hpp
@@ -0,0 +1,66 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "utils/cpu_test_utils.hpp"
+#include "common_test_utils/node_builders/moe_builders.hpp"
+#include "shared_test_classes/subgraph/weights_decompression_params.hpp"
+#include "utils/fusing_test_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov::test {
+enum class MoEType { MoE2GeMM, MoE3GeMM };
+
+struct MoeTestShapeParams {
+ InputShape data_shape;
+ size_t topk;
+ size_t number_of_experts;
+ size_t intermediate_size;
+};
+using MoeTestParams = std::tuple<MoeTestShapeParams,
+ MoEType, // MoE builder type
+ MoEActivationType, // gate activation type (3GeMM only)
+ ov::AnyMap>; // additional config
+
+using MoeCompressedWeightsTestParams = std::tuple<MoeTestShapeParams,
+ MoEType, // MoE builder type
+ MoEActivationType, // gate activation type (3GeMM only)
+ ov::test::ElementType, // weights precision
+ ov::test::ElementType, // decompression precision
+ ov::test::ElementType, // scale precision
+ ov::test::utils::DecompressionType, // decompression multiply type
+ ov::test::utils::DecompressionType, // decompression subtract type
+ bool, // reshape on decompression constants
+ int, // decompression_group_size
+ ov::AnyMap, // additional config
+ bool>; // use_matmul_decompression_impl
+class MoESubgraphTest : public testing::WithParamInterface<MoeTestParams>,
+ virtual public SubgraphBaseTest,
+ public CpuTestWithFusing {
+public:
+ static std::string generateBaseMoeTestName(const MoeTestShapeParams& moe_params,
+ const MoEType& moe_type,
+ const ov::AnyMap& additional_config);
+ static std::string getTestCaseName(const testing::TestParamInfo<MoeTestParams>& obj);
+ static size_t get_expected_gather_mm_count(MoEType moe_type);
+ static std::set<std::shared_ptr<ov::Node>> get_gather_mm_nodes(const std::shared_ptr<const ov::Model>& model,
+ MoEType moe_type);
+protected:
+ void SetUp() override;
+ void check_results();
+
+};
+class MoECompressedWeightsSubgraphTest : public testing::WithParamInterface<MoeCompressedWeightsTestParams>,
+ virtual public SubgraphBaseTest,
+ public CpuTestWithFusing {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<MoeCompressedWeightsTestParams>& obj);
+protected:
+ void SetUp() override;
+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
+ void check_results();
+};
+} // namespace ov::test
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/gathermatmul.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/gathermatmul.cpp
new file mode 100644
index 00000000000..6267ee56220
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/gathermatmul.cpp
@@ -0,0 +1,94 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "custom/subgraph_tests/src/classes/moe.hpp"
+
+using namespace CPUTestUtils;
+namespace ov {
+namespace test {
+namespace {
+const std::vector<MoEType> moe_types = {MoEType::MoE2GeMM, MoEType::MoE3GeMM};
+
+const std::vector<MoeTestShapeParams> moe_params_smoke = {
+ {
+ {{-1, -1, 256}, {{2, 15, 256}, {2, 1, 256}, {3, 8, 256}}}, // data_shape,
+ // seq_len=dynamic, hidden_size=256
+ 4, // topk
+ 8, // number_of_experts
+ 512 // intermediate_size
+ },
+ {
+ {{-1, -1, 128}, {{1, 32, 128}, {1, 1, 128}, {1, 16, 128}}}, // Different seq length
+ 2, // topk
+ 4, // number_of_experts
+ 256 // intermediate_size
+ },
+};
+
+std::vector<ov::AnyMap> generate_additional_config() {
+ std::vector<ov::AnyMap> additional_config = {{{ov::hint::inference_precision.name(), ov::element::f32}}};
+ if (ov::with_cpu_x86_bfloat16()) {
+ additional_config.push_back({{ov::hint::inference_precision.name(), ov::element::bf16}});
+ }
+ return additional_config;
+}
+
+} // namespace
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_basic,
+ MoESubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::ValuesIn(moe_types),
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::ValuesIn(generate_additional_config())),
+ MoESubgraphTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_3gemm_gelu,
+ MoESubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::Values(MoEType::MoE3GeMM),
+ ::testing::Values(MoEActivationType::GELU),
+ ::testing::ValuesIn(generate_additional_config())),
+ MoESubgraphTest::getTestCaseName);
+
+const std::vector<ov::test::ElementType> decompression_precisions = {ov::element::f32};
+const std::vector<ov::test::ElementType> weights_precisions = {ov::element::u8,
+ ov::element::i8,
+ ov::element::u4,
+ ov::element::i4};
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights,
+ MoECompressedWeightsSubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::ValuesIn(moe_types),
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(false), // reshape on decompression
+ ::testing::Values(16), // decompression group size
+ ::testing::ValuesIn(generate_additional_config()),
+ ::testing::Values(true)), // use_matmul_decompression_impl
+ MoECompressedWeightsSubgraphTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights_3gemm_gelu,
+ MoECompressedWeightsSubgraphTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::Values(MoEType::MoE3GeMM),
+ ::testing::Values(MoEActivationType::GELU),
+ ::testing::ValuesIn(weights_precisions),
+ ::testing::ValuesIn(decompression_precisions),
+ ::testing::Values(ov::element::f32),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(false), // reshape on decompression
+ ::testing::Values(16), // decompression group size
+ ::testing::ValuesIn(generate_additional_config()),
+ ::testing::Values(true)), // use_matmul_decompression_impl
+ MoECompressedWeightsSubgraphTest::getTestCaseName);
+
+} // namespace test
+} // namespace ov
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/moe.cpp b/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/moe.cpp
deleted file mode 100644
index 7a1e7e9da95..00000000000
--- a/src/plugins/intel_cpu/tests/functional/custom/subgraph_tests/src/x64/moe.cpp
+++ /dev/null
@@ -1,402 +0,0 @@
-// Copyright (C) 2018-2026 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include <gtest/gtest.h>
-
-#include <memory>
-#include <sstream>
-#include <vector>
-
-#include "common_test_utils/node_builders/moe_builders.hpp"
-#include "common_test_utils/subgraph_builders/weights_decompression_builders.hpp"
-#include "shared_test_classes/base/ov_subgraph.hpp"
-#include "shared_test_classes/subgraph/weights_decompression_params.hpp"
-#include "utils/cpu_test_utils.hpp"
-#include "utils/fusing_test_utils.hpp"
-
-using namespace CPUTestUtils;
-
-namespace ov::test {
-
-enum class MoEType { MoE2GeMM, MoE3GeMM };
-
-inline std::ostream& operator<<(std::ostream& os, const MoEType& type) {
- switch (type) {
- case MoEType::MoE2GeMM:
- return os << "MoE2GeMM";
- case MoEType::MoE3GeMM:
- return os << "MoE3GeMM";
- default:
- OPENVINO_THROW("Unsupported MoEType");
- }
-}
-
-inline std::ostream& operator<<(std::ostream& os, const MoEActivationType& type) {
- switch (type) {
- case MoEActivationType::SWISH:
- return os << "SWISH";
- case MoEActivationType::GELU:
- return os << "GELU";
- default:
- OPENVINO_THROW("Unsupported MoEActivationType");
- }
-}
-
-struct MoeTestShapeParams {
- InputShape data_shape;
- size_t topk;
- size_t number_of_experts;
- size_t intermediate_size;
-};
-
-using MoeTestParams = std::tuple<MoeTestShapeParams,
- MoEType, // MoE builder type
- MoEActivationType, // gate activation type (3GeMM only)
- ov::AnyMap>; // additional config
-
-using MoeCompressedWeightsTestParams = std::tuple<MoeTestShapeParams,
- MoEType, // MoE builder type
- MoEActivationType, // gate activation type (3GeMM only)
- ov::test::ElementType, // weights precision
- ov::test::ElementType, // decompression precision
- ov::test::ElementType, // scale precision
- ov::test::utils::DecompressionType, // decompression multiply type
- ov::test::utils::DecompressionType, // decompression subtract type
- bool, // reshape on decompression constants
- int, // decompression_group_size
- ov::AnyMap, // additional config
- bool>; // use_matmul_decompression_impl
-
-class MoESubgraphTest : public testing::WithParamInterface<MoeTestParams>,
- virtual public SubgraphBaseTest,
- public CpuTestWithFusing {
-public:
- static std::string generateBaseMoeTestName(const MoeTestShapeParams& moe_params,
- const MoEType& moe_type,
- const ov::AnyMap& additional_config) {
- std::ostringstream result;
- result << "IS=" << ov::test::utils::partialShape2str({moe_params.data_shape.first}) << "_";
- result << "TS=";
- for (const auto& static_shape : moe_params.data_shape.second) {
- result << ov::test::utils::vec2str(static_shape) << ",";
- }
- result << "top_k_experts=" << moe_params.topk << "_";
- result << "total_experts=" << moe_params.number_of_experts << "_";
- result << "intermediate_size=" << moe_params.intermediate_size << "_";
- result << "moe_type=" << moe_type << "_";
-
- result << "config=(";
- for (const auto& configEntry : additional_config) {
- result << configEntry.first << "=" << configEntry.second.as<std::string>() << "_";
- }
- result << ")";
-
- return result.str();
- }
- static std::string getTestCaseName(const testing::TestParamInfo<MoeTestParams>& obj) {
- const auto& [moe_params, moe_type, activation_type, additional_config] = obj.param;
- std::ostringstream result;
- result << generateBaseMoeTestName(moe_params, moe_type, additional_config);
- result << "_act=" << activation_type;
- return result.str();
- }
-
- static size_t get_expected_gather_mm_count(MoEType moe_type) {
- switch (moe_type) {
- case MoEType::MoE2GeMM:
- return 2;
- case MoEType::MoE3GeMM:
- return 3;
- default:
- OPENVINO_THROW("Unsupported MoEType");
- }
- }
-
- static std::set<std::shared_ptr<ov::Node>> get_gather_mm_nodes(const std::shared_ptr<const ov::Model>& model,
- MoEType moe_type) {
- const std::string expected_gather_mm_type = "GatherMatmul";
- std::set<std::shared_ptr<ov::Node>> gather_mm_nodes;
- for (const auto& node : model->get_ordered_ops()) {
- if (node->get_rt_info().at(ov::exec_model_info::LAYER_TYPE).as<std::string>() == expected_gather_mm_type) {
- gather_mm_nodes.insert(node);
- }
- }
- return gather_mm_nodes;
- }
-
-protected:
- void SetUp() override {
- targetDevice = ov::test::utils::DEVICE_CPU;
- const auto& [moe_params, moe_type, activation_type, additional_config] = GetParam();
-
- configuration.insert(additional_config.begin(), additional_config.end());
- init_input_shapes({moe_params.data_shape});
- const MoePatternParams shape_params{moe_params.data_shape.first,
- moe_params.topk,
- moe_params.number_of_experts,
- moe_params.intermediate_size};
- inType = outType = ov::element::f32;
-
- auto itr = configuration.find(ov::hint::inference_precision.name());
- if (itr != configuration.end() && itr->second == ov::element::bf16) {
- rel_threshold = 0.1f;
- abs_threshold = 0.1f;
- inType = outType = ov::element::bf16;
- }
-
- if (moe_type == MoEType::MoE2GeMM) {
- ASSERT_TRUE(activation_type == MoEActivationType::SWISH) << "MoE2GeMM only supports SWISH activation";
- function = initMoE2GeMMSubgraph(shape_params, ov::element::f32, ov::element::f32);
- } else if (moe_type == MoEType::MoE3GeMM) {
- function = initMoE3GeMMSubgraph(shape_params,
- ov::element::f32,
- ov::element::f32,
- false,
- std::nullopt,
- std::nullopt,
- std::nullopt,
- std::nullopt,
- std::nullopt,
- std::nullopt,
- MoERoutingType::SOFTMAX,
- activation_type);
- } else {
- OPENVINO_THROW("Unsupported MoEType");
- }
- }
-
- void check_results() {
- const auto& moe_type = std::get<1>(GetParam());
- const auto& gather_mm_nodes = get_gather_mm_nodes(compiledModel.get_runtime_model(), moe_type);
- const size_t expected_gather_mm_count = get_expected_gather_mm_count(moe_type);
- EXPECT_EQ(gather_mm_nodes.size(), expected_gather_mm_count);
- }
-};
-
-class MoECompressedWeightsSubgraphTest : public testing::WithParamInterface<MoeCompressedWeightsTestParams>,
- virtual public SubgraphBaseTest,
- public CpuTestWithFusing {
-public:
- static std::string getTestCaseName(const testing::TestParamInfo<MoeCompressedWeightsTestParams>& obj) {
- const auto& [moe_params,
- moe_type,
- activation_type,
- weights_precision,
- decompression_precision,
- scale_precision,
- decompression_multiply_type,
- decompression_subtract_type,
- reshape_on_decompression,
- decompression_group_size,
- additional_config,
- use_matmul_decompression_impl] = obj.param;
- std::ostringstream result;
- result << MoESubgraphTest::generateBaseMoeTestName(moe_params, moe_type, additional_config) << "_";
- result << "act=" << activation_type << "_";
- result << "_WP=" << weights_precision << "_";
- result << "DP=" << decompression_precision << "_";
- result << "SP=" << scale_precision << "_";
- result << "DM=" << decompression_multiply_type << "_";
- result << "DS=" << decompression_subtract_type << "_";
- result << "RD=" << reshape_on_decompression << "_";
- result << "GS=" << decompression_group_size << "_";
- result << "use_matmul_decompression_impl=" << use_matmul_decompression_impl << "_";
- return result.str();
- }
-
-protected:
- void SetUp() override {
- targetDevice = ov::test::utils::DEVICE_CPU;
-
- rel_threshold = 1e-3f;
- abs_threshold = 1e-3f;
-
- const auto& [moe_params,
- moe_type,
- activation_type,
- weights_precision,
- decompression_precision,
- scale_precision,
- decompression_multiply_type,
- decompression_subtract_type,
- reshape_on_decompression,
- decompression_group_size,
- additional_config,
- use_matmul_decompression_impl] = GetParam();
-
- configuration.insert(additional_config.begin(), additional_config.end());
- init_input_shapes({moe_params.data_shape});
- const MoePatternParams shape_params{moe_params.data_shape.first,
- moe_params.topk,
- moe_params.number_of_experts,
- moe_params.intermediate_size};
- inType = outType = ov::element::f32;
-
- auto itr = configuration.find(ov::hint::inference_precision.name());
- if (itr != configuration.end() && itr->second == ov::element::bf16) {
- rel_threshold = 0.1f;
- abs_threshold = 0.1f;
- inType = outType = ov::element::bf16;
- }
-
- if (moe_type == MoEType::MoE2GeMM) {
- ASSERT_TRUE(activation_type == MoEActivationType::SWISH) << "MoE2GeMM only supports SWISH activation";
- function = initMoE2GeMMSubgraph(shape_params,
- ov::element::f32,
- weights_precision,
- true,
- decompression_precision,
- scale_precision,
- decompression_multiply_type,
- decompression_subtract_type,
- reshape_on_decompression,
- decompression_group_size);
- } else if (moe_type == MoEType::MoE3GeMM) {
- function = initMoE3GeMMSubgraph(shape_params,
- ov::element::f32,
- weights_precision,
- true,
- decompression_precision,
- scale_precision,
- decompression_multiply_type,
- decompression_subtract_type,
- reshape_on_decompression,
- decompression_group_size,
- MoERoutingType::SOFTMAX,
- activation_type);
- } else {
- OPENVINO_THROW("Unsupported MoEType");
- }
- }
-
- void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {
- inputs.clear();
- const auto& itTargetShape = targetInputStaticShapes.front();
- const auto& params = function->get_parameters();
- ASSERT_EQ(params.size(), 1);
- auto param = params.front();
- auto type = param->get_element_type();
-
- auto input_tensor =
- ov::test::utils::create_and_fill_tensor(type,
- itTargetShape,
- ov::test::utils::InputGenerateData(0.125, 2, 8, 1234));
-
- inputs.insert({param, input_tensor});
- }
-
- void check_results() {
- const auto& test_param = GetParam();
- const auto& moe_type = std::get<1>(GetParam());
- const auto& gather_mm_nodes = MoESubgraphTest::get_gather_mm_nodes(compiledModel.get_runtime_model(), moe_type);
- const size_t expected_gather_mm_count = MoESubgraphTest::get_expected_gather_mm_count(moe_type);
- EXPECT_EQ(gather_mm_nodes.size(), expected_gather_mm_count);
-
- const ov::element::Type compressed_weights_precision = std::get<3>(test_param);
- const bool use_matmul_decompression_impl = std::get<11>(test_param);
- for (const auto& gather_mm_node : gather_mm_nodes) {
- const auto& expected_weights_precision = use_matmul_decompression_impl
- ? compressed_weights_precision
- : gather_mm_node->get_input_element_type(0);
- EXPECT_EQ(gather_mm_node->get_input_element_type(1), expected_weights_precision);
- }
- }
-};
-
-TEST_P(MoESubgraphTest, CompareWithRefs) {
- SKIP_IF_CURRENT_TEST_IS_DISABLED()
- run();
- check_results();
-}
-
-TEST_P(MoECompressedWeightsSubgraphTest, CompareWithRefs) {
- SKIP_IF_CURRENT_TEST_IS_DISABLED()
- run();
- check_results();
-}
-
-namespace {
-const std::vector<MoEType> moe_types = {MoEType::MoE2GeMM, MoEType::MoE3GeMM};
-
-const std::vector<MoeTestShapeParams> moe_params_smoke = {
- {
- {{-1, -1, 256}, {{2, 15, 256}, {2, 1, 256}, {3, 8, 256}}}, // data_shape,
- // seq_len=dynamic, hidden_size=256
- 4, // topk
- 8, // number_of_experts
- 512 // intermediate_size
- },
- {
- {{-1, -1, 128}, {{1, 32, 128}, {1, 1, 128}, {1, 16, 128}}}, // Different seq length
- 2, // topk
- 4, // number_of_experts
- 256 // intermediate_size
- },
-};
-
-std::vector<ov::AnyMap> generate_additional_config() {
- std::vector<ov::AnyMap> additional_config = {{{ov::hint::inference_precision.name(), ov::element::f32}}};
- if (ov::with_cpu_x86_bfloat16()) {
- additional_config.push_back({{ov::hint::inference_precision.name(), ov::element::bf16}});
- }
- return additional_config;
-}
-
-} // namespace
-
-INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_basic,
- MoESubgraphTest,
- ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
- ::testing::ValuesIn(moe_types),
- ::testing::Values(MoEActivationType::SWISH),
- ::testing::ValuesIn(generate_additional_config())),
- MoESubgraphTest::getTestCaseName);
-
-INSTANTIATE_TEST_SUITE_P(smoke_MoESubgraph_3gemm_gelu,
- MoESubgraphTest,
- ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
- ::testing::Values(MoEType::MoE3GeMM),
- ::testing::Values(MoEActivationType::GELU),
- ::testing::ValuesIn(generate_additional_config())),
- MoESubgraphTest::getTestCaseName);
-
-const std::vector<ov::test::ElementType> decompression_precisions = {ov::element::f32};
-const std::vector<ov::test::ElementType> weights_precisions = {ov::element::u8,
- ov::element::i8,
- ov::element::u4,
- ov::element::i4};
-
-INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights,
- MoECompressedWeightsSubgraphTest,
- ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
- ::testing::ValuesIn(moe_types),
- ::testing::Values(MoEActivationType::SWISH),
- ::testing::ValuesIn(weights_precisions),
- ::testing::ValuesIn(decompression_precisions),
- ::testing::Values(ov::element::f32),
- ::testing::Values(ov::test::utils::DecompressionType::full),
- ::testing::Values(ov::test::utils::DecompressionType::full),
- ::testing::Values(false), // reshape on decompression
- ::testing::Values(16), // decompression group size
- ::testing::ValuesIn(generate_additional_config()),
- ::testing::Values(true)), // use_matmul_decompression_impl
- MoECompressedWeightsSubgraphTest::getTestCaseName);
-
-INSTANTIATE_TEST_SUITE_P(smoke_MoeCompressedWeights_3gemm_gelu,
- MoECompressedWeightsSubgraphTest,
- ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
- ::testing::Values(MoEType::MoE3GeMM),
- ::testing::Values(MoEActivationType::GELU),
- ::testing::ValuesIn(weights_precisions),
- ::testing::ValuesIn(decompression_precisions),
- ::testing::Values(ov::element::f32),
- ::testing::Values(ov::test::utils::DecompressionType::full),
- ::testing::Values(ov::test::utils::DecompressionType::full),
- ::testing::Values(false), // reshape on decompression
- ::testing::Values(16), // decompression group size
- ::testing::ValuesIn(generate_additional_config()),
- ::testing::Values(true)), // use_matmul_decompression_impl
- MoECompressedWeightsSubgraphTest::getTestCaseName);
-
-} // namespace ov::test