Skip to content

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

[Snippets][CPU] Used Plugin Reorder for weights repacking (#31024)

Alexandra Sidorova2e2acdfbcb57
diff --git a/src/common/snippets/src/pass/fuse_transpose_brgemm.cpp b/src/common/snippets/src/pass/fuse_transpose_brgemm.cpp
index 5990aa29b26..d1dc00246f6 100644
--- a/src/common/snippets/src/pass/fuse_transpose_brgemm.cpp
+++ b/src/common/snippets/src/pass/fuse_transpose_brgemm.cpp
@@ -32,27 +32,26 @@ bool FuseTransposeBrgemm::is_supported_transpose_order(const std::vector<int32_t
FuseTransposeBrgemm::FuseTransposeBrgemm() {
MATCHER_SCOPE(FuseTransposeBrgemm);
- auto constant = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();
- auto transpose = ov::pass::pattern::wrap_type<ov::op::v1::Transpose>({ov::pass::pattern::any_input(), constant},
- is_supported_transpose);
- auto transpose_matcher = std::make_shared<ov::pass::pattern::Matcher>(transpose);
+ auto m_constant = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();
+ auto m_transpose = ov::pass::pattern::wrap_type<ov::op::v1::Transpose>({ov::pass::pattern::any_input(), m_constant},
+ is_supported_transpose);
// Pattern 0: Transpose on 0-th input of MatMul
- auto brgemm_in0 = ov::pass::pattern::wrap_type<op::Brgemm>({transpose, ov::pass::pattern::any_input()});
+ auto m_brgemm_in0 = ov::pass::pattern::wrap_type<op::Brgemm>({m_transpose, ov::pass::pattern::any_input()});
// Pattern 1: Transpose on 1-st input of MatMul
- auto brgemm_in1 = ov::pass::pattern::wrap_type<op::Brgemm>({ov::pass::pattern::any_input(), transpose});
+ auto m_brgemm_in1 = ov::pass::pattern::wrap_type<op::Brgemm>({ov::pass::pattern::any_input(), m_transpose});
// Pattern 2: Transpose on output of MatMul
- auto brgemm_out =
+ auto m_brgemm_out =
ov::pass::pattern::wrap_type<op::Brgemm>({ov::pass::pattern::any_input(), ov::pass::pattern::any_input()});
- auto transpose2 =
- ov::pass::pattern::wrap_type<ov::op::v1::Transpose>({brgemm_out, constant}, is_supported_transpose);
+ auto m_transpose2 =
+ ov::pass::pattern::wrap_type<ov::op::v1::Transpose>({m_brgemm_out, m_constant}, is_supported_transpose);
- auto brgemm_or_transpose =
- std::make_shared<ov::pass::pattern::op::Or>(OutputVector{brgemm_in0, brgemm_in1, transpose2});
+ auto m_brgemm_or_transpose =
+ std::make_shared<ov::pass::pattern::op::Or>(OutputVector{m_brgemm_in0, m_brgemm_in1, m_transpose2});
- auto callback = [=](ov::pass::pattern::Matcher& m) {
+ auto callback = [](ov::pass::pattern::Matcher& m) {
OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "FuseTransposeBrgemm")
auto brgemm = ov::as_type_ptr<op::Brgemm>(m.get_match_root());
@@ -89,7 +88,7 @@ FuseTransposeBrgemm::FuseTransposeBrgemm() {
for (size_t i = 0; i < brgemm->get_input_size(); i++) {
const auto& in = brgemm->input(i);
const auto& in_value = in.get_source_output();
- if (transpose_matcher->match(in_value)) {
+ if (is_supported_transpose(in_value)) {
const auto& transpose = as_type_ptr<ov::op::v1::Transpose>(in_value.get_node_shared_ptr());
const auto& const_order =
ov::as_type_ptr<ov::op::v0::Constant>(transpose->get_input_node_shared_ptr(1));
@@ -108,7 +107,7 @@ FuseTransposeBrgemm::FuseTransposeBrgemm() {
return true;
};
- register_matcher(std::make_shared<ov::pass::pattern::Matcher>(brgemm_or_transpose, matcher_name), callback);
+ register_matcher(std::make_shared<ov::pass::pattern::Matcher>(m_brgemm_or_transpose, matcher_name), callback);
}
} // namespace pass
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.hpp b/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.hpp
index 727eddc7b7f..a86ca5c3b40 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.hpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.hpp
@@ -9,8 +9,8 @@
#include <vector>
#include "cache/multi_cache.h"
+#include "emitters/snippets/input_repacker.hpp"
#include "emitters/snippets/jit_snippets_call_args.hpp"
-#include "emitters/snippets/repacked_input.hpp"
#include "openvino/core/rtti.hpp"
#include "snippets/lowered/linear_ir.hpp"
#include "snippets/runtime_configurator.hpp"
@@ -34,7 +34,7 @@ public:
};
RepackingImplType repacking_impl_type = RepackingImplType::NONE;
- RepackedInputConfig repacked_input_config;
+ InputRepackerMap input_repackers;
std::vector<jit_snippets_call_args::loop_args_t> loop_args;
};
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/repacked_input.cpp b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
similarity index 76%
rename from src/plugins/intel_cpu/src/emitters/snippets/repacked_input.cpp
rename to src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
index 8a374e2f039..b41a59260d2 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/repacked_input.cpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "repacked_input.hpp"
+#include "input_repacker.hpp"
#include <memory>
#include <utility>
@@ -13,7 +13,7 @@
namespace ov::intel_cpu {
-RepackedInput::RepackedInput(std::shared_ptr<const RepackedInputKernel> kernel,
+InputRepacker::InputRepacker(std::shared_ptr<const InputRepackerKernel> kernel,
CpuBlockedMemoryDescPtr desc,
VectorDims in_offsets,
VectorDims out_offsets)
@@ -25,15 +25,15 @@ RepackedInput::RepackedInput(std::shared_ptr<const RepackedInputKernel> kernel,
OPENVINO_ASSERT(m_desc, "Descriptor is empty");
}
-const CpuBlockedMemoryDescPtr& RepackedInput::desc() const {
+const CpuBlockedMemoryDescPtr& InputRepacker::desc() const {
return m_desc;
}
-const VectorDims& RepackedInput::in_offsets() const {
+const VectorDims& InputRepacker::in_offsets() const {
return m_in_offsets;
}
-const VectorDims& RepackedInput::out_offsets() const {
+const VectorDims& InputRepacker::out_offsets() const {
return m_out_offsets;
}
} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/repacked_input.hpp b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
similarity index 67%
rename from src/plugins/intel_cpu/src/emitters/snippets/repacked_input.hpp
rename to src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
index 8211abbbea4..4d27a6fb365 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/repacked_input.hpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/input_repacker.hpp
@@ -15,20 +15,20 @@
namespace ov::intel_cpu {
-struct RepackedInputKernel {
- RepackedInputKernel() = default;
- virtual ~RepackedInputKernel() = default;
+struct InputRepackerKernel {
+ InputRepackerKernel() = default;
+ virtual ~InputRepackerKernel() = default;
virtual void operator()(const void* args) const = 0;
};
-struct RepackedInput {
- RepackedInput() = default;
- RepackedInput(std::shared_ptr<const RepackedInputKernel> kernel,
+struct InputRepacker {
+ InputRepacker() = default;
+ InputRepacker(std::shared_ptr<const InputRepackerKernel> kernel,
CpuBlockedMemoryDescPtr desc,
VectorDims in_offsets,
VectorDims out_offsets);
- template <class T = RepackedInputKernel, std::enable_if_t<std::is_base_of_v<RepackedInputKernel, T>, bool> = true>
+ template <class T = InputRepackerKernel, std::enable_if_t<std::is_base_of_v<InputRepackerKernel, T>, bool> = true>
[[nodiscard]] std::shared_ptr<const T> kernel() const {
const auto ker = std::dynamic_pointer_cast<const T>(m_kernel);
OPENVINO_ASSERT(ker, "Kernel is empty!");
@@ -40,12 +40,12 @@ struct RepackedInput {
[[nodiscard]] const VectorDims& out_offsets() const;
private:
- std::shared_ptr<const RepackedInputKernel> m_kernel{nullptr};
+ std::shared_ptr<const InputRepackerKernel> m_kernel{nullptr};
CpuBlockedMemoryDescPtr m_desc{nullptr};
VectorDims m_in_offsets;
VectorDims m_out_offsets;
};
-using RepackedInputConfig = std::unordered_map<size_t, ov::intel_cpu::RepackedInput>;
+using InputRepackerMap = std::unordered_map<size_t, ov::intel_cpu::InputRepacker>;
} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_base.cpp b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_base.cpp
index d2a8033d7e6..95d88c8e478 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_base.cpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_base.cpp
@@ -130,7 +130,9 @@ void BrgemmBaseKernelExecutor::update_config(const ov::snippets::lowered::Expres
// In case of data repacking LDB is chosen in accordance with repacking buffer size
const auto& brgemm_config = brgemm_node->get_config();
if (brgemm_config.with_wei_repacking()) {
- LDB = brgemm_utils::repacking::compute_LDB(LDB, brgemm_config.wei_n_blk(), brgemm_config.are_wei_blocked());
+ LDB = brgemm_utils::repacking::compute_K_blocked_stride(LDB,
+ brgemm_config.wei_n_blk(),
+ brgemm_config.are_wei_blocked());
}
config.update(M, N, K, LDA, LDB, LDC, beta);
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.cpp b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.cpp
index 0a6b4ba640a..b5d3368297f 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.cpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.cpp
@@ -217,11 +217,12 @@ BrgemmCopyBKernel::BrgemmCopyBKernel(const BrgemmCopyBKernelConfig& conf)
ker_(nullptr) {
const auto orig_wei_data_size = dnnl_data_type_size(conf.get_original_wei_dt());
const auto wei_data_size = dnnl_data_type_size(conf.get_wei_dt());
- const auto vnni_factor = data_type_vnni_granularity(conf.get_wei_dt());
- const auto buffer_repacked_k_dim = brgemm_utils::repacking::compute_blocked_dim(K, conf.get_wei_K_blk());
+ const auto prc = DnnlExtensionUtils::DataTypeToElementType(static_cast<dnnl::memory::data_type>(conf.get_wei_dt()));
+ const auto n_stride =
+ brgemm_utils::repacking::compute_N_blocked_stride(K, conf.get_wei_K_blk(), prc, conf.are_wei_blocked());
stride_in = conf.is_transposed_B() ? conf.get_K() * wei_N_blk * orig_wei_data_size : wei_N_blk * orig_wei_data_size;
- stride_out = wei_N_blk * wei_data_size * (conf.are_wei_blocked() ? buffer_repacked_k_dim : vnni_factor);
+ stride_out = wei_N_blk * n_stride * wei_data_size;
init_brgemm_copy_b_kernel(dnnl_brgemm_copy_b_kernel, conf);
OV_CPU_JIT_EMITTER_ASSERT(dnnl_brgemm_copy_b_kernel, "Kernel is missed!");
@@ -455,7 +456,8 @@ void BrgemmCopyBKernelExecutor::update_config(const ov::snippets::lowered::Expre
// Dimension N
init(N_dim, N_blk, 0);
- const auto LDB = brgemm_utils::repacking::compute_LDB(N_dim, config.get_wei_N_blk(), config.are_wei_blocked());
+ const auto LDB =
+ brgemm_utils::repacking::compute_K_blocked_stride(N_dim, config.get_wei_N_blk(), config.are_wei_blocked());
OPENVINO_ASSERT(LDB >= 0, "Invalid LDB value (less than 0)");
const auto copy_B_wei_stride =
ov::snippets::utils::get_dim_stride(expr->get_input_port(0), config.is_transposed_B() ? 0 : 1) *
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp
index 770f71dad50..2388de286ed 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp
@@ -19,7 +19,7 @@
#include "cache/multi_cache.h"
#include "emitters/snippets/cpu_kernel_executor_table.hpp"
-#include "emitters/snippets/repacked_input.hpp"
+#include "emitters/snippets/input_repacker.hpp"
#include "openvino/core/type/element_type.hpp"
#include "snippets/emitter.hpp"
#include "snippets/kernel_executor_table.hpp"
@@ -166,7 +166,7 @@ private:
size_t m_hash{SIZE_MAX};
};
-struct BrgemmCopyBKernel : public RepackedInputKernel, public dnnl::impl::cpu::x64::jit_generator {
+struct BrgemmCopyBKernel : public InputRepackerKernel, public dnnl::impl::cpu::x64::jit_generator {
DECLARE_CPU_JIT_AUX_FUNCTIONS(BrgemmCopyBKernel)
struct call_args {
const void* src = nullptr;
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
index f9cbfea743a..c70e44a7812 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.cpp
@@ -9,7 +9,11 @@
#include <cstdint>
#include <memory>
#include <oneapi/dnnl/dnnl.hpp>
+#include <oneapi/dnnl/dnnl_common.hpp>
+#include <string>
+#include <unordered_map>
+#include "cache/multi_cache.h"
#include "cpu_memory.h"
#include "dnnl_extension_utils.h"
#include "memory_desc/cpu_memory_desc_utils.h"
@@ -18,19 +22,37 @@
#include "nodes/reorder.h"
#include "openvino/core/except.hpp"
#include "openvino/core/type/element_type.hpp"
+#include "weights_cache.hpp"
namespace ov::intel_cpu::utils {
-MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
- const DnnlMemoryDescPtr dstWeightDesc,
- const MemoryCPtr weightsMem,
- const ExecutorContext::CPtr context,
+MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr& srcWeightDesc,
+ const DnnlMemoryDescPtr& dstWeightDesc,
+ const MemoryCPtr& weightsMem,
+ const ExecutorContext::CPtr& context,
const bool needShiftSignedToUnsigned) {
- const auto& eng = context->getEngine();
- const auto& format = dstWeightDesc->serializeFormat();
-
- const auto privateWeightCache = context->getPrivateWeighCache();
+ const auto privateWeightCache = context->getPrivateWeightCache();
OPENVINO_ASSERT(privateWeightCache, "privateWeightCache is nullptr");
+
+ return prepareWeightsMemory(srcWeightDesc,
+ dstWeightDesc,
+ weightsMem,
+ context->getEngine(),
+ context->getRuntimeCache(),
+ context->getWeightsCache(),
+ privateWeightCache,
+ needShiftSignedToUnsigned);
+}
+
+MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr& srcWeightDesc,
+ const DnnlMemoryDescPtr& dstWeightDesc,
+ const MemoryCPtr& weightsMem,
+ const dnnl::engine& eng,
+ const MultiCachePtr& rtCache,
+ const WeightsSharing::Ptr& globalWeightCache,
+ const std::shared_ptr<std::unordered_map<std::string, MemoryPtr>>& privateWeightCache,
+ bool needShiftSignedToUnsigned) {
+ const auto format = dstWeightDesc->serializeFormat();
if (privateWeightCache) {
auto itr = privateWeightCache->find(format);
if (privateWeightCache->end() != itr) {
@@ -49,7 +71,6 @@ MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
// prevent reorderData from doing conversion
Memory srcMemory{eng, srcWeightDesc->cloneWithNewPrecision(dst_wdt), weightsMem->getData()};
MemoryPtr _ptr = std::make_shared<Memory>(eng, dstWeightDesc);
- auto rtCache = context->getRuntimeCache();
node::Reorder::reorderData(srcMemory, *_ptr, rtCache);
// do shift
@@ -74,13 +95,11 @@ MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
Memory srcMemory{eng, srcWeightDesc, weightsMem->getData()};
MemoryPtr _ptr = std::make_shared<Memory>(eng, dstWeightDesc);
- auto rtCache = context->getRuntimeCache();
node::Reorder::reorderData(srcMemory, *_ptr, rtCache);
return _ptr;
};
- auto globalWeightCache = context->getWeightsCache();
MemoryPtr ptr;
if (globalWeightCache && dnnl::memory::format_kind::blocked == dstWeightDesc->getDnnlDesc().get_format_kind()) {
ptr = *globalWeightCache->findOrCreate(DnnlExtensionUtils::computeWeightsStringHash(weightsMem, dstWeightDesc),
@@ -89,7 +108,9 @@ MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr srcWeightDesc,
ptr = create();
}
- (*privateWeightCache)[format] = ptr;
+ if (privateWeightCache) {
+ (*privateWeightCache)[format] = ptr;
+ }
return ptr;
}
diff --git a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
index 8aedc9a9f6d..1df2bdcbf2e 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/dnnl/dnnl_utils.hpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2023 Intel Corporation
+// Copyright (C) 2018-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
// @file dnnl_utils.hpp
@@ -7,14 +7,29 @@
#pragma once
+#include <oneapi/dnnl/dnnl_common.hpp>
+#include <string>
+#include <unordered_map>
+
+#include "cache/multi_cache.h"
#include "cpu_memory.h"
#include "memory_desc/dnnl_memory_desc.h"
#include "nodes/executors/executor.hpp"
+#include "weights_cache.hpp"
namespace ov::intel_cpu::utils {
-MemoryPtr prepareWeightsMemory(DnnlMemoryDescPtr srcWeightDesc,
- DnnlMemoryDescPtr dstWeightDesc,
- MemoryCPtr weightsMem,
- ExecutorContext::CPtr context,
+MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr& srcWeightDesc,
+ const DnnlMemoryDescPtr& dstWeightDesc,
+ const MemoryCPtr& weightsMem,
+ const ExecutorContext::CPtr& context,
+ bool needShiftSignedToUnsigned = false);
+
+MemoryPtr prepareWeightsMemory(const DnnlMemoryDescPtr& srcWeightDesc,
+ const DnnlMemoryDescPtr& dstWeightDesc,
+ const MemoryCPtr& weightsMem,
+ const dnnl::engine& eng,
+ const MultiCachePtr& rtCache,
+ const WeightsSharing::Ptr& globalWeightCache,
+ const std::shared_ptr<std::unordered_map<std::string, MemoryPtr>>& privateWeightCache,
bool needShiftSignedToUnsigned = false);
} // namespace ov::intel_cpu::utils
diff --git a/src/plugins/intel_cpu/src/nodes/executors/executor.hpp b/src/plugins/intel_cpu/src/nodes/executors/executor.hpp
index a3f440f4de2..dbd923650e9 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/executor.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/executor.hpp
@@ -64,7 +64,7 @@ public:
return scratchPads[curNumaNodeId];
}
- [[nodiscard]] std::shared_ptr<std::unordered_map<std::string, MemoryPtr>> getPrivateWeighCache() const {
+ [[nodiscard]] std::shared_ptr<std::unordered_map<std::string, MemoryPtr>> getPrivateWeightCache() const {
return privateWeighCache;
}
diff --git a/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.cpp b/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.cpp
index 098bb5ad5bf..37313e79a55 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.cpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.cpp
@@ -21,11 +21,10 @@
#include "cpu_types.h"
#include "dnnl_extension_utils.h"
#include "emitters/snippets/cpu_runtime_configurator.hpp"
+#include "emitters/snippets/input_repacker.hpp"
#include "emitters/snippets/jit_snippets_call_args.hpp"
-#include "emitters/snippets/repacked_input.hpp"
#include "emitters/snippets/x64/cpu_generator.hpp"
#include "emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp"
-#include "graph_context.h"
#include "memory_desc/blocked_memory_desc.h"
#include "memory_desc/cpu_memory_desc_utils.h"
#include "nodes/executors/subgraph.hpp"
@@ -100,13 +99,13 @@ SubgraphExecutor::SubgraphExecutor(const std::shared_ptr<CPURuntimeConfig>& snip
start_offset_out,
allocator,
kernel_cache),
- m_repacked_input_config(snippet_config->repacked_input_config),
+ m_input_repackers(snippet_config->input_repackers),
m_repacking_impl_type(snippet_config->repacking_impl_type) {
auto external_buffer_size =
- std::accumulate(m_repacked_input_config.begin(),
- m_repacked_input_config.end(),
+ std::accumulate(m_input_repackers.begin(),
+ m_input_repackers.end(),
static_cast<size_t>(0),
- [](size_t sum, const std::pair<size_t, RepackedInput>& p) {
+ [](size_t sum, const std::pair<size_t, InputRepacker>& p) {
auto curr_mem_size = p.second.desc()->getCurrentMemSize();
OPENVINO_ASSERT(curr_mem_size != ov::intel_cpu::MemoryDesc::UNDEFINED_SIZE,
"Current repacking buffer memory size is undefined");
@@ -150,7 +149,7 @@ SubgraphExecutor::SubgraphExecutor(const std::shared_ptr<CPURuntimeConfig>& snip
void SubgraphExecutor::separately_repack_input(const MemoryPtr& src_mem_ptr,
const MemoryPtr& dst_mem_ptr,
- const ov::intel_cpu::RepackedInput& repacked_input,
+ const ov::intel_cpu::InputRepacker& input_repacker,
size_t tensor_rank) {
auto get_offset = [](const BlockedMemoryDescPtr& desc) {
return static_cast<ptrdiff_t>(desc->getOffsetPadding() * desc->getPrecision().size());
@@ -165,12 +164,12 @@ void SubgraphExecutor::separately_repack_input(const MemoryPtr& src_mem_ptr,
OPENVINO_ASSERT(shape.size() <= tensor_rank, "Unsupported shape rank of repacking data");
init_parallel_domain(shape, tensor_rank, 2LU, dom);
- const auto& in_strides = repacked_input.in_offsets();
- const auto& out_strides = repacked_input.out_offsets();
+ const auto& in_strides = input_repacker.in_offsets();
+ const auto& out_strides = input_repacker.out_offsets();
OPENVINO_ASSERT(everyone_is(tensor_rank, in_strides.size(), out_strides.size(), dom.size()),
"Unsupported shape rank of repacking data");
- const auto& kernel = repacked_input.kernel<BrgemmCopyBKernel>();
+ const auto& kernel = input_repacker.kernel<BrgemmCopyBKernel>();
if (tensor_rank == rank6D) {
parallel4d_repacking(kernel.get(), dom, in_strides, out_strides, src_ptr, dst_ptr);
} else {
@@ -178,38 +177,6 @@ void SubgraphExecutor::separately_repack_input(const MemoryPtr& src_mem_ptr,
}
}
-std::vector<MemoryPtr> SubgraphExecutor::prepare_weights(const std::vector<MemoryPtr>& in_mem_ptrs,
- const RepackedInputConfig& repacked_const_input_config,
- const GraphContext::CPtr& context) {
- std::vector<MemoryPtr> repacked_mem_ptrs = in_mem_ptrs;
- for (const auto& p : repacked_const_input_config) {
- const auto idx = p.first;
- const auto& repacked_input = p.second;
- OPENVINO_ASSERT(idx < in_mem_ptrs.size(), "Incorrect index of repacked input");
- const auto& src_mem_ptr = in_mem_ptrs[idx];
-
- auto create = [&]() {
- const auto& dst_mem_ptr = std::make_shared<Memory>(context->getEngine(), repacked_input.desc());
- separately_repack_input(src_mem_ptr, dst_mem_ptr, repacked_input, dst_mem_ptr->getShape().getDims().size());
- return dst_mem_ptr;
- };
-
- auto weight_cache = context->getWeightsCache();
- if (weight_cache != nullptr) {
- const auto& wgt_dims = src_mem_ptr->getStaticDims();
- OPENVINO_ASSERT(wgt_dims.size() > 1, "Unexpected weight shape rank");
- const auto string_hash =
- "brgemm_snippets_" + DnnlExtensionUtils::computeWeightsStringHash(
- src_mem_ptr,
- MemoryDescUtils::convertToDnnlMemoryDesc(repacked_input.desc()));
- repacked_mem_ptrs[idx] = *weight_cache->findOrCreate(string_hash, create);
- } else {
- repacked_mem_ptrs[idx] = create();
- }
- }
- return repacked_mem_ptrs;
-}
-
#if defined(__linux__) && defined(SNIPPETS_DEBUG_CAPS)
// NOLINTBEGIN(misc-include-cleaner) bug in clang-tidy
void SubgraphExecutor::segfault_detector() const {
@@ -235,14 +202,14 @@ std::vector<MemoryPtr> SubgraphExecutor::separately_repack_inputs(const dnnl::st
const std::vector<MemoryPtr>& src_mem_ptrs) {
auto reordered_in_ptrs = src_mem_ptrs;
size_t offset = m_internal_buffer_size;
- for (const auto& [in_idx, repacked_input] : m_repacked_input_config) {
- const auto& desc = repacked_input.desc();
+ for (const auto& [in_idx, input_repacker] : m_input_repackers) {
+ const auto& desc = input_repacker.desc();
const void* data_ptr = m_buffer_scratchpad->getDataAs<uint8_t>() + offset;
OPENVINO_ASSERT(in_idx < src_mem_ptrs.size(), "Incorrect index of input repacked mem ptr");
const auto& src_mem = src_mem_ptrs[in_idx];
const auto& dst_mem = std::make_shared<Memory>(strm.get_engine(), desc, data_ptr, false);
- separately_repack_input(src_mem, dst_mem, repacked_input, m_tensor_rank);
+ separately_repack_input(src_mem, dst_mem, input_repacker, m_tensor_rank);
reordered_in_ptrs[in_idx] = dst_mem;
offset += desc->getCurrentMemSize();
@@ -255,9 +222,9 @@ void SubgraphExecutor::in_parallel_repack_inputs(const std::vector<MemoryPtr>& i
int ithr,
jit_snippets_call_args& call_args) {
size_t repacked_offset_idx = 0;
- for (const auto& [in_idx, repacked_input] : m_repacked_input_config) {
+ for (const auto& [in_idx, input_repacker] : m_input_repackers) {
size_t src_offset = m_start_offset_in[in_idx];
- init_offset(repacked_input.in_offsets(), indexes, src_offset);
+ init_offset(input_repacker.in_offsets(), indexes, src_offset);
auto* repacked_ptr = get_external_scratchpad_ptr(ithr, in_idx);
@@ -266,7 +233,7 @@ void SubgraphExecutor::in_parallel_repack_inputs(const std::vector<MemoryPtr>& i
BrgemmCopyBKernel::call_args args;
args.src = in_mem_ptrs[in_idx]->getDataAs<const uint8_t>() + src_offset;
args.tr_src = repacked_ptr;
- (*repacked_input.kernel<BrgemmCopyBKernel>())(&args);
+ (*input_repacker.kernel<BrgemmCopyBKernel>())(&args);
last_processed_src_offset = src_offset;
}
diff --git a/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.hpp b/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.hpp
index bd5e7b3b6ec..5d5d9228938 100644
--- a/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.hpp
+++ b/src/plugins/intel_cpu/src/nodes/executors/x64/subgraph.hpp
@@ -15,8 +15,8 @@
#include "cache/multi_cache.h"
#include "cpu_memory.h"
#include "emitters/snippets/cpu_runtime_configurator.hpp"
+#include "emitters/snippets/input_repacker.hpp"
#include "emitters/snippets/jit_snippets_call_args.hpp"
-#include "emitters/snippets/repacked_input.hpp"
#include "graph_context.h"
#include "nodes/executors/subgraph.hpp"
#include "openvino/core/except.hpp"
@@ -37,14 +37,10 @@ public:
const std::vector<MemoryPtr>& in_mem_ptrs,
const std::vector<MemoryPtr>& out_mem_ptrs) override;
- static std::vector<MemoryPtr> prepare_weights(const std::vector<MemoryPtr>& in_mem_ptrs,
- const RepackedInputConfig& repacked_const_input_config,
- const GraphContext::CPtr& context);
-
protected:
static void separately_repack_input(const MemoryPtr& src_mem_ptr,
const MemoryPtr& dst_mem_ptr,
- const ov::intel_cpu::RepackedInput& repacked_input,
+ const ov::intel_cpu::InputRepacker& input_repacker,
size_t tensor_rank);
std::vector<MemoryPtr> separately_repack_inputs(const dnnl::stream& strm,
@@ -55,12 +51,12 @@ protected:
jit_snippets_call_args& call_args);
void* get_external_scratchpad_ptr(size_t ithr, size_t idx) const {
- if (m_repacked_input_config.empty()) {
+ if (m_input_repackers.empty()) {
return nullptr;
}
uint8_t* data_ptr = m_buffer_scratchpad->getDataAs<uint8_t>() + m_internal_buffer_size;
- for (const auto& p : m_repacked_input_config) {
+ for (const auto& p : m_input_repackers) {
const auto& desc = p.second.desc();
const auto size = desc->getCurrentMemSize();
if (p.first == idx) {
@@ -73,7 +69,7 @@ protected:
// [ Thread Index -> Index of input with repacking data - > last repacked src_offset ]
std::vector<std::vector<size_t>> m_repacked_offsets_by_threads;
- RepackedInputConfig m_repacked_input_config;
+ InputRepackerMap m_input_repackers;
std::function<void(const std::vector<size_t>&, const std::vector<size_t>&, size_t&)> init_offset;
@@ -83,7 +79,7 @@ protected:
}
void clean_repacked_offsets(size_t ithr) {
- m_repacked_offsets_by_threads[ithr].assign(m_repacked_input_config.size(), std::numeric_limits<size_t>::max());
+ m_repacked_offsets_by_threads[ithr].assign(m_input_repackers.size(), std::numeric_limits<size_t>::max());
}
#ifdef SNIPPETS_DEBUG_CAPS
diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.cpp b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
index 906f1f49531..00ced3bdcd9 100644
--- a/src/plugins/intel_cpu/src/nodes/subgraph.cpp
+++ b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
@@ -70,9 +70,9 @@
# include "transformations/snippets/x64/pass/lowered/adjust_brgemm_copy_b_loop_ports.hpp"
# include "transformations/snippets/x64/pass/lowered/brgemm_cpu_blocking.hpp"
# include "transformations/snippets/x64/pass/lowered/fuse_load_store_and_convert.hpp"
-# include "transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.hpp"
# include "transformations/snippets/x64/pass/lowered/insert_brgemm_copy_buffers.hpp"
# include "transformations/snippets/x64/pass/remove_converts.hpp"
+# include "transformations/snippets/x64/pass/repack_matmul_weights.hpp"
# include "transformations/snippets/x64/shape_inference.hpp"
#endif
@@ -398,7 +398,6 @@ void Subgraph::createPrimitive() {
initPluginBlockedShapes();
initAttributes();
optimizeIR();
- prepareWeights();
// Init starts offsets should be after `prepareWeights`
initStartOffsets();
}
@@ -559,8 +558,13 @@ Subgraph::DataFlowPasses Subgraph::getDataFlowPasses() {
SNIPPETS_REGISTER_PASS_RELATIVE_X86_64(Place::After,
ov::intel_cpu::pass::BrgemmToBrgemmCPU,
ov::intel_cpu::pass::EliminateBrgemmCopyB,
- cpu_config->repacked_input_config,
- repacked_constant_input_config);
+ cpu_config->input_repackers);
+ SNIPPETS_REGISTER_PASS_RELATIVE_X86_64(Place::After,
+ ov::intel_cpu::pass::EliminateBrgemmCopyB,
+ ov::intel_cpu::pass::RepackMatMulWeights,
+ context,
+ cpu_config->input_repackers,
+ srcMemPtrs);
}
SNIPPETS_REGISTER_PASS_ABSOLUTE_X86_64(Place::PipelineEnd, ov::intel_cpu::pass::RemoveConverts);
SNIPPETS_REGISTER_PASS_RELATIVE_ARM64(Place::Before,
@@ -642,10 +646,6 @@ Subgraph::ControlFlowPasses Subgraph::getControlFlowPasses() {
SNIPPETS_REGISTER_PASS_RELATIVE_X86_64(Place::Before,
ov::snippets::lowered::pass::InsertBuffers,
ov::intel_cpu::pass::InsertBrgemmCopyBuffers);
- SNIPPETS_REGISTER_PASS_ABSOLUTE_X86_64(Place::PipelineEnd,
- ov::intel_cpu::pass::InitRepackedConstantInputs,
- context->getSnippetsParamsCache(),
- repacked_constant_input_config);
SNIPPETS_REGISTER_PASS_RELATIVE_ARM64(Place::After,
ov::snippets::lowered::pass::MarkLoops,
ov::intel_cpu::pass::GemmCPUBlocking);
@@ -753,18 +753,6 @@ void Subgraph::optimizeIR() {
control_flow_passes);
}
-void Subgraph::prepareWeights() {
- if (repacked_constant_input_config.empty()) {
- return;
- }
-
-#if defined(OPENVINO_ARCH_X86_64)
- srcMemPtrs = SubgraphExecutor::prepare_weights(srcMemPtrs, repacked_constant_input_config, context);
-#else
- OPENVINO_THROW("Weight repacking is unimplemented on this platform");
-#endif
-}
-
void Subgraph::prepareParams() {
#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)
const auto& cache = context->getSnippetsParamsCache();
diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.h b/src/plugins/intel_cpu/src/nodes/subgraph.h
index 984e581a1c0..7911a67d242 100644
--- a/src/plugins/intel_cpu/src/nodes/subgraph.h
+++ b/src/plugins/intel_cpu/src/nodes/subgraph.h
@@ -15,7 +15,7 @@
#include "cpu_memory.h"
#include "cpu_types.h"
-#include "emitters/snippets/repacked_input.hpp"
+#include "emitters/snippets/input_repacker.hpp"
#include "executors/subgraph.hpp"
#include "graph_context.h"
#include "node.h"
@@ -63,7 +63,6 @@ private:
void initStartOffsets();
void initPluginBlockedShapes() const;
void optimizeIR();
- void prepareWeights();
snippets::op::Subgraph::BlockedShapeVector getSnippetsBlockedShapes() const;
std::pair<std::vector<ov::element::Type>, std::vector<ov::element::Type>> getIOPrecisions() const;
@@ -101,7 +100,6 @@ private:
std::vector<ptrdiff_t> start_offset_in;
std::vector<ptrdiff_t> start_offset_out;
- RepackedInputConfig repacked_constant_input_config;
// Ptr indices which are not processed by the snippets kernel
// and used directly in the specific emitters (e.g. jit_brgemm_emitter)
std::set<size_t> external_ptrs_idces;
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.cpp
index 9a9dcf3149e..d586c72d9d0 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.cpp
@@ -6,8 +6,10 @@
#include <oneapi/dnnl/dnnl_common_types.h>
+#include <algorithm>
#include <cpu/x64/cpu_isa_traits.hpp>
#include <cstddef>
+#include <numeric>
#include <string>
#include "dnnl_extension_utils.h"
@@ -17,6 +19,7 @@
#include "openvino/core/type.hpp"
#include "openvino/core/type/element_type.hpp"
#include "snippets/lowered/expression.hpp"
+#include "snippets/op/subgraph.hpp"
#include "snippets/shape_types.hpp"
#include "snippets/utils/utils.hpp"
#include "transformations/snippets/x64/op/brgemm_copy_b.hpp"
@@ -148,7 +151,7 @@ void BrgemmConfig::validate() const {
OPENVINO_ASSERT(m_wei_n_blk > 0 && m_wei_k_blk > 0, "Weight block sizes must be positive");
}
-size_t BrgemmConfig::get_elems_in_vec(const ov::element::Type& precision) {
+size_t get_elems_in_vec(const ov::element::Type& precision) {
using namespace dnnl::impl::cpu;
OV_CPU_JIT_EMITTER_ASSERT(x64::mayiuse(x64::avx2), "doesn't support non avx512 platforms");
const auto vlen =
@@ -162,13 +165,80 @@ size_t compute_vnni_factor(const ov::element::Type& precision) {
}
namespace repacking {
-ov::snippets::VectorDims compute_buffer_b_allocation_shape(size_t K, size_t N, size_t wei_k_blk, size_t wei_n_blk) {
+ov::snippets::VectorDims compute_buffer_b_allocation_shape(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked,
+ bool is_transposed) {
OPENVINO_ASSERT(
!ov::snippets::utils::is_dynamic_value(wei_k_blk) && !ov::snippets::utils::is_dynamic_value(wei_n_blk),
"wei_k_blk and wei_n_blk cannot be dynamic");
+ OPENVINO_ASSERT(planar_shape.size() >= 2, "Incorrect rank of buffer B: ", planar_shape.size());
+ const auto K = *++planar_shape.rbegin();
+ const auto N = *planar_shape.rbegin();
const size_t new_N = compute_blocked_dim(N, wei_n_blk);
- return ov::snippets::VectorDims{ov::snippets::utils::div_up(K, wei_k_blk), new_N, wei_k_blk};
+ // - In case of transpose, K dimension must be rounded-up to number of elems in vector register
+ // For the details, please see 'transpose16x8' and 'fixup16x16' implementations and usage in
+ // onednn/src/cpu/x64/matmul/brgemm_matmul_copy_utils.cpp
+ // - Low precision repacking writes the result by VNNIFactor * wei_n_blk blocks
+ // despite the actual size of the input data. Because of that we have to round-up the allocation shape to always
+ // have enough memory allocated. For the details, please see 'copy_4x64' and 'copy_2x32' implementations and
+ // usage in onednn/src/cpu/x64/matmul/brgemm_matmul_copy_utils.cpp
+ // Due to these facts, we cannot use just blocked shapes to calculate allocation size. BrgemmCopyB requires greater
+ // size.
+ const auto vnni_f = brgemm_utils::compute_vnni_factor(prc);
+ size_t K_alignment = are_wei_blocked ? wei_k_blk * vnni_f : vnni_f;
+ if (is_transposed) {
+ K_alignment = std::max(K_alignment, brgemm_utils::get_elems_in_vec(prc));
+ }
+ const size_t new_K = compute_blocked_dim(K, K_alignment);
+
+ ov::snippets::VectorDims allocation_shape(planar_shape);
+ *++allocation_shape.rbegin() = new_K;
+ *allocation_shape.rbegin() = new_N;
+
+ return allocation_shape;
+}
+
+ov::snippets::op::Subgraph::BlockedShape get_wei_blocked_shape(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked) {
+ const auto rank = planar_shape.size();
+ OPENVINO_ASSERT(rank >= 2, "Incorrect rank of weights: ", rank);
+ const auto K_idx = rank - 2;
+ const auto N_idx = rank - 1;
+ const auto K = planar_shape[K_idx];
+ const auto N = planar_shape[N_idx];
+ const auto vnni_factor = compute_vnni_factor(prc);
+ ov::snippets::VectorDims blocked_dims(planar_shape.cbegin(), planar_shape.cbegin() + (rank - 2));
+ ov::snippets::VectorDims blocked_order(rank - 2);
+ std::iota(blocked_order.begin(), blocked_order.end(), 0);
+
+ if (are_wei_blocked) {
+ blocked_order.insert(blocked_order.end(), {N_idx, K_idx, K_idx, N_idx, K_idx});
+ blocked_dims.insert(blocked_dims.end(),
+ {ov::snippets::utils::div_up(N, wei_n_blk),
+ ov::snippets::utils::div_up(K, wei_k_blk * vnni_factor),
+ wei_k_blk,
+ wei_n_blk,
+ vnni_factor});
+ } else {
+ blocked_order.insert(blocked_order.end(), {K_idx, N_idx, K_idx});
+ blocked_dims.insert(
+ blocked_dims.end(),
+ {ov::snippets::utils::div_up(K, vnni_factor), ov::snippets::utils::rnd_up(N, wei_n_blk), vnni_factor});
+ }
+
+ if (vnni_factor == 1) {
+ blocked_order.pop_back();
+ blocked_dims.pop_back();
+ }
+
+ return {blocked_dims, blocked_order};
}
ov::snippets::lowered::ExpressionPtr get_copy_b_expr(const ov::snippets::lowered::ExpressionPtr& brgemm_expr) {
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.hpp
index a800403bc46..e63a2a7efa4 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/op/brgemm_utils.hpp
@@ -13,6 +13,7 @@
#include "openvino/core/rtti.hpp"
#include "openvino/core/type/element_type.hpp"
#include "snippets/lowered/expression.hpp"
+#include "snippets/op/subgraph.hpp"
#include "snippets/shape_types.hpp"
#include "snippets/utils/utils.hpp"
@@ -81,7 +82,6 @@ private:
static dnnl::impl::cpu::x64::cpu_isa_t get_prim_isa(const ov::element::Type& src_dt,
const ov::element::Type& wei_dt);
- static size_t get_elems_in_vec(const ov::element::Type& precision);
dnnl::impl::cpu::x64::cpu_isa_t m_isa = dnnl::impl::cpu::x64::cpu_isa_t::isa_undef;
@@ -108,6 +108,8 @@ private:
/// \brief Computes VNNI factor used by OneDNN implementation. Depends on tensor precision
size_t compute_vnni_factor(const ov::element::Type& precision);
+/// \brief Computes element count in vector register using precision
+size_t get_elems_in_vec(const ov::element::Type& precision);
/// \brief The following helpers return True if the target precision is supported by BRGEMM on the current platform
inline bool is_fp32_supported() {
@@ -137,16 +139,49 @@ inline T compute_blocked_dim(T dim, size_t blk) {
return ov::snippets::utils::rnd_up(dim, static_cast<T>(blk));
}
-/// \brief Computes LDB
+/// \brief Computes stride for N dim in blocked shape
template <typename T,
typename = typename std::enable_if_t<(std::is_same_v<T, size_t> || std::is_same_v<T, int64_t>), bool>>
-inline T compute_LDB(T n, size_t wei_n_blk, bool are_wei_blocked) {
+inline T compute_N_blocked_stride(T K, size_t wei_k_blk, const ov::element::Type& prc, bool are_wei_blocked) {
+ // - if weights are not in blocked format, account for the VNNI format
+ // - if weights are blocked, account for zero padding in K dimension
+ assert(!ov::snippets::utils::is_dynamic_value(wei_k_blk) && "wei_k_blk cannot be dynamic");
+ if (snippets::utils::is_dynamic_value(K)) {
+ return snippets::utils::get_dynamic_value<T>();
+ }
+
+ const auto vnni_factor = brgemm_utils::compute_vnni_factor(prc);
+ if (are_wei_blocked) {
+ return brgemm_utils::repacking::compute_blocked_dim(K, wei_k_blk * vnni_factor);
+ }
+
+ return vnni_factor;
+}
+
+/// \brief Computes stride for K dim in blocked shape
+template <typename T,
+ typename = typename std::enable_if_t<(std::is_same_v<T, size_t> || std::is_same_v<T, int64_t>), bool>>
+inline T compute_K_blocked_stride(T N, size_t wei_n_blk, bool are_wei_blocked) {
assert(!ov::snippets::utils::is_dynamic_value(wei_n_blk) && "wei_n_blk cannot be dynamic");
- return are_wei_blocked ? wei_n_blk : compute_blocked_dim(n, wei_n_blk);
+ return are_wei_blocked ? wei_n_blk : compute_blocked_dim(N, wei_n_blk);
}
-/// \brief Computes allocation shape for Buffer between BrgemmCopyB and Brgemm
-ov::snippets::VectorDims compute_buffer_b_allocation_shape(size_t K, size_t N, size_t wei_k_blk, size_t wei_n_blk);
+/// \brief Computes allocation shape for Buffer between BrgemmCopyB and Brgemm.
+/// Note: BrgemmCopyB requires buffer with allocation size which is greater than blocked shapes.
+/// Please see the explanation inside
+ov::snippets::VectorDims compute_buffer_b_allocation_shape(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked,
+ bool is_transposed);
+
+/// \brief Compute blocked shape for repacked weights
+ov::snippets::op::Subgraph::BlockedShape get_wei_blocked_shape(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked);
/**
* @brief Retrieves the expression pointer for the brgemm_copy_b expression corresponding to the given BrgemmCPU
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.cpp
index 8a92f1cb6ed..7dc32ae3621 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.cpp
@@ -52,7 +52,7 @@ bool pass::EliminateBrgemmCopyB::run_on_model(const std::shared_ptr<ov::Model>&
const auto& layout = in_desc->get_layout();
// TODO [157340]: support external repacking for copyB with compensations
- if (copy_b_node->get_config().with_compensations() || transformation_callback(copy_b_node)) {
+ if (brgemm_config.with_compensations() || transformation_callback(copy_b_node)) {
return false;
}
@@ -61,9 +61,7 @@ bool pass::EliminateBrgemmCopyB::run_on_model(const std::shared_ptr<ov::Model>&
OPENVINO_ASSERT(param_idx < model->get_parameters().size(),
"Parameter index is invalid in EliminateBrgemmCopyB transformation");
// Update external repacking config for the further pipeline stages to mark this input as repacked
- auto& config =
- brgemm_config.are_wei_constant() ? m_repacked_constant_inputs_config : m_repacked_runtime_inputs_config;
- config[param_idx] = RepackedInput();
+ m_input_repackers[param_idx] = InputRepacker();
// Since repacking is moved out of Subgraph body,
// the rest weights subgraph must be updated with precision after repacking
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.hpp
index 3af0fae63b1..8c644ddfc3a 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/eliminate_brgemm_copy_b.hpp
@@ -9,7 +9,7 @@
#include <set>
#include <utility>
-#include "emitters/snippets/repacked_input.hpp"
+#include "emitters/snippets/input_repacker.hpp"
#include "openvino/core/model.hpp"
#include "openvino/pass/pass.hpp"
@@ -26,16 +26,12 @@ namespace ov::intel_cpu::pass {
class EliminateBrgemmCopyB : public ov::pass::ModelPass {
public:
OPENVINO_MODEL_PASS_RTTI("EliminateBrgemmCopyB");
- EliminateBrgemmCopyB(ov::intel_cpu::RepackedInputConfig& repacked_runtime_inputs_config,
- ov::intel_cpu::RepackedInputConfig& repacked_constant_inputs_config)
- : m_repacked_runtime_inputs_config(repacked_runtime_inputs_config),
- m_repacked_constant_inputs_config(repacked_constant_inputs_config) {}
+ EliminateBrgemmCopyB(ov::intel_cpu::InputRepackerMap& input_repackers) : m_input_repackers(input_repackers) {}
bool run_on_model(const std::shared_ptr<ov::Model>& model) override;
private:
- ov::intel_cpu::RepackedInputConfig& m_repacked_runtime_inputs_config;
- ov::intel_cpu::RepackedInputConfig& m_repacked_constant_inputs_config;
+ ov::intel_cpu::InputRepackerMap& m_input_repackers;
};
} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/adjust_brgemm_copy_b_loop_ports.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/adjust_brgemm_copy_b_loop_ports.cpp
index 41a2ab93fd4..d2f760ba0a0 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/adjust_brgemm_copy_b_loop_ports.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/adjust_brgemm_copy_b_loop_ports.cpp
@@ -53,7 +53,6 @@ bool pass::AdjustBrgemmCopyBLoopPorts::update_loop_info(
const auto& node = p.get_expr()->get_node();
if (auto brg = as_type_ptr<BrgemmCPU>(node)) {
const auto& brgemm_config = brg->get_config();
- const auto& precision = node->get_input_element_type(1);
/*
* The BrgemmCopyB operation repacks the weights in the following way:
* 1) Not-FP32 Brgemm requires inner K block which is equal to VNNI factor
@@ -66,23 +65,23 @@ bool pass::AdjustBrgemmCopyBLoopPorts::update_loop_info(
int64_t blocked_shape_ptr_inc = 0;
if (loop_port.get_dim_idx() == 1) {
// Blocking loop by K dimension:
- // - pointer increment is equal to LDB
- blocked_shape_ptr_inc = brgemm_utils::repacking::compute_LDB(loop_desc.ptr_increment,
- brgemm_config.wei_n_blk(),
- brgemm_config.are_wei_blocked());
+ blocked_shape_ptr_inc =
+ brgemm_utils::repacking::compute_K_blocked_stride(loop_desc.ptr_increment,
+ brgemm_config.wei_n_blk(),
+ brgemm_config.are_wei_blocked());
} else if (loop_port.get_dim_idx() == 0) {
// Blocking loop by N dimension:
- // - if weights are not in blocked format, account for the VNNI format
- // - if weights are blocked, account for zero padding in K dimension
+ // Attention: blocked_shape_ptr_inc is int64_t while K dimension is uint64_t
const auto& shape = loop_port.get_expr_port()->get_descriptor_ptr()->get_shape();
- const auto k_dim = *++shape.rbegin();
- if (snippets::utils::is_dynamic_value(k_dim)) {
+ const auto K_dim = *++shape.rbegin();
+ if (snippets::utils::is_dynamic_value(K_dim)) {
blocked_shape_ptr_inc = snippets::utils::get_dynamic_value<int64_t>();
- } else if (brgemm_config.are_wei_blocked()) {
- blocked_shape_ptr_inc = static_cast<int64_t>(
- brgemm_utils::repacking::compute_blocked_dim(k_dim, brgemm_config.wei_k_blk()));
} else {
- blocked_shape_ptr_inc = static_cast<int64_t>(brgemm_utils::compute_vnni_factor(precision));
+ blocked_shape_ptr_inc =
+ brgemm_utils::repacking::compute_N_blocked_stride(static_cast<int64_t>(K_dim),
+ brgemm_config.wei_k_blk(),
+ brgemm_config.wei_dt(),
+ brgemm_config.are_wei_blocked());
}
} else {
OPENVINO_THROW("Unexpected loop port dimension index in AdjustBrgemmCopyBLoopPorts");
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/expressions/brgemm_copy_b_buffer_expressions.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/expressions/brgemm_copy_b_buffer_expressions.cpp
index aeb4a0da4f1..ff2f4467bf1 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/expressions/brgemm_copy_b_buffer_expressions.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/expressions/brgemm_copy_b_buffer_expressions.cpp
@@ -50,16 +50,17 @@ void RepackedWeightsBufferExpression::init_allocation_size(
OPENVINO_ASSERT(brgemm_copy_b, "RepackedWeightsBufferExpression expects BrgemmCopyB as parent expression");
const auto& brgemm_config = brgemm_copy_b->get_config();
- const auto& in_subtensor = ov::snippets::utils::get_projected_subtensor(parent_expr->get_input_port(0));
+ const auto in_subtensor = ov::snippets::utils::get_projected_subtensor(parent_expr->get_input_port(0));
const size_t n_blk = *in_subtensor.rbegin();
const size_t k_blk = *++in_subtensor.rbegin();
- const auto buffer_b_shape = brgemm_utils::repacking::compute_buffer_b_allocation_shape(k_blk,
- n_blk,
- brgemm_config.wei_k_blk(),
- brgemm_config.wei_n_blk());
-
- OPENVINO_ASSERT(buffer_b_shape.size() == 3, "Unexpected buffer B shape rank");
+ const auto buffer_b_shape =
+ brgemm_utils::repacking::compute_buffer_b_allocation_shape({k_blk, n_blk},
+ brgemm_config.wei_dt(),
+ brgemm_config.wei_k_blk(),
+ brgemm_config.wei_n_blk(),
+ brgemm_config.are_wei_blocked(),
+ brgemm_config.transposed_b());
m_allocation_size =
std::accumulate(buffer_b_shape.cbegin(), buffer_b_shape.cend(), size_t(1), [](size_t a, size_t b) {
return snippets::utils::dynamic_safe_mul(a, b);
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
index 62d42387616..f6ec95c0f0f 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.cpp
@@ -21,6 +21,7 @@
#include "onednn/dnnl.h"
#include "openvino/core/except.hpp"
#include "openvino/core/type.hpp"
+#include "openvino/core/type/element_type.hpp"
#include "openvino/itt.hpp"
#include "snippets/itt.hpp"
#include "snippets/lowered/expression.hpp"
@@ -39,14 +40,14 @@ BrgemmExternalRepackingAdjuster::BrgemmExternalRepackingAdjuster(const ov::snipp
const CPURuntimeConfigurator* configurator)
: snippets::lowered::pass::RuntimeOptimizer(configurator) {
const auto& cpu_config = ov::as_type_ptr<CPURuntimeConfig>(m_configurator->get_config());
- const auto& repacked_inputs_config = cpu_config->repacked_input_config;
+ const auto& input_repackers = cpu_config->input_repackers;
const auto& params = linear_ir->get_parameters();
- for (const auto& [idx, _] : repacked_inputs_config) {
+ for (const auto& [idx, _] : input_repackers) {
OPENVINO_ASSERT(idx < params.size(), "Incorrect index of repacked input");
m_executors[idx] = create_executor(params[idx], configurator->get_cache());
}
- OPENVINO_ASSERT(repacked_inputs_config.size() == m_executors.size(), "Incorrect count of repacked inputs");
+ OPENVINO_ASSERT(input_repackers.size() == m_executors.size(), "Incorrect count of repacked inputs");
}
BrgemmExternalRepackingAdjuster::RepackExecutorPtr BrgemmExternalRepackingAdjuster::create_executor(
@@ -78,24 +79,20 @@ BrgemmExternalRepackingAdjuster::RepackExecutorPtr BrgemmExternalRepackingAdjust
return executor;
}
-VectorDims BrgemmExternalRepackingAdjuster::get_blk_order(size_t shape_rank) {
- VectorDims order(shape_rank - brgemm_kernel_rank);
- std::iota(order.begin(), order.end(), 0);
- const auto last_idx = shape_rank - 1;
- order.insert(order.end(), {last_idx - 1, last_idx, last_idx - 1});
- return order;
-}
-
-VectorDims BrgemmExternalRepackingAdjuster::get_blk_shape(const VectorDims& planar_shape,
- size_t wei_n_blk,
- size_t wei_k_blk) {
- const auto K = *++planar_shape.rbegin();
- const auto N = *planar_shape.rbegin();
- const auto buffer_b_shape = brgemm_utils::repacking::compute_buffer_b_allocation_shape(K, N, wei_k_blk, wei_n_blk);
- OPENVINO_ASSERT(buffer_b_shape.size() == 3, "Unexpected buffer B shape rank");
- VectorDims blk_shape(planar_shape.begin(), planar_shape.end() - brgemm_kernel_rank);
- blk_shape.insert(blk_shape.end(), buffer_b_shape.cbegin(), buffer_b_shape.cend());
- return blk_shape;
+CpuBlockedMemoryDescPtr BrgemmExternalRepackingAdjuster::get_desc(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked,
+ bool is_transposed) {
+ const auto allocation_shape = brgemm_utils::repacking::compute_buffer_b_allocation_shape(planar_shape,
+ prc,
+ wei_k_blk,
+ wei_n_blk,
+ are_wei_blocked,
+ is_transposed);
+ const auto blocked_order = ov::snippets::utils::get_planar_layout(allocation_shape.size());
+ return std::make_shared<CpuBlockedMemoryDesc>(prc, Shape(planar_shape), allocation_shape, blocked_order);
}
void BrgemmExternalRepackingAdjuster::update_kernel(const RepackExecutorPtr& executor,
@@ -108,7 +105,8 @@ void BrgemmExternalRepackingAdjuster::update_kernel(const RepackExecutorPtr& exe
const auto idx = config->is_transposed_B() ? 0 : 1;
const auto copy_wei_stride =
ov::snippets::utils::get_dim_in_stride(shape, layout, idx) * dnnl_data_type_size(config->get_original_wei_dt());
- const auto LDB = brgemm_utils::repacking::compute_LDB(N, config->get_wei_N_blk(), config->are_wei_blocked());
+ const auto LDB =
+ brgemm_utils::repacking::compute_K_blocked_stride(N, config->get_wei_N_blk(), config->are_wei_blocked());
OPENVINO_ASSERT(LDB >= 0, "Invalid LDB value (less than 0)");
config->update(N, N, K, K, copy_wei_stride, LDB);
executor->update_by_config(*config);
@@ -125,6 +123,7 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
const auto& shape = cpu_config->io_shapes[i];
const auto& layout = cpu_config->io_layouts[i];
+ const auto& prc = linear_ir.get_parameters()[i]->get_node()->get_output_element_type(0);
const auto planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);
const auto& K = *++planar_shape.rbegin();
const auto& N = *planar_shape.rbegin();
@@ -132,14 +131,24 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
update_kernel(executor, shape, layout, N, K);
const auto& config = static_cast<const BrgemmCopyBKernelConfig&>(executor->get_config());
- const auto blk_shape = get_blk_shape(planar_shape, config.get_wei_N_blk(), config.get_wei_K_blk());
+
+ const auto buffer_b_allocation_shape =
+ brgemm_utils::repacking::compute_buffer_b_allocation_shape({K, N},
+ prc,
+ config.get_wei_K_blk(),
+ config.get_wei_N_blk(),
+ config.are_wei_blocked(),
+ config.is_transposed_B());
const auto src_dt_size = dnnl_data_type_size(config.get_original_wei_dt());
const auto dst_dt_size = dnnl_data_type_size(config.get_wei_dt());
// src data + dst data per kernel call
const auto src_data = N * K * src_dt_size;
- const auto dst_data =
- std::accumulate(blk_shape.rbegin(), blk_shape.rbegin() + 3, dst_dt_size, std::multiplies<>());
+ const auto dst_data = std::accumulate(buffer_b_allocation_shape.cbegin(),
+ buffer_b_allocation_shape.cend(),
+ size_t(1),
+ std::multiplies<>()) *
+ dst_dt_size;
data_size += src_data + dst_data;
}
@@ -158,29 +167,37 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
const auto& shape = cpu_config->io_shapes[i];
const auto& layout = cpu_config->io_layouts[i];
- auto& repacked_in = cpu_config->repacked_input_config[i];
+ auto& input_repacker = cpu_config->input_repackers[i];
const auto& prc = linear_ir.get_parameters()[i]->get_node()->get_output_element_type(0);
auto planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);
-
- const auto& config = static_cast<const BrgemmCopyBKernelConfig&>(executor->get_config());
- auto blk_shape = get_blk_shape(planar_shape, config.get_wei_N_blk(), config.get_wei_K_blk());
-
- // In parallel impl, each thread needs buffer with only shape [K_blk, N_blk, VNNI] to store repacking data
+ // In parallel impl, each thread needs buffer with only inner blocked shape to store repacking datata
if (is_impl_parallel) {
- std::fill(planar_shape.rbegin() + brgemm_kernel_rank, planar_shape.rend(), 1);
- std::fill(blk_shape.rbegin() + brgemm_kernel_rank + 1, blk_shape.rend(), 1);
+ const auto batch_count = planar_shape.size() - brgemm_kernel_rank;
+ std::fill(planar_shape.begin(), planar_shape.begin() + batch_count, 1);
}
- const auto order = get_blk_order(planar_shape.size());
- const auto desc = std::make_shared<CpuBlockedMemoryDesc>(prc, Shape(planar_shape), blk_shape, order);
+
+ const auto& config = static_cast<const BrgemmCopyBKernelConfig&>(executor->get_config());
+ const auto [blocked_dims, blocked_order] =
+ brgemm_utils::repacking::get_wei_blocked_shape(planar_shape,
+ prc,
+ config.get_wei_K_blk(),
+ config.get_wei_N_blk(),
+ config.are_wei_blocked());
+ const auto desc = get_desc(planar_shape,
+ prc,
+ config.get_wei_K_blk(),
+ config.get_wei_N_blk(),
+ config.are_wei_blocked(),
+ config.is_transposed_B());
// Save original input offsets for input before repacking.
- // If the shape has not been changed, it means that we already created `RepackedInput` for this input
+ // If the shape has not been changed, it means that we already created `InputRepacker` for this input
// on previous pass call and now `cpu_config->io_data_offsets[i]` contains offsets not for original input -
// they were updated for blocked shapes/zeroed for previous initialization and we cannot use them as original
// offsets.
const auto in_offsets =
- shape == cpu_config->latest_shapes[i] ? repacked_in.in_offsets() : cpu_config->io_data_offsets[i];
+ shape == cpu_config->latest_shapes[i] ? input_repacker.in_offsets() : cpu_config->io_data_offsets[i];
// In parallel case Kernel should not add offsets to repacked inputs because
// they will be applied during repacking in execution stage
@@ -188,13 +205,21 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
auto& offsets = cpu_config->io_data_offsets[i];
std::fill(offsets.begin(), offsets.end(), 0);
} else {
- ov::snippets::VectorDims shape_for_offset(cpu_config->tensor_rank - shape.size(), 1);
- shape_for_offset.insert(shape_for_offset.end(), blk_shape.begin(), blk_shape.end());
- m_configurator->compute_offsets(shape_for_offset, i, 0);
+ const auto inner_blocks_num = blocked_dims.size() - planar_shape.size();
+ const auto rank = in_offsets.size() + inner_blocks_num; // to align with src offsets rank
+ OPENVINO_ASSERT(rank >= blocked_dims.size(), "Incorrect target rank for dst offsets");
+
+ ov::snippets::VectorDims shape_for_offset(rank - blocked_dims.size(), 1);
+ shape_for_offset.insert(shape_for_offset.end(), blocked_dims.begin(), blocked_dims.end());
+
+ ov::snippets::VectorDims dst_offsets;
+ ov::snippets::utils::init_strides(shape_for_offset, rank, prc.size(), 0, dst_offsets);
+ dst_offsets.resize(dst_offsets.size() - inner_blocks_num);
+ cpu_config->io_data_offsets[i] = dst_offsets;
}
const auto out_offsets = cpu_config->io_data_offsets[i];
- repacked_in = RepackedInput(p.second->get_kernel(), desc, in_offsets, out_offsets);
+ input_repacker = InputRepacker(p.second->get_kernel(), desc, in_offsets, out_offsets);
}
return true;
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
index b9add88e982..462f43dee3a 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp
@@ -20,16 +20,12 @@
namespace ov::intel_cpu::pass {
-class InitRepackedConstantInputs;
-
/**
* @class BrgemmExternalRepackingAdjuster
* @brief A runtime optimizer that creates the memory descs for BRGEMM inputs which require external repacking.
* The generated memory descs are stored in the CPU runtime config.
*/
class BrgemmExternalRepackingAdjuster : public ov::snippets::lowered::pass::RuntimeOptimizer {
- friend class InitRepackedConstantInputs;
-
public:
OPENVINO_RTTI("BrgemmExternalRepackingAdjuster", "", RuntimeOptimizer)
BrgemmExternalRepackingAdjuster() = default;
@@ -43,8 +39,12 @@ public:
private:
using RepackExecutorPtr = std::shared_ptr<BrgemmCopyBKernelExecutor>;
- static VectorDims get_blk_order(size_t shape_rank);
- static VectorDims get_blk_shape(const VectorDims& planar_shape, size_t wei_n_blk, size_t wei_k_blk);
+ static CpuBlockedMemoryDescPtr get_desc(const ov::snippets::VectorDims& planar_shape,
+ const ov::element::Type& prc,
+ size_t wei_k_blk,
+ size_t wei_n_blk,
+ bool are_wei_blocked,
+ bool is_transposed);
static void update_kernel(const RepackExecutorPtr& executor,
const VectorDims& shape,
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.cpp
deleted file mode 100644
index dafc99da541..00000000000
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-// Copyright (C) 2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "init_repacked_constant_inputs.hpp"
-
-#include <oneapi/dnnl/dnnl.h>
-
-#include <algorithm>
-#include <memory>
-
-#include "cpu_shape.h"
-#include "emitters/snippets/x64/kernel_executors/brgemm_copy_b.hpp"
-#include "external_repacking_adjuster.hpp"
-#include "memory_desc/cpu_blocked_memory_desc.h"
-#include "openvino/core/except.hpp"
-#include "openvino/core/type.hpp"
-#include "openvino/itt.hpp"
-#include "snippets/itt.hpp"
-#include "snippets/lowered/expression.hpp"
-#include "snippets/lowered/linear_ir.hpp"
-#include "snippets/lowered/port_descriptor.hpp"
-#include "snippets/op/memory_access.hpp"
-#include "snippets/op/reorder.hpp"
-#include "snippets/shape_types.hpp"
-#include "snippets/utils/utils.hpp"
-
-namespace ov::intel_cpu::pass {
-
-bool InitRepackedConstantInputs::run(const snippets::lowered::LinearIR& linear_ir) {
- OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::InitRepackedConstantInputs")
-
- bool modified = false;
-
- const auto& params = linear_ir.get_parameters();
- for (const auto& [idx, _] : m_repacked_const_inputs_config) {
- OPENVINO_ASSERT(idx < params.size(), "Incorrect index of repacked input");
-
- const auto& param = params[idx];
- const auto executor = BrgemmExternalRepackingAdjuster::create_executor(params[idx], m_cache);
-
- const auto& shape_infer_seq = ov::snippets::utils::get_first_child_shape_infer_expr_seq(param);
- ov::snippets::lowered::ExpressionPtr desc_expr = param;
- ov::snippets::lowered::PortDescriptorPtr port_desc = nullptr;
- if (!shape_infer_seq.empty()) {
- // [160048] Reorder, as any another ShapeInferOp, should just propagate input shape to output using target
- // order
- // without data movement. However, currently we have to save desc of input of the Reorder
- // to support correct input data offsets calculations.
- // Please, remove this code part when the mentioned ticket is completed.
- const auto& reorder_it = std::find_if(shape_infer_seq.cbegin(),
- shape_infer_seq.cend(),
- [](const ov::snippets::lowered::ExpressionPtr& expr) {
- return ov::is_type<ov::snippets::op::Reorder>(expr->get_node());
- });
- if (reorder_it != shape_infer_seq.cend()) {
- desc_expr = (*reorder_it);
- port_desc = desc_expr->get_input_port_descriptor(0);
- } else {
- desc_expr = shape_infer_seq.back();
- }
- }
-
- if (!port_desc) {
- for (const auto& child_input : desc_expr->get_output_port_connector(0)->get_consumers()) {
- const auto ma =
- std::dynamic_pointer_cast<snippets::modifier::MemoryAccess>(child_input.get_expr()->get_node());
- if (ma && ma->is_memory_access_input_port(child_input.get_index())) {
- port_desc = child_input.get_descriptor_ptr();
- break;
- }
- }
- }
-
- OPENVINO_ASSERT(port_desc, "The target Port Descriptor has not been initialized!");
- const auto& layout = port_desc->get_layout();
- const auto& shape = port_desc->get_shape();
-
- const auto& planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);
- OPENVINO_ASSERT(planar_shape.size() > 1, "Incorrect shape of repacked input of Brgemm");
- const auto& K = *++planar_shape.rbegin();
- const auto& N = *planar_shape.rbegin();
- const auto& dst_prc = param->get_node()->get_output_element_type(0);
-
- BrgemmExternalRepackingAdjuster::update_kernel(executor, shape, layout, N, K);
-
- const auto& config = static_cast<const BrgemmCopyBKernelConfig&>(executor->get_config());
- const auto& blk_shape = BrgemmExternalRepackingAdjuster::get_blk_shape(planar_shape,
- config.get_wei_N_blk(),
- config.get_wei_K_blk());
- const auto& order = BrgemmExternalRepackingAdjuster::get_blk_order(planar_shape.size());
- const auto& desc = std::make_shared<CpuBlockedMemoryDesc>(dst_prc, Shape(planar_shape), blk_shape, order);
-
- const auto src_prc_size = dnnl_data_type_size(config.get_original_wei_dt());
- const auto dst_prc_size = dst_prc.size();
-
- ov::snippets::VectorDims src_offsets;
- ov::snippets::VectorDims dst_offsets;
- ov::snippets::utils::init_strides(shape, shape.size(), src_prc_size, 0, src_offsets);
- ov::snippets::utils::init_strides(blk_shape, blk_shape.size(), dst_prc_size, 0, dst_offsets);
- // Last three dimensions of blocked shapes are processed in the kernel. To align with src, we removed last
- // stride
- dst_offsets.pop_back();
-
- m_repacked_const_inputs_config.at(idx) = RepackedInput(executor->get_kernel(), desc, src_offsets, dst_offsets);
- }
-
- return modified;
-}
-
-} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.hpp
deleted file mode 100644
index 16828a5789c..00000000000
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/lowered/init_repacked_constant_inputs.hpp
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright (C) 2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include <utility>
-
-#include "cache/multi_cache.h"
-#include "emitters/snippets/repacked_input.hpp"
-#include "openvino/core/rtti.hpp"
-#include "snippets/lowered/linear_ir.hpp"
-#include "snippets/lowered/pass/pass.hpp"
-
-namespace ov::intel_cpu::pass {
-
-/**
- * @class InitRepackedConstantInputs
- * @brief The pass initializes RepackedInputConfig of Brgemms' constant inputs which
- * should be repacked at model compilation stage
- */
-class InitRepackedConstantInputs : public ov::snippets::lowered::pass::ConstPass {
-public:
- OPENVINO_RTTI("InitRepackedConstantInputs", "", ov::snippets::lowered::pass::ConstPass)
- InitRepackedConstantInputs(ov::intel_cpu::MultiCacheWeakPtr cache,
- ov::intel_cpu::RepackedInputConfig& repacked_const_inputs_config)
- : m_cache(std::move(cache)),
- m_repacked_const_inputs_config(repacked_const_inputs_config) {}
-
- bool run(const snippets::lowered::LinearIR& linear_ir) override;
-
-private:
- ov::intel_cpu::MultiCacheWeakPtr m_cache;
- ov::intel_cpu::RepackedInputConfig& m_repacked_const_inputs_config;
-};
-
-} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp
new file mode 100644
index 00000000000..b1ad52580d4
--- /dev/null
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.cpp
@@ -0,0 +1,118 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "repack_matmul_weights.hpp"
+
+#include <cstddef>
+#include <memory>
+#include <numeric>
+#include <unordered_set>
+#include <vector>
+
+#include "cpu_memory.h"
+#include "cpu_types.h"
+#include "dnnl_extension_utils.h"
+#include "memory_desc/cpu_memory_desc_utils.h"
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/executors/dnnl/dnnl_utils.hpp"
+#include "openvino/cc/pass/itt.hpp"
+#include "openvino/core/except.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/core/type.hpp"
+#include "openvino/itt.hpp"
+#include "snippets/itt.hpp"
+#include "snippets/lowered/port_descriptor.hpp"
+#include "snippets/op/reorder.hpp"
+#include "snippets/utils/utils.hpp"
+#include "transformations/snippets/x64/op/brgemm_cpu.hpp"
+#include "transformations/snippets/x64/op/brgemm_utils.hpp"
+
+namespace ov::intel_cpu::pass {
+
+using namespace brgemm_utils;
+
+DnnlMemoryDescPtr RepackMatMulWeights::get_src_desc(const VectorDims& shape,
+ const VectorDims& layout,
+ const BrgemmConfig& brgemm_config) {
+ const auto planar_shape = Shape{ov::snippets::utils::get_preordered_vdims(shape, layout)};
+ return MemoryDescUtils::convertToDnnlMemoryDesc(
+ std::make_shared<CpuBlockedMemoryDesc>(brgemm_config.orig_wei_dt(), planar_shape, shape, layout));
+}
+
+DnnlMemoryDescPtr RepackMatMulWeights::get_dst_desc(const Shape& shape, const BrgemmConfig& brgemm_config) {
+ const auto [blocked_dims, blocked_order] =
+ brgemm_utils::repacking::get_wei_blocked_shape(shape.getStaticDims(),
+ brgemm_config.wei_dt(),
+ brgemm_config.wei_k_blk(),
+ brgemm_config.wei_n_blk(),
+ brgemm_config.are_wei_blocked());
+
+ return MemoryDescUtils::convertToDnnlMemoryDesc(
+ std::make_shared<CpuBlockedMemoryDesc>(brgemm_config.wei_dt(), shape, blocked_dims, blocked_order));
+}
+
+bool RepackMatMulWeights::run_on_model(const std::shared_ptr<ov::Model>& model) {
+ RUN_ON_MODEL_SCOPE(RepackMatMulWeights);
+ OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "ov::intel_cpu::pass::RepackMatMulWeights")
+
+ const auto& params = model->get_parameters();
+ std::unordered_set<size_t> weights_idxs;
+ for (const auto& [i, input_repacker] : m_input_repackers) {
+ const auto& parameter = params[i];
+
+ const auto shape_infer_leaf = ov::snippets::utils::get_leaf_node_of_first_child_shape_infer_seq(parameter);
+ const auto& first_child = shape_infer_leaf ? shape_infer_leaf : parameter;
+ const auto consumers = first_child->output(0).get_target_inputs();
+
+ const auto brgemm_cpu = ov::as_type_ptr<BrgemmCPU>(consumers.cbegin()->get_node()->shared_from_this());
+ OPENVINO_ASSERT(consumers.size() == 1 && brgemm_cpu != nullptr, "Expected one consumer - BrgemmCPU");
+
+ const auto& brgemm_config = brgemm_cpu->get_config();
+ if (!brgemm_config.are_wei_constant()) {
+ continue;
+ }
+
+ const auto& orig_src_mem_ptr = m_src_mem_ptrs[i];
+
+ VectorDims shape;
+ VectorDims layout;
+ if (const auto& reorder =
+ ov::as_type_ptr<ov::snippets::op::Reorder>(brgemm_cpu->input_value(1).get_node_shared_ptr())) {
+ const auto& port_desc =
+ ov::snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(reorder->input(0));
+ layout = port_desc->get_layout();
+ shape = ov::snippets::utils::pshape_to_vdims(reorder->get_input_partial_shape(0));
+ } else {
+ shape = orig_src_mem_ptr->getShape().getStaticDims();
+ layout.resize(shape.size());
+ std::iota(layout.begin(), layout.end(), 0);
+ }
+
+ const auto& eng = m_context->getEngine();
+ const auto src_mem_desc = get_src_desc(shape, layout, brgemm_config);
+ const auto dst_mem_desc = get_dst_desc(src_mem_desc->getShape(), brgemm_config);
+ const auto src_mem_ptr = std::make_shared<Memory>(eng, src_mem_desc, orig_src_mem_ptr->getData());
+
+ // Pass empty privateWeightCache because:
+ // - there might be several FCs in Subgraph. Then each FC should have own private cache
+ // - currently, we repack weights only once on model compilation stage - no need to save them to the private
+ // cache
+ m_src_mem_ptrs[i] = ov::intel_cpu::utils::prepareWeightsMemory(src_mem_desc,
+ dst_mem_desc,
+ src_mem_ptr,
+ eng,
+ m_context->getParamsCache(),
+ m_context->getWeightsCache(),
+ nullptr);
+ weights_idxs.insert(i);
+ }
+
+ // Removed already repacked inputs: remaining inputs will be repacked in runtime configurator on inference stage
+ for (const auto& weight_idx : weights_idxs) {
+ m_input_repackers.erase(weight_idx);
+ }
+
+ return !weights_idxs.empty();
+}
+} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp
new file mode 100644
index 00000000000..4fcb87259ee
--- /dev/null
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/repack_matmul_weights.hpp
@@ -0,0 +1,51 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstddef>
+#include <memory>
+#include <set>
+#include <utility>
+
+#include "cpu_memory.h"
+#include "emitters/snippets/input_repacker.hpp"
+#include "graph_context.h"
+#include "memory_desc/dnnl_memory_desc.h"
+#include "nodes/executors/executor.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/pass/pass.hpp"
+#include "transformations/snippets/x64/op/brgemm_utils.hpp"
+
+namespace ov::intel_cpu::pass {
+
+/**
+ * @interface RepackMatMulWeights
+ * @brief The pass calls plugin-helper "ReorderData" which repacks constant inputs of MatMuls to target blocked format
+ * @ingroup snippets
+ */
+class RepackMatMulWeights : public ov::pass::ModelPass {
+public:
+ OPENVINO_MODEL_PASS_RTTI("RepackMatMulWeights");
+ RepackMatMulWeights(GraphContext::CPtr context,
+ ov::intel_cpu::InputRepackerMap& input_repackers,
+ std::vector<MemoryPtr>& src_mem_ptrs)
+ : m_context(std::move(context)),
+ m_input_repackers(input_repackers),
+ m_src_mem_ptrs(src_mem_ptrs) {}
+
+ bool run_on_model(const std::shared_ptr<ov::Model>& model) override;
+
+private:
+ static DnnlMemoryDescPtr get_src_desc(const VectorDims& shape,
+ const VectorDims& layout,
+ const brgemm_utils::BrgemmConfig& brgemm_config);
+ static DnnlMemoryDescPtr get_dst_desc(const Shape& shape, const brgemm_utils::BrgemmConfig& brgemm_config);
+
+ const GraphContext::CPtr m_context;
+ ov::intel_cpu::InputRepackerMap& m_input_repackers;
+ std::vector<MemoryPtr>& m_src_mem_ptrs;
+};
+
+} // namespace ov::intel_cpu::pass
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/fully_connected.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/fully_connected.cpp
index 26bd476f4c3..4cb7d243b7b 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/fully_connected.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/snippets/fully_connected.cpp
@@ -129,12 +129,13 @@ std::vector<std::vector<ov::test::InputShape>> fc_bias_shapes{
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_FullyConnectedBias, MatMulBias,
::testing::Combine(
::testing::ValuesIn(fc_bias_shapes),
- ::testing::ValuesIn(precisions(false)),
+ ::testing::ValuesIn(precisions(true)),
::testing::Values(MatMulType::FullyConnected),
::testing::Values(1), // Subgraph;
::testing::Values(1), // Tokenized MatMul+Bias
::testing::Values(ov::test::utils::DEVICE_CPU),
- ::testing::Values(CPUTestUtils::empty_plugin_config)),
+ ::testing::ValuesIn({CPUTestUtils::empty_plugin_config,
+ CPUTestUtils::cpu_bf16_plugin_config})),
MatMul::getTestCaseName);
INSTANTIATE_TEST_SUITE_P(smoke_Snippets_FullyConnectedBiasQuantized, MatMulBiasQuantized,