Skip to content

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

[CPU]Optmize MatMul with small spatial dimension (#30634)

Chenhu Wangb54133a3b071
diff --git a/src/plugins/intel_cpu/src/nodes/executors/matmul.hpp b/src/plugins/intel_cpu/src/nodes/executors/matmul.hpp
new file mode 100644
index 00000000000..a725f88e936
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/matmul.hpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <oneapi/dnnl/dnnl.hpp>
+
+#include "cpu_memory.h"
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/common/dnnl_executor.h"
+
+namespace ov::intel_cpu {
+
+class IMatmulExecutor {
+public:
+ virtual void exec(const std::unordered_map<int, dnnl::memory>& primArgs, const dnnl::stream& strm) = 0;
+ [[nodiscard]] virtual DnnlMemoryDescPtr getScratchPadDesc() const = 0;
+ virtual ~IMatmulExecutor() = default;
+};
+
+class DnnlMatmulExecutor : public IMatmulExecutor {
+public:
+ DnnlMatmulExecutor(const dnnl::primitive_desc& pd) : m_executor(pd) {}
+ void exec(const std::unordered_map<int, dnnl::memory>& primArgs, const dnnl::stream& strm) override {
+ m_executor.exec(primArgs, strm);
+ }
+ DnnlMemoryDescPtr getScratchPadDesc() const override {
+ return m_executor.getScratchPadDesc();
+ }
+
+private:
+ DnnlExecutorLegacy m_executor;
+};
+
+} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.cpp b/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.cpp
new file mode 100644
index 00000000000..ca98038122c
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.cpp
@@ -0,0 +1,118 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "matmul_small.hpp"
+
+#include <oneapi/dnnl/dnnl_types.h>
+
+#include <common/primitive_attr.hpp>
+#include <cpu/x64/cpu_isa_traits.hpp>
+#include <cstddef>
+#include <functional>
+#include <memory>
+#include <numeric>
+#include <oneapi/dnnl/dnnl.hpp>
+#include <oneapi/dnnl/dnnl_common.hpp>
+#include <unordered_map>
+#include <utility>
+
+#include "dnnl_extension_utils.h"
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/kernels/x64/jit_matmul_small.hpp"
+#include "openvino/core/except.hpp"
+#include "openvino/core/parallel.hpp"
+
+using namespace dnnl;
+using namespace dnnl::impl;
+
+namespace ov::intel_cpu {
+
+MatMulSmallExecutor::MatMulSmallExecutor(MatMulSmallAttrs attrs) : m_matmul_attrs(std::move(attrs)) {
+ auto jcp = jit_matmul_small_config_params();
+ jcp.M = m_matmul_attrs.M;
+ jcp.N = m_matmul_attrs.N;
+ jcp.K = m_matmul_attrs.K;
+ if (mayiuse(cpu::x64::avx512_core)) {
+ m_matmul_kernel =
+ std::make_shared<jit_uni_matmul_small_kernel_f32<cpu::x64::avx512_core>>(jcp, *m_matmul_attrs.attr.get());
+ } else if (mayiuse(cpu::x64::avx2)) {
+ m_matmul_kernel =
+ std::make_shared<jit_uni_matmul_small_kernel_f32<cpu::x64::avx2>>(jcp, *m_matmul_attrs.attr.get());
+ } else if (mayiuse(cpu::x64::sse41)) {
+ m_matmul_kernel =
+ std::make_shared<jit_uni_matmul_small_kernel_f32<cpu::x64::sse41>>(jcp, *m_matmul_attrs.attr.get());
+ } else {
+ OPENVINO_THROW("Can't create jit jit_uni_matmul_small_kernel_f32 kernel");
+ }
+
+ if (m_matmul_kernel) {
+ m_matmul_kernel->create_ker();
+ }
+}
+
+void MatMulSmallExecutor::exec(const std::unordered_map<int, dnnl::memory>& primArgs,
+ [[maybe_unused]] const dnnl::stream& strm) {
+ const auto& in1 = primArgs.at(DNNL_ARG_SRC_0);
+ const auto& in2 = primArgs.at(DNNL_ARG_WEIGHTS_0);
+ const auto& out = primArgs.at(DNNL_ARG_DST);
+ // prepare_binary_args
+ if (!(*m_matmul_attrs.attr.get()).post_ops_.entry_.empty()) {
+ prepare_binary_args(primArgs);
+ }
+ const auto& src_shape = in1.get_desc().get_dims();
+ const auto& wei_shape = in2.get_desc().get_dims();
+ const auto* src_data = static_cast<float*>(in1.get_data_handle());
+ const auto* wei_data = static_cast<float*>(in2.get_data_handle());
+ auto* dst_data = static_cast<float*>(out.get_data_handle());
+ const auto& M = src_shape[src_shape.size() - 2];
+ const auto& K = src_shape[src_shape.size() - 1];
+ const auto& N = wei_shape[wei_shape.size() - 1];
+ const auto& out_shape = out.get_desc().get_dims();
+ const auto& OC = out_shape.size() >= 3 ? out_shape[out_shape.size() - 3] : 1;
+ const auto& src_spatial_size = M * K;
+ const auto& wei_spatial_size = K * N;
+ const auto& dst_spatial_size = M * N;
+ const size_t threads_num = parallel_get_max_threads();
+ const size_t wa =
+ std::accumulate(src_shape.begin(), src_shape.end() - 2, static_cast<size_t>(1), std::multiplies<>());
+ parallel_nt(threads_num, [&](const int ithr, [[maybe_unused]] const int nthr) {
+ size_t start = 0, end = 0;
+ splitter(wa, nthr, ithr, start, end);
+ jit_matmul_small_call_args args{};
+ args.input1 = src_data + start * src_spatial_size;
+ args.input2 = wei_data + start * wei_spatial_size;
+ args.output = dst_data + start * dst_spatial_size;
+ args.B = end - start;
+ args.oc_off = start % static_cast<size_t>(OC);
+ args.oc = OC;
+ args.post_op_data = reinterpret_cast<void*>(m_post_ops_args.data());
+ (*m_matmul_kernel)(&args);
+ });
+}
+
+// Extract post ops args from primArgs
+// comply Eltwise::appendPostOps and depthwise injector prepare_binary_args() on how to map
+void MatMulSmallExecutor::prepare_binary_args(const std::unordered_map<int, dnnl::memory>& primArgs) {
+ std::unordered_map<int, dnnl::memory> c_args = primArgs;
+ m_post_ops_args.clear();
+ const auto& attr = *m_matmul_attrs.attr.get();
+ const auto& post_ops = attr.post_ops_;
+ m_post_ops_args.reserve(post_ops.entry_.size());
+ unsigned idx = 0;
+ for (const auto& post_op : post_ops.entry_) {
+ if (post_op.is_binary() || post_op.is_depthwise() || post_op.is_quantization()) {
+ m_post_ops_args.emplace_back(
+ c_args[DNNL_ARG_ATTR_MULTIPLE_POST_OP(idx) | DNNL_ARG_SRC_1].get_data_handle());
+ }
+ ++idx;
+ }
+}
+
+DnnlMemoryDescPtr MatMulSmallExecutor::getScratchPadDesc() const {
+ dnnl::memory::dims dims;
+ dnnl::memory::desc zero_md{dims, dnnl::memory::data_type::u8, dnnl::memory::format_tag::undef};
+ return DnnlExtensionUtils::makeDescriptor(zero_md);
+}
+
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.hpp b/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.hpp
new file mode 100644
index 00000000000..0ef2bddf65c
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/executors/x64/matmul_small.hpp
@@ -0,0 +1,36 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <oneapi/dnnl/dnnl.hpp>
+
+#include "nodes/executors/matmul.hpp"
+#include "nodes/kernels/x64/jit_matmul_small.hpp"
+
+namespace ov::intel_cpu {
+
+struct MatMulSmallAttrs {
+ size_t M = 0UL;
+ size_t N = 0UL;
+ size_t K = 0UL;
+ dnnl::primitive_attr attr;
+};
+
+class MatMulSmallExecutor : public IMatmulExecutor {
+public:
+ MatMulSmallExecutor(MatMulSmallAttrs attrs);
+ void exec(const std::unordered_map<int, dnnl::memory>& primArgs, const dnnl::stream& strm) override;
+ [[nodiscard]] DnnlMemoryDescPtr getScratchPadDesc() const override;
+
+private:
+ // set post_ops_args based on primArgs and post_ops
+ void prepare_binary_args(const std::unordered_map<int, dnnl::memory>& primArgs);
+
+ MatMulSmallAttrs m_matmul_attrs;
+ std::shared_ptr<jit_uni_matmul_small_kernel> m_matmul_kernel;
+ std::vector<const void*> m_post_ops_args;
+};
+
+} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.cpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.cpp
new file mode 100644
index 00000000000..cabbb58cdc9
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.cpp
@@ -0,0 +1,199 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "jit_matmul_small.hpp"
+
+#include <cpu/x64/xbyak/xbyak.h>
+
+#include <cassert>
+#include <common/c_types_map.hpp>
+#include <cpu/x64/cpu_isa_traits.hpp>
+#include <cpu/x64/jit_generator.hpp>
+#include <cstddef>
+
+#include "cpu/x64/injectors/jit_uni_depthwise_injector.hpp"
+#include "cpu/x64/injectors/jit_uni_eltwise_injector.hpp"
+#include "cpu/x64/injectors/jit_uni_quantization_injector.hpp"
+#include "openvino/core/type/element_type.hpp"
+
+using namespace dnnl::impl;
+using namespace dnnl::impl::cpu::x64;
+using namespace dnnl::impl::utils;
+
+#define GET_OFF(field) offsetof(jit_matmul_small_call_args, field)
+
+namespace ov::intel_cpu {
+
+template <cpu::x64::cpu_isa_t isa>
+void jit_uni_matmul_small_kernel_f32<isa>::generate() {
+ const auto& p = attr_.post_ops_;
+ for (int i = 0; i < p.len(); i++) {
+ auto& post_op = p.entry_[i];
+ if (post_op.is_eltwise()) {
+ eltwise_injectors.push_back(std::make_shared<jit_uni_eltwise_injector<isa>>(this,
+ post_op.eltwise.alg,
+ post_op.eltwise.alpha,
+ post_op.eltwise.beta,
+ 1.f,
+ data_type::f32));
+ } else if (post_op.is_depthwise()) {
+ depthwise_injectors.push_back(std::make_shared<jit_uni_depthwise_injector_f32<isa>>(this, post_op));
+ } else if (post_op.is_quantization()) {
+ quantization_injectors.push_back(std::make_shared<jit_uni_quantization_injector_f32<isa>>(this,
+ post_op,
+ vmm_d_weights,
+ vmm_d_bias,
+ reg_d_weights,
+ reg_d_bias));
+ }
+ }
+
+ this->preamble();
+
+ mov(reg_input1, ptr[reg_params + GET_OFF(input1)]);
+ mov(reg_input2, ptr[reg_params + GET_OFF(input2)]);
+ mov(reg_out, ptr[reg_params + GET_OFF(output)]);
+ mov(reg_work_amount, ptr[reg_params + GET_OFF(B)]);
+ if (jcp_.M > 2 || jcp_.N > 2 || jcp_.K > 2) {
+ assert("matmul_small_kernel only support M/N/K smaller than 3.");
+ }
+
+ if (attr_.post_ops_.len() != 0) {
+ mov(reg_post_ops_data, ptr[reg_params + GET_OFF(post_op_data)]);
+ mov(reg_oc, ptr[reg_params + GET_OFF(oc)]);
+ mov(reg_oc_off, ptr[reg_params + GET_OFF(oc_off)]);
+ }
+
+ Xbyak::Label loop_label;
+ Xbyak::Label loop_end_label;
+ Xbyak::Label offset_reset_label;
+ L(loop_label);
+ {
+ cmp(reg_work_amount, 1);
+ jl(loop_end_label, T_NEAR);
+
+ // loop unrolling and register utilization in each batch
+ // load
+ for (size_t m = 0; m < jcp_.M; m++) {
+ for (size_t k = 0; k < jcp_.K; k++) {
+ uni_vmovss(vmm_input1[m * jcp_.K + k], ptr[reg_input1]);
+ add(reg_input1, sizeof(float));
+ }
+ }
+ for (size_t k = 0; k < jcp_.K; k++) {
+ for (size_t n = 0; n < jcp_.N; n++) {
+ uni_vmovss(vmm_input2[k * jcp_.N + n], ptr[reg_input2]);
+ add(reg_input2, sizeof(float));
+ }
+ }
+
+ for (size_t m = 0; m < jcp_.M; m++) {
+ for (size_t n = 0; n < jcp_.N; n++) {
+ uni_vpxor(vmm_output[m * jcp_.N + n], vmm_output[m * jcp_.N + n], vmm_output[m * jcp_.N + n]);
+ }
+ }
+ // outer most K to reduce RAW dependency.
+ for (size_t k = 0; k < jcp_.K; k++) {
+ for (size_t m = 0; m < jcp_.M; m++) {
+ for (size_t n = 0; n < jcp_.N; n++) {
+ uni_vfmadd231ps(vmm_output[m * jcp_.N + n], vmm_input1[m * jcp_.K + k], vmm_input2[k * jcp_.N + n]);
+ }
+ }
+ }
+
+ // store
+ for (size_t m = 0; m < jcp_.M; m++) {
+ for (size_t n = 0; n < jcp_.N; n++) {
+ if (attr_.post_ops_.len() != 0) {
+ apply_post_ops(element::f32, vmm_output[m * jcp_.N + n].getIdx(), true);
+ }
+ uni_vmovss(ptr[reg_out], vmm_output[m * jcp_.N + n]);
+ add(reg_out, sizeof(float));
+ }
+ }
+
+ if (attr_.post_ops_.len() != 0) {
+ add(reg_oc_off, 1);
+ cmp(reg_oc_off, reg_oc);
+ jl(offset_reset_label, T_NEAR);
+ mov(reg_oc_off, 0);
+ L(offset_reset_label);
+ }
+
+ sub(reg_work_amount, 1);
+ jmp(loop_label, T_NEAR);
+ }
+ L(loop_end_label);
+
+ this->postamble();
+
+ for (auto& inj : eltwise_injectors) {
+ inj->prepare_table();
+ }
+}
+
+// apply post ops on same channel. reg_oc_off is reset to start if reach oc.
+template <dnnl::impl::cpu::x64::cpu_isa_t isa>
+void jit_uni_matmul_small_kernel_f32<isa>::apply_post_ops(ov::element::Type dst_prc,
+ size_t vmm_idx,
+ bool is_broadcast) {
+ const auto& p = attr_.post_ops_;
+ int eltwise_inj_idx = 0;
+ int depthwise_inj_idx = 0;
+ int quantization_inj_idx = 0;
+ int post_ops_data_offset = 0;
+ for (int i = 0; i < p.len(); i++) {
+ auto& post_op = p.entry_[i];
+ if (post_op.is_eltwise()) {
+ eltwise_injectors[eltwise_inj_idx]->compute_vector_range(vmm_idx, vmm_idx + 1);
+ eltwise_inj_idx++;
+ } else if (post_op.is_depthwise()) {
+ mov(reg_d_weights, ptr[reg_post_ops_data + post_ops_data_offset]);
+ add(reg_d_weights, reg_oc_off);
+
+ // weight and bias is padded. scalar as vector.
+ depthwise_injectors[depthwise_inj_idx]->compute_vector_range(vmm_idx,
+ vmm_idx + 1,
+ reg_d_weights,
+ reg_d_weights,
+ is_broadcast);
+
+ post_ops_data_offset += depthwise_injectors[depthwise_inj_idx]->memoryStep();
+ depthwise_inj_idx++;
+ } else if (post_op.is_quantization()) {
+ bool do_dequantization = post_op.quantization.alg == alg_kind::quantization_quantize_dequantize;
+ bool do_rounding = do_dequantization || dst_prc == ov::element::f32 || i != p.len() - 1;
+
+ quantization_injectors[quantization_inj_idx]->init_crop_ptrs(reg_post_ops_data + post_ops_data_offset,
+ reg_oc_off);
+ quantization_injectors[quantization_inj_idx]->compute_crop(vmm_idx, vmm_idx + 1, 0, 0, is_broadcast);
+
+ quantization_injectors[quantization_inj_idx]->init_input_scale_shift_ptrs(
+ reg_post_ops_data + post_ops_data_offset,
+ reg_oc_off);
+ quantization_injectors[quantization_inj_idx]
+ ->compute_input_scale_shift(vmm_idx, vmm_idx + 1, 0, do_rounding, 0, is_broadcast);
+
+ if (do_dequantization) {
+ quantization_injectors[quantization_inj_idx]->init_output_scale_shift_ptrs(
+ reg_post_ops_data + post_ops_data_offset,
+ reg_oc_off);
+ quantization_injectors[quantization_inj_idx]->compute_output_scale_shift(vmm_idx,
+ vmm_idx + 1,
+ 0,
+ 0,
+ is_broadcast);
+ }
+
+ post_ops_data_offset += quantization_injectors[quantization_inj_idx]->memoryStep();
+ quantization_inj_idx++;
+ }
+ }
+}
+
+template struct jit_uni_matmul_small_kernel_f32<cpu::x64::sse41>;
+template struct jit_uni_matmul_small_kernel_f32<cpu::x64::avx2>;
+template struct jit_uni_matmul_small_kernel_f32<cpu::x64::avx512_core>;
+
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.hpp b/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.hpp
new file mode 100644
index 00000000000..3c2906e5653
--- /dev/null
+++ b/src/plugins/intel_cpu/src/nodes/kernels/x64/jit_matmul_small.hpp
@@ -0,0 +1,108 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cpu/x64/xbyak/xbyak.h>
+
+#include <cassert>
+#include <common/utils.hpp>
+#include <cstddef>
+#include <memory>
+
+#include "cpu/x64/cpu_isa_traits.hpp"
+#include "cpu/x64/injectors/jit_uni_depthwise_injector.hpp"
+#include "cpu/x64/injectors/jit_uni_eltwise_injector.hpp"
+#include "cpu/x64/injectors/jit_uni_quantization_injector.hpp"
+#include "cpu/x64/jit_generator.hpp"
+#include "openvino/core/type/element_type.hpp"
+
+namespace ov::intel_cpu {
+
+using namespace dnnl::impl::cpu::x64;
+
+struct jit_matmul_small_config_params {
+ size_t M = 0UL;
+ size_t K = 0UL;
+ size_t N = 0UL;
+};
+
+struct jit_matmul_small_call_args {
+ const void* input1;
+ const void* input2;
+ const void* post_op_data;
+ void* output;
+ size_t oc;
+ size_t oc_off;
+ size_t B;
+};
+
+struct jit_uni_matmul_small_kernel {
+ void (*ker_)(const jit_matmul_small_call_args*) = nullptr;
+
+ void operator()(const jit_matmul_small_call_args* args) const {
+ assert(ker_);
+ ker_(args);
+ }
+
+ explicit jit_uni_matmul_small_kernel(jit_matmul_small_config_params jcp, const dnnl_primitive_attr& attr)
+ : jcp_(jcp),
+ attr_(attr) {}
+ virtual ~jit_uni_matmul_small_kernel() = default;
+
+ virtual void create_ker() = 0;
+
+ jit_matmul_small_config_params jcp_;
+ const dnnl_primitive_attr& attr_;
+};
+
+template <dnnl::impl::cpu::x64::cpu_isa_t isa>
+struct jit_uni_matmul_small_kernel_f32 : public jit_uni_matmul_small_kernel,
+ public dnnl::impl::cpu::x64::jit_generator {
+ DECLARE_CPU_JIT_AUX_FUNCTIONS(jit_uni_matmul_small_kernel_f32)
+
+ explicit jit_uni_matmul_small_kernel_f32(jit_matmul_small_config_params jcp, const dnnl_primitive_attr& attr)
+ : jit_uni_matmul_small_kernel(jcp, attr),
+ jit_generator(jit_name()) {}
+
+ void create_ker() override {
+ jit_generator::create_kernel();
+ ker_ = (decltype(ker_))jit_ker();
+ }
+
+ void generate() override;
+
+private:
+ void apply_post_ops(ov::element::Type dst_prc, size_t vmm_idx, bool is_broadcast);
+ using Vmm = typename dnnl::impl::utils::conditional3<isa == dnnl::impl::cpu::x64::sse41,
+ Xbyak::Xmm,
+ isa == dnnl::impl::cpu::x64::avx2,
+ Xbyak::Ymm,
+ Xbyak::Zmm>::type;
+
+ const int vlen = dnnl::impl::cpu::x64::cpu_isa_traits<isa>::vlen;
+
+ Xbyak::Reg64 reg_input1 = r8;
+ Xbyak::Reg64 reg_input2 = r9;
+ Xbyak::Reg64 reg_post_ops_data = rbx;
+ Xbyak::Reg64 reg_out = r10;
+ Xbyak::Reg64 reg_work_amount = r11;
+ Xbyak::Reg64 reg_oc = r12;
+ Xbyak::Reg64 reg_oc_off = r13;
+ Xbyak::Reg64 reg_params = Xbyak::Reg64(dnnl::impl::cpu::x64::abi_param_regs[0]);
+ Xbyak::Reg64 reg_d_weights = r14;
+ Xbyak::Reg64 reg_d_bias = r15;
+
+ Vmm vmm_input1[4] = {Vmm(0), Vmm(1), Vmm(2), Vmm(3)};
+ Vmm vmm_input2[4] = {Vmm(4), Vmm(5), Vmm(6), Vmm(7)};
+ Vmm vmm_output[4] = {Vmm(8), Vmm(9), Vmm(10), Vmm(11)};
+ Vmm vmm_d_weights = Vmm(12);
+ Vmm vmm_d_bias = Vmm(13);
+
+ std::vector<std::shared_ptr<jit_uni_eltwise_injector<isa>>> eltwise_injectors;
+ std::vector<std::shared_ptr<jit_uni_depthwise_injector_f32<isa>>> depthwise_injectors;
+ std::vector<std::shared_ptr<jit_uni_quantization_injector_f32<isa>>> quantization_injectors;
+};
+
+} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/nodes/matmul.cpp b/src/plugins/intel_cpu/src/nodes/matmul.cpp
index 6d88c9c2906..4c98938e649 100644
--- a/src/plugins/intel_cpu/src/nodes/matmul.cpp
+++ b/src/plugins/intel_cpu/src/nodes/matmul.cpp
@@ -32,7 +32,7 @@
#include "memory_desc/dnnl_blocked_memory_desc.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "node.h"
-#include "nodes/common/dnnl_executor.h"
+#include "nodes/executors/matmul.hpp"
#include "nodes/node_config.h"
#include "onednn/iml_type_mapper.h"
#include "openvino/core/except.hpp"
@@ -43,6 +43,10 @@
#include "shape_inference/custom/matmul.hpp"
#include "utils/debug_capabilities.h"
#include "utils/general_utils.h"
+#ifdef OPENVINO_ARCH_X86_64
+# include "executors/x64/matmul_small.hpp"
+#endif
+
using namespace dnnl;
namespace ov::intel_cpu::node {
@@ -662,6 +666,48 @@ void MatMul::prepareParams() {
auto engine = getEngine();
auto builder = [&engine](const MatMulKey& key) -> executorPtr {
+#ifdef OPENVINO_ARCH_X86_64
+ const bool can_optimize = [&key]() -> bool {
+ const auto& prec_in0 = key.inp0->getDataType();
+ const auto& prec_in1 = key.inp1->getDataType();
+ const auto& prec_out = key.out->getDataType();
+ if (!everyone_is(dnnl::memory::data_type::f32, prec_in0, prec_in1, prec_out)) {
+ return false;
+ }
+ const auto& stride_in0 = key.inp0->getDnnlDesc().get_strides();
+ const auto& stride_in1 = key.inp1->getDnnlDesc().get_strides();
+ if (stride_in0.back() != 1 || stride_in1.back() != 1) {
+ return false;
+ }
+ if (key.bias != nullptr) {
+ return false;
+ }
+ const auto& shape_src = key.inp0->getShape().getStaticDims();
+ const auto& shape_wei = key.inp1->getShape().getStaticDims();
+ auto src_rank = shape_src.size();
+ auto wei_rank = shape_wei.size();
+ if (src_rank < 2 || src_rank != wei_rank) {
+ return false;
+ }
+ for (size_t i = 0; i < src_rank - 2; i++) {
+ if (shape_src[i] != shape_wei[i]) {
+ return false;
+ }
+ }
+ return (shape_src[src_rank - 1] <= 2) && (shape_src[src_rank - 2] <= 2) && (shape_wei[wei_rank - 1] <= 2) &&
+ (shape_wei[wei_rank - 2] <= 2);
+ }();
+ if (can_optimize) {
+ MatMulSmallAttrs matmul_attr;
+ const auto& shape_in0 = key.inp0->getShape().getStaticDims();
+ const auto& shape_in1 = key.inp1->getShape().getStaticDims();
+ matmul_attr.M = *++shape_in0.rbegin();
+ matmul_attr.K = *shape_in0.rbegin();
+ matmul_attr.N = *shape_in1.rbegin();
+ matmul_attr.attr = key.attr;
+ return std::make_shared<MatMulSmallExecutor>(matmul_attr);
+ }
+#endif
dnnl::matmul::primitive_desc prim_desc;
if (key.bias) {
@@ -683,14 +729,14 @@ void MatMul::prepareParams() {
const bool found = DnnlExtensionUtils::find_implementation(prim_desc, key.implType);
if (found) {
- return std::make_shared<DnnlExecutorLegacy>(prim_desc);
+ return std::make_shared<DnnlMatmulExecutor>(prim_desc);
}
// In case of dynamic shapes an implementation type chosen as optimal for a primitive_desc with
// undefined input shapes, is not necessarily available for the primitive_desc with defined shape.
// Example: brgemm_avx512_amx (Intel Sapphire Rapids Platform) is available for a primitive with
// undefined input shapes but not available for primitive_desc with input batch 1.
- return std::make_shared<DnnlExecutorLegacy>(first_desc);
+ return std::make_shared<DnnlMatmulExecutor>(first_desc);
};
auto cache = context->getParamsCache();
@@ -712,10 +758,6 @@ void MatMul::prepareParams() {
}
appendPostOpArgs(*attr, primArgs, postOpsArgs);
-#ifdef CPU_DEBUG_CAPS
- const auto* pd = execPtr->getPrimitiveDesc();
- DEBUG_LOG("verbose##", getName(), "##", DnnlExtensionUtils::query_pd_info(pd), "\n");
-#endif
}
void MatMul::execute(const dnnl::stream& strm) {
diff --git a/src/plugins/intel_cpu/src/nodes/matmul.h b/src/plugins/intel_cpu/src/nodes/matmul.h
index e649d146ba1..7ab5a8827db 100644
--- a/src/plugins/intel_cpu/src/nodes/matmul.h
+++ b/src/plugins/intel_cpu/src/nodes/matmul.h
@@ -14,6 +14,7 @@
#include "common/dnnl_executor.h"
#include "cpu_types.h"
+#include "executors/matmul.hpp"
#include "graph_context.h"
#include "memory_desc/cpu_memory_desc.h"
#include "memory_desc/dnnl_blocked_memory_desc.h"
@@ -63,7 +64,7 @@ protected:
AttrPtr initPrimitiveAttr(const VectorDims& dims);
private:
- using executorPtr = std::shared_ptr<DnnlExecutorLegacy>;
+ using executorPtr = std::shared_ptr<IMatmulExecutor>;
executorPtr execPtr = nullptr;
dnnl::memory::desc getBiasDescFrom(const DnnlMemoryDescCPtr& outMemDesc);
[[nodiscard]] std::pair<Shape, Shape> makeDummyInputShapes(const Shape& in0,
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/matmul.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/matmul.cpp
index aecb5b7bdb3..f33f061459d 100644
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/matmul.cpp
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/matmul.cpp
@@ -1168,6 +1168,54 @@ const auto notFuseTestParamsSmoke =
INSTANTIATE_TEST_SUITE_P(smoke_FC, FCNotFuseFQCPUTest, notFuseTestParamsSmoke, FCNotFuseFQCPUTest::getTestCaseName);
+// small matmul implementation test
+class SmallMatMulLayerCPUTest : public MatMulLayerCPUTest {
+ void SetUp() override {
+ MatMulLayerCPUTest::SetUp();
+ expectPostOpsToBeFused = true;
+ }
+};
+
+TEST_P(SmallMatMulLayerCPUTest, CompareWithRefs) {
+ run();
+}
+
+const std::vector<ShapeRelatedParams> IS_x64_small = {
+ {static_shapes_to_test_representation({{128, 2, 2}, {128, 2, 2}}), {false, false}},
+ {static_shapes_to_test_representation({{10, 64, 2, 2}, {10, 64, 2, 2}}), {false, false}},
+ {static_shapes_to_test_representation({{100, 1, 2, 2}, {100, 1, 2, 1}}), {false, false}},
+ {static_shapes_to_test_representation({{100, 2, 1, 2}, {100, 2, 2, 1}}), {false, false}},
+ {
+ {
+ {{-1, -1, -1, -1}, {{10, 10, 2, 2}, {2, 2, 1, 2}, {1, 10, 2, 2}}},
+ {{-1, -1, -1, -1}, {{10, 10, 2, 2}, {2, 2, 2, 2}, {1, 10, 2, 1}}}
+ },
+ {false, false}
+ },
+ {
+ {
+ {{-1, -1, -1, -1, -1}, {{128, 1, 1, 2, 2}, {1, 1, 64, 1, 2}, {10, 3, 3, 2, 1}}},
+ {{-1, -1, -1, -1, -1}, {{128, 1, 1, 2, 2}, {1, 1, 64, 2, 1}, {10, 3, 3, 1, 2}}}
+ },
+ {false, false}
+ }
+};
+
+const auto matMulParams_x64_small = ::testing::Combine(::testing::ValuesIn(IS_x64_small),
+ ::testing::Values(element::f32),
+ ::testing::Values(ElementType::f32),
+ ::testing::Values(ElementType::f32),
+ ::testing::Values(utils::InputLayerType::PARAMETER),
+ ::testing::Values(ov::test::utils::DEVICE_CPU),
+ ::testing::Values(ov::AnyMap{}));
+
+const auto testParams_IS_x64_small = ::testing::Combine(matMulParams_x64_small,
+ ::testing::Values(MatMulNodeType::MatMul),
+ ::testing::ValuesIn(matmulFusingParams()),
+ ::testing::Values(CPUSpecificParams()));
+
+INSTANTIATE_TEST_SUITE_P(smoke_MM_IS_x64_small, SmallMatMulLayerCPUTest, testParams_IS_x64_small, SmallMatMulLayerCPUTest::getTestCaseName);
+
} // namespace
} // namespace MatMul
} // namespace test
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/mat_mul.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/mat_mul.cpp
index d4e6a1ba189..f7027b129e6 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/mat_mul.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/mat_mul.cpp
@@ -32,6 +32,14 @@ std::vector<std::vector<ov::Shape>> input_shapes_no_transpose_static {
{ {1, 5}, {5} },
{ {5}, {5, 1} },
{ {5}, {5} },
+ { {1, 128, 1, 1}, {1, 128, 1, 1} },
+ { {1, 128, 1, 1}, {1, 128, 1, 2} },
+ { {1, 16, 1, 2}, {1, 16, 2, 1} },
+ { {1, 16, 1, 2}, {1, 16, 2, 2} },
+ { {2, 2, 2, 1}, {2, 2, 1, 1} },
+ { {2, 2, 2, 1}, {2, 2, 1, 2} },
+ { {8, 2, 2}, {8, 2, 1} },
+ { {2, 2}, {2, 2} },
};
std::vector<std::vector<ov::Shape>> input_shapes_first_transpose_static {