From f8cd53bb77f4792a60e1ef45a97941536244b6ee Mon Sep 17 00:00:00 2001 From: Pawel Raasz Date: Thu, 2 Nov 2023 13:35:05 +0100 Subject: [PATCH] [core]Migrate Floor operator to new API (#20830) * Migrate Floor operator to new API * Remove `visit_attributes` is same as base class --- src/core/include/openvino/op/floor.hpp | 5 +- .../include/openvino/reference/floor.hpp | 33 ++++- src/core/src/op/floor.cpp | 118 +++++++----------- 3 files changed, 77 insertions(+), 79 deletions(-) diff --git a/src/core/include/openvino/op/floor.hpp b/src/core/include/openvino/op/floor.hpp index 9321a911b36493..a88cb4a8ae4933 100644 --- a/src/core/include/openvino/op/floor.hpp +++ b/src/core/include/openvino/op/floor.hpp @@ -21,11 +21,8 @@ class OPENVINO_API Floor : public util::UnaryElementwiseArithmetic { /// \param arg Node that produces the input tensor. Floor(const Output& arg); - bool visit_attributes(AttributeVisitor& visitor) override; std::shared_ptr clone_with_new_inputs(const OutputVector& new_args) const override; - OPENVINO_SUPPRESS_DEPRECATED_START - bool evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const override; - OPENVINO_SUPPRESS_DEPRECATED_END + bool evaluate(TensorVector& outputs, const TensorVector& inputs) const override; bool has_evaluate() const override; }; } // namespace v0 diff --git a/src/core/reference/include/openvino/reference/floor.hpp b/src/core/reference/include/openvino/reference/floor.hpp index d7fca06c5f3393..223463e46f91af 100644 --- a/src/core/reference/include/openvino/reference/floor.hpp +++ b/src/core/reference/include/openvino/reference/floor.hpp @@ -7,13 +7,36 @@ #include #include +#include "openvino/reference/copy.hpp" +#include "openvino/reference/utils/type_util.hpp" + namespace ov { namespace reference { -template -void floor(const T* arg, T* out, size_t count) { - for (size_t i = 0; i < count; i++) { - out[i] = std::floor(arg[i]); - } + +/** + * @brief Reference implementation of Floor operator (integral types). + * + * @param arg Input pointer to data. + * @param out Output pointer to results. + * @param count Number of elements in input buffer. + */ +template ::value>::type* = nullptr> +void floor(const T* arg, T* out, const size_t count) { + reference::copy(arg, out, count); +} + +/** + * @brief Reference implementation of Floor operator (floating point types). + * + * @param arg Input pointer to data. + * @param out Output pointer to results. + * @param count Number of elements in input buffer. + */ +template ()>::type* = nullptr> +void floor(const T* arg, T* out, const size_t count) { + std::transform(arg, arg + count, out, [](const T v) { + return std::floor(v); + }); } } // namespace reference } // namespace ov diff --git a/src/core/src/op/floor.cpp b/src/core/src/op/floor.cpp index 864b7dd8188448..c884dac18cab36 100644 --- a/src/core/src/op/floor.cpp +++ b/src/core/src/op/floor.cpp @@ -2,95 +2,73 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "ngraph/op/floor.hpp" +#include "openvino/op/floor.hpp" +#include "element_visitor.hpp" #include "itt.hpp" -#include "ngraph/op/util/eval_copy.hpp" -#include "ngraph/runtime/host_tensor.hpp" -#include "openvino/reference/copy.hpp" #include "openvino/reference/floor.hpp" -using namespace std; -using namespace ngraph; +namespace ov { +namespace op { +namespace floor { -op::Floor::Floor(const Output& arg) : UnaryElementwiseArithmetic(arg) { - constructor_validate_and_infer_types(); -} +struct Evaluate : element::NoAction { + using element::NoAction::visit; -bool ngraph::op::v0::Floor::visit_attributes(AttributeVisitor& visitor) { - OV_OP_SCOPE(v0_Floor_visit_attributes); - return true; -} + template > + static result_type visit(const Tensor& arg, Tensor& out, const size_t count) { + reference::floor(arg.data(), out.data(), count); + return true; + } +}; +} // namespace floor -shared_ptr op::Floor::clone_with_new_inputs(const OutputVector& new_args) const { - OV_OP_SCOPE(v0_Floor_clone_with_new_inputs); - check_new_args_count(this, new_args); - return make_shared(new_args.at(0)); -} +namespace v0 { -OPENVINO_SUPPRESS_DEPRECATED_START -namespace floorop { -namespace { -// function used by TYPE_CASE -template -inline bool evaluate(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) { - using T = typename element_type_traits::value_type; - ov::reference::floor(arg0->get_data_ptr(), out->get_data_ptr(), count); - return true; +Floor::Floor(const Output& arg) : UnaryElementwiseArithmetic(arg) { + constructor_validate_and_infer_types(); } -// function used by COPY_TENSOR -template -inline bool copy_tensor(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) { - ov::reference::copy(arg0->get_data_ptr(), out->get_data_ptr(), count); - return true; +std::shared_ptr Floor::clone_with_new_inputs(const OutputVector& new_args) const { + OV_OP_SCOPE(v0_Floor_clone_with_new_inputs); + check_new_args_count(this, new_args); + return std::make_shared(new_args.at(0)); } -bool evaluate_floor(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) { - bool rc = true; - out->set_unary(arg0); +bool Floor::evaluate(TensorVector& outputs, const TensorVector& inputs) const { + OV_OP_SCOPE(v0_Floor_evaluate); + OPENVINO_ASSERT(outputs.size() == 1); + OPENVINO_ASSERT(inputs.size() == 1); - switch (arg0->get_element_type()) { - OPENVINO_COPY_TENSOR(evaluate_floor, i8, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, i16, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, i32, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, i64, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, u8, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, u16, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, u32, arg0, out, count); - OPENVINO_COPY_TENSOR(evaluate_floor, u64, arg0, out, count); - OPENVINO_TYPE_CASE(evaluate_floor, f16, arg0, out, count); - OPENVINO_TYPE_CASE(evaluate_floor, f32, arg0, out, count); - default: - rc = false; - break; - } - return rc; -} -} // namespace -} // namespace floorop + const auto& in_shape = inputs[0].get_shape(); + outputs[0].set_shape(in_shape); -bool op::Floor::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const { - OV_OP_SCOPE(v0_Floor_evaluate); - return floorop::evaluate_floor(inputs[0], outputs[0], shape_size(inputs[0]->get_shape())); + using namespace ov::element; + return IfTypeOf::apply( + inputs[0].get_element_type(), + inputs[0], + outputs[0], + shape_size(in_shape)); } -bool op::Floor::has_evaluate() const { +bool Floor::has_evaluate() const { OV_OP_SCOPE(v0_Floor_has_evaluate); switch (get_input_element_type(0)) { - case ngraph::element::i8: - case ngraph::element::i16: - case ngraph::element::i32: - case ngraph::element::i64: - case ngraph::element::u8: - case ngraph::element::u16: - case ngraph::element::u32: - case ngraph::element::u64: - case ngraph::element::f16: - case ngraph::element::f32: + case element::f16: + case element::f32: + case element::i8: + case element::i16: + case element::i32: + case element::i64: + case element::u8: + case element::u16: + case element::u32: + case element::u64: return true; default: - break; + return false; } - return false; } +} // namespace v0 +} // namespace op +} // namespace ov