[core]Migrate Less and Greater operators to new API (#20628)

* Migrate Less operator to new API

* Migrate Greater operator to new API
- use less implementation in greater to reduce bin size

---------

Co-authored-by: Michal Lukaszewski <michal.lukaszewski@intel.com>
This commit is contained in:
Pawel Raasz
2023-10-25 05:25:05 +02:00
committed by GitHub
parent ee6263a141
commit 2b65855793
6 changed files with 143 additions and 147 deletions

View File

@@ -26,9 +26,7 @@ public:
const AutoBroadcastSpec& auto_broadcast = AutoBroadcastSpec(AutoBroadcastType::NUMPY));
std::shared_ptr<Node> 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 v1

View File

@@ -26,9 +26,7 @@ public:
const AutoBroadcastSpec& auto_broadcast = AutoBroadcastSpec(AutoBroadcastType::NUMPY));
std::shared_ptr<Node> 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 v1

View File

@@ -4,25 +4,26 @@
#pragma once
#include <cstddef>
#include "openvino/core/shape.hpp"
#include "openvino/op/util/attr_types.hpp"
#include "openvino/reference/autobroadcast_binop.hpp"
#include "openvino/reference/less.hpp"
namespace ov {
namespace reference {
template <typename T>
void greater(const T* arg0,
const T* arg1,
char* out,
size_t count) // TODO: using char for bool, is this right?
{
for (size_t i = 0; i < count; i++) {
out[i] = arg0[i] > arg1[i];
}
void greater(const T* arg0, const T* arg1, char* out, size_t count) {
less(arg1, arg0, out, count);
}
/**
* @brief Reference implementation of binary elementwise Greater operator.
*
* @param arg0 Pointer to input 0 data.
* @param arg1 Pointer to input 1 data.
* @param out Pointer to output data.
* @param arg0_shape Input 0 shape.
* @param arg1_shape Input 1 shape.
* @param broadcast_spec Broadcast specification mode.
*/
template <typename T, typename U>
void greater(const T* arg0,
const T* arg1,
@@ -30,9 +31,7 @@ void greater(const T* arg0,
const Shape& arg0_shape,
const Shape& arg1_shape,
const op::AutoBroadcastSpec& broadcast_spec) {
autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, [](T x, T y) -> U {
return static_cast<U>(x > y);
});
less(arg1, arg0, out, arg1_shape, arg0_shape, broadcast_spec);
}
} // namespace reference
} // namespace ov

View File

@@ -4,7 +4,7 @@
#pragma once
#include <cstddef>
#include <algorithm>
#include "openvino/core/shape.hpp"
#include "openvino/op/util/attr_types.hpp"
@@ -12,17 +12,30 @@
namespace ov {
namespace reference {
namespace func {
// Use custom implementation as function instead std::less functor, gives smaller binary size.
// If removed or replace check impact on library binary size.
template <class T>
constexpr bool less(const T lhs, const T rhs) {
return lhs < rhs;
}
} // namespace func
template <typename T>
void less(const T* arg0,
const T* arg1,
char* out,
size_t count) // TODO: using char for bool, is this right?
{
for (size_t i = 0; i < count; i++) {
out[i] = arg0[i] < arg1[i];
}
void less(const T* arg0, const T* arg1, char* out, const size_t count) {
std::transform(arg0, std::next(arg0, count), arg1, out, func::less<T>);
}
/**
* @brief Reference implementation of binary elementwise Less operator.
*
* @param arg0 Pointer to input 0 data.
* @param arg1 Pointer to input 1 data.
* @param out Pointer to output data.
* @param arg0_shape Input 0 shape.
* @param arg1_shape Input 1 shape.
* @param broadcast_spec Broadcast specification mode.
*/
template <typename T, typename U>
void less(const T* arg0,
const T* arg1,
@@ -30,9 +43,7 @@ void less(const T* arg0,
const Shape& arg0_shape,
const Shape& arg1_shape,
const op::AutoBroadcastSpec& broadcast_spec) {
autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, [](T x, T y) -> U {
return static_cast<U>(x < y);
});
autobroadcast_binop(arg0, arg1, out, arg0_shape, arg1_shape, broadcast_spec, func::less<T>);
}
} // namespace reference
} // namespace ov

View File

@@ -2,86 +2,81 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/op/greater.hpp"
#include "openvino/op/greater.hpp"
#include "element_visitor.hpp"
#include "itt.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "openvino/reference/greater.hpp"
#include "utils.hpp"
using namespace std;
using namespace ngraph;
namespace ov {
namespace op {
namespace greater {
OPENVINO_SUPPRESS_DEPRECATED_START
namespace greaterop {
namespace {
template <element::Type_t ET>
bool evaluate(const HostTensorPtr& arg0,
const HostTensorPtr& arg1,
const HostTensorPtr& out,
const op::AutoBroadcastSpec& broadcast_spec) {
ov::reference::greater(arg0->get_data_ptr<ET>(),
arg1->get_data_ptr<ET>(),
out->get_data_ptr<element::Type_t::boolean>(),
arg0->get_shape(),
arg1->get_shape(),
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& in0,
const Tensor& in1,
Tensor& out,
const Shape& shape0,
const Shape& shape1,
const AutoBroadcastSpec& broadcast_spec) {
reference::greater(in0.data<const T>(),
in1.data<const T>(),
out.data<fundamental_type_for<element::boolean>>(),
shape0,
shape1,
broadcast_spec);
return true;
}
bool evaluate_greater(const HostTensorPtr& arg0,
const HostTensorPtr& arg1,
const HostTensorPtr& out,
const op::AutoBroadcastSpec& broadcast_spec) {
bool rc = true;
out->set_broadcast(broadcast_spec, arg0, arg1, element::boolean);
switch (arg0->get_element_type()) {
OPENVINO_TYPE_CASE(evaluate_greater, boolean, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, i32, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, i64, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, u32, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, u64, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, f16, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_greater, f32, arg0, arg1, out, broadcast_spec);
default:
rc = false;
break;
return true;
}
return rc;
}
} // namespace
} // namespace greaterop
};
} // namespace greater
//-------------------------------------- v1 ------------------------------------
op::v1::Greater::Greater(const Output<Node>& arg0, const Output<Node>& arg1, const AutoBroadcastSpec& auto_broadcast)
namespace v1 {
Greater::Greater(const Output<Node>& arg0, const Output<Node>& arg1, const AutoBroadcastSpec& auto_broadcast)
: BinaryElementwiseComparison(arg0, arg1, auto_broadcast) {
constructor_validate_and_infer_types();
}
shared_ptr<Node> op::v1::Greater::clone_with_new_inputs(const OutputVector& new_args) const {
std::shared_ptr<Node> Greater::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v1_Greater_clone_with_new_inputs);
check_new_args_count(this, new_args);
return make_shared<op::v1::Greater>(new_args.at(0), new_args.at(1), this->get_autob());
return std::make_shared<Greater>(new_args.at(0), new_args.at(1), get_autob());
}
bool op::v1::Greater::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const {
bool Greater::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
OV_OP_SCOPE(v1_Greater_evaluate);
return greaterop::evaluate_greater(inputs[0], inputs[1], outputs[0], get_autob());
OPENVINO_ASSERT(outputs.size() == 1);
outputs[0].set_shape(infer_broadcast_shape(this, inputs));
using namespace ov::element;
return IfTypeOf<boolean, f16, f32, i32, i64, u32, u64>::apply<greater::Evaluate>(inputs[0].get_element_type(),
inputs[0],
inputs[1],
outputs[0],
inputs[0].get_shape(),
inputs[1].get_shape(),
get_autob());
}
bool op::v1::Greater::has_evaluate() const {
bool Greater::has_evaluate() const {
OV_OP_SCOPE(v1_Greater_has_evaluate);
switch (get_input_element_type(0)) {
case ngraph::element::boolean:
case ngraph::element::i32:
case ngraph::element::i64:
case ngraph::element::u32:
case ngraph::element::u64:
case ngraph::element::f16:
case ngraph::element::f32:
case element::boolean:
case element::f16:
case element::f32:
case element::i32:
case element::i64:
case element::u32:
case element::u64:
return true;
default:
break;
return false;
}
return false;
}
} // namespace v1
} // namespace op
} // namespace ov

View File

@@ -2,85 +2,80 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/op/less.hpp"
#include "openvino/op/less.hpp"
#include "itt.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "openvino/reference/less.hpp"
#include "utils.hpp"
using namespace std;
using namespace ngraph;
namespace ov {
namespace op {
namespace less {
OPENVINO_SUPPRESS_DEPRECATED_START
namespace lessop {
namespace {
template <element::Type_t ET>
bool evaluate(const HostTensorPtr& arg0,
const HostTensorPtr& arg1,
const HostTensorPtr& out,
const op::AutoBroadcastSpec& broadcast_spec) {
ov::reference::less(arg0->get_data_ptr<ET>(),
arg1->get_data_ptr<ET>(),
out->get_data_ptr<element::Type_t::boolean>(),
arg0->get_shape(),
arg1->get_shape(),
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& in0,
const Tensor& in1,
Tensor& out,
const Shape& shape0,
const Shape& shape1,
const AutoBroadcastSpec& broadcast_spec) {
reference::less(in0.data<const T>(),
in1.data<const T>(),
out.data<fundamental_type_for<element::boolean>>(),
shape0,
shape1,
broadcast_spec);
return true;
}
bool evaluate_less(const HostTensorPtr& arg0,
const HostTensorPtr& arg1,
const HostTensorPtr& out,
const op::AutoBroadcastSpec& broadcast_spec) {
bool rc = true;
out->set_broadcast(broadcast_spec, arg0, arg1, element::boolean);
switch (arg0->get_element_type()) {
OPENVINO_TYPE_CASE(evaluate_less, boolean, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, i32, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, i64, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, u32, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, u64, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, f16, arg0, arg1, out, broadcast_spec);
OPENVINO_TYPE_CASE(evaluate_less, f32, arg0, arg1, out, broadcast_spec);
default:
rc = false;
break;
return true;
}
return rc;
}
} // namespace
} // namespace lessop
};
} // namespace less
// ----------------------------- v1 --------------------------------------------
op::v1::Less::Less(const Output<Node>& arg0, const Output<Node>& arg1, const AutoBroadcastSpec& auto_broadcast)
namespace v1 {
Less::Less(const Output<Node>& arg0, const Output<Node>& arg1, const AutoBroadcastSpec& auto_broadcast)
: BinaryElementwiseComparison(arg0, arg1, auto_broadcast) {
constructor_validate_and_infer_types();
}
shared_ptr<Node> op::v1::Less::clone_with_new_inputs(const OutputVector& new_args) const {
std::shared_ptr<Node> Less::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v1_Less_clone_with_new_inputs);
check_new_args_count(this, new_args);
return make_shared<op::v1::Less>(new_args.at(0), new_args.at(1), this->get_autob());
return std::make_shared<Less>(new_args.at(0), new_args.at(1), get_autob());
}
bool op::v1::Less::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const {
bool Less::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
OV_OP_SCOPE(v1_Less_evaluate);
return lessop::evaluate_less(inputs[0], inputs[1], outputs[0], get_autob());
OPENVINO_ASSERT(outputs.size() == 1);
outputs[0].set_shape(infer_broadcast_shape(this, inputs));
using namespace ov::element;
return IfTypeOf<boolean, f16, f32, i32, i64, u32, u64>::apply<less::Evaluate>(inputs[0].get_element_type(),
inputs[0],
inputs[1],
outputs[0],
inputs[0].get_shape(),
inputs[1].get_shape(),
get_autob());
}
bool op::v1::Less::has_evaluate() const {
bool Less::has_evaluate() const {
OV_OP_SCOPE(v1_Less_has_evaluate);
switch (get_input_element_type(0)) {
case ngraph::element::boolean:
case ngraph::element::i32:
case ngraph::element::i64:
case ngraph::element::u32:
case ngraph::element::u64:
case ngraph::element::f16:
case ngraph::element::f32:
case element::boolean:
case element::f16:
case element::f32:
case element::i32:
case element::i64:
case element::u32:
case element::u64:
return true;
default:
break;
return false;
}
return false;
}
} // namespace v1
} // namespace op
} // namespace ov