Skip to content

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

[Snippets][CPU] Fix full dim subtensor setting for port descriptor (#29457)

Arseniy Obolenskiyc7af98a5ead6
diff --git a/src/common/snippets/include/snippets/lowered/port_descriptor.hpp b/src/common/snippets/include/snippets/lowered/port_descriptor.hpp
index f6cd126f668..1a61e80e0ab 100644
--- a/src/common/snippets/include/snippets/lowered/port_descriptor.hpp
+++ b/src/common/snippets/include/snippets/lowered/port_descriptor.hpp
@@ -42,8 +42,8 @@ public:
const Reg& get_reg() const { return m_reg; }
void set_shape(const VectorDims& tensor);
- void set_layout(const std::vector<size_t>& layout) { m_layout = layout; }
- void set_subtensor(const VectorDims& subtensor) { m_subtensor_shape = subtensor; }
+ void set_layout(const std::vector<size_t>& layout);
+ void set_subtensor(const VectorDims& subtensor);
void set_reg(Reg reg) { m_reg = std::move(reg); }
void set_reg_type(RegType type) { m_reg.type = type; }
void set_reg_idx(size_t idx) { m_reg.idx = idx; }
diff --git a/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp b/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp
index ea7a98d9a7a..85beb784db3 100644
--- a/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp
+++ b/src/common/snippets/src/lowered/pass/propagate_subtensors.cpp
@@ -153,8 +153,12 @@ void propagate_updated_subtensor_through_loop(const LinearIR& linear_ir,
// After subtensor propagation, the original shapes must be restored
for (const auto& elem : original_shapes)
elem.first->set_shape(elem.second);
- for (auto expr_it = begin; expr_it != shape_inference_end_it; expr_it++)
- (*expr_it)->updateShapes();
+ for (auto expr_it = begin; expr_it != shape_inference_end_it; expr_it++) {
+ const auto expr = *expr_it;
+ if (ov::is_type<snippets::op::LoopBase>(expr->get_node()))
+ continue;
+ expr->updateShapes();
+ }
}
} // namespace
diff --git a/src/common/snippets/src/lowered/port_descriptor.cpp b/src/common/snippets/src/lowered/port_descriptor.cpp
index 7e3234d788d..87cb199944e 100644
--- a/src/common/snippets/src/lowered/port_descriptor.cpp
+++ b/src/common/snippets/src/lowered/port_descriptor.cpp
@@ -38,6 +38,8 @@ void PortDescriptor::validate_arguments() {
// NCHW layout by default
std::iota(m_layout.begin(), m_layout.end(), 0);
}
+ OPENVINO_ASSERT(m_subtensor_shape.size() <= m_tensor_shape->size(),
+ "Snippets tensor descriptor: Subtensor shape must be less than or equal to tensor shape");
OPENVINO_ASSERT(m_layout.size() == m_tensor_shape->size(), "Snippets tensor descriptor: Layout size must be equal to the shape size");
}
@@ -48,9 +50,22 @@ const VectorDims& PortDescriptor::get_shape() const {
void PortDescriptor::set_shape(const VectorDims& tensor) {
OPENVINO_ASSERT(m_tensor_shape, "Failed to set_shape: Tensor Shape is nullptr");
+ OPENVINO_ASSERT(m_subtensor_shape.size() <= tensor.size(),
+ "Snippets tensor descriptor: Subtensor shape must be less than or equal to tensor shape");
*m_tensor_shape = tensor;
}
+void PortDescriptor::set_layout(const std::vector<size_t>& layout) {
+ OPENVINO_ASSERT(layout.size() == m_tensor_shape->size(),
+ "Snippets tensor descriptor: Layout size must be equal to the shape size");
+ m_layout = layout;
+}
+void PortDescriptor::set_subtensor(const VectorDims& subtensor) {
+ OPENVINO_ASSERT(subtensor.size() <= m_tensor_shape->size(),
+ "Subtensor shape must be less than or equal to tensor shape");
+ m_subtensor_shape = subtensor;
+}
+
void PortDescriptor::set_subtensor_dim(size_t idx, VectorDims::value_type value) {
OPENVINO_ASSERT(idx < m_subtensor_shape.size(), "Failed to set subtensor value: idx should be less than size");
*(m_subtensor_shape.rbegin() + idx) = value;
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/brgemm_to_brgemm_cpu.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/brgemm_to_brgemm_cpu.cpp
index 946f4dc35c6..281a473b1c3 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/brgemm_to_brgemm_cpu.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/pass/brgemm_to_brgemm_cpu.cpp
@@ -27,8 +27,8 @@ namespace {
template <typename T>
void set_full_port_desc(const T& port) {
const auto& shape_rank = port.get_partial_shape().size();
- static const std::vector<size_t> full_dim_subtensor(std::min(shape_rank, static_cast<size_t>(2)),
- ov::snippets::utils::get_full_dim_value());
+ const std::vector<size_t> full_dim_subtensor(std::min(shape_rank, static_cast<size_t>(2)),
+ ov::snippets::utils::get_full_dim_value());
PortDescriptorUtils::set_port_descriptor(port, full_dim_subtensor);
}
} // namespace
diff --git a/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/eltwise_to_eltwise_tpp.cpp b/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/eltwise_to_eltwise_tpp.cpp
index 059b96a21d9..2d8432d59f3 100644
--- a/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/eltwise_to_eltwise_tpp.cpp
+++ b/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/eltwise_to_eltwise_tpp.cpp
@@ -40,10 +40,18 @@ EltwiseToEltwiseTPP::EltwiseToEltwiseTPP() {
ov::is_type<ov::snippets::op::ReduceBase>(node) ? ov::snippets::utils::get_full_dim_value() : 64;
ov::replace_node_update_name(node, tpp_eltwise);
for (size_t i = 0; i < node->get_input_size(); i++) {
- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(tpp_eltwise->input(i), {M_block, N_block});
+ auto subtensor = snippets::VectorDims{M_block, N_block};
+ if (tpp_eltwise->get_input_partial_shape(i).size() < 2) {
+ subtensor = snippets::VectorDims{N_block};
+ }
+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(tpp_eltwise->input(i), subtensor);
}
- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(tpp_eltwise->output(0), {M_block, N_block});
+ auto subtensor = snippets::VectorDims{M_block, N_block};
+ if (tpp_eltwise->output(0).get_partial_shape().size() < 2) {
+ subtensor = snippets::VectorDims{N_block};
+ }
+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(tpp_eltwise->output(0), subtensor);
return true;
};
diff --git a/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/fuse_tpp_to_equations.cpp b/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/fuse_tpp_to_equations.cpp
index 3214142bf6c..3ad1d95e5d8 100644
--- a/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/fuse_tpp_to_equations.cpp
+++ b/src/plugins/intel_cpu/src/transformations/tpp/x64/pass/fuse_tpp_to_equations.cpp
@@ -82,9 +82,20 @@ bool FuseTPPToEquations::fuse_from_root(const NodePtr& root, const std::shared_p
kv.second = equation;
replace_nodes(m, {}, node_replace_map);
for (const auto& in : equation->inputs()) {
- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(in, root_subtensor);
+ auto subtensor = root_subtensor;
+ if (in.get_partial_shape().size() < root_subtensor.size()) {
+ subtensor.erase(subtensor.begin(),
+ subtensor.begin() + (root_subtensor.size() - in.get_partial_shape().size()));
+ }
+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(in, subtensor);
+ }
+ auto subtensor = root_subtensor;
+ const auto& out = equation->output(0);
+ if (out.get_partial_shape().size() < root_subtensor.size()) {
+ subtensor.erase(subtensor.begin(),
+ subtensor.begin() + (root_subtensor.size() - out.get_partial_shape().size()));
}
- ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(equation->output(0), root_subtensor);
+ ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor(equation->output(0), subtensor);
return true;
}
diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/buffer_allocation.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/buffer_allocation.cpp
index 7c425b0bca6..d1a429294ad 100644
--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/buffer_allocation.cpp
+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/x64/lowered/buffer_allocation.cpp
@@ -113,13 +113,24 @@ protected:
virtual std::shared_ptr<ov::Model> GetModel(const std::vector<ov::PartialShape>& shapes) const = 0;
- void MarkOp(const std::shared_ptr<ov::Node>& node, const std::vector<size_t>& subtensor) const {
- for (const auto& input : node->inputs())
+ void MarkOp(const std::shared_ptr<ov::Node>& node,
+ const std::vector<std::vector<size_t>>& in_subtensors,
+ const std::vector<std::vector<size_t>>& out_subtensors) const {
+ OPENVINO_ASSERT(in_subtensors.size() == node->inputs().size(), "Incorrect count of input subtensors");
+ OPENVINO_ASSERT(out_subtensors.size() == node->outputs().size(), "Incorrect count of output subtensors");
+ // Mark input and output ports with the first supported subtensor
+ for (size_t i = 0; i < node->inputs().size(); ++i) {
+ const auto& input = node->input(i);
ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor_ptr(
- input, std::make_shared<ov::snippets::lowered::PortDescriptor>(input, subtensor));
- for (const auto& output : node->outputs())
+ input,
+ std::make_shared<ov::snippets::lowered::PortDescriptor>(input, in_subtensors[i]));
+ }
+ for (size_t i = 0; i < node->outputs().size(); ++i) {
+ const auto& output = node->output(i);
ov::snippets::lowered::PortDescriptorUtils::set_port_descriptor_ptr(
- output, std::make_shared<ov::snippets::lowered::PortDescriptor>(output, subtensor));
+ output,
+ std::make_shared<ov::snippets::lowered::PortDescriptor>(output, out_subtensors[i]));
+ }
}
ov::snippets::lowered::LinearIR m_linear_ir;
@@ -173,12 +184,12 @@ protected:
const auto body = std::make_shared<ov::Model>(std::make_shared<ov::op::v0::Result>(relu2), ov::ParameterVector{parameter0, parameter1, parameter2});
- MarkOp(load_reshape, subtensor_scalar);
- MarkOp(store, subtensor_scalar);
- MarkOp(power, subtensor_power);
+ MarkOp(load_reshape, {subtensor_scalar}, {subtensor_scalar});
+ MarkOp(store, {subtensor_scalar}, {subtensor_scalar});
+ MarkOp(power, {subtensor_power}, {subtensor_power});
- MarkOp(brgemm_cpu0, subtensor_full);
- MarkOp(brgemm_cpu1, subtensor_full);
+ MarkOp(brgemm_cpu0, {subtensor_full, subtensor_full}, {subtensor_full});
+ MarkOp(brgemm_cpu1, {subtensor_full, subtensor_full}, {subtensor_full});
ov::snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(load_reshape->input(0))->set_layout(order);
@@ -192,6 +203,7 @@ protected:
const auto subtensor_scalar = std::vector<size_t>{1};
const auto subtensor_power = std::vector<size_t>{1, ov::snippets::utils::get_full_dim_value()};
const auto subtensor_full = std::vector<size_t>(2, ov::snippets::utils::get_full_dim_value());
+ const auto subtensor_flat = std::vector<size_t>(1, ov::snippets::utils::get_full_dim_value());
OPENVINO_ASSERT(shapes.size() == 3, "Incorrect count of input shapes");
const auto parameter0 = std::make_shared<ov::op::v0::Parameter>(ov::element::bf16, shapes[0]);
@@ -234,16 +246,16 @@ protected:
const auto body = std::make_shared<ov::Model>(std::make_shared<ov::op::v0::Result>(relu2), ov::ParameterVector{parameter0, parameter1, parameter2});
- MarkOp(load_reshape, subtensor_scalar);
- MarkOp(store, subtensor_scalar);
- MarkOp(power, subtensor_power);
+ MarkOp(load_reshape, {subtensor_scalar}, {subtensor_scalar});
+ MarkOp(store, {subtensor_scalar}, {subtensor_scalar});
+ MarkOp(power, {subtensor_power}, {subtensor_power});
- MarkOp(brgemm_cpu0, subtensor_full);
- MarkOp(brgemm_cpu1, subtensor_full);
- MarkOp(brgemm_copyb0, subtensor_full);
- MarkOp(brgemm_copyb1, subtensor_full);
- MarkOp(scratch0, subtensor_full);
- MarkOp(scratch1, subtensor_full);
+ MarkOp(brgemm_cpu0, {subtensor_full, subtensor_full, subtensor_flat}, {subtensor_full});
+ MarkOp(brgemm_cpu1, {subtensor_full, subtensor_full, subtensor_flat}, {subtensor_full});
+ MarkOp(brgemm_copyb0, {subtensor_flat}, {subtensor_full});
+ MarkOp(brgemm_copyb1, {subtensor_flat}, {subtensor_full});
+ MarkOp(scratch0, {}, {subtensor_flat});
+ MarkOp(scratch1, {}, {subtensor_flat});
ov::snippets::lowered::PortDescriptorUtils::get_port_descriptor_ptr(load_reshape->input(0))->set_layout(order);