OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Snippets][CPU][ARM] Enable weights repacking (#35531)
Arseniy Obolenskiyeed9db2e7a7d
diff --git a/src/plugins/intel_cpu/CMakeLists.txt b/src/plugins/intel_cpu/CMakeLists.txtindex e07bb4dd6e1..2e533b0307e 100644--- a/src/plugins/intel_cpu/CMakeLists.txt+++ b/src/plugins/intel_cpu/CMakeLists.txt@@ -183,6 +183,10 @@ if(NOT X86_64)${CMAKE_CURRENT_SOURCE_DIR}/src/transformations/tpp/x64/*)endif()+if(X86_64 OR AARCH64)+ list(APPEND EXCLUDE_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/src/transformations/snippets/common/shape_inference_fallback.cpp)+endif()+if(NOT (AARCH64 OR ARM))list(APPEND EXCLUDE_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/src/transformations/cpu_opset/arm/*${CMAKE_CURRENT_SOURCE_DIR}/src/transformations/tpp/aarch64/*diff --git a/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.cpp b/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.cppindex 61da0e18098..592de3b7677 100644--- a/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.cpp+++ b/src/plugins/intel_cpu/src/emitters/snippets/cpu_runtime_configurator.cpp@@ -26,6 +26,7 @@# include "transformations/snippets/x64/pass/lowered/external_repacking_adjuster.hpp"#endif#ifdef OPENVINO_ARCH_ARM64+# include "transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.hpp"# include "transformations/snippets/aarch64/pass/lowered/gemm_copy_b_loop_ports_adjuster.hpp"#endif#ifdef SNIPPETS_DEBUG_CAPS@@ -76,6 +77,9 @@ void CPURuntimeConfigurator::initialization(const ov::snippets::lowered::LinearIRuntimeOptimizer::register_if_applicable<pass::aarch64::GemmCopyBLoopPortsAdjuster>(m_intermediate_optimizers,linear_ir,this);+ RuntimeOptimizer::register_if_applicable<pass::aarch64::GemmExternalRepackingAdjuster>(m_final_optimizers,+ linear_ir,+ this);#endif}diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.cpp b/src/plugins/intel_cpu/src/nodes/subgraph.cppindex 03c1cf57eba..bfed9a30929 100644--- a/src/plugins/intel_cpu/src/nodes/subgraph.cpp+++ b/src/plugins/intel_cpu/src/nodes/subgraph.cpp@@ -61,9 +61,11 @@# include "snippets/lowered/pass/insert_buffers.hpp"# include "snippets/lowered/pass/insert_loops.hpp"# include "transformations/snippets/aarch64/pass/brgemm_to_gemm_cpu.hpp"+# include "transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.hpp"# include "transformations/snippets/aarch64/pass/lowered/adjust_gemm_copy_b_loop_ports.hpp"# include "transformations/snippets/aarch64/pass/lowered/gemm_cpu_blocking.hpp"# include "transformations/snippets/aarch64/pass/lowered/insert_gemm_copy_buffers.hpp"+# include "transformations/snippets/aarch64/pass/repack_matmul_weights.hpp"#elif defined(OPENVINO_ARCH_RISCV64)# include <nodes/kernels/riscv64/cpu_isa_traits.hpp>@@ -546,7 +548,7 @@ void Subgraph::updateIsDynamic() {}void Subgraph::initConstantRepackedMask() {-#if defined(OPENVINO_ARCH_X86_64)+#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)// Compute the mask while input_repackers is fully populated after data-flow passes.// This value must be stable across repeated prepareParams() calls: BrgemmExternalRepackingAdjuster// erases already-repacked entries from input_repackers at runtime, so a later call to@@ -634,8 +636,11 @@ Subgraph::DataFlowPasses Subgraph::getDataFlowPasses() {ov::snippets::pass::PropagatePrecision,ov::intel_cpu::pass::BrgemmToBrgemmCPU,getConstantInputIndexes());+ SNIPPETS_REGISTER_PASS_RELATIVE_ARM64(Place::Before,+ ov::snippets::pass::PropagatePrecision,+ ov::intel_cpu::pass::BrgemmToGemmCPU);if (has_domain_sensitive_ops()) {-#if defined(OPENVINO_ARCH_X86_64)+#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)const auto cpu_config =ov::as_type_ptr<CPURuntimeConfig>(subgraph_attrs->snippet->get_runtime_configurator()->get_config());#endif@@ -645,19 +650,27 @@ Subgraph::DataFlowPasses Subgraph::getDataFlowPasses() {external_ptrs_idces);SNIPPETS_REGISTER_PASS_RELATIVE_X86_64(Place::After,ov::intel_cpu::pass::BrgemmToBrgemmCPU,- ov::intel_cpu::pass::EliminateBrgemmCopyB,+ ov::intel_cpu::pass::x64::EliminateBrgemmCopyB,cpu_config->input_repackers);SNIPPETS_REGISTER_PASS_RELATIVE_X86_64(Place::After,- ov::intel_cpu::pass::EliminateBrgemmCopyB,- ov::intel_cpu::pass::RepackMatMulWeights,+ ov::intel_cpu::pass::x64::EliminateBrgemmCopyB,+ ov::intel_cpu::pass::x64::RepackMatMulWeights,context,cpu_config->input_repackers,srcMemPtrs);+ SNIPPETS_REGISTER_PASS_RELATIVE_ARM64(Place::After,+ ov::intel_cpu::pass::BrgemmToGemmCPU,+ ov::intel_cpu::pass::aarch64::EliminateGemmCopyB,+ cpu_config->input_repackers,+ getConstantInputIndexes());+ SNIPPETS_REGISTER_PASS_RELATIVE_ARM64(Place::After,+ ov::intel_cpu::pass::aarch64::EliminateGemmCopyB,+ ov::intel_cpu::pass::aarch64::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,- ov::snippets::pass::PropagatePrecision,- ov::intel_cpu::pass::BrgemmToGemmCPU);SNIPPETS_REGISTER_PASS_ABSOLUTE_COMMON(Place::PipelineEnd, ov::intel_cpu::pass::MulAddToFMA);#ifdef SNIPPETS_LIBXSMM_TPP@@ -797,7 +810,7 @@ uint32_t Subgraph::getBroadcastingMask(const std::vector<VectorDims>& input_shapreturn mask;}-#if defined(OPENVINO_ARCH_X86_64)+#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)uint32_t Subgraph::getConstantRepackedMask() const {// Returns a bitmask of inputs pre-packed at compile time (constant weights via RepackMatMulWeights).const auto& preconfigured =diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.h b/src/plugins/intel_cpu/src/nodes/subgraph.hindex e2be0a07a4e..4feaffaef86 100644--- a/src/plugins/intel_cpu/src/nodes/subgraph.h+++ b/src/plugins/intel_cpu/src/nodes/subgraph.h@@ -75,7 +75,7 @@ private:static uint64_t getBodyHash(const std::shared_ptr<snippets::op::Subgraph>& snippet);uint32_t getBroadcastingMask(const std::vector<VectorDims>& input_shapes);void initConstantRepackedMask();-#if defined(OPENVINO_ARCH_X86_64)+#if defined(OPENVINO_ARCH_X86_64) || defined(OPENVINO_ARCH_ARM64)uint32_t getConstantRepackedMask() const;#endifstd::set<size_t> getConstantInputIndexes() const;diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.cpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.cppnew file mode 100644index 00000000000..a4e59664bfd--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.cpp@@ -0,0 +1,26 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "eliminate_gemm_copy_b.hpp"++#include <memory>++#include "openvino/core/except.hpp"+#include "openvino/core/node.hpp"+#include "openvino/core/type.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "transformations/snippets/aarch64/op/gemm_copy_b.hpp"++namespace ov::intel_cpu::pass::aarch64 {++std::shared_ptr<ov::Node> EliminateGemmCopyB::get_copy_b_pattern(const std::shared_ptr<ov::Node>& input) const {+ return ov::pass::pattern::wrap_type<ov::intel_cpu::aarch64::GemmCopyB>({input});+}++bool EliminateGemmCopyB::is_supported_copy_b(const std::shared_ptr<ov::Node>& node) const {+ OPENVINO_ASSERT(ov::is_type<ov::intel_cpu::aarch64::GemmCopyB>(node), "EliminateGemmCopyB expects GemmCopyB node");+ return true;+}++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.hpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.hppnew file mode 100644index 00000000000..c96b277537b--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/eliminate_gemm_copy_b.hpp@@ -0,0 +1,35 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <cstddef>+#include <memory>+#include <set>+#include <utility>++#include "emitters/snippets/input_repacker.hpp"+#include "openvino/core/node.hpp"+#include "transformations/snippets/common/pass/eliminate_copy_b.hpp"++namespace ov::intel_cpu::pass::aarch64 {++/**+ * @interface EliminateGemmCopyB+ * @brief AArch64 specialization of common CopyB elimination for GemmCopyB nodes.+ * @ingroup snippets+ */+class EliminateGemmCopyB : public ov::intel_cpu::pass::EliminateCopyB {+public:+ OPENVINO_MODEL_PASS_RTTI("EliminateGemmCopyB");+ explicit EliminateGemmCopyB(ov::intel_cpu::InputRepackerMap& input_repackers,+ std::set<size_t> compile_time_repacking_idxs)+ : EliminateCopyB(input_repackers, false, std::move(compile_time_repacking_idxs)) {}++private:+ [[nodiscard]] std::shared_ptr<ov::Node> get_copy_b_pattern(const std::shared_ptr<ov::Node>& input) const override;+ [[nodiscard]] bool is_supported_copy_b(const std::shared_ptr<ov::Node>& node) const override;+};++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.cpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.cppnew file mode 100644index 00000000000..e3d613b3b4c--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.cpp@@ -0,0 +1,80 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "external_repacking_adjuster.hpp"++#include <cstddef>++#include "emitters/snippets/cpu_runtime_configurator.hpp"+#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/linear_ir.hpp"+#include "snippets/lowered/pass/runtime_optimizer.hpp"+#include "snippets/shape_types.hpp"+#include "snippets/utils/utils.hpp"+#include "transformations/snippets/aarch64/op/gemm_utils.hpp"++namespace ov::intel_cpu::pass::aarch64 {+namespace {++ov::snippets::VectorDims get_repacked_offsets(const ov::snippets::VectorDims& planar_shape,+ size_t target_rank,+ const ov::element::Type& precision) {+ OPENVINO_ASSERT(planar_shape.size() >= 2, "GEMM weights must have rank >= 2");+ OPENVINO_ASSERT(target_rank >= planar_shape.size(), "Incorrect target rank for repacked GEMM weights offsets");++ const auto K = *++planar_shape.rbegin();+ const auto N = *planar_shape.rbegin();+ OPENVINO_ASSERT(!ov::snippets::utils::is_dynamic_value(N) && !ov::snippets::utils::is_dynamic_value(K),+ "N and K shape should not be dynamic for pre-packed aarch64 GEMM weights");++ const auto packed_bytes = ov::intel_cpu::aarch64::gemm_utils::repacking::get_rhs_packed_size(precision, N, K);+ OPENVINO_ASSERT(packed_bytes % precision.size() == 0, "Unexpected packed weights byte size alignment");++ auto allocation_shape = planar_shape;+ allocation_shape[allocation_shape.size() - 2] = 1;+ allocation_shape[allocation_shape.size() - 1] = packed_bytes / precision.size();++ ov::snippets::VectorDims shape_for_offset(target_rank - allocation_shape.size(), 1);+ shape_for_offset.insert(shape_for_offset.end(), allocation_shape.begin(), allocation_shape.end());++ ov::snippets::VectorDims dst_offsets;+ ov::snippets::utils::init_strides(shape_for_offset, target_rank, precision.size(), 0, dst_offsets);+ return dst_offsets;+}++} // namespace++GemmExternalRepackingAdjuster::GemmExternalRepackingAdjuster(const ov::snippets::lowered::LinearIRCPtr& linear_ir,+ const CPURuntimeConfigurator* configurator)+ : ov::snippets::lowered::pass::RuntimeOptimizer(configurator) {+ const auto& cpu_config = ov::as_type_ptr<CPURuntimeConfig>(m_configurator->get_config());+ const auto& params = linear_ir->get_parameters();+ for (const auto& [idx, input_repacker] : cpu_config->input_repackers) {+ OPENVINO_ASSERT(idx < params.size(), "Incorrect index of repacked input");+ OPENVINO_ASSERT(input_repacker.already_repacked(), "Runtime aarch64 GEMM weights repacking is not supported");+ m_repacked_inputs.push_back(idx);+ }+}++bool GemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& linear_ir) {+ OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "Snippets::GemmExternalRepackingAdjuster")+ const auto& cpu_config = ov::as_type_ptr<CPURuntimeConfig>(m_configurator->get_config());+ for (const auto idx : m_repacked_inputs) {+ const auto& shape = cpu_config->io_shapes[idx];+ const auto& layout = cpu_config->io_layouts[idx];+ const auto& precision = linear_ir.get_parameters()[idx]->get_node()->get_output_element_type(0);+ const auto planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);++ cpu_config->io_data_offsets[idx] =+ get_repacked_offsets(planar_shape, cpu_config->io_data_offsets[idx].size(), precision);+ cpu_config->input_repackers.erase(idx);+ }+ return true;+}++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.hpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.hppnew file mode 100644index 00000000000..18aa9cf1b78--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/external_repacking_adjuster.hpp@@ -0,0 +1,37 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <cstddef>+#include <vector>++#include "emitters/snippets/cpu_runtime_configurator.hpp"+#include "openvino/core/rtti.hpp"+#include "snippets/lowered/linear_ir.hpp"+#include "snippets/lowered/pass/runtime_optimizer.hpp"++namespace ov::intel_cpu::pass::aarch64 {++/**+ * @class GemmExternalRepackingAdjuster+ * @brief A runtime optimizer that adjusts input offsets for aarch64 GEMM inputs pre-packed at compile time.+ */+class GemmExternalRepackingAdjuster : public ov::snippets::lowered::pass::RuntimeOptimizer {+public:+ OPENVINO_RTTI("GemmExternalRepackingAdjuster", "", RuntimeOptimizer)+ GemmExternalRepackingAdjuster() = default;+ GemmExternalRepackingAdjuster(const ov::snippets::lowered::LinearIRCPtr& linear_ir,+ const CPURuntimeConfigurator* configurator);++ bool run(const snippets::lowered::LinearIR& linear_ir) override;+ bool applicable() const override {+ return !m_repacked_inputs.empty();+ }++private:+ std::vector<size_t> m_repacked_inputs;+};++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/insert_gemm_copy_buffers.cpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/insert_gemm_copy_buffers.cppindex 424fc34cb68..9c5dd234d16 100644--- a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/insert_gemm_copy_buffers.cpp+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/lowered/insert_gemm_copy_buffers.cpp@@ -55,8 +55,6 @@ bool InsertGemmCopyBuffers::run(LinearIR& linear_ir, LinearIR::constExprIt beginOPENVINO_ASSERT(copy_b_expr->get_output_count() == 1, "gemm copyb must have only one output");insert_copy_b_buffer(copy_b_expr, insertion_it);modified = true;- } else {- OPENVINO_THROW("GemmCopyB must connect to gemmCPU in subgraph, and not be extracted from the body");}}}diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.cpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.cppnew file mode 100644index 00000000000..34bbe96aa73--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.cpp@@ -0,0 +1,181 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "repack_matmul_weights.hpp"++#include <algorithm>+#include <cstddef>+#include <cstdint>+#include <functional>+#include <memory>+#include <numeric>+#include <optional>++#include "cpu_memory.h"+#include "cpu_types.h"+#include "emitters/snippets/aarch64/kernel_executors/gemm_copy_b.hpp"+#include "graph_context.h"+#include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_x16p32x1b_x16_x16_neon.h"+#include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_x32p16x1b_x32_x32_neon.h"+#include "memory_desc/cpu_blocked_memory_desc.h"+#include "nodes/reorder.h"+#include "openvino/core/except.hpp"+#include "openvino/core/node.hpp"+#include "openvino/core/type.hpp"+#include "openvino/core/type/element_type.hpp"+#include "snippets/utils/utils.hpp"+#include "transformations/snippets/aarch64/op/gemm_cpu.hpp"+#include "transformations/snippets/aarch64/op/gemm_utils.hpp"+#include "transformations/snippets/common/pass/repack_matmul_weights.hpp"++namespace ov::intel_cpu::pass::aarch64 {+namespace {++CpuBlockedMemoryDescPtr get_dst_cpu_desc(const VectorDims& planar_shape, ov::element::Type precision) {+ OPENVINO_ASSERT(planar_shape.size() >= 2, "GEMM weights must have rank >= 2");+ return std::make_shared<CpuBlockedMemoryDesc>(precision, Shape{planar_shape});+}++template <auto rhs_pack_kxn, typename UkernelT>+void repack_matrix(size_t N,+ size_t K,+ size_t row_stride_bytes,+ size_t col_stride_bytes,+ const UkernelT& uk,+ const uint8_t* src,+ uint8_t* dst) {+ const auto n_blk_size = ov::intel_cpu::aarch64::GemmCopyBKernelKaiConfig::get_N_blk();+ const size_t nr = uk.get_nr();+ const size_t kr = uk.get_kr();+ const size_t sr = uk.get_sr();+ const size_t n_blocks = ov::snippets::utils::div_up(N, n_blk_size);+ for (size_t n_block = 0; n_block < n_blocks; n_block++) {+ const size_t n_start = n_block * n_blk_size;+ const size_t n_end = std::min(n_start + n_blk_size, N);+ const size_t n_step = n_end - n_start;+ const auto* src_ptr = src + n_start * col_stride_bytes;+ const size_t packed_off = uk.get_rhs_packed_offset(n_start, K);+ auto* dst_ptr = dst + packed_off;+ rhs_pack_kxn(1,+ n_step,+ K,+ nr,+ kr,+ sr,+ row_stride_bytes,+ const_cast<uint8_t*>(src_ptr),+ nullptr,+ nullptr,+ dst_ptr,+ 0,+ nullptr);+ }+}++void repack_matrix(size_t N, size_t K, ov::element::Type precision, const uint8_t* src, uint8_t* dst) {+ const auto row_stride_bytes = N * precision.size();+ const auto col_stride_bytes = precision.size();+ if (precision == ov::element::f16) {+ repack_matrix<kai_run_rhs_pack_kxn_x16p32x1b_x16_x16_neon>(+ N,+ K,+ row_stride_bytes,+ col_stride_bytes,+ ov::intel_cpu::aarch64::GemmCopyBCompiledKernelF16::ukernel,+ src,+ dst);+ } else if (precision == ov::element::f32) {+ repack_matrix<kai_run_rhs_pack_kxn_x32p16x1b_x32_x32_neon>(+ N,+ K,+ row_stride_bytes,+ col_stride_bytes,+ ov::intel_cpu::aarch64::GemmCopyBCompiledKernelF32::ukernel,+ src,+ dst);+ } else {+ OPENVINO_THROW("Unsupported precision for aarch64 GEMM weights repacking: ", precision.get_type_name());+ }+}++MemoryPtr prepare_weights_memory(const GraphContext::CPtr& context,+ const CpuBlockedMemoryDescPtr& src_desc,+ const MemoryPtr& orig_src_mem_ptr,+ const CpuBlockedMemoryDescPtr& dst_desc,+ bool is_src_planar,+ ov::element::Type precision) {+ const auto planar_shape = src_desc->getShape().getStaticDims();+ OPENVINO_ASSERT(planar_shape.size() >= 2, "GEMM weights must have rank >= 2");+ OPENVINO_ASSERT(std::none_of(planar_shape.begin(),+ planar_shape.end(),+ [](size_t dim) {+ return ov::snippets::utils::is_dynamic_value(dim);+ }),+ "Static weights are expected for aarch64 GEMM weights repacking");++ const auto K = *++planar_shape.rbegin();+ const auto N = *planar_shape.rbegin();+ const auto packed_bytes = ov::intel_cpu::aarch64::gemm_utils::repacking::get_rhs_packed_size(precision, N, K);+ OPENVINO_ASSERT(packed_bytes % precision.size() == 0, "Unexpected packed weights byte size alignment");+ const auto batch =+ std::accumulate(planar_shape.cbegin(), planar_shape.cend() - 2, static_cast<size_t>(1), std::multiplies<>());++ const auto& eng = context->getEngine();+ auto dst_block = std::make_shared<DnnlMemoryBlock>(std::make_unique<MemoryBlockWithReuse>());+ dst_block->resize(batch * packed_bytes);+ auto dst_mem = std::make_shared<Memory>(eng, dst_desc, dst_block);+ if (N == 0 || K == 0) {+ return dst_mem;+ }++ auto repack_matrices = [&](const uint8_t* src) {+ const auto src_matrix_bytes = K * N * precision.size();+ auto* dst = dst_mem->getDataAs<uint8_t>();+ context->getCpuParallel()->parallel_for(batch, [&](size_t batch_idx) {+ repack_matrix(N, K, precision, src + batch_idx * src_matrix_bytes, dst + batch_idx * packed_bytes);+ });+ };++ if (is_src_planar) {+ repack_matrices(orig_src_mem_ptr->getDataAs<const uint8_t>());+ } else {+ Memory src_mem{eng, src_desc, orig_src_mem_ptr->getData()};+ Memory planar_mem{eng, std::make_shared<CpuBlockedMemoryDesc>(precision, Shape{planar_shape})};+ // KAI packers consume dense KxN matrices, while the original constant can keep a plugin-specific layout.+ // Normalize to planar memory before applying the KAI packing kernel.+ node::Reorder::reorderData(src_mem,+ planar_mem,+ context->getParamsCache(),+ context->getCpuParallel()->get_thread_pool());+ repack_matrices(planar_mem.getDataAs<const uint8_t>());+ }++ // Do not use the shared weights cache here: snippets repacks constants once during subgraph compilation, and each+ // extracted input needs its own Memory object for the runtime configurator.+ return dst_mem;+}++} // namespace++std::optional<RepackMatMulWeights::RepackedMatMulWeights> RepackMatMulWeights::repack(+ const std::shared_ptr<ov::Node>& consumer,+ const RepackMatMulWeights::MatMulWeightsSource& source,+ const MemoryPtr& orig_src_mem_ptr) {+ const auto gemm_cpu = ov::as_type_ptr<ov::intel_cpu::aarch64::GemmCPU>(consumer);+ OPENVINO_ASSERT(gemm_cpu != nullptr, "Expected one consumer - GemmCPU");++ const auto precision = gemm_cpu->get_input_element_type(1);+ const auto src_desc = get_src_cpu_desc(source, precision);+ const auto planar_shape = src_desc->getShape().getStaticDims();+ const auto dst_desc = get_dst_cpu_desc(planar_shape, precision);+ return RepackedMatMulWeights{prepare_weights_memory(m_context,+ src_desc,+ orig_src_mem_ptr,+ dst_desc,+ ov::snippets::utils::is_planar_layout(source.layout),+ precision),+ dst_desc};+}++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.hpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.hppnew file mode 100644index 00000000000..37a39dbb569--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/pass/repack_matmul_weights.hpp@@ -0,0 +1,40 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <memory>+#include <optional>+#include <utility>+#include <vector>++#include "cpu_memory.h"+#include "openvino/core/node.hpp"+#include "transformations/snippets/common/pass/repack_matmul_weights.hpp"++namespace ov::intel_cpu::pass::aarch64 {++/**+ * @interface RepackMatMulWeights+ * @brief AArch64 specialization of MatMul weights repacking for GemmCPU.+ * @ingroup snippets+ */+class RepackMatMulWeights : public ov::intel_cpu::pass::RepackMatMulWeights {+public:+ OPENVINO_MODEL_PASS_RTTI("RepackMatMulWeights");+ RepackMatMulWeights(GraphContext::CPtr context,+ ov::intel_cpu::InputRepackerMap& input_repackers,+ std::vector<MemoryPtr>& src_mem_ptrs)+ : ov::intel_cpu::pass::RepackMatMulWeights(std::move(context), input_repackers, src_mem_ptrs) {}++private:+ [[nodiscard]] std::optional<RepackedMatMulWeights> repack(const std::shared_ptr<ov::Node>& consumer,+ const MatMulWeightsSource& source,+ const MemoryPtr& orig_src_mem_ptr) override;+ [[nodiscard]] bool supports_runtime_repacking() const override {+ return false;+ }+};++} // namespace ov::intel_cpu::pass::aarch64diff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.cpp b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.cppnew file mode 100644index 00000000000..3e1f55f54aa--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.cpp@@ -0,0 +1,98 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "eliminate_copy_b.hpp"++#include <cstddef>+#include <memory>++#include "openvino/cc/pass/itt.hpp"+#include "openvino/core/except.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/core/model.hpp"+#include "openvino/core/node.hpp"+#include "openvino/core/type.hpp"+#include "openvino/itt.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/pass/pattern/matcher.hpp"+#include "openvino/pass/pattern/op/optional.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "snippets/itt.hpp"+#include "snippets/lowered/port_descriptor.hpp"+#include "snippets/op/rank_normalization.hpp"+#include "snippets/op/reorder.hpp"+#include "snippets/utils/utils.hpp"++namespace ov::intel_cpu {++bool pass::EliminateCopyB::should_extract(size_t param_idx) const {+ return m_runtime_repacking_supported || m_compile_time_repacking_idxs.count(param_idx) > 0;+}++bool pass::EliminateCopyB::run_on_model(const std::shared_ptr<ov::Model>& model) {+ RUN_ON_MODEL_SCOPE(EliminateCopyB);+ OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "ov::intel_cpu::pass::EliminateCopyB")++ auto m_param = ov::pass::pattern::wrap_type<ov::op::v0::Parameter>();+ auto m_rank_norm = ov::pass::pattern::optional<ov::snippets::op::RankNormalization>(m_param);+ auto m_copy_b = get_copy_b_pattern(m_rank_norm);+ auto matcher = std::make_shared<ov::pass::pattern::Matcher>(m_copy_b);++ bool status = false;+ for (const auto& n : model->get_ordered_ops()) {+ if (!matcher->match(n)) {+ continue;+ }++ const auto& pattern_map = matcher->get_pattern_value_map();+ const auto& copy_b_out = pattern_map.at(m_copy_b);+ const auto copy_b_node = copy_b_out.get_node_shared_ptr();+ OPENVINO_ASSERT(copy_b_node, "CopyB node is null in EliminateCopyB transformation");+ if (!is_supported_copy_b(copy_b_node) || transformation_callback(copy_b_node)) {+ return false;+ }++ const auto& param = ov::as_type_ptr<ov::op::v0::Parameter>(pattern_map.at(m_param).get_node_shared_ptr());+ const auto param_idx = static_cast<size_t>(model->get_parameter_index(param));+ OPENVINO_ASSERT(param_idx < model->get_parameters().size(),+ "Parameter index is invalid in EliminateCopyB transformation");+ if (!should_extract(param_idx)) {+ continue;+ }++ const auto& in_desc = snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(copy_b_node->input(0));+ const auto& layout = in_desc->get_layout();++ // Update external repacking config for the further pipeline stages to mark this input as repacked.+ 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.+ param->set_element_type(copy_b_node->get_output_element_type(0));+ // Note: validation is called manually since set_element_type doesn't update output element type.+ param->validate_and_infer_types();+ if (pattern_map.count(m_rank_norm)) {+ pattern_map.at(m_rank_norm).get_node_shared_ptr()->validate_and_infer_types();+ }++ // If there is non-planar layout, insert Reorder to support shape inference.+ if (!ov::snippets::utils::is_planar_layout(layout)) {+ const auto& subtensor = in_desc->get_subtensor();+ const auto& reorder = std::make_shared<ov::snippets::op::Reorder>(copy_b_node->input_value(0), layout);+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(reorder->input(0), subtensor, layout);+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(reorder->output(0), subtensor);++ OPENVINO_ASSERT(ov::replace_node_update_name(copy_b_node, reorder),+ "Failed to replace output in EliminateCopyB transformation");+ } else {+ OPENVINO_ASSERT(ov::replace_output_update_name(copy_b_out, copy_b_node->input_value(0)),+ "Failed to replace output in EliminateCopyB transformation");+ }++ status = true;+ }++ return status;+}+} // namespace ov::intel_cpudiff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.hpp b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.hppnew file mode 100644index 00000000000..67479c2a930--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/eliminate_copy_b.hpp@@ -0,0 +1,51 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <cstddef>+#include <memory>+#include <set>+#include <utility>++#include "emitters/snippets/input_repacker.hpp"+#include "openvino/core/model.hpp"+#include "openvino/core/node.hpp"+#include "openvino/pass/pass.hpp"++namespace ov::intel_cpu::pass {++/**+ * @interface EliminateCopyB+ * @brief Identifies CopyB nodes which can be inferred outside the Subgraph.+ * If this is possible, CopyB node is removed, and the external repacking is configured on the further pipeline stages.+ *+ * @ingroup snippets+ */+class EliminateCopyB : public ov::pass::ModelPass {+public:+ OPENVINO_MODEL_PASS_RTTI("EliminateCopyB");++ bool run_on_model(const std::shared_ptr<ov::Model>& model) override;++protected:+ EliminateCopyB(ov::intel_cpu::InputRepackerMap& input_repackers,+ bool runtime_repacking_supported,+ std::set<size_t> compile_time_repacking_idxs)+ : m_input_repackers(input_repackers),+ m_runtime_repacking_supported(runtime_repacking_supported),+ m_compile_time_repacking_idxs(std::move(compile_time_repacking_idxs)) {}++private:+ [[nodiscard]] virtual std::shared_ptr<ov::Node> get_copy_b_pattern(+ const std::shared_ptr<ov::Node>& input) const = 0;+ [[nodiscard]] virtual bool is_supported_copy_b(const std::shared_ptr<ov::Node>& node) const = 0;+ [[nodiscard]] bool should_extract(size_t param_idx) const;++ ov::intel_cpu::InputRepackerMap& m_input_repackers;+ bool m_runtime_repacking_supported = false;+ std::set<size_t> m_compile_time_repacking_idxs;+};++} // namespace ov::intel_cpu::passdiff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.cpp b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.cppnew file mode 100644index 00000000000..fceecb9e096--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.cpp@@ -0,0 +1,88 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "repack_matmul_weights.hpp"++#include <cstddef>+#include <memory>+#include <numeric>+#include <unordered_set>+#include <utility>++#include "cpu_memory.h"+#include "cpu_types.h"+#include "memory_desc/cpu_blocked_memory_desc.h"+#include "openvino/cc/pass/itt.hpp"+#include "openvino/core/except.hpp"+#include "openvino/core/model.hpp"+#include "openvino/core/node.hpp"+#include "openvino/core/type.hpp"+#include "openvino/core/type/element_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"++namespace ov::intel_cpu::pass {++RepackMatMulWeights::MatMulWeightsSource RepackMatMulWeights::get_weights_source(+ const std::shared_ptr<ov::Node>& matmul_node,+ [[maybe_unused]] const MemoryPtr& orig_src_mem_ptr) {+ if (const auto& reorder =+ ov::as_type_ptr<ov::snippets::op::Reorder>(matmul_node->input_value(1).get_node_shared_ptr())) {+ const auto& port_desc = ov::snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(reorder->input(0));+ return {ov::snippets::utils::pshape_to_vdims(reorder->get_input_partial_shape(0)), port_desc->get_layout()};+ }++ auto shape = ov::snippets::utils::pshape_to_vdims(matmul_node->get_input_partial_shape(1));+ VectorDims layout(shape.size());+ std::iota(layout.begin(), layout.end(), 0);+ return {std::move(shape), std::move(layout)};+}++CpuBlockedMemoryDescPtr RepackMatMulWeights::get_src_cpu_desc(const RepackMatMulWeights::MatMulWeightsSource& source,+ ov::element::Type precision) {+ const auto planar_shape = Shape{ov::snippets::utils::get_preordered_vdims(source.shape, source.layout)};+ return std::make_shared<CpuBlockedMemoryDesc>(precision, planar_shape, source.shape, source.layout);+}++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& repacker_entry : m_input_repackers) {+ const auto i = repacker_entry.first;+ OPENVINO_ASSERT(i < params.size(), "Incorrect index of externally repacked weights");+ OPENVINO_ASSERT(i < m_src_mem_ptrs.size(), "Incorrect memory index of externally repacked weights");+ 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();+ OPENVINO_ASSERT(consumers.size() == 1, "Expected one consumer for externally repacked weights");+ const auto consumer = consumers.cbegin()->get_node()->shared_from_this();++ const auto& orig_src_mem_ptr = m_src_mem_ptrs[i];+ const auto repacked = repack(consumer, get_weights_source(consumer, orig_src_mem_ptr), orig_src_mem_ptr);+ if (!repacked) {+ OPENVINO_ASSERT(supports_runtime_repacking(),+ "Failed to repack weights input ",+ i,+ " for ",+ consumer->get_friendly_name(),+ ": runtime repacking is not supported");+ continue;+ }++ m_src_mem_ptrs[i] = repacked->memory;+ m_input_repackers[i] = InputRepacker(nullptr, repacked->desc, {}, {});+ weights_idxs.insert(i);+ }++ return !weights_idxs.empty();+}+} // namespace ov::intel_cpu::passdiff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.hpp b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.hppnew file mode 100644index 00000000000..e49b7f98fdc--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/pass/repack_matmul_weights.hpp@@ -0,0 +1,75 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <memory>+#include <optional>+#include <utility>+#include <vector>++#include "cpu_memory.h"+#include "emitters/snippets/input_repacker.hpp"+#include "graph_context.h"+#include "memory_desc/cpu_blocked_memory_desc.h"+#include "openvino/core/model.hpp"+#include "openvino/core/node.hpp"+#include "openvino/pass/pass.hpp"++namespace ov::intel_cpu::pass {++/**+ * @interface RepackMatMulWeights+ * @brief Repack constant MatMul weights to the target backend 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;++protected:+ struct MatMulWeightsSource {+ VectorDims shape;+ VectorDims layout;+ };++ struct RepackedMatMulWeights {+ MemoryPtr memory;+ CpuBlockedMemoryDescPtr desc;+ };++ [[nodiscard]] static MatMulWeightsSource get_weights_source(const std::shared_ptr<ov::Node>& matmul_node,+ const MemoryPtr& orig_src_mem_ptr);+ [[nodiscard]] static CpuBlockedMemoryDescPtr get_src_cpu_desc(const MatMulWeightsSource& source,+ ov::element::Type precision);++ /**+ * @brief Repack a constant MatMul weights input for a backend-specific MatMul/GEMM consumer.+ * @param consumer Backend MatMul/GEMM node that consumes the constant weights.+ * @param source Logical weights shape and layout after CopyB extraction. When extraction inserts a graph Reorder,+ * this metadata is not recoverable from @p orig_src_mem_ptr alone.+ * @param orig_src_mem_ptr Original constant weights memory buffer.+ * @return Repacked weights memory with its CPU descriptor, or std::nullopt when this consumer cannot be repacked.+ */+ [[nodiscard]] virtual std::optional<RepackedMatMulWeights> repack(const std::shared_ptr<ov::Node>& consumer,+ const MatMulWeightsSource& source,+ const MemoryPtr& orig_src_mem_ptr) = 0;+ [[nodiscard]] virtual bool supports_runtime_repacking() const {+ return true;+ }++ const GraphContext::CPtr m_context;+ ov::intel_cpu::InputRepackerMap& m_input_repackers;+ std::vector<MemoryPtr>& m_src_mem_ptrs;+};++} // namespace ov::intel_cpu::passdiff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.cpp b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.cppindex e62d7814c84..62bcaae9b9a 100644--- a/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.cpp+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.cpp@@ -72,9 +72,4 @@ ShapeInferPtr CPUShapeInferSnippetsFactory::get_specific_op_shape_infer(const ovreturn {};}-#if !defined(OPENVINO_ARCH_X86_64) && !defined(OPENVINO_ARCH_ARM64)-const CPUShapeInferSnippetsFactory::TRegistry CPUShapeInferSnippetsFactory::specific_ops_registry =- detail::make_common_cpu_shape_infer_registry();-#endif-} // namespace ov::snippetsdiff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference_fallback.cpp b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference_fallback.cppnew file mode 100644index 00000000000..4ec1874e6ed--- /dev/null+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference_fallback.cpp@@ -0,0 +1,12 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/snippets/common/shape_inference.hpp"++namespace ov::snippets {++const CPUShapeInferSnippetsFactory::TRegistry CPUShapeInferSnippetsFactory::specific_ops_registry =+ detail::make_common_cpu_shape_infer_registry();++} // namespace ov::snippetsdiff --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.cppindex d0c29899a0b..75448007cad 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@@ -4,91 +4,25 @@#include "eliminate_brgemm_copy_b.hpp"-#include <cstddef>#include <memory>-#include <vector>-#include "openvino/cc/pass/itt.hpp"#include "openvino/core/except.hpp"-#include "openvino/core/graph_util.hpp"-#include "openvino/core/model.hpp"+#include "openvino/core/node.hpp"#include "openvino/core/type.hpp"-#include "openvino/itt.hpp"-#include "openvino/op/parameter.hpp"-#include "openvino/pass/pattern/matcher.hpp"-#include "openvino/pass/pattern/op/optional.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"-#include "snippets/itt.hpp"-#include "snippets/lowered/port_descriptor.hpp"-#include "snippets/op/rank_normalization.hpp"-#include "snippets/op/reorder.hpp"-#include "snippets/utils/utils.hpp"#include "transformations/snippets/x64/op/brgemm_copy_b.hpp"-namespace ov::intel_cpu {+namespace ov::intel_cpu::pass::x64 {-bool pass::EliminateBrgemmCopyB::run_on_model(const std::shared_ptr<ov::Model>& model) {- RUN_ON_MODEL_SCOPE(EliminateBrgemmCopyB);- OV_ITT_SCOPED_TASK(ov::pass::itt::domains::SnippetsTransform, "ov::intel_cpu::pass::EliminateBrgemmCopyB")-- auto m_param = ov::pass::pattern::wrap_type<ov::op::v0::Parameter>();- auto m_rank_norm = ov::pass::pattern::optional<ov::snippets::op::RankNormalization>(m_param);- auto m_copy_b = ov::pass::pattern::wrap_type<BrgemmCopyB>({m_rank_norm});- auto matcher = std::make_shared<ov::pass::pattern::Matcher>(m_copy_b);-- bool status = false;- for (const auto& n : model->get_ordered_ops()) {- if (!matcher->match(n)) {- continue;- }-- const auto& pattern_map = matcher->get_pattern_value_map();- const auto& copy_b_out = pattern_map.at(m_copy_b);- const auto copy_b_node = ov::as_type_ptr<BrgemmCopyB>(copy_b_out.get_node_shared_ptr());- OPENVINO_ASSERT(copy_b_node, "BrgemmCopyB node is null in EliminateBrgemmCopyB transformation");- const auto& brgemm_config = copy_b_node->get_config();-- const auto& in_desc = snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(copy_b_node->input(0));- const auto& layout = in_desc->get_layout();-- // TODO [157340]: support external repacking for copyB with compensations- if (brgemm_config.with_compensations() || transformation_callback(copy_b_node)) {- return false;- }-- const auto& param = ov::as_type_ptr<ov::op::v0::Parameter>(pattern_map.at(m_param).get_node_shared_ptr());- const auto param_idx = static_cast<size_t>(model->get_parameter_index(param));- 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- 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- param->set_element_type(copy_b_node->get_config().wei_dt());- // Note: validation is called manually since set_element_type doesn't update output element type- param->validate_and_infer_types();- if (pattern_map.count(m_rank_norm)) {- pattern_map.at(m_rank_norm).get_node_shared_ptr()->validate_and_infer_types();- }-- // If there is non-planar layout, we should insert reshape to support shape inference- if (!ov::snippets::utils::is_planar_layout(layout)) {- const auto& subtensor = in_desc->get_subtensor();- const auto& reorder = std::make_shared<ov::snippets::op::Reorder>(copy_b_node->input_value(0), layout);- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(reorder->input(0), subtensor, layout);- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(reorder->output(0), subtensor);-- OPENVINO_ASSERT(ov::replace_node_update_name(copy_b_node, reorder),- "Failed to replace output in EliminateBrgemmCopyB transformation");- } else {- OPENVINO_ASSERT(ov::replace_output_update_name(copy_b_out, copy_b_node->input_value(0)),- "Failed to replace output in EliminateBrgemmCopyB transformation");- }-- status = true;- }+std::shared_ptr<ov::Node> EliminateBrgemmCopyB::get_copy_b_pattern(const std::shared_ptr<ov::Node>& input) const {+ return ov::pass::pattern::wrap_type<ov::intel_cpu::BrgemmCopyB>({input});+}- return status;+bool EliminateBrgemmCopyB::is_supported_copy_b(const std::shared_ptr<ov::Node>& node) const {+ const auto copy_b = ov::as_type_ptr<ov::intel_cpu::BrgemmCopyB>(node);+ OPENVINO_ASSERT(copy_b, "EliminateBrgemmCopyB expects BrgemmCopyB node");+ // TODO [157340]: support external repacking for copyB with compensations.+ return !copy_b->get_config().with_compensations();}-} // namespace ov::intel_cpu++} // namespace ov::intel_cpu::pass::x64diff --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.hppindex 9f505193adc..7909e5845ef 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@@ -6,33 +6,27 @@#include <cstddef>#include <memory>-#include <set>-#include <utility>#include "emitters/snippets/input_repacker.hpp"-#include "openvino/core/model.hpp"-#include "openvino/pass/pass.hpp"+#include "openvino/core/node.hpp"+#include "transformations/snippets/common/pass/eliminate_copy_b.hpp"-namespace ov::intel_cpu::pass {+namespace ov::intel_cpu::pass::x64 {/*** @interface EliminateBrgemmCopyB- * @brief EliminateBrgemmCopyB identifies BrgemmCopyB nodes which can be inferred outside the Subgraph.- * If this is possible, CopyB node is removed, and the external repacking is configured on the further pipeline stages- * in RuntimeConfigurator.- *+ * @brief x64 specialization of common CopyB elimination for BrgemmCopyB nodes.* @ingroup snippets*/-class EliminateBrgemmCopyB : public ov::pass::ModelPass {+class EliminateBrgemmCopyB : public ov::intel_cpu::pass::EliminateCopyB {public:OPENVINO_MODEL_PASS_RTTI("EliminateBrgemmCopyB");explicit EliminateBrgemmCopyB(ov::intel_cpu::InputRepackerMap& input_repackers)- : m_input_repackers(input_repackers) {}-- bool run_on_model(const std::shared_ptr<ov::Model>& model) override;+ : EliminateCopyB(input_repackers, true, {}) {}private:- ov::intel_cpu::InputRepackerMap& m_input_repackers;+ [[nodiscard]] std::shared_ptr<ov::Node> get_copy_b_pattern(const std::shared_ptr<ov::Node>& input) const override;+ [[nodiscard]] bool is_supported_copy_b(const std::shared_ptr<ov::Node>& node) const override;};-} // namespace ov::intel_cpu::pass+} // namespace ov::intel_cpu::pass::x64diff --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.cppindex d3f4b16fec5..decc99d6f3e 100644--- 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@@ -4,41 +4,29 @@#include "repack_matmul_weights.hpp"-#include <cstddef>#include <memory>-#include <numeric>-#include <unordered_set>-#include <vector>+#include <optional>+#include <utility>#include "cpu_memory.h"-#include "cpu_types.h"#include "dnnl_extension_utils.h"#include "memory_desc/cpu_blocked_memory_desc.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/node.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 {+namespace ov::intel_cpu::pass::x64 {using namespace brgemm_utils;-DnnlMemoryDescPtr RepackMatMulWeights::get_src_desc(const VectorDims& shape,- const VectorDims& layout,+DnnlMemoryDescPtr RepackMatMulWeights::get_src_desc(const RepackMatMulWeights::MatMulWeightsSource& source,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));+ return MemoryDescUtils::convertToDnnlMemoryDesc(get_src_cpu_desc(source, brgemm_config.orig_wei_dt()));}CpuBlockedMemoryDescPtr RepackMatMulWeights::get_dst_cpu_desc(const Shape& shape, const BrgemmConfig& brgemm_config) {@@ -56,66 +44,36 @@ DnnlMemoryDescPtr RepackMatMulWeights::get_dst_desc(const Shape& shape, const Brreturn MemoryDescUtils::convertToDnnlMemoryDesc(get_dst_cpu_desc(shape, brgemm_config));}-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")+std::optional<RepackMatMulWeights::RepackedMatMulWeights> RepackMatMulWeights::repack(+ const std::shared_ptr<ov::Node>& consumer,+ const RepackMatMulWeights::MatMulWeightsSource& source,+ const MemoryPtr& orig_src_mem_ptr) {+ const auto brgemm_cpu = ov::as_type_ptr<BrgemmCPU>(consumer);+ OPENVINO_ASSERT(brgemm_cpu != nullptr, "Expected one consumer - BrgemmCPU");- 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,- m_context->getCpuParallel()->get_thread_pool());-- m_input_repackers[i] =- InputRepacker(nullptr, get_dst_cpu_desc(src_mem_desc->getShape(), brgemm_config), {}, {});- weights_idxs.insert(i);+ const auto& brgemm_config = brgemm_cpu->get_config();+ if (!brgemm_config.are_wei_constant()) {+ return std::nullopt;}- return !weights_idxs.empty();+ const auto& eng = m_context->getEngine();+ const auto src_mem_desc = get_src_desc(source, 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+ auto repacked_mem = ov::intel_cpu::utils::prepareWeightsMemory(src_mem_desc,+ dst_mem_desc,+ src_mem_ptr,+ eng,+ m_context->getParamsCache(),+ m_context->getWeightsCache(),+ nullptr,+ m_context->getCpuParallel()->get_thread_pool());++ return RepackedMatMulWeights{std::move(repacked_mem), get_dst_cpu_desc(src_mem_desc->getShape(), brgemm_config)};}-} // namespace ov::intel_cpu::pass++} // namespace ov::intel_cpu::pass::x64diff --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.hppindex c8108ca4a45..60af7d6351a 100644--- 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@@ -4,50 +4,43 @@#pragma once-#include <cstddef>#include <memory>-#include <set>+#include <optional>#include <utility>+#include <vector>#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 "openvino/core/node.hpp"+#include "transformations/snippets/common/pass/repack_matmul_weights.hpp"#include "transformations/snippets/x64/op/brgemm_utils.hpp"-namespace ov::intel_cpu::pass {+namespace ov::intel_cpu::pass::x64 {/*** @interface RepackMatMulWeights- * @brief The pass calls plugin-helper "ReorderData" which repacks constant inputs of MatMuls to target blocked format+ * @brief x64 specialization of MatMul weights repacking for BrgemmCPU.* @ingroup snippets*/-class RepackMatMulWeights : public ov::pass::ModelPass {+class RepackMatMulWeights : public ov::intel_cpu::pass::RepackMatMulWeights {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;+ : ov::intel_cpu::pass::RepackMatMulWeights(std::move(context), input_repackers, src_mem_ptrs) {}private:- static DnnlMemoryDescPtr get_src_desc(const VectorDims& shape,- const VectorDims& layout,- const brgemm_utils::BrgemmConfig& brgemm_config);- static CpuBlockedMemoryDescPtr get_dst_cpu_desc(const Shape& shape,- 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;+ [[nodiscard]] static DnnlMemoryDescPtr get_src_desc(const MatMulWeightsSource& source,+ const brgemm_utils::BrgemmConfig& brgemm_config);+ [[nodiscard]] static CpuBlockedMemoryDescPtr get_dst_cpu_desc(const Shape& shape,+ const brgemm_utils::BrgemmConfig& brgemm_config);+ [[nodiscard]] static DnnlMemoryDescPtr get_dst_desc(const Shape& shape,+ const brgemm_utils::BrgemmConfig& brgemm_config);++ [[nodiscard]] std::optional<RepackedMatMulWeights> repack(const std::shared_ptr<ov::Node>& consumer,+ const MatMulWeightsSource& source,+ const MemoryPtr& orig_src_mem_ptr) override;};-} // namespace ov::intel_cpu::pass+} // namespace ov::intel_cpu::pass::x64