[core] Migrate SoftSign operator to new API (#20958)
* Align code style * Use Evaluate in place of switch case * Use std::transform in place of for loop
This commit is contained in:
1 parent
4bde741de4
commit
6e073b1165
3 files changed
+44
-58
No files matched your search
@@ -22,14 +22,12 @@ public:
|
||||
///
|
||||
SoftSign(const Output<Node>& arg);
|
||||
|
||||
bool visit_attributes(AttributeVisitor& visitor) override;
|
||||
|
||||
void validate_and_infer_types() override;
|
||||
|
||||
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
|
||||
bool evaluate(ov::TensorVector& outputs,
|
||||
const ov::TensorVector& inputs,
|
||||
const ov::EvaluationContext& evaluation_context) const override;
|
||||
bool evaluate(TensorVector& outputs,
|
||||
const TensorVector& inputs,
|
||||
const EvaluationContext& evaluation_context) const override;
|
||||
bool has_evaluate() const override;
|
||||
};
|
||||
} // namespace v9
|
||||
|
||||
@@ -4,16 +4,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
namespace ov {
|
||||
namespace reference {
|
||||
template <typename T>
|
||||
void softsign(const T* arg, T* out, size_t count) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
out[i] = arg[i] / (1 + std::abs(arg[i]));
|
||||
}
|
||||
std::transform(arg, arg + count, out, [](const T v) {
|
||||
return v / (T{1} + static_cast<T>(std::abs(v)));
|
||||
});
|
||||
}
|
||||
} // namespace reference
|
||||
} // namespace ov
|
||||
@@ -3,44 +3,33 @@
|
||||
//
|
||||
#include "openvino/op/softsign.hpp"
|
||||
|
||||
#include <openvino/core/validation_util.hpp>
|
||||
|
||||
#include "element_visitor.hpp"
|
||||
#include "itt.hpp"
|
||||
#include "openvino/core/attribute_visitor.hpp"
|
||||
#include "openvino/core/shape_util.hpp"
|
||||
#include "openvino/core/validation_util.hpp"
|
||||
#include "openvino/reference/softsign.hpp"
|
||||
#include "openvino/runtime/tensor.hpp"
|
||||
|
||||
namespace {
|
||||
template <ov::element::Type_t ET>
|
||||
inline bool evaluate(const ov::Tensor& arg, const ov::Tensor& out, const size_t count) {
|
||||
using T = typename ov::element_type_traits<ET>::value_type;
|
||||
ov::reference::softsign<T>(arg.data<T>(), out.data<T>(), count);
|
||||
return true;
|
||||
}
|
||||
namespace ov {
|
||||
namespace op {
|
||||
namespace softsign {
|
||||
struct Evaluate : element::NoAction<bool> {
|
||||
using element::NoAction<bool>::visit;
|
||||
|
||||
bool evaluate_softsign(const ov::Tensor& arg, const ov::Tensor& out) {
|
||||
bool rc = true;
|
||||
size_t count = arg.get_size();
|
||||
|
||||
switch (arg.get_element_type()) {
|
||||
OPENVINO_TYPE_CASE(evaluate_softsign, bf16, arg, out, count);
|
||||
OPENVINO_TYPE_CASE(evaluate_softsign, f16, arg, out, count);
|
||||
OPENVINO_TYPE_CASE(evaluate_softsign, f32, arg, out, count);
|
||||
OPENVINO_TYPE_CASE(evaluate_softsign, f64, arg, out, count);
|
||||
default:
|
||||
rc = false;
|
||||
break;
|
||||
template <element::Type_t ET, class T = fundamental_type_for<ET>>
|
||||
static result_type visit(const Tensor& in, Tensor& out, const size_t count) {
|
||||
reference::softsign(in.data<const T>(), out.data<T>(), count);
|
||||
return true;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
} // namespace
|
||||
};
|
||||
|
||||
ov::op::v9::SoftSign::SoftSign(const Output<Node>& arg) : UnaryElementwiseArithmetic(arg) {
|
||||
} // namespace softsign
|
||||
namespace v9 {
|
||||
SoftSign::SoftSign(const Output<Node>& arg) : UnaryElementwiseArithmetic(arg) {
|
||||
constructor_validate_and_infer_types();
|
||||
}
|
||||
|
||||
void ov::op::v9::SoftSign::validate_and_infer_types() {
|
||||
void SoftSign::validate_and_infer_types() {
|
||||
OV_OP_SCOPE(v9_SoftSign_validate_and_infer_types);
|
||||
const element::Type& input_et = get_input_element_type(0);
|
||||
|
||||
@@ -52,34 +41,28 @@ void ov::op::v9::SoftSign::validate_and_infer_types() {
|
||||
UnaryElementwiseArithmetic::validate_and_infer_types();
|
||||
}
|
||||
|
||||
bool ov::op::v9::SoftSign::visit_attributes(AttributeVisitor& visitor) {
|
||||
OV_OP_SCOPE(v9_SoftSign_visit_attributes);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<ov::Node> ov::op::v9::SoftSign::clone_with_new_inputs(const OutputVector& new_args) const {
|
||||
std::shared_ptr<Node> SoftSign::clone_with_new_inputs(const OutputVector& new_args) const {
|
||||
OV_OP_SCOPE(v9_SoftSign_clone_with_new_inputs);
|
||||
check_new_args_count(this, new_args);
|
||||
return std::make_shared<ov::op::v9::SoftSign>(new_args.at(0));
|
||||
return std::make_shared<SoftSign>(new_args.at(0));
|
||||
}
|
||||
|
||||
bool ov::op::v9::SoftSign::has_evaluate() const {
|
||||
bool SoftSign::has_evaluate() const {
|
||||
OV_OP_SCOPE(v9_SoftSign_has_evaluate);
|
||||
switch (get_input_element_type(0)) {
|
||||
case ov::element::bf16:
|
||||
case ov::element::f16:
|
||||
case ov::element::f32:
|
||||
case ov::element::f64:
|
||||
case element::bf16:
|
||||
case element::f16:
|
||||
case element::f32:
|
||||
case element::f64:
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ov::op::v9::SoftSign::evaluate(ov::TensorVector& outputs,
|
||||
const ov::TensorVector& inputs,
|
||||
const ov::EvaluationContext& evaluation_context) const {
|
||||
bool SoftSign::evaluate(TensorVector& outputs,
|
||||
const TensorVector& inputs,
|
||||
const EvaluationContext& evaluation_context) const {
|
||||
OV_OP_SCOPE(v9_SoftSign_evaluate);
|
||||
|
||||
OPENVINO_ASSERT(outputs.size() == 1 && inputs.size() == 1,
|
||||
@@ -89,9 +72,14 @@ bool ov::op::v9::SoftSign::evaluate(ov::TensorVector& outputs,
|
||||
outputs.size(),
|
||||
" output(s).");
|
||||
|
||||
const auto& in = inputs[0];
|
||||
auto& out = outputs[0];
|
||||
|
||||
out.set_shape(in.get_shape());
|
||||
return evaluate_softsign(in, out);
|
||||
const auto& input_shape = inputs[0].get_shape();
|
||||
outputs[0].set_shape(input_shape);
|
||||
using namespace ov::element;
|
||||
return IfTypeOf<bf16, f16, f32, f64>::apply<softsign::Evaluate>(inputs[0].get_element_type(),
|
||||
inputs[0],
|
||||
outputs[0],
|
||||
shape_size(input_shape));
|
||||
}
|
||||
} // namespace v9
|
||||
} // namespace op
|
||||
} // namespace ov
|
||||
Reference in new issue
Block a user