Skip to content

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

Fix the accuracy issue by update celu formula based on spec (#33500)

liang79d14c5929fa
diff --git a/src/frontends/onnx/frontend/src/op/celu.cpp b/src/frontends/onnx/frontend/src/op/celu.cpp
index d67dbfcdd8d..fce4bc68fe6 100644
--- a/src/frontends/onnx/frontend/src/op/celu.cpp
+++ b/src/frontends/onnx/frontend/src/op/celu.cpp
@@ -1,14 +1,20 @@
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
+//
#include <memory>
#include "core/operator_set.hpp"
#include "exceptions.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/elu.hpp"
+#include "openvino/op/exp.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/minimum.hpp"
#include "openvino/op/multiply.hpp"
+#include "openvino/op/subtract.hpp"
#include "utils/common.hpp"
using namespace ov::op;
@@ -19,12 +25,20 @@ namespace ai_onnx {
namespace opset_1 {
ov::OutputVector celu(const ov::frontend::onnx::Node& node) {
auto alpha_node = node.get_attribute_as_constant<float>("alpha", 1.0f);
+ auto zero = v0::Constant::create(alpha_node->get_element_type(), ov::Shape{}, {0.0f});
auto x_celu = node.get_ov_inputs().at(0);
+ auto x_max = std::make_shared<v1::Maximum>(x_celu, zero);
+
auto divide_node = std::make_shared<v1::Divide>(x_celu, alpha_node);
- auto elu_node = std::make_shared<v0::Elu>(divide_node, 1.0);
+ auto exp_node = std::make_shared<v0::Exp>(divide_node);
+ auto exp_minus_one =
+ std::make_shared<v1::Subtract>(exp_node,
+ v0::Constant::create(exp_node->get_element_type(), ov::Shape{}, {1.0f}));
+ auto celu_temp = std::make_shared<v1::Multiply>(alpha_node, exp_minus_one);
+ auto min_node = std::make_shared<v1::Minimum>(celu_temp, zero);
- return {std::make_shared<v1::Multiply>(alpha_node, elu_node)};
+ return {std::make_shared<v1::Add>(x_max, min_node)};
}
ONNX_OP("Celu", OPSET_SINCE(1), ai_onnx::opset_1::celu);
} // namespace opset_1
diff --git a/src/frontends/onnx/tests/models/activation_celu.prototxt b/src/frontends/onnx/tests/models/activation_celu.prototxt
new file mode 100644
index 00000000000..dac0cfc00b5
--- /dev/null
+++ b/src/frontends/onnx/tests/models/activation_celu.prototxt
@@ -0,0 +1,47 @@
+ir_version: 12
+producer_name: "ONNX Celu"
+model_version: 0
+graph {
+ name: "test"
+ node {
+ input: "X"
+ output: "Y"
+ name: "node1"
+ op_type: "Celu"
+ attribute {
+ name: "alpha"
+ f: -0.5
+ type: FLOAT
+ }
+ doc_string: "Celu"
+ }
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 13
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 13
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 12
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index 67a3dc71323..6500e1fcb7e 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -2674,6 +2674,48 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_matmul_vec_ten3d) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_activation_celu) {
+ auto model = convert_model("activation_celu.onnx");
+
+ // -1.0f, 0, 1.0f, 10.f, normal input values for activation
+ // 100.0f, -100.0f, 1000.0f, -1000.0f, input values that leads to exp() overflow
+ // FLT_MIN, FLT_MIN / 16, -FLT_MIN / 16, min, denorm, -denorm
+ // FLT_MAX, -FLT_MAX, max, -max;
+ Inputs inputs{std::vector<float>{-1.0f,
+ 0,
+ 1.0f,
+ 10.f,
+ 100.0f,
+ -100.0f,
+ 1000.0f,
+ -1000.0f,
+ FLT_MIN,
+ FLT_MIN / 16,
+ -FLT_MIN / 16,
+ FLT_MAX,
+ -FLT_MAX}};
+
+ const auto inf = std::numeric_limits<float>::infinity();
+ std::vector<float> output{-3.194527864456177f,
+ 0.0f,
+ 1.f,
+ 10.f,
+ 100.0f,
+ -inf,
+ 1000.0f,
+ -inf,
+ 1.175494350822288e-38f,
+ 7.346839692639297e-40f,
+ 0.0f,
+ inf,
+ -inf};
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_multiple_inputs(inputs);
+ test_case.add_expected_output(output);
+ test_case.run();
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_softplus) {
auto model = convert_model("softplus.onnx");
diff --git a/src/frontends/pytorch/src/op/celu.cpp b/src/frontends/pytorch/src/op/celu.cpp
index 0246e134704..0bb0fb3435a 100644
--- a/src/frontends/pytorch/src/op/celu.cpp
+++ b/src/frontends/pytorch/src/op/celu.cpp
@@ -3,9 +3,14 @@
//
#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/elu.hpp"
+#include "openvino/op/exp.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/minimum.hpp"
#include "openvino/op/multiply.hpp"
+#include "openvino/op/subtract.hpp"
#include "utils.hpp"
namespace ov {
@@ -26,11 +31,21 @@ OutputVector translate_celu(const NodeContext& context) {
alpha = context.get_input(1);
}
+ // CELU(x)=max(0,x)+min(0,a*(exp(x/a)-1))
+ auto zero = context.mark_node(v0::Constant::create(element::f32, Shape{}, {0.}));
+ zero = context.mark_node(std::make_shared<v1::ConvertLike>(zero, x));
+ auto x_max = context.mark_node(std::make_shared<v1::Maximum>(x, zero));
+
alpha = context.mark_node(std::make_shared<v1::ConvertLike>(alpha, x));
auto divide_node = context.mark_node(std::make_shared<v1::Divide>(x, alpha));
- auto elu_node = context.mark_node(std::make_shared<v0::Elu>(divide_node, 1.));
+ auto exp_node = context.mark_node(std::make_shared<v0::Exp>(divide_node));
+ auto one = context.mark_node(v0::Constant::create(element::f32, Shape{}, {1.}));
+ one = context.mark_node(std::make_shared<v1::ConvertLike>(one, x));
+ auto exp_minus_one = context.mark_node(std::make_shared<v1::Subtract>(exp_node, one));
+ auto elu_node = context.mark_node(std::make_shared<v1::Multiply>(alpha, exp_minus_one));
+ auto min_node = context.mark_node(std::make_shared<v1::Minimum>(elu_node, zero));
- auto elu = context.mark_node(std::make_shared<v1::Multiply>(alpha, elu_node));
+ auto elu = context.mark_node(std::make_shared<v1::Add>(x_max, min_node));
return {elu};
};