Skip to content

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

[Snippets][CPU]Process transposed_b=true of MatMuls via BrgemmCopyB (#29153)

Alexandra Sidorovab903f4695c0a
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 dfceb8cc211..38bcea8ef75 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
@@ -209,6 +209,9 @@ void BrgemmCopyBKernel::init_brgemm_copy_b_kernel(
brgCopyKernelConf.has_zero_point_b = false;
brgCopyKernelConf.src_zp_type = dnnl::impl::cpu::x64::none;
+ brgCopyKernelConf.apply_scales_in_buffer_b = false;
+ brgCopyKernelConf.with_wei_decompression = false;
+
OV_CPU_JIT_EMITTER_ASSERT(matmul::create_brgemm_matmul_copy_b(kernel, &brgCopyKernelConf) == dnnl_success,
"cannot create kernel due to invalid params");
}
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 e1a9f733f83..9bcb8c7daa3 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
@@ -123,6 +123,23 @@ size_t compute_inner_k_block(const ov::element::Type& precision) {
return brgemm_utils::get_elems_in_vec(precision);
}
+ov::snippets::VectorDims compute_buffer_b_allocation_shape(size_t K,
+ size_t N,
+ const ov::element::Type& prc,
+ bool is_transposed) {
+ const size_t new_N = compute_repacked_n_dim(N, prc);
+ // - 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
+ const size_t K_alignment =
+ is_transposed ? brgemm_utils::get_elems_in_vec(prc) : brgemm_utils::compute_vnni_factor(prc);
+ return ov::snippets::VectorDims{new_N, ov::snippets::utils::div_up(K, K_alignment), K_alignment};
+}
+
ov::snippets::lowered::ExpressionPtr get_copy_b_expr(const ov::snippets::lowered::ExpressionPtr& brgemm_expr) {
OPENVINO_ASSERT(ov::is_type<BrgemmCPU>(brgemm_expr->get_node()),
"get_copy_b_expr must be called only for BrgemmCPU node");
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 538c0fb7dfc..6d5442cf3c8 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
@@ -69,6 +69,13 @@ template <typename T,
inline T compute_repacked_n_dim(T n, const ov::element::Type& precision) {
return ov::snippets::utils::rnd_up(n, static_cast<T>(compute_inner_n_block(precision)));
}
+
+/// \brief Computes allocation shape for Buffer between BrgemmCopyB and Brgemm
+ov::snippets::VectorDims compute_buffer_b_allocation_shape(size_t K,
+ size_t N,
+ const ov::element::Type& prc,
+ bool is_transposed);
+
/**
* @brief Retrieves the expression pointer for the brgemm_copy_b expression corresponding to the given BrgemmCPU
* expression.
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 b9735ac319b..a8baaea9189 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
@@ -31,13 +31,8 @@ pass::EliminateBrgemmCopyB::EliminateBrgemmCopyB() {
const auto& in_desc = snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(copy_b_node->input(0));
const auto& layout = in_desc->get_layout();
- auto is_supported_layout = [](const std::vector<size_t>& layout) {
- return layout.empty() || (layout.size() - 1 == layout.back());
- };
-
// TODO [157340]: support external repacking for copyB with compensations
- if (!is_supported_layout(layout) || brgemm_utils::with_compensations(copy_b_node->get_type()) ||
- transformation_callback(copy_b_node)) {
+ if (brgemm_utils::with_compensations(copy_b_node->get_type()) || transformation_callback(copy_b_node)) {
return false;
}
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 10c2964bf06..2e3f26fc0da 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
@@ -45,23 +45,16 @@ void RepackedWeightsBufferExpression::init_allocation_size(
const size_t k_blk = *++in_subtensor.rbegin();
const auto& precision = get_node()->get_input_element_type(0);
- // Repacking buffer shape is set in accordance to OneDNN requirements
- const size_t N_dim = compute_repacked_n_dim(n_blk, precision);
- if (!in_layout.empty() && in_layout.back() != in_layout.size() - 1) {
- // 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
- const auto elems_in_vec = brgemm_utils::get_elems_in_vec(precision);
- m_allocation_size = snippets::utils::dynamic_safe_mul(N_dim, snippets::utils::rnd_up(k_blk, elems_in_vec));
- } else {
- // Low precision repacking writes the result by m_brgemmVNNIFactor * m_inner_n_block 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
- const auto brgemmVNNIFactor = brgemm_utils::compute_vnni_factor(precision);
- OPENVINO_ASSERT(brgemmVNNIFactor > 0, "brgemmVNNIFactor value must be positive.");
- m_allocation_size = snippets::utils::dynamic_safe_mul(N_dim, snippets::utils::rnd_up(k_blk, brgemmVNNIFactor));
- }
+ const auto buffer_b_shape =
+ brgemm_utils::repacking::compute_buffer_b_allocation_shape(k_blk,
+ n_blk,
+ precision,
+ BrgemmCopyB::is_transposed(in_layout));
+ OPENVINO_ASSERT(buffer_b_shape.size() == 3, "Unexpected buffer B shape rank");
+ 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);
+ });
}
CompensationsBufferExpression::CompensationsBufferExpression(
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 82569a66a0c..568701af187 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
@@ -51,14 +51,15 @@ VectorDims BrgemmExternalRepackingAdjuster::get_blk_order(size_t shape_rank) {
return order;
}
-VectorDims BrgemmExternalRepackingAdjuster::get_blk_shape(const VectorDims& planar_shape, ov::element::Type prc) {
- const auto vnni_factor = brgemm_utils::compute_vnni_factor(prc);
+VectorDims BrgemmExternalRepackingAdjuster::get_blk_shape(const VectorDims& planar_shape,
+ ov::element::Type prc,
+ bool is_transposed) {
const auto K = *++planar_shape.rbegin();
const auto N = *planar_shape.rbegin();
- const auto new_K = snippets::utils::div_up(K, vnni_factor);
- const auto new_N = brgemm_utils::repacking::compute_repacked_n_dim(N, prc);
+ const auto buffer_b_shape = brgemm_utils::repacking::compute_buffer_b_allocation_shape(K, N, prc, is_transposed);
+ 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(), {new_K, new_N, vnni_factor});
+ blk_shape.insert(blk_shape.end(), buffer_b_shape.cbegin(), buffer_b_shape.cend());
return blk_shape;
}
@@ -93,7 +94,7 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
const auto& N = *planar_shape.rbegin();
const auto& prc = linear_ir.get_parameters()[i]->get_node()->get_output_element_type(0);
- const auto blk_shape = get_blk_shape(planar_shape, prc);
+ const auto blk_shape = get_blk_shape(planar_shape, prc, BrgemmCopyB::is_transposed(layout));
// src data + dst data per kernel call
const auto src_data = N * K * prc.size();
@@ -116,11 +117,12 @@ bool BrgemmExternalRepackingAdjuster::run(const snippets::lowered::LinearIR& lin
for (const auto& p : m_executors) {
const auto& i = p.first;
const auto& shape = cpu_config->io_shapes[i];
+ const auto& layout = cpu_config->io_layouts[i];
auto& repacked_in = cpu_config->repacked_inputs[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, cpu_config->io_layouts[i]);
- auto blk_shape = get_blk_shape(planar_shape, prc);
+ auto planar_shape = ov::snippets::utils::get_planar_vdims(shape, layout);
+ auto blk_shape = get_blk_shape(planar_shape, prc, BrgemmCopyB::is_transposed(layout));
// In parallel impl, each thread needs buffer with only shape [K_blk, N_blk, VNNI] to store repacking data
if (is_impl_parallel) {
std::fill(planar_shape.rbegin() + brgemm_kernel_rank, planar_shape.rend(), 1);
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 989bfa398a9..a63d1e206cc 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
@@ -31,7 +31,7 @@ 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, ov::element::Type prc);
+ static VectorDims get_blk_shape(const VectorDims& planar_shape, ov::element::Type prc, bool is_transposed);
void update_kernel(const RepackExecutorPtr& executor,
const VectorDims& shape,
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index 11f7aceabe0..3f34c36e30c 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -1353,38 +1353,34 @@ void Transformations::MainSnippets() {
},
snippets::pass::TokenizeSnippets);
- auto mm_supports_transpose_b = [this, ignoreCallback](const std::shared_ptr<const ov::Node>& n) {
+ auto mm_supports_transpose_b = [this](const std::shared_ptr<const ov::Node>& n) {
MAYBE_UNUSED(config.inferencePrecision);
- if (!ignoreCallback) {
- return false;
- }
// Note: BrgemmTPP doesn't support transposed KN natively
// so we should extract transposes for the corresponding matmul nodes
#if defined(SNIPPETS_LIBXSMM_TPP)
// TPP doesn't support dynamic shapes -> there will be BrgemmCPU node
- if (n->is_dynamic())
- return true;
- std::vector<std::vector<size_t>> layouts(3);
- const auto matmul = ov::as_type_ptr<const ov::op::v0::MatMul>(n);
- OPENVINO_ASSERT(matmul, "ExplicitTransposeMatMulInputs callback must be called for matmul node");
- if (matmul->get_transpose_b()) {
- std::vector<size_t> transposed_layout(n->get_input_partial_shape(1).size());
- std::iota(transposed_layout.begin(), transposed_layout.end(), 0);
- std::swap(*transposed_layout.rbegin(), *(transposed_layout.rbegin() + 1));
- layouts[1] = std::move(transposed_layout);
+ if (!n->is_dynamic()) {
+ std::vector<std::vector<size_t>> layouts(3);
+ const auto matmul = ov::as_type_ptr<const ov::op::v0::MatMul>(n);
+ OPENVINO_ASSERT(matmul, "ExplicitTransposeMatMulInputs callback must be called for matmul node");
+ if (matmul->get_transpose_b()) {
+ std::vector<size_t> transposed_layout(n->get_input_partial_shape(1).size());
+ std::iota(transposed_layout.begin(), transposed_layout.end(), 0);
+ std::swap(*transposed_layout.rbegin(), *(transposed_layout.rbegin() + 1));
+ layouts[1] = std::move(transposed_layout);
+ }
+ ov::element::TypeVector precisions;
+ auto push_precision = [&](const ov::element::Type& precision) {
+ if (config.inferencePrecision == ov::element::bf16 && precision == ov::element::f32)
+ precisions.push_back(ov::element::bf16);
+ else
+ precisions.push_back(precision);
+ };
+ push_precision(n->get_input_element_type(0));
+ push_precision(n->get_input_element_type(1));
+ push_precision(n->get_output_element_type(0));
+ return !ov::intel_cpu::tpp::pass::BrgemmToBrgemmTPP::is_supported_brgemm_configuration(layouts, precisions);
}
- ov::element::TypeVector precisions;
- auto push_precision = [&](const ov::element::Type& precision) {
- if (config.inferencePrecision == ov::element::bf16 && precision == ov::element::f32)
- precisions.push_back(ov::element::bf16);
- else
- precisions.push_back(precision);
- };
- push_precision(n->get_input_element_type(0));
- push_precision(n->get_input_element_type(1));
- push_precision(n->get_output_element_type(0));
- if (ov::intel_cpu::tpp::pass::BrgemmToBrgemmTPP::is_supported_brgemm_configuration(layouts, precisions))
- return false;
#endif
return true;
};
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 4cffac67409..e25c8fd43ad 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -277,8 +277,6 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*smoke_LoopForCommon/LoopLayerCPUTest.CompareWithRefs/Input0_IS=\[1..10.1.1..10\]_.*_Input1_IS=\[1..8.1.1..8\]_.*_Input2_IS=\[1..10.\?.1..10\]_TS=.*_types=0_0_1_trip_count_type=.*_trip_count=(1|5)_exec_cond=1_netType=i8.*)",
// Issue: 142997
R"(.*smoke_TestsROIAlign.*)",
- // Issue: 148527
- R"(.*Snippets.*MatMulTransposeB.*i8.*i8.*)",
// Issue: 136881
R"(.*smoke_CompareWithRefs_4D_BitwiseShift_overflow_i32_cast.*_eltwise_op_type=BitwiseLeft.*_model_type=.*(i16|u16).*)",
// Issue: 163083