[core]Migrate Sigmoid operator to new API (#20780)

* Migrate Sigmoid operator to new API

* Add missing include
This commit is contained in:
Pawel Raasz 2023-10-31 12:11:29 +01:00 committed by GitHub
parent 2932e9e938
commit 3077bad26f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 74 deletions

View File

@ -18,9 +18,7 @@ public:
Sigmoid(const Output<Node>& arg); Sigmoid(const Output<Node>& arg);
Sigmoid() = default; Sigmoid() = default;
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override; std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
OPENVINO_SUPPRESS_DEPRECATED_START bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override;
bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override;
OPENVINO_SUPPRESS_DEPRECATED_END
bool has_evaluate() const override; bool has_evaluate() const override;
}; };
} // namespace v0 } // namespace v0

View File

@ -4,28 +4,30 @@
#pragma once #pragma once
#include <algorithm>
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <type_traits>
#include "openvino/reference/utils/type_util.hpp"
namespace ov { namespace ov {
namespace reference { namespace reference {
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true> namespace func {
void sigmoid(const T* arg, T* out, size_t count) { template <class T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
T exp_value; T sigmoid(const T value) {
for (size_t i = 0; i < count; i++) { const auto exp_value = static_cast<T>(std::exp(-static_cast<typename std::make_signed<T>::type>(value)));
exp_value = static_cast<T>(std::exp(-static_cast<typename std::make_signed<T>::type>(arg[i]))); return 1 / (1 + exp_value);
out[i] = static_cast<T>(1 / (1 + exp_value));
}
} }
template <typename T, typename std::enable_if<!std::is_integral<T>::value, bool>::type = true> template <class T, typename std::enable_if<ov::is_floating_point<T>()>::type* = nullptr>
void sigmoid(const T* arg, T* out, size_t count) { T sigmoid(const T value) {
T exp_value; return 1 / (1 + std::exp(-value));
for (size_t i = 0; i < count; i++) { }
exp_value = static_cast<T>(std::exp(-arg[i])); } // namespace func
out[i] = static_cast<T>(1 / (1 + exp_value));
} template <class T>
void sigmoid(const T* arg, T* out, const size_t count) {
std::transform(arg, arg + count, out, func::sigmoid<T>);
} }
} // namespace reference } // namespace reference
} // namespace ov } // namespace ov

View File

@ -2,80 +2,68 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
#include "ngraph/op/sigmoid.hpp" #include "openvino/op/sigmoid.hpp"
#include <ngraph/validation_util.hpp>
#include "element_visitor.hpp"
#include "itt.hpp" #include "itt.hpp"
#include "ngraph/log.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "ngraph/util.hpp"
#include "openvino/reference/sigmoid.hpp" #include "openvino/reference/sigmoid.hpp"
using namespace std; namespace ov {
using namespace ngraph; namespace op {
namespace sigmoid {
shared_ptr<Node> ov::op::v0::Sigmoid::clone_with_new_inputs(const OutputVector& new_args) const { struct Evaluate : element::NoAction<bool> {
using element::NoAction<bool>::visit;
template <element::Type_t ET, class T = fundamental_type_for<ET>>
static result_type visit(const Tensor& arg0, Tensor& out, const size_t count) {
reference::sigmoid(arg0.data<const T>(), out.data<T>(), count);
return true;
}
};
} // namespace sigmoid
namespace v0 {
std::shared_ptr<Node> Sigmoid::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v0_Sigmoid_clone_with_new_inputs); OV_OP_SCOPE(v0_Sigmoid_clone_with_new_inputs);
check_new_args_count(this, new_args); check_new_args_count(this, new_args);
return make_shared<Sigmoid>(new_args.at(0)); return std::make_shared<Sigmoid>(new_args.at(0));
} }
ov::op::v0::Sigmoid::Sigmoid(const Output<Node>& arg) : UnaryElementwiseArithmetic(arg) { Sigmoid::Sigmoid(const Output<Node>& arg) : UnaryElementwiseArithmetic(arg) {
constructor_validate_and_infer_types(); constructor_validate_and_infer_types();
} }
OPENVINO_SUPPRESS_DEPRECATED_START bool Sigmoid::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
namespace sigmoid {
namespace {
template <element::Type_t ET>
inline bool evaluate(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) {
using T = typename element_type_traits<ET>::value_type;
ov::reference::sigmoid<T>(arg0->get_data_ptr<ET>(), out->get_data_ptr<ET>(), count);
return true;
}
bool evaluate_sigmoid(const HostTensorPtr& arg0, const HostTensorPtr& out) {
bool rc = true;
size_t count = shape_size(arg0->get_shape());
out->set_unary(arg0);
switch (arg0->get_element_type()) {
OPENVINO_TYPE_CASE(evaluate_sigmoid, i32, arg0, out, count);
OPENVINO_TYPE_CASE(evaluate_sigmoid, i64, arg0, out, count);
OPENVINO_TYPE_CASE(evaluate_sigmoid, u32, arg0, out, count);
OPENVINO_TYPE_CASE(evaluate_sigmoid, u64, arg0, out, count);
OPENVINO_TYPE_CASE(evaluate_sigmoid, f16, arg0, out, count);
OPENVINO_TYPE_CASE(evaluate_sigmoid, f32, arg0, out, count);
default:
rc = false;
break;
}
return rc;
}
} // namespace
} // namespace sigmoid
bool ov::op::v0::Sigmoid::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const {
OV_OP_SCOPE(v0_Sigmoid_evaluate); OV_OP_SCOPE(v0_Sigmoid_evaluate);
OPENVINO_SUPPRESS_DEPRECATED_START OPENVINO_ASSERT(outputs.size() == 1);
OPENVINO_ASSERT(validate_host_tensor_vector(outputs, 1) && validate_host_tensor_vector(inputs, 1)); OPENVINO_ASSERT(inputs.size() == 1);
OPENVINO_SUPPRESS_DEPRECATED_END
return sigmoid::evaluate_sigmoid(inputs[0], outputs[0]); const auto& in_shape = inputs[0].get_shape();
outputs[0].set_shape(in_shape);
using namespace ov::element;
return IfTypeOf<f16, f32, i32, i64, u32, u64>::apply<sigmoid::Evaluate>(inputs[0].get_element_type(),
inputs[0],
outputs[0],
shape_size(in_shape));
} }
bool ov::op::v0::Sigmoid::has_evaluate() const { bool Sigmoid::has_evaluate() const {
OV_OP_SCOPE(v0_Sigmoid_has_evaluate); OV_OP_SCOPE(v0_Sigmoid_has_evaluate);
switch (get_input_element_type(0)) { switch (get_input_element_type(0)) {
case ngraph::element::i32: case element::f16:
case ngraph::element::i64: case element::f32:
case ngraph::element::u32: case element::i32:
case ngraph::element::u64: case element::i64:
case ngraph::element::f16: case element::u32:
case ngraph::element::f32: case element::u64:
return true; return true;
default: default:
break; return false;
} }
return false;
} }
} // namespace v0
} // namespace op
} // namespace ov