[core]Migrate BatchToSpace to new API (#20450)

* Migrate BatchToSpace to new API

* Remove not required suppression macro
This commit is contained in:
Pawel Raasz authored and GitHub committed 2023-10-18 09:09:27 +00:00
1 parent 222fbb1aec
commit 2415f0c7cf
2 files changed
+50 -66

No files matched your search

@@ -37,9 +37,7 @@ public:
const Output<Node>& block_shape,
const Output<Node>& crops_begin,
const Output<Node>& crops_end);
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;
void validate_and_infer_types() override;
+49 -63
View File
@@ -2,33 +2,23 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/op/batch_to_space.hpp"
#include <batch_to_space_shape_inference.hpp>
#include <cmath>
#include <cstddef>
#include <memory>
#include <ngraph/ops.hpp>
#include <ngraph/validation_util.hpp>
#include <numeric>
#include "openvino/op/batch_to_space.hpp"
#include "batch_to_space_shape_inference.hpp"
#include "itt.hpp"
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/node.hpp"
#include "ngraph/opsets/opset3.hpp"
#include "ngraph/shape.hpp"
#include "openvino/op/util/precision_sensitive_attribute.hpp"
#include "openvino/op/util/slice_plan.hpp"
#include "openvino/reference/reshape.hpp"
#include "openvino/reference/strided_slice.hpp"
using namespace std;
using namespace ngraph;
namespace ov {
namespace op {
namespace v1 {
ngraph::op::v1::BatchToSpace::BatchToSpace(const ngraph::Output<ngraph::Node>& data,
const ngraph::Output<ngraph::Node>& block_shape,
const ngraph::Output<ngraph::Node>& crops_begin,
const ngraph::Output<ngraph::Node>& crops_end)
BatchToSpace::BatchToSpace(const Output<Node>& data,
const Output<Node>& block_shape,
const Output<Node>& crops_begin,
const Output<Node>& crops_end)
: Op({data, block_shape, crops_begin, crops_end}) {
ov::mark_as_precision_sensitive(input(1));
ov::mark_as_precision_sensitive(input(2));
@@ -36,7 +26,7 @@ ngraph::op::v1::BatchToSpace::BatchToSpace(const ngraph::Output<ngraph::Node>& d
constructor_validate_and_infer_types();
}
void op::v1::BatchToSpace::validate_and_infer_types() {
void BatchToSpace::validate_and_infer_types() {
OV_OP_SCOPE(v1_BatchToSpace_validate_and_infer_types);
const auto& data_et = get_input_element_type(0);
@@ -66,30 +56,29 @@ void op::v1::BatchToSpace::validate_and_infer_types() {
set_output_type(0, data_et, output_shape);
}
std::shared_ptr<ngraph::Node> ngraph::op::v1::BatchToSpace::clone_with_new_inputs(const OutputVector& new_args) const {
std::shared_ptr<Node> BatchToSpace::clone_with_new_inputs(const OutputVector& new_args) const {
OV_OP_SCOPE(v1_BatchToSpace_clone_with_new_inputs);
check_new_args_count(this, new_args);
return make_shared<BatchToSpace>(new_args.at(0), new_args.at(1), new_args.at(2), new_args.at(3));
return std::make_shared<BatchToSpace>(new_args.at(0), new_args.at(1), new_args.at(2), new_args.at(3));
}
bool ngraph::op::v1::BatchToSpace::visit_attributes(ngraph::AttributeVisitor& visitor) {
bool BatchToSpace::visit_attributes(AttributeVisitor& visitor) {
OV_OP_SCOPE(v1_BatchToSpace_visit_attributes);
return true;
}
OPENVINO_SUPPRESS_DEPRECATED_START
namespace {
bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) {
auto data = inputs[0];
const auto elem_size = data->get_element_type().size();
bool batch_to_space_evaluate(TensorVector& outputs, const TensorVector& inputs) {
const auto& in = inputs[0];
const auto elem_size = in.get_element_type().size();
auto data_shape = data->get_shape();
auto data_shape = in.get_shape();
auto const block_values_size = shape_size(inputs[1]->get_shape());
auto const block_values_size = shape_size(inputs[1].get_shape());
const auto* block_values = inputs[1]->get_data_ptr<int64_t>();
const auto* crops_begin_values = inputs[2]->get_data_ptr<int64_t>();
const auto* crops_end_values = inputs[3]->get_data_ptr<int64_t>();
const auto* block_values = inputs[1].data<int64_t>();
const auto* crops_begin_values = inputs[2].data<int64_t>();
const auto* crops_end_values = inputs[3].data<int64_t>();
ov::Shape dispersed_shape(1);
dispersed_shape.insert(dispersed_shape.end(), data_shape.begin(), data_shape.end());
@@ -101,7 +90,13 @@ bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVe
return false;
}
auto* flat_data = data->get_data_ptr<char>();
auto* in_first = static_cast<const char*>(in.data());
// Copy input tensor to not overwrite evaluate's inputs tensors passed as const.
// The evaluate algorithm should be improved to avoid additional data copy.
auto flat_in = Tensor(in.get_element_type(), data_shape);
auto* flat_data = static_cast<char*>(flat_in.data());
std::memcpy(flat_data, in_first, flat_in.get_byte_size());
std::vector<char> dispersed_data(shape_size(data_shape) * elem_size);
ov::Shape post_transpose_shape(axes_order.size());
@@ -117,15 +112,15 @@ bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVe
dispersed_shape,
elem_size);
size_t val = 1;
for (size_t axis_idx = 0; axis_idx <= block_values_size; ++axis_idx) {
for (size_t axis_idx = 0, val = 1; axis_idx <= block_values_size; ++axis_idx) {
if ((block_idx + 1) == axis_idx) {
axes_order[axis_idx] = 0;
} else {
axes_order[axis_idx] = val;
val++;
++val;
}
}
for (size_t axis_idx = 0; axis_idx < axes_order.size(); ++axis_idx) {
post_transpose_shape[axis_idx] = dispersed_shape[axes_order[axis_idx]];
}
@@ -148,61 +143,52 @@ bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVe
data_shape = squeezed_shape;
}
std::vector<int64_t> upperbounds_values(data_shape.size());
std::vector<int64_t> upper_bounds_values(data_shape.size());
for (size_t i = 0; i < data_shape.size(); ++i) {
upperbounds_values[i] = data_shape[i] - crops_end_values[i];
upper_bounds_values[i] = data_shape[i] - crops_end_values[i];
}
std::vector<size_t> begin_mask(data_shape.size(), 0);
std::vector<size_t> end_mask(data_shape.size(), 0);
std::vector<int64_t> begins(shape_size(inputs[2]->get_shape()));
begins.assign(crops_begin_values, crops_begin_values + shape_size(inputs[2]->get_shape()));
std::vector<int64_t> begins(shape_size(inputs[2].get_shape()));
begins.assign(crops_begin_values, crops_begin_values + shape_size(inputs[2].get_shape()));
std::vector<int64_t> default_strides(begins.size(), 1);
const auto slice_plan = ov::op::util::make_slice_plan(data_shape,
begins,
upperbounds_values,
upper_bounds_values,
default_strides,
begin_mask,
end_mask,
AxisSet(),
AxisSet(),
AxisSet());
ov::reference::strided_slice(flat_data, outputs[0]->get_data_ptr<char>(), data_shape, slice_plan, elem_size);
ov::reference::strided_slice(flat_data, static_cast<char*>(outputs[0].data()), data_shape, slice_plan, elem_size);
return true;
}
} // namespace
bool ngraph::op::v1::BatchToSpace::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const {
bool BatchToSpace::evaluate(TensorVector& outputs, const TensorVector& inputs) const {
OV_OP_SCOPE(v1_BatchToSpace_evaluate);
OPENVINO_SUPPRESS_DEPRECATED_START
OPENVINO_ASSERT(validate_host_tensor_vector(inputs, 4));
OPENVINO_ASSERT(validate_host_tensor_vector(outputs, 1));
OPENVINO_SUPPRESS_DEPRECATED_END
OPENVINO_ASSERT(outputs.size() == 1);
if (outputs[0]->get_partial_shape().is_dynamic()) {
std::vector<ov::PartialShape> input_shapes;
input_shapes.reserve(inputs.size());
for (size_t i = 0; i < inputs.size(); ++i) {
input_shapes.push_back(inputs[i]->get_partial_shape());
if (input_shapes.back().is_dynamic()) {
return false;
}
}
const auto output_shape = shape_infer(this, input_shapes, ov::make_tensor_accessor(inputs)).front().to_shape();
outputs[0]->set_element_type(inputs[0]->get_element_type());
outputs[0]->set_shape(output_shape);
std::vector<ov::PartialShape> input_shapes;
for (const auto& in : inputs) {
input_shapes.emplace_back(in.get_shape());
}
const auto output_shape = shape_infer(this, input_shapes, ov::make_tensor_accessor(inputs)).front().to_shape();
outputs[0].set_shape(output_shape);
return batch_to_space_evaluate(outputs, inputs);
}
bool ngraph::op::v1::BatchToSpace::has_evaluate() const {
bool BatchToSpace::has_evaluate() const {
OV_OP_SCOPE(v1_BatchToSpace_has_evaluate);
return !get_input_partial_shape(0).is_dynamic() && get_input_shape(0).size() >= 2 &&
get_input_shape(0).size() <= shape_size(get_input_shape(1));
}
} // namespace v1
} // namespace op
} // namespace ov