Skip to content

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

[Snippets][CPU][RV64] Enable v7 GELU support with tanh approximation mode (#34355)

Arseniy Obolenskiyd5e40163c6c5
diff --git a/src/plugins/intel_cpu/src/emitters/snippets/riscv64/cpu_generator.cpp b/src/plugins/intel_cpu/src/emitters/snippets/riscv64/cpu_generator.cpp
index 0913f930fb4..493e8361c1f 100644
--- a/src/plugins/intel_cpu/src/emitters/snippets/riscv64/cpu_generator.cpp
+++ b/src/plugins/intel_cpu/src/emitters/snippets/riscv64/cpu_generator.cpp
@@ -94,6 +94,37 @@
# include "emitters/snippets/riscv64/verbose.hpp"
#endif
+#define CREATE_GELU_V7_EMITTER(e_type_erf, e_type_tanh) \
+ {[this](const snippets::lowered::ExpressionPtr& expr) -> std::shared_ptr<snippets::Emitter> { \
+ const auto& n = expr->get_node(); \
+ const auto& gelu = ov::as_type_ptr<ov::op::v7::Gelu>(n); \
+ if (gelu == nullptr) { \
+ OPENVINO_THROW("Can't cast to ov::op::v7::Gelu"); \
+ } \
+ const auto approximationMode = gelu->get_approximation_mode(); \
+ if (approximationMode == ov::op::GeluApproximationMode::ERF) { \
+ return std::make_shared<e_type_erf>(h.get(), isa, n); \
+ } \
+ if (approximationMode == ov::op::GeluApproximationMode::TANH) { \
+ return std::make_shared<e_type_tanh>(h.get(), isa, n); \
+ } \
+ OPENVINO_THROW("Unsupported Gelu approximation mode"); \
+ }, \
+ [](const std::shared_ptr<ov::Node>& n) -> std::set<std::vector<ov::element::Type>> { \
+ const auto& gelu = ov::as_type_ptr<ov::op::v7::Gelu>(n); \
+ if (gelu == nullptr) { \
+ OPENVINO_THROW("Can't cast to ov::op::v7::Gelu"); \
+ } \
+ const auto approximationMode = gelu->get_approximation_mode(); \
+ if (approximationMode == ov::op::GeluApproximationMode::ERF) { \
+ return e_type_erf::get_supported_precisions(n); \
+ } \
+ if (approximationMode == ov::op::GeluApproximationMode::TANH) { \
+ return e_type_tanh::get_supported_precisions(n); \
+ } \
+ OPENVINO_THROW("Unsupported Gelu approximation mode"); \
+ }}
+
#define CREATE_ROUND_V5_EMITTER(e_type_from_zero, e_type_even) \
{[this](const snippets::lowered::ExpressionPtr& expr) -> std::shared_ptr<snippets::Emitter> { \
const auto& n = expr->get_node(); \
@@ -280,7 +311,8 @@ CPUTargetMachine::CPUTargetMachine(ov::intel_cpu::riscv64::cpu_isa_t host_isa, o
jitters[ov::op::v0::Floor::get_type_info_static()] = emitter_factory.from_node<jit_floor_emitter>();
jitters[ov::op::v1::FloorMod::get_type_info_static()] = emitter_factory.from_node<jit_floor_mod_emitter>();
jitters[ov::op::v0::Gelu::get_type_info_static()] = emitter_factory.from_node<jit_gelu_erf_emitter>();
- jitters[ov::op::v7::Gelu::get_type_info_static()] = emitter_factory.from_node<jit_gelu_erf_emitter>();
+ jitters[ov::op::v7::Gelu::get_type_info_static()] =
+ CREATE_GELU_V7_EMITTER(jit_gelu_erf_emitter, jit_gelu_tanh_emitter);
jitters[ov::op::v5::HSigmoid::get_type_info_static()] = emitter_factory.from_node<jit_hsigmoid_emitter>();
jitters[ov::op::v4::HSwish::get_type_info_static()] = emitter_factory.from_node<jit_hswish_emitter>();
jitters[ov::op::v10::IsFinite::get_type_info_static()] = emitter_factory.from_node<jit_is_finite_emitter>();
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index ed2e4668e6d..9607f7205de 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -269,7 +269,6 @@
#endif
#if defined(OPENVINO_ARCH_RISCV64)
-# include "openvino/op/gelu.hpp"
# include "openvino/op/power.hpp"
# include "openvino/op/select.hpp"
# include "openvino/op/swish.hpp"
@@ -1402,9 +1401,6 @@ void Transformations::MainSnippets() {
// CPU Plugin supports Swish in Subgraph via conversion to SwishCPU that requires scalar beta.
// CPU Plugin does not support Mish for x64
auto is_unsupported = [](const std::shared_ptr<const ov::Node>& n) {
-#if defined(OPENVINO_ARCH_RISCV64)
- const auto gelu_v7 = ov::as_type_ptr<const ov::op::v7::Gelu>(n);
-#endif
return (ov::is_type<const ov::op::v4::Swish>(n) && n->inputs().size() > 1 &&
!ov::is_type<const ov::op::v0::Constant>(n->get_input_node_shared_ptr(1)))
#if defined(OPENVINO_ARCH_X86_64)
@@ -1412,7 +1408,6 @@ void Transformations::MainSnippets() {
#elif defined(OPENVINO_ARCH_RISCV64)
// These operations are not currently supported in the RISC-V snippets target machine.
|| ov::is_type<const ov::op::v4::Swish>(n) ||
- (gelu_v7 && gelu_v7->get_approximation_mode() == ov::op::GeluApproximationMode::TANH) ||
ov::is_type_any_of<const ov::op::v0::Ceiling,
const ov::op::v1::Power,
const ov::op::v1::Select,