Reduce binary size of CumSum implementation (#20004)

This commit is contained in:
Pawel Raasz authored and GitHub committed 2023-09-22 20:41:32 +02:00
1 parent efb8484124
commit 93f2a1947c
3 files changed
+56 -73

No files matched your search

@@ -10,18 +10,13 @@ namespace ov {
namespace reference {
template <typename T, typename P>
void cumsum(const T* arg,
const P* axis_tensor,
T* out,
const Shape& tensor_shape,
const bool exclusive,
const bool reverse) {
void cumsum(const T* arg, const P axis, T* out, const Shape& tensor_shape, const bool exclusive, const bool reverse) {
const auto rank = tensor_shape.size();
const auto axis = axis_tensor[0] >= 0 ? axis_tensor[0] : rank + axis_tensor[0];
const auto axis_dim = tensor_shape[axis];
const auto normalized_axis = axis >= 0 ? axis : rank + axis;
const auto axis_dim = tensor_shape[normalized_axis];
const auto size_before_axis = shape_size(Shape(tensor_shape.begin(), tensor_shape.begin() + axis));
const auto size_after_axis = shape_size(Shape(tensor_shape.begin() + axis + 1, tensor_shape.end()));
const auto size_before_axis = shape_size(tensor_shape.begin(), tensor_shape.begin() + normalized_axis);
const auto size_after_axis = shape_size(tensor_shape.begin() + normalized_axis + 1, tensor_shape.end());
const auto reverse_shift = reverse ? -1 : 1;
const auto element_shift = exclusive ? size_after_axis * reverse_shift : 0;
+50 -62
View File
@@ -4,36 +4,70 @@
#include "openvino/op/cum_sum.hpp"
#include "element_visitor.hpp"
#include "itt.hpp"
#include "openvino/core/attribute_visitor.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/reference/cum_sum.hpp"
using namespace std;
#include "utils.hpp"
namespace ov {
op::v0::CumSum::CumSum(const Output<Node>& arg, const Output<Node>& axis, const bool exclusive, const bool reverse)
namespace op {
namespace cumsum {
struct Evaluate : ov::element::NoAction<bool> {
using ov::element::NoAction<bool>::visit;
template <element::Type_t ET>
static result_type visit(const Tensor& in0,
Tensor& out,
const int64_t axis,
const bool exclusive,
const bool reverse) {
using T = typename element_type_traits<ET>::value_type;
reference::cumsum(in0.data<const T>(), axis, out.data<T>(), in0.get_shape(), exclusive, reverse);
return true;
}
};
namespace {
bool evaluate(TensorVector& outputs, const TensorVector& inputs, const bool exclusive, const bool reverse) {
outputs[0].set_shape(inputs[0].get_shape());
const auto axis = ov::get_tensor_data_as<int64_t>(inputs[1]).front();
using namespace ov::element;
return IfTypeOf<f32>::apply<cumsum::Evaluate>(inputs[0].get_element_type(),
inputs[0],
outputs[0],
axis,
exclusive,
reverse);
}
} // namespace
} // namespace cumsum
namespace v0 {
CumSum::CumSum(const Output<Node>& arg, const Output<Node>& axis, const bool exclusive, const bool reverse)
: Op({arg, axis}),
m_exclusive(exclusive),
m_reverse(reverse) {
constructor_validate_and_infer_types();
}
op::v0::CumSum::CumSum(const Output<Node>& arg, const bool exclusive, const bool reverse)
CumSum::CumSum(const Output<Node>& arg, const bool exclusive, const bool reverse)
: Op({arg, op::v0::Constant::create(element::i32, Shape{}, {0})}),
m_exclusive(exclusive),
m_reverse(reverse) {
constructor_validate_and_infer_types();
}
bool op::v0::CumSum::visit_attributes(AttributeVisitor& visitor) {
bool CumSum::visit_attributes(AttributeVisitor& visitor) {
OV_OP_SCOPE(v0_CumSum_visit_attributes);
visitor.on_attribute("exclusive", m_exclusive);
visitor.on_attribute("reverse", m_reverse);
return true;
}
void op::v0::CumSum::validate_and_infer_types() {
void CumSum::validate_and_infer_types() {
OV_OP_SCOPE(v0_CumSum_validate_and_infer_types);
set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));
@@ -47,76 +81,30 @@ void op::v0::CumSum::validate_and_infer_types() {
// No axis input shape check for backward compatibility
}
shared_ptr<Node> op::v0::CumSum::clone_with_new_inputs(const OutputVector& new_args) const {
std::shared_ptr<Node> CumSum::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v0_CumSum_clone_with_new_inputs);
check_new_args_count(this, new_args);
if (new_args.size() == 2)
return make_shared<op::v0::CumSum>(new_args.at(0), new_args.at(1), m_exclusive, m_reverse);
return std::make_shared<CumSum>(new_args.at(0), new_args.at(1), m_exclusive, m_reverse);
else {
return make_shared<op::v0::CumSum>(new_args.at(0), m_exclusive, m_reverse);
return std::make_shared<CumSum>(new_args.at(0), m_exclusive, m_reverse);
}
}
namespace {
template <element::Type_t DATA_ET, element::Type_t AXIS_ET>
bool evaluate_cum_sum(TensorVector& outputs, const TensorVector& inputs, const bool exclusive, const bool reverse) {
using data_t = fundamental_type_for<DATA_ET>;
using axis_t = fundamental_type_for<AXIS_ET>;
ov::reference::cumsum<data_t, axis_t>(inputs[0].data<data_t>(),
inputs[1].data<axis_t>(),
outputs[0].data<data_t>(),
inputs[0].get_shape(),
exclusive,
reverse);
return true;
}
#define CUM_SUM_TYPE_CASE(a, ...) \
case element::Type_t::a: { \
OV_OP_SCOPE(OV_PP_CAT4(evaluate_cum_sum, _, a, AXIS_ET)); \
return evaluate_cum_sum<element::Type_t::a, AXIS_ET>(__VA_ARGS__); \
}
template <element::Type_t AXIS_ET>
bool evaluate(TensorVector& outputs, const TensorVector& inputs, const bool exclusive, const bool reverse) {
switch (inputs[0].get_element_type()) {
CUM_SUM_TYPE_CASE(f32, outputs, inputs, exclusive, reverse);
default:
return false;
}
}
bool evaluate_cum_sum(TensorVector& outputs, const TensorVector& inputs, const bool exclusive, const bool reverse) {
auto rc = true;
switch (inputs[1].get_element_type()) {
NGRAPH_TYPE_CASE(evaluate_cum_sum, i32, outputs, inputs, exclusive, reverse);
NGRAPH_TYPE_CASE(evaluate_cum_sum, i64, outputs, inputs, exclusive, reverse);
default:
rc = false;
break;
}
return rc;
}
} // namespace
bool op::v0::CumSum::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
bool CumSum::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
OV_OP_SCOPE(v0_CumSum_evaluate);
OPENVINO_ASSERT(inputs.size() == 2,
"Invalid size of inputs argument of evaluate method of CumSum operation. Provided: ",
inputs.size(),
". Expected: 2");
OPENVINO_ASSERT(outputs.size() == 1,
"Invalid size of outputs argument of evaluate method of CumSum operation. Provided: ",
outputs.size(),
". Expected: 1");
OPENVINO_ASSERT(outputs.size() == 1);
OPENVINO_ASSERT(inputs.size() == 2);
return evaluate_cum_sum(outputs, inputs, is_exclusive(), is_reverse());
return cumsum::evaluate(outputs, inputs, is_exclusive(), is_reverse());
}
bool op::v0::CumSum::has_evaluate() const {
bool CumSum::has_evaluate() const {
OV_OP_SCOPE(v0_CumSum_has_evaluate);
const auto& input_0_et = get_input_element_type(0);
const auto& input_1_et = get_input_element_type(1);
return input_0_et == element::f32 && (input_1_et == element::i32 || input_1_et == element::i64);
}
} // namespace v0
} // namespace op
} // namespace ov
+1 -1
View File
@@ -15,7 +15,7 @@ inline void evaluate(const std::shared_ptr<ov::op::v0::CumSum>& op,
using T1 = typename ov::element_type_traits<t1>::value_type;
using T2 = typename ov::element_type_traits<t2>::value_type;
ov::reference::cumsum<T1, T2>(inputs[0].data<T1>(),
inputs[1].data<T2>(),
*inputs[1].data<T2>(),
outputs[0].data<T1>(),
inputs[0].get_shape(),
op->is_exclusive(),