Skip to content

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

[Snippets][CPU] Remove shape inference factory duplications (#31116)

Arseniy Obolenskiy86c34723cbee
diff --git a/src/plugins/intel_cpu/src/nodes/subgraph.cpp b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
index 00ced3bdcd9..92d19188133 100644
--- a/src/plugins/intel_cpu/src/nodes/subgraph.cpp
+++ b/src/plugins/intel_cpu/src/nodes/subgraph.cpp
@@ -49,6 +49,7 @@
#include "snippets/shape_types.hpp"
#include "transformations/cpu_opset/common/pass/convert_to_swish_cpu.hpp"
#include "transformations/snippets/common/pass/mul_add_to_fma.hpp"
+#include "transformations/snippets/common/shape_inference.hpp"
#include "utils/general_utils.h"
#if defined(OPENVINO_ARCH_ARM64)
@@ -59,7 +60,6 @@
# 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/shape_inference.hpp"
#else
# include "emitters/snippets/x64/cpu_generator.hpp"
# include "executors/x64/subgraph.hpp"
@@ -73,7 +73,6 @@
# 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
#include <utility>
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.cpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.cpp
index 37de45c05d3..3310945c08a 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "shape_inference.hpp"
+#include "transformations/snippets/common/shape_inference.hpp"
#include <memory>
@@ -28,33 +28,12 @@ ShapeInferPtr CPUShapeInferSnippetsFactory::get_specific_op_shape_infer(const ov
return {};
}
-#define SHAPE_INFER_PREDEFINED(OP, InferType) \
- { \
- OP::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<InferType>(); \
- } \
- }
-#define SHAPE_INFER_OP_SPECIFIC(OP) \
- { \
- OP::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<OP::ShapeInfer>(n); \
- } \
- }
-#define SHAPE_INFER_OP_SPECIFIC_EXTERNAL(OP, InferType) \
- { \
- OP::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<InferType>(n); \
- } \
- }
-
const CPUShapeInferSnippetsFactory::TRegistry CPUShapeInferSnippetsFactory::specific_ops_registry{
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::FusedMulAdd, NumpyBroadcastShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::SwishNode, PassThroughShapeInfer),
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::tpp::op::BrgemmTPP, BrgemmShapeInfer),
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::aarch64::GemmCPU, BrgemmShapeInfer),
- SHAPE_INFER_OP_SPECIFIC(ov::intel_cpu::aarch64::GemmCopyB),
+ make_predefined<ov::intel_cpu::FusedMulAdd, NumpyBroadcastShapeInfer>(),
+ make_predefined<ov::intel_cpu::SwishNode, PassThroughShapeInfer>(),
+ make_specific_external<ov::intel_cpu::tpp::op::BrgemmTPP, BrgemmShapeInfer>(),
+ make_specific_external<ov::intel_cpu::aarch64::GemmCPU, BrgemmShapeInfer>(),
+ make_specific<ov::intel_cpu::aarch64::GemmCopyB>(),
};
-#undef SHAPE_INFER_OP_SPECIFIC
-#undef SHAPE_INFER_PREDEFINED
} // namespace ov::snippets
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.hpp b/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.hpp
deleted file mode 100644
index 35bebade2cd..00000000000
--- a/src/plugins/intel_cpu/src/transformations/snippets/aarch64/shape_inference.hpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (C) 2024 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include <memory>
-#include <snippets/shape_inference/shape_inference.hpp>
-
-#include "openvino/core/node.hpp"
-#include "openvino/core/type.hpp"
-
-namespace ov::snippets {
-
-/**
- * \brief Shape infer factory that can create shape-infer instances for cpu-specific operations
- */
-class CPUShapeInferSnippetsFactory : public IShapeInferSnippetsFactory {
- /** \brief Factory makers registry which can be specialized for key and value. */
- static const TRegistry specific_ops_registry;
-
-protected:
- /**
- * @brief get shape infer instances for operations from backend-specific opset
- * @return register ShapeInferPtr
- */
- [[nodiscard]] ShapeInferPtr get_specific_op_shape_infer(const ov::DiscreteTypeInfo& key,
- const std::shared_ptr<ov::Node>& op) const override;
-};
-
-} // namespace ov::snippets
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.hpp b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.hpp
new file mode 100644
index 00000000000..6196c5c7f6e
--- /dev/null
+++ b/src/plugins/intel_cpu/src/transformations/snippets/common/shape_inference.hpp
@@ -0,0 +1,55 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <snippets/shape_inference/shape_inference.hpp>
+
+#include "openvino/core/node.hpp"
+#include "openvino/core/type.hpp"
+
+namespace ov::snippets {
+
+/**
+ * \brief Shape infer factory that can create shape-infer instances for cpu-specific operations
+ */
+class CPUShapeInferSnippetsFactory : public IShapeInferSnippetsFactory {
+ /** \brief Factory makers registry which can be specialized for key and value. */
+ static const TRegistry specific_ops_registry;
+
+protected:
+ /**
+ * @brief get shape infer instances for operations from backend-specific opset
+ * @return register ShapeInferPtr
+ */
+ [[nodiscard]] ShapeInferPtr get_specific_op_shape_infer(const ov::DiscreteTypeInfo& key,
+ const std::shared_ptr<ov::Node>& op) const override;
+
+ // Helper to register an operation with a stateless shape infer implementation
+ template <typename Op, typename InferType>
+ static IShapeInferSnippetsFactory::TRegistry::value_type make_predefined() {
+ return {Op::get_type_info_static(), []([[maybe_unused]] const std::shared_ptr<ov::Node>&) {
+ return std::make_shared<InferType>();
+ }};
+ }
+
+ // Helper to register an operation with ShapeInfer nested in the operation
+ template <typename Op>
+ static IShapeInferSnippetsFactory::TRegistry::value_type make_specific() {
+ return {Op::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) {
+ return std::make_shared<typename Op::ShapeInfer>(n);
+ }};
+ }
+
+ // Helper to register an operation with external shape infer implementation
+ template <typename Op, typename InferType>
+ static IShapeInferSnippetsFactory::TRegistry::value_type make_specific_external() {
+ return {Op::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) {
+ return std::make_shared<InferType>(n);
+ }};
+ }
+};
+
+} // namespace ov::snippets
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.cpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.cpp
index e25ffa3f148..b8ce1317527 100644
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.cpp
+++ b/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.cpp
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "shape_inference.hpp"
+#include "transformations/snippets/common/shape_inference.hpp"
#include <memory>
#include <snippets/shape_inference/shape_infer_instances.hpp>
@@ -36,47 +36,26 @@ ShapeInferPtr CPUShapeInferSnippetsFactory::get_specific_op_shape_infer(const ov
return {};
}
-#define SHAPE_INFER_PREDEFINED(OP, InferType) \
- { \
- OP::get_type_info_static(), []([[maybe_unused]] const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<InferType>(); \
- } \
- }
-#define SHAPE_INFER_OP_SPECIFIC(OP) \
- { \
- OP::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<OP::ShapeInfer>(n); \
- } \
- }
-#define SHAPE_INFER_OP_SPECIFIC_EXTERNAL(OP, InferType) \
- { \
- OP::get_type_info_static(), [](const std::shared_ptr<ov::Node>& n) { \
- return std::make_shared<InferType>(n); \
- } \
- }
-
const CPUShapeInferSnippetsFactory::TRegistry CPUShapeInferSnippetsFactory::specific_ops_registry{
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::FusedMulAdd, NumpyBroadcastShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::SwishNode, PassThroughShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::LoadConvertSaturation, PassThroughShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::LoadConvertTruncation, PassThroughShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::StoreConvertSaturation, PassThroughShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::StoreConvertTruncation, PassThroughShapeInfer),
+ make_predefined<ov::intel_cpu::FusedMulAdd, NumpyBroadcastShapeInfer>(),
+ make_predefined<ov::intel_cpu::SwishNode, PassThroughShapeInfer>(),
+ make_predefined<ov::intel_cpu::LoadConvertSaturation, PassThroughShapeInfer>(),
+ make_predefined<ov::intel_cpu::LoadConvertTruncation, PassThroughShapeInfer>(),
+ make_predefined<ov::intel_cpu::StoreConvertSaturation, PassThroughShapeInfer>(),
+ make_predefined<ov::intel_cpu::StoreConvertTruncation, PassThroughShapeInfer>(),
#ifdef SNIPPETS_DEBUG_CAPS
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::PerfCountRdtscBegin, EmptyShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::PerfCountRdtscEnd, EmptyShapeInfer),
+ make_predefined<ov::intel_cpu::PerfCountRdtscBegin, EmptyShapeInfer>(),
+ make_predefined<ov::intel_cpu::PerfCountRdtscEnd, EmptyShapeInfer>(),
#endif
#ifdef SNIPPETS_LIBXSMM_TPP
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::tpp::op::BrgemmTPP, BrgemmShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::tpp::op::EquationTPP, NumpyBroadcastShapeInfer),
- SHAPE_INFER_PREDEFINED(ov::intel_cpu::tpp::op::Scalar, SingleElementShapeInfer),
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::tpp::op::ReduceMax, ReduceShapeInfer),
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::tpp::op::ReduceSum, ReduceShapeInfer),
+ make_specific_external<ov::intel_cpu::tpp::op::BrgemmTPP, BrgemmShapeInfer>(),
+ make_predefined<ov::intel_cpu::tpp::op::EquationTPP, NumpyBroadcastShapeInfer>(),
+ make_predefined<ov::intel_cpu::tpp::op::Scalar, SingleElementShapeInfer>(),
+ make_specific_external<ov::intel_cpu::tpp::op::ReduceMax, ReduceShapeInfer>(),
+ make_specific_external<ov::intel_cpu::tpp::op::ReduceSum, ReduceShapeInfer>(),
#endif
- SHAPE_INFER_OP_SPECIFIC_EXTERNAL(ov::intel_cpu::BrgemmCPU, BrgemmShapeInfer),
- SHAPE_INFER_OP_SPECIFIC(ov::intel_cpu::BrgemmCopyB),
+ make_specific_external<ov::intel_cpu::BrgemmCPU, BrgemmShapeInfer>(),
+ make_specific<ov::intel_cpu::BrgemmCopyB>(),
};
-#undef SHAPE_INFER_OP_SPECIFIC
-#undef SHAPE_INFER_PREDEFINED
} // namespace ov::snippets
diff --git a/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.hpp b/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.hpp
deleted file mode 100644
index 4c3a547b010..00000000000
--- a/src/plugins/intel_cpu/src/transformations/snippets/x64/shape_inference.hpp
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (C) 2023 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include <memory>
-#include <snippets/shape_inference/shape_inference.hpp>
-
-#include "openvino/core/node.hpp"
-#include "openvino/core/type.hpp"
-
-namespace ov::snippets {
-
-/**
- * \brief Shape infer factory that can create shape-infer instances for cpu-specific operations
- */
-class CPUShapeInferSnippetsFactory : public IShapeInferSnippetsFactory {
- /** \brief Factory makers registry which can be specialized for key and value. */
- static const TRegistry specific_ops_registry;
-
-protected:
- /**
- * @brief get shape infer instances for operations from backend-specific opset
- * @return register ShapeInferPtr
- */
- [[nodiscard]] ShapeInferPtr get_specific_op_shape_infer(const ov::DiscreteTypeInfo& key,
- const std::shared_ptr<ov::Node>& op) const override;
-};
-
-} // namespace ov::snippets
diff --git a/src/plugins/intel_cpu/tests/unit/snippets_transformations/common/mul_add_to_fma.cpp b/src/plugins/intel_cpu/tests/unit/snippets_transformations/common/mul_add_to_fma.cpp
index 63975275ba8..995b4a5c040 100644
--- a/src/plugins/intel_cpu/tests/unit/snippets_transformations/common/mul_add_to_fma.cpp
+++ b/src/plugins/intel_cpu/tests/unit/snippets_transformations/common/mul_add_to_fma.cpp
@@ -5,9 +5,9 @@
#include <gtest/gtest.h>
#include <subgraph_simple.hpp>
#include <transformations/snippets/common/pass/mul_add_to_fma.hpp>
-#include <transformations/snippets/x64/shape_inference.hpp>
+#include <transformations/snippets/common/shape_inference.hpp>
#include <transformations/snippets/common/op/fused_mul_add.hpp>
-#include <transformations/snippets/x64/shape_inference.hpp>
+#include <transformations/snippets/common/shape_inference.hpp>
#include "snippets/op/scalar.hpp"
#include "lowering_utils.hpp"
#include "common_test_utils/common_utils.hpp"
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 e56a4f18bc8..3088ddf2be0 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
@@ -16,7 +16,7 @@
#include "snippets/lowered/pass/insert_buffers.hpp"
#include "snippets/lowered/pass/reduce_decomposition.hpp"
-#include "transformations/snippets/x64/shape_inference.hpp"
+#include "transformations/snippets/common/shape_inference.hpp"
#include "transformations/snippets/x64/pass/lowered/brgemm_cpu_blocking.hpp"
#include "transformations/snippets/x64/pass/lowered/insert_brgemm_copy_buffers.hpp"
#include "transformations/snippets/x64/op/brgemm_cpu.hpp"