Skip to content

Commit

Permalink
[ONNX] Extend ONNX Frontend with Function Celu-12 (#22002)
Browse files Browse the repository at this point in the history
### Details:
 - created implementation for `Celu-12` operator
 - created tests

### Tickets:
 - Closes #20543

---------

Co-authored-by: Andrei Kochin <[email protected]>
Co-authored-by: Georgy Krivoruchko <[email protected]>
Co-authored-by: Katarzyna Mitrus <[email protected]>
  • Loading branch information
4 people authored Mar 5, 2024
1 parent e269340 commit a97abd5
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/frontends/onnx/frontend/src/core/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ namespace transform {

using ::ONNX_NAMESPACE::ModelProto;

static const std::vector<std::string> onnx_functions_to_expand =
{"AffineGrid", "Bernoulli", "Celu", "CenterCropPad", "NegativeLogLikelihoodLoss", "SoftmaxCrossEntropyLoss"};
static const std::vector<std::string> onnx_functions_to_expand = {"AffineGrid",
"Bernoulli",
"CenterCropPad",
"NegativeLogLikelihoodLoss",
"SoftmaxCrossEntropyLoss"};

/// \brief Replace nodes with expanded body of ONNX functions
///
Expand Down
35 changes: 35 additions & 0 deletions src/frontends/onnx/frontend/src/op/celu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "op/celu.hpp"

#include <memory>

#include "exceptions.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/elu.hpp"
#include "openvino/op/multiply.hpp"
#include "utils/common.hpp"

using namespace ov::op;

namespace ov {
namespace frontend {
namespace onnx {
namespace op {
namespace set_1 {
ov::OutputVector celu(const ov::frontend::onnx::Node& node) {
auto alpha_node = node.get_attribute_as_constant<float>("alpha", 1.0f);
auto x_celu = node.get_ov_inputs().at(0);

auto divide_node = std::make_shared<v1::Divide>(x_celu, alpha_node);
auto elu_node = std::make_shared<v0::Elu>(divide_node, 1.0);

return {std::make_shared<v1::Multiply>(alpha_node, elu_node)};
}
} // namespace set_1
} // namespace op
} // namespace onnx
} // namespace frontend
} // namespace ov
20 changes: 20 additions & 0 deletions src/frontends/onnx/frontend/src/op/celu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include "core/node.hpp"

namespace ov {
namespace frontend {
namespace onnx {
namespace op {
namespace set_1 {
ov::OutputVector celu(const ov::frontend::onnx::Node& node);

} // namespace set_1
} // namespace op
} // namespace onnx
} // namespace frontend
} // namespace ov
2 changes: 2 additions & 0 deletions src/frontends/onnx/frontend/src/ops_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "op/cast.hpp"
#include "op/cast_like.hpp"
#include "op/ceil.hpp"
#include "op/celu.hpp"
#include "op/clip.hpp"
#include "op/com.microsoft/attention.hpp"
#include "op/com.microsoft/bias_gelu.hpp"
Expand Down Expand Up @@ -368,6 +369,7 @@ OperatorsBridge::OperatorsBridge() {
REGISTER_OPERATOR("Cast", 1, cast);
REGISTER_OPERATOR("CastLike", 1, cast_like);
REGISTER_OPERATOR("Ceil", 1, ceil);
REGISTER_OPERATOR("Celu", 1, celu);
REGISTER_OPERATOR("Clip", 1, clip);
REGISTER_OPERATOR("Clip", 11, clip);
REGISTER_OPERATOR("Concat", 1, concat);
Expand Down
44 changes: 44 additions & 0 deletions src/frontends/onnx/tests/models/celu_float.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ir_version: 7
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "X"
output: "Y"
op_type: "Celu"
attribute {
name: "alpha"
f: 1.0
type: FLOAT
}
}
name: "test_celu_float"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 12
}
44 changes: 44 additions & 0 deletions src/frontends/onnx/tests/models/celu_float_alpha.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ir_version: 7
producer_name: "OpenVINO ONNX Frontend"
graph {
node {
input: "X"
output: "Y"
op_type: "Celu"
attribute {
name: "alpha"
f: 3.0
type: FLOAT
}
}
name: "test_celu_float_alpha"
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 4
}
}
}
}
}
}
opset_import {
version: 12
}
20 changes: 20 additions & 0 deletions src/frontends/onnx/tests/onnx_import.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6228,6 +6228,26 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_bitwise_not) {
test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_celu_float) {
auto model = convert_model("celu_float.onnx");

auto test_case = ov::test::TestCase(model, s_device);
test_case.add_input<float>(Shape{2}, {-45.f, 22.98f});
test_case.add_expected_output<float>(Shape{2}, {-1.f, 22.98f});

test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_celu_float_alpha) {
auto model = convert_model("celu_float_alpha.onnx");

auto test_case = ov::test::TestCase(model, s_device);
test_case.add_input<float>(Shape{4}, {-5.f, -4.25f, -10.f, 7.3f});
test_case.add_expected_output<float>(Shape{4}, {-2.43337319f, -2.27243678f, -2.89297802f, 7.3f});

test_case.run();
}

OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gelu_float) {
auto model = convert_model("gelu_float.onnx");

Expand Down

0 comments on commit a97abd5

Please sign in to comment.