[Ref] Drop legacy API - leftovers (#20271)

* Merge opt_kernel into reference

* Remove get_default_order

* Use ov:: in jit generators

* Remove unused template function

* Add reshape parameter for consistency with Op spec

* Add brief description and such

* Remove unused param from reshape ref

* Use C++ casting
This commit is contained in:
Tomasz Jankowski 2023-10-10 03:27:22 +02:00 committed by Alexander Nesterov
parent 4c14ad365c
commit b79ed8ba6e
22 changed files with 477 additions and 582 deletions

View File

@ -1,21 +0,0 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "ngraph/axis_vector.hpp"
#include "ngraph/shape.hpp"
namespace ngraph {
namespace runtime {
namespace opt_kernel {
void reshape(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size);
}
} // namespace runtime
} // namespace ngraph

View File

@ -98,35 +98,5 @@ void group_convolution_backprop_data(const T* in,
}
}
}
// DEPRECATED, can't be removed currently due to arm-plugin dependency
template <typename OUTPUT, typename FILTER, typename INPUT, typename ACCUMULATION = typename widen<INPUT>::type>
OPENVINO_DEPRECATED("group_convolution_backprop_data function without output_paddings is deprecated, "
"use the one with output_padding.")
void group_convolution_backprop_data(const INPUT* in,
const FILTER* f,
OUTPUT* out,
const Shape& in_shape,
const Shape& filter_shape,
const Shape& out_shape,
const Strides& strides,
const Strides& dilation,
const CoordinateDiff& pads_begin,
const CoordinateDiff& pads_end) {
const CoordinateDiff output_padding(in_shape.size() - 2, 0);
group_convolution_backprop_data(in,
f,
out,
in_shape,
filter_shape,
out_shape,
strides,
dilation,
pads_begin,
pads_end,
output_padding);
}
} // namespace reference
} // namespace ov

View File

@ -9,8 +9,8 @@
#include <utility>
#include <vector>
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/reference/broadcast.hpp"
#include "openvino/reference/reshape.hpp"
namespace ov {
namespace reference {
@ -100,12 +100,12 @@ void matmul(const T* arg0,
std::vector<T> tmp(shape_size(arg0_shape));
auto axis_vector = details::get_transpose_order(arg0_shape);
std::swap(arg0_shape_tmp[arg0_rank - 1], arg0_shape_tmp[arg0_rank - 2]);
ngraph::runtime::opt_kernel::reshape(reinterpret_cast<const char*>(arg0_data),
reinterpret_cast<char*>(tmp.data()),
arg0_shape,
axis_vector,
arg0_shape_tmp,
sizeof(T));
reshape(reinterpret_cast<const char*>(arg0_data),
reinterpret_cast<char*>(tmp.data()),
arg0_shape,
axis_vector,
arg0_shape_tmp,
sizeof(T));
arg0_new_data.swap(tmp);
arg0_data = arg0_new_data.data();
}
@ -114,12 +114,12 @@ void matmul(const T* arg0,
std::vector<T> tmp(shape_size(arg1_shape));
auto axis_vector = details::get_transpose_order(arg1_shape);
std::swap(arg1_shape_tmp[arg1_rank - 1], arg1_shape_tmp[arg1_rank - 2]);
ngraph::runtime::opt_kernel::reshape(reinterpret_cast<const char*>(arg1_data),
reinterpret_cast<char*>(tmp.data()),
arg1_shape,
axis_vector,
arg1_shape_tmp,
sizeof(T));
reshape(reinterpret_cast<const char*>(arg1_data),
reinterpret_cast<char*>(tmp.data()),
arg1_shape,
axis_vector,
arg1_shape_tmp,
sizeof(T));
arg1_new_data.swap(tmp);
arg1_data = arg1_new_data.data();
}

View File

@ -14,7 +14,7 @@
namespace ov {
namespace reference {
// Return type is `void`, only enabled if `T` is a built-in FP
// type, or nGraph's `bfloat16` or `float16` type.
// type, or OpenVINO's `bfloat16` or `float16` type.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value || std::is_same<T, bfloat16>::value ||
std::is_same<T, float16>::value>::type

View File

@ -9,11 +9,35 @@
namespace ov {
namespace reference {
void reshape(const char* arg,
/**
* @brief Basic reshape operation, without axes reorder.
*
* @param in Pointer to input data.
* @param out Pointer to output data.
* @param in_shape Input data shape.
* @param out_shape Output data shape.
* @param elem_size Single data element size im bytes.
*/
inline void reshape(const char* in, char* out, const Shape& in_shape, size_t elem_size) {
std::memcpy(out, in, shape_size(in_shape) * elem_size);
}
/**
* @brief Permutes data shape and axes.
*
* @param in Pointer to input data.
* @param out Pointer to output data.
* @param in_shape Input data shape.
* @param axes_order Axes order.
* @param out_shape Output data shape.
* @param elem_size Single data element size im bytes.
*/
void reshape(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size);
}
} // namespace reference
} // namespace ov

View File

@ -4,9 +4,8 @@
#pragma once
#include <cmath>
#include "openvino/reference/utils/coordinate_transform.hpp"
#include "openvino/core/axis_set.hpp"
#include "openvino/core/shape.hpp"
namespace ov {
namespace reference {

View File

@ -7,7 +7,7 @@
#if defined(OPENVINO_ARCH_X86) || defined(OPENVINO_ARCH_X86_64)
# include "jit_generator.hpp"
using namespace ngraph::runtime;
using namespace ov::runtime;
#endif
namespace ov {

View File

@ -7,8 +7,8 @@
#include <cmath>
#include <numeric>
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/core/except.hpp"
#include "openvino/reference/reshape.hpp"
namespace ov {
namespace reference {
@ -96,7 +96,7 @@ void depth_to_space(const char* const in,
post_transpose_shape[axis_idx] = dispersed_shape[axes_order[axis_idx]];
}
ngraph::runtime::opt_kernel::reshape(in, out, dispersed_shape, axes_order, post_transpose_shape, elem_size);
reshape(in, out, dispersed_shape, axes_order, post_transpose_shape, elem_size);
}
} // namespace reference

View File

@ -249,14 +249,6 @@ Shape compute_matmul_output_shape(const Shape& common_sub_shape,
return matmul_output_shape;
}
/// @brief Prepares default order axis vector
///
AxisVector get_default_order(size_t rank) {
AxisVector default_order(rank);
std::iota(begin(default_order), end(default_order), 0);
return default_order;
}
/// \brief Update a vector of inputs and subscripts by removing items for
/// inputs with indices input_ind1 and input_ind2 and inserted new input and
/// the corresponsing subscript in the tail
@ -296,15 +288,12 @@ ov::Tensor unsqueeze_input(const ov::Tensor& input, std::vector<int64_t>& unsque
}
auto output = ov::Tensor(input.get_element_type(), output_shape);
const auto order = get_default_order(input_shape.size());
const auto element_type = input.get_element_type();
reference::reshape(reinterpret_cast<const char*>(input.data<T>()),
reinterpret_cast<char*>(output.data<T>()),
input_shape,
order,
output_shape,
element_type.size());
reshape(static_cast<const char*>(input.data()),
static_cast<char*>(output.data()),
input_shape,
element_type.size());
return output;
}
@ -653,14 +642,11 @@ ov::Tensor reshape_input_for_matmul(const ov::Tensor& input,
const auto element_type = input.get_element_type();
const auto& input_shape = input.get_shape();
auto output = ov::Tensor(element_type, new_shape);
const auto order = get_default_order(input_shape.size());
reference::reshape(reinterpret_cast<const char*>(input.data<T>()),
reinterpret_cast<char*>(output.data<T>()),
input_shape,
order,
new_shape,
element_type.size());
reshape(static_cast<const char*>(input.data()),
static_cast<char*>(output.data()),
input_shape,
element_type.size());
return output;
}
@ -930,13 +916,10 @@ void contract_two_inputs(ov::TensorVector& inputs,
back_shape.insert(back_shape.end(), separate2_sub_shape.begin(), separate2_sub_shape.end());
auto contract_output = ov::Tensor(matmul_output.get_element_type(), back_shape);
const auto order = get_default_order(matmul_output.get_shape().size());
reference::reshape(reinterpret_cast<const char*>(matmul_output.data<T>()),
reinterpret_cast<char*>(contract_output.data<T>()),
matmul_output.get_shape(),
order,
back_shape,
matmul_output.get_element_type().size());
reshape(static_cast<const char*>(matmul_output.data()),
static_cast<char*>(contract_output.data()),
matmul_output.get_shape(),
matmul_output.get_element_type().size());
update_operands(inputs, input_subscripts, input_ind1, input_ind2, contract_output, resultant_subscript);
}

View File

@ -12,9 +12,9 @@
# include <xbyak/xbyak_util.h>
# include "jit_generator.hpp"
# include "ngraph/type/float16.hpp"
# include "openvino/core/type/float16.hpp"
namespace ngraph {
namespace ov {
namespace runtime {
namespace jit {
using namespace Xbyak;
@ -186,6 +186,6 @@ void Generator::copy<float>(const Xbyak::Reg64& dst, const Xbyak::Reg64& src, co
}
} // namespace jit
} // namespace runtime
} // namespace ngraph
} // namespace ov
#endif // OPENVINO_ARCH_X86 || OPENVINO_ARCH_X86_64

View File

@ -11,12 +11,9 @@
#include <functional>
#include <xbyak/xbyak.h>
namespace ngraph
{
namespace runtime
{
namespace jit
{
namespace ov {
namespace runtime {
namespace jit {
#ifdef XBYAK64
static const Xbyak::Operand::Code abi_save_gpr_regs[] = {
Xbyak::Operand::RBX,
@ -94,4 +91,4 @@ namespace ngraph
};
}
}
}
} // namespace ov

View File

@ -8,12 +8,290 @@
#include <numeric>
#include "openvino/core/except.hpp"
#include "openvino/core/parallel.hpp"
#include "openvino/reference/utils/coordinate_range.hpp"
#include "openvino/reference/utils/coordinate_transform.hpp"
namespace ov {
namespace reference {
namespace {
static size_t get_threshold() {
// TODO: find a better way, not hardcoded value
return (1 << 9) * parallel_get_num_threads();
}
static inline void copy_element(char* out, const char* in, size_t elem_size) {
#define CASE(type) \
case sizeof(type): \
*reinterpret_cast<type*>(out) = *reinterpret_cast<const type*>(in); \
break;
switch (elem_size) {
CASE(int32_t)
CASE(int64_t)
CASE(int16_t)
CASE(int8_t)
default:
std::memcpy(out, in, elem_size);
break;
}
#undef CASE
}
void reshape_2D(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off = i;
for (size_t j = 0; j < out_shape[1]; j++) {
copy_element(out, in + off * elem_size, elem_size);
out += elem_size;
off += out_shape[0];
}
}
} else {
ov::parallel_for2d(out_shape[0], out_shape[1], [in, out, &out_shape, elem_size](size_t i, size_t j) {
size_t in_off = j * out_shape[0] + i;
size_t out_off = i * out_shape[1] + j;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
static std::vector<size_t> get_strides(size_t rank, size_t elem_size, const AxisVector& order, const Shape& in_shape) {
std::vector<size_t> rev_order(rank);
for (size_t i = 0; i < rank; i++) {
rev_order[order[i]] = i;
}
std::vector<size_t> strides(rank);
strides[rev_order[rank - 1]] = elem_size;
for (size_t i = rank - 1; i > 0; i--) {
strides[rev_order[i - 1]] = strides[rev_order[i]] * in_shape[i];
}
return strides;
}
void reshape_3D(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(3, elem_size, axes_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t in_off = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for3d(out_shape[0],
out_shape[1],
out_shape[2],
[in, out, axes_order, &in_shape, &out_shape, elem_size](size_t i, size_t j, size_t k) {
size_t in_indexes[3];
in_indexes[axes_order[0]] = i;
in_indexes[axes_order[1]] = j;
in_indexes[axes_order[2]] = k;
size_t in_off =
(in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2];
size_t out_off = (i * out_shape[1] + j) * out_shape[2] + k;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_4D(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(4, elem_size, axes_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t in_off = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for4d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
[in, out, axes_order, &in_shape, &out_shape, elem_size](size_t i, size_t j, size_t k, size_t l) {
size_t in_indexes[4];
in_indexes[axes_order[0]] = i;
in_indexes[axes_order[1]] = j;
in_indexes[axes_order[2]] = k;
in_indexes[axes_order[3]] = l;
size_t in_off =
((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3];
size_t out_off = ((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_5D(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(5, elem_size, axes_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t off_3 = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
size_t in_off = off_3;
for (size_t m = 0; m < out_shape[4]; m++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[4];
}
off_3 += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for5d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
out_shape[4],
[in, out, axes_order, &in_shape, &out_shape, elem_size](size_t i, size_t j, size_t k, size_t l, size_t m) {
size_t in_indexes[5];
in_indexes[axes_order[0]] = i;
in_indexes[axes_order[1]] = j;
in_indexes[axes_order[2]] = k;
in_indexes[axes_order[3]] = l;
in_indexes[axes_order[4]] = m;
size_t in_off =
(((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3]) *
in_shape[4] +
in_indexes[4];
size_t out_off = (((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l) * out_shape[4] + m;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_6D(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(6, elem_size, axes_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t off_3 = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
size_t off_4 = off_3;
for (size_t m = 0; m < out_shape[4]; m++) {
size_t in_off = off_4;
for (size_t n = 0; n < out_shape[5]; n++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[5];
}
off_4 += strides[4];
}
off_3 += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for6d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
out_shape[4],
out_shape[5],
[=, &axes_order, &in_shape, &out_shape](size_t i, size_t j, size_t k, size_t l, size_t m, size_t n) {
size_t in_indexes[6];
in_indexes[axes_order[0]] = i;
in_indexes[axes_order[1]] = j;
in_indexes[axes_order[2]] = k;
in_indexes[axes_order[3]] = l;
in_indexes[axes_order[4]] = m;
in_indexes[axes_order[5]] = n;
size_t in_off =
((((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3]) *
in_shape[4] +
in_indexes[4]) *
in_shape[5] +
in_indexes[5];
size_t out_off = ((((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l) * out_shape[4] + m) *
out_shape[5] +
n;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
std::vector<size_t> reorder(const std::vector<size_t>& origin, const AxisVector& order) {
std::vector<size_t> reordered = origin;
auto out = begin(reordered);
@ -24,31 +302,77 @@ std::vector<size_t> reorder(const std::vector<size_t>& origin, const AxisVector&
}
return reordered;
}
} // namespace
void reshape(const char* arg,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
if (shape_size(in_shape) == 1) {
std::memcpy(out, arg, elem_size);
return;
}
void reshape_ND(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
char* output = out;
const char* const output_end = out + shape_size(out_shape) * elem_size;
const auto axis_strides = reorder(row_major_strides(in_shape), in_axis_order);
for (const auto& coordinate : CoordinateTransformBasic(reorder(in_shape, in_axis_order))) {
const auto axis_strides = reorder(row_major_strides(in_shape), axes_order);
for (const auto& coordinate : CoordinateTransformBasic(reorder(in_shape, axes_order))) {
if (output >= output_end) {
break;
}
const auto elem_offset = std::inner_product(begin(coordinate), end(coordinate), begin(axis_strides), 0ll);
const auto input = arg + elem_offset * elem_size;
std::memcpy(output, input, elem_size);
const auto input = in + elem_offset * elem_size;
copy_element(output, input, elem_size);
output += elem_size;
}
}
bool no_axis_reordering(const AxisVector& axis_order) {
auto tmp = axis_order;
std::sort(begin(tmp), end(tmp));
tmp.erase(std::unique(begin(tmp), end(tmp)), end(tmp));
return tmp == axis_order;
}
} // namespace
void reshape(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& axes_order,
const Shape& out_shape,
size_t elem_size) {
if (no_axis_reordering(axes_order)) {
std::memcpy(out, in, shape_size(in_shape) * elem_size);
return;
}
if (shape_size(in_shape) == 1) {
copy_element(out, in, elem_size);
return;
}
switch (in_shape.size()) {
case 0:
copy_element(out, in, elem_size);
break;
case 1:
std::memcpy(out, in, in_shape[0] * elem_size);
break;
case 2:
reshape_2D(in, out, in_shape, axes_order, out_shape, elem_size);
break;
case 3:
reshape_3D(in, out, in_shape, axes_order, out_shape, elem_size);
break;
case 4:
reshape_4D(in, out, in_shape, axes_order, out_shape, elem_size);
break;
case 5:
reshape_5D(in, out, in_shape, axes_order, out_shape, elem_size);
break;
case 6:
reshape_6D(in, out, in_shape, axes_order, out_shape, elem_size);
break;
default:
reshape_ND(in, out, in_shape, axes_order, out_shape, elem_size);
break;
}
}
} // namespace reference
} // namespace ov

View File

@ -4,7 +4,7 @@
#include "openvino/reference/shuffle_channels.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/reference/reshape.hpp"
namespace ov {
namespace reference {
@ -42,7 +42,7 @@ void shuffle_channels(const char* arg,
reshaped_input_shape[1],
reshaped_input_shape[3]};
AxisVector axis_vector{0, 2, 1, 3};
ngraph::runtime::opt_kernel::reshape(arg, out, reshaped_input_shape, axis_vector, transposed_shape, elem_size);
reshape(arg, out, reshaped_input_shape, axis_vector, transposed_shape, elem_size);
// Reshaped 4D tensor is interpreted as ND output tensor with original shape of data
// input

View File

@ -6,8 +6,8 @@
#include <vector>
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/core/except.hpp"
#include "openvino/reference/reshape.hpp"
namespace ov {
namespace reference {
@ -84,7 +84,7 @@ void space_to_depth(const char* const in,
post_transpose_shape[axis_idx] = dispersed_shape[axes_order[axis_idx]];
}
ngraph::runtime::opt_kernel::reshape(in, out, dispersed_shape, axes_order, post_transpose_shape, elem_size);
reshape(in, out, dispersed_shape, axes_order, post_transpose_shape, elem_size);
}
} // namespace reference
} // namespace ov

View File

@ -9,17 +9,19 @@
#include <cmath>
#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/reference/reshape.hpp"
#include "openvino/reference/reverse.hpp"
#include "openvino/reference/slice.hpp"
using namespace ov;
NGRAPH_SUPPRESS_DEPRECATED_START
namespace ov {
namespace reference {
void reference::strided_slice(const char* arg,
char* out,
const Shape& arg_shape,
const op::util::SlicePlan& sp,
size_t elem_type) {
auto hasZeroDims = [](const ov::Shape& shape) -> bool {
void strided_slice(const char* arg,
char* out,
const Shape& arg_shape,
const op::util::SlicePlan& sp,
size_t elem_type) {
auto hasZeroDims = [](const Shape& shape) -> bool {
return std::any_of(shape.begin(), shape.end(), [](const size_t& dim) {
return dim == 0;
});
@ -28,6 +30,7 @@ void reference::strided_slice(const char* arg,
return;
}
OPENVINO_SUPPRESS_DEPRECATED_START
ngraph::runtime::AlignedBuffer slice_out_buffer(shape_size(sp.reshape_in_shape) * elem_type);
slice(reinterpret_cast<const char*>(arg),
slice_out_buffer.get_ptr<char>(),
@ -39,12 +42,7 @@ void reference::strided_slice(const char* arg,
elem_type);
ngraph::runtime::AlignedBuffer reshape_out_buffer(shape_size(sp.reshape_out_shape) * elem_type);
ngraph::runtime::opt_kernel::reshape(slice_out_buffer.get_ptr<char>(),
reshape_out_buffer.get_ptr<char>(),
sp.reshape_in_shape,
ngraph::get_default_order(sp.reshape_in_shape.size()),
sp.reshape_out_shape,
elem_type);
reshape(slice_out_buffer.get_ptr<char>(), reshape_out_buffer.get_ptr<char>(), sp.reshape_in_shape, elem_type);
reverse(reshape_out_buffer.get_ptr<char>(),
out,
@ -52,4 +50,7 @@ void reference::strided_slice(const char* arg,
sp.reshape_out_shape,
sp.reverse_axes,
elem_type);
OPENVINO_SUPPRESS_DEPRECATED_END
}
} // namespace reference
} // namespace ov

View File

@ -9,8 +9,8 @@
#include <numeric>
#include <vector>
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/core/shape.hpp"
#include "openvino/reference/reshape.hpp"
namespace ov {
namespace reference {
@ -20,10 +20,10 @@ void transpose(const char* data,
size_t element_size,
const int64_t* axes_order,
Shape out_shape) {
// To reuse opt_kernel::reshape axes order vector has to be converted to AxisVector
// To reuse reference::reshape axes order vector has to be converted to AxisVector
// Negative axes are not supported, it is validated by transpose evaluate method
std::vector<size_t> axis_vector(axes_order, axes_order + data_shape.size());
ngraph::runtime::opt_kernel::reshape(data, out, data_shape, axis_vector, out_shape, element_size);
reshape(data, out, data_shape, axis_vector, out_shape, element_size);
}
} // namespace reference
} // namespace ov

View File

@ -1,375 +0,0 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include <algorithm>
#include <cstring>
#include "openvino/core/parallel.hpp"
#include "openvino/reference/reshape.hpp"
using namespace ngraph;
namespace {
void reshape_in0(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
memcpy(out, in, elem_size);
}
void reshape_in1(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t size[1];
size_t in_index[1];
size_t* map_index[1];
for (size_t i = 0; i < 1; i++) {
size[i] = in_shape[in_axis_order[i]];
map_index[in_axis_order[i]] = &in_index[i];
}
for (in_index[0] = 0; in_index[0] < size[0]; ++in_index[0]) {
memcpy(out, in + *map_index[0] * elem_size, elem_size);
out += elem_size;
}
}
static size_t get_threshold() {
// TODO: find a better way, not hardcoded value
return (1 << 9) * parallel_get_num_threads();
}
static inline void copy_element(char* out, const char* in, size_t elem_size) {
#define CASE(type) \
case sizeof(type): \
*reinterpret_cast<type*>(out) = *reinterpret_cast<const type*>(in); \
break;
switch (elem_size) {
CASE(int32_t)
CASE(int64_t)
CASE(int16_t)
CASE(int8_t)
default:
std::memcpy(out, in, elem_size);
break;
}
#undef CASE
}
void reshape_in2(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off = i;
for (size_t j = 0; j < out_shape[1]; j++) {
copy_element(out, in + off * elem_size, elem_size);
out += elem_size;
off += out_shape[0];
}
}
} else {
ov::parallel_for2d(out_shape[0], out_shape[1], [in, out, &out_shape, elem_size](size_t i, size_t j) {
size_t in_off = j * out_shape[0] + i;
size_t out_off = i * out_shape[1] + j;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
static std::vector<size_t> get_strides(size_t rank, size_t elem_size, const AxisVector& order, const Shape& in_shape) {
std::vector<size_t> rev_order(rank);
for (size_t i = 0; i < rank; i++) {
rev_order[order[i]] = i;
}
std::vector<size_t> strides(rank);
strides[rev_order[rank - 1]] = elem_size;
for (size_t i = rank - 1; i > 0; i--) {
strides[rev_order[i - 1]] = strides[rev_order[i]] * in_shape[i];
}
return strides;
}
void reshape_in3(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(3, elem_size, in_axis_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t in_off = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for3d(out_shape[0],
out_shape[1],
out_shape[2],
[in, out, in_axis_order, &in_shape, &out_shape, elem_size](size_t i, size_t j, size_t k) {
size_t in_indexes[3];
in_indexes[in_axis_order[0]] = i;
in_indexes[in_axis_order[1]] = j;
in_indexes[in_axis_order[2]] = k;
size_t in_off =
(in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2];
size_t out_off = (i * out_shape[1] + j) * out_shape[2] + k;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_in4(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(4, elem_size, in_axis_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t in_off = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for4d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
[in, out, in_axis_order, &in_shape, &out_shape, elem_size](size_t i, size_t j, size_t k, size_t l) {
size_t in_indexes[4];
in_indexes[in_axis_order[0]] = i;
in_indexes[in_axis_order[1]] = j;
in_indexes[in_axis_order[2]] = k;
in_indexes[in_axis_order[3]] = l;
size_t in_off =
((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3];
size_t out_off = ((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_in5(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(5, elem_size, in_axis_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t off_3 = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
size_t in_off = off_3;
for (size_t m = 0; m < out_shape[4]; m++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[4];
}
off_3 += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for5d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
out_shape[4],
[in, out, in_axis_order, &in_shape, &out_shape, elem_size](size_t i,
size_t j,
size_t k,
size_t l,
size_t m) {
size_t in_indexes[5];
in_indexes[in_axis_order[0]] = i;
in_indexes[in_axis_order[1]] = j;
in_indexes[in_axis_order[2]] = k;
in_indexes[in_axis_order[3]] = l;
in_indexes[in_axis_order[4]] = m;
size_t in_off =
(((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3]) *
in_shape[4] +
in_indexes[4];
size_t out_off = (((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l) * out_shape[4] + m;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
void reshape_in6(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
size_t num_elements = shape_size(in_shape);
if (num_elements <= get_threshold()) {
const auto strides = get_strides(6, elem_size, in_axis_order, in_shape);
size_t off_0 = 0;
for (size_t i = 0; i < out_shape[0]; i++) {
size_t off_1 = off_0;
for (size_t j = 0; j < out_shape[1]; j++) {
size_t off_2 = off_1;
for (size_t k = 0; k < out_shape[2]; k++) {
size_t off_3 = off_2;
for (size_t l = 0; l < out_shape[3]; l++) {
size_t off_4 = off_3;
for (size_t m = 0; m < out_shape[4]; m++) {
size_t in_off = off_4;
for (size_t n = 0; n < out_shape[5]; n++) {
copy_element(out, in + in_off, elem_size);
out += elem_size;
in_off += strides[5];
}
off_4 += strides[4];
}
off_3 += strides[3];
}
off_2 += strides[2];
}
off_1 += strides[1];
}
off_0 += strides[0];
}
} else {
ov::parallel_for6d(
out_shape[0],
out_shape[1],
out_shape[2],
out_shape[3],
out_shape[4],
out_shape[5],
[in, out, in_axis_order, &in_shape, &out_shape, elem_size](size_t i,
size_t j,
size_t k,
size_t l,
size_t m,
size_t n) {
size_t in_indexes[6];
in_indexes[in_axis_order[0]] = i;
in_indexes[in_axis_order[1]] = j;
in_indexes[in_axis_order[2]] = k;
in_indexes[in_axis_order[3]] = l;
in_indexes[in_axis_order[4]] = m;
in_indexes[in_axis_order[5]] = n;
size_t in_off =
((((in_indexes[0] * in_shape[1] + in_indexes[1]) * in_shape[2] + in_indexes[2]) * in_shape[3] +
in_indexes[3]) *
in_shape[4] +
in_indexes[4]) *
in_shape[5] +
in_indexes[5];
size_t out_off = ((((i * out_shape[1] + j) * out_shape[2] + k) * out_shape[3] + l) * out_shape[4] + m) *
out_shape[5] +
n;
copy_element(out + out_off * elem_size, in + in_off * elem_size, elem_size);
});
}
}
bool no_axis_reordering(const AxisVector& axis_order) {
auto tmp = axis_order;
std::sort(begin(tmp), end(tmp));
tmp.erase(std::unique(begin(tmp), end(tmp)), end(tmp));
return tmp == axis_order;
}
} // namespace
void runtime::opt_kernel::reshape(const char* in,
char* out,
const Shape& in_shape,
const AxisVector& in_axis_order,
const Shape& out_shape,
size_t elem_size) {
if (no_axis_reordering(in_axis_order)) {
std::memcpy(out, in, shape_size(in_shape) * elem_size);
return;
}
switch (in_shape.size()) {
case 0:
reshape_in0(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 1:
reshape_in1(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 2:
reshape_in2(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 3:
reshape_in3(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 4:
reshape_in4(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 5:
reshape_in5(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
case 6:
reshape_in6(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
default:
ov::reference::reshape(in, out, in_shape, in_axis_order, out_shape, elem_size);
break;
}
}

View File

@ -16,10 +16,10 @@
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/node.hpp"
#include "ngraph/opsets/opset3.hpp"
#include "ngraph/runtime/opt_kernel/reshape.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;
@ -110,12 +110,12 @@ bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVe
for (size_t block_idx = 1; block_idx < block_values_size; ++block_idx) {
dispersed_shape[0] = block_values[block_idx];
dispersed_shape[1] /= block_values[block_idx];
runtime::opt_kernel::reshape(flat_data,
dispersed_data.data(),
data_shape,
plain_axes_order,
dispersed_shape,
elem_size);
ov::reference::reshape(flat_data,
dispersed_data.data(),
data_shape,
plain_axes_order,
dispersed_shape,
elem_size);
size_t val = 1;
for (size_t axis_idx = 0; axis_idx <= block_values_size; ++axis_idx) {
@ -130,21 +130,21 @@ bool batch_to_space_evaluate(const HostTensorVector& outputs, const HostTensorVe
post_transpose_shape[axis_idx] = dispersed_shape[axes_order[axis_idx]];
}
runtime::opt_kernel::reshape(dispersed_data.data(),
post_transpose_data.data(),
dispersed_shape,
axes_order,
post_transpose_shape,
elem_size);
ov::reference::reshape(dispersed_data.data(),
post_transpose_data.data(),
dispersed_shape,
axes_order,
post_transpose_shape,
elem_size);
squeezed_shape[0] = dispersed_shape[1];
squeezed_shape[block_idx] *= block_values[block_idx];
dispersed_shape[block_idx + 1] = squeezed_shape[block_idx];
runtime::opt_kernel::reshape(post_transpose_data.data(),
flat_data,
post_transpose_shape,
plain_axes_order,
squeezed_shape,
elem_size);
ov::reference::reshape(post_transpose_data.data(),
flat_data,
post_transpose_shape,
plain_axes_order,
squeezed_shape,
elem_size);
data_shape = squeezed_shape;
}

View File

@ -10,7 +10,6 @@
#include "bound_evaluate.hpp"
#include "compare.hpp"
#include "itt.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "ngraph/util.hpp"
#include "openvino/core/dimension_tracker.hpp"
#include "openvino/core/validation_util.hpp"
@ -177,15 +176,10 @@ bool op::v1::Reshape::evaluate_reshape(ov::TensorVector& outputs, const ov::Tens
OPENVINO_ASSERT(ov::PartialShape(output_shape).is_static());
outputs[0].set_shape(ov::PartialShape(output_shape).to_shape());
OPENVINO_SUPPRESS_DEPRECATED_START
const AxisVector order = ngraph::get_default_order(inputs[0].get_shape());
OPENVINO_SUPPRESS_DEPRECATED_END
ngraph::runtime::opt_kernel::reshape(static_cast<char*>(inputs[0].data()),
static_cast<char*>(outputs[0].data()),
inputs[0].get_shape(),
order,
outputs[0].get_shape(),
inputs[0].get_element_type().size());
ov::reference::reshape(static_cast<char*>(inputs[0].data()),
static_cast<char*>(outputs[0].data()),
inputs[0].get_shape(),
inputs[0].get_element_type().size());
return true;
}

View File

@ -11,7 +11,6 @@
#include "ngraph/attribute_visitor.hpp"
#include "ngraph/builder/reshape.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "ngraph/type/element_type.hpp"
#include "ngraph/type/element_type_traits.hpp"
#include "openvino/core/validation_util.hpp"

View File

@ -15,10 +15,10 @@
#include "ngraph/builder/make_constant.hpp"
#include "ngraph/node.hpp"
#include "ngraph/ops.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "ngraph/shape.hpp"
#include "openvino/op/util/precision_sensitive_attribute.hpp"
#include "openvino/reference/pad.hpp"
#include "openvino/reference/reshape.hpp"
using namespace std;
using namespace ngraph;
@ -169,32 +169,32 @@ bool ngraph::op::v1::SpaceToBatch::evaluate_space_to_batch(const HostTensorVecto
}
}
ngraph::runtime::opt_kernel::reshape(flat_data.data(),
dispersed_data.data(),
data_shape,
plain_axes_order,
dispersed_shape,
elem_size);
ov::reference::reshape(flat_data.data(),
dispersed_data.data(),
data_shape,
plain_axes_order,
dispersed_shape,
elem_size);
ov::Shape post_transpose_shape(axes_order.size());
for (size_t i = 0; i < axes_order.size(); ++i) {
post_transpose_shape[i] = dispersed_shape[axes_order[i]];
}
ngraph::runtime::opt_kernel::reshape(dispersed_data.data(),
post_transpose_data.data(),
dispersed_shape,
axes_order,
post_transpose_shape,
elem_size);
ov::reference::reshape(dispersed_data.data(),
post_transpose_data.data(),
dispersed_shape,
axes_order,
post_transpose_shape,
elem_size);
squeezed_shape[0] *= block_values[block_idx];
squeezed_shape[block_idx] /= block_values[block_idx];
ngraph::runtime::opt_kernel::reshape(post_transpose_data.data(),
flat_data.data(),
post_transpose_shape,
plain_axes_order,
squeezed_shape,
elem_size);
ov::reference::reshape(post_transpose_data.data(),
flat_data.data(),
post_transpose_shape,
plain_axes_order,
squeezed_shape,
elem_size);
data_shape = squeezed_shape;
}

View File

@ -8,8 +8,8 @@
#include <vector>
#include "common_test_utils/ndarray.hpp"
#include "ngraph/runtime/opt_kernel/reshape.hpp"
#include "openvino/core/axis_vector.hpp"
#include "openvino/reference/reshape.hpp"
using namespace ov;
@ -50,12 +50,12 @@ TEST_P(ReshapeOptKernel, reshape_opt_kernel) {
for (size_t i = 0; i < out_shape.size(); i++)
out_shape[i] = in_shape[axis_order[i]];
ngraph::runtime::opt_kernel::reshape((const char*)p.input.data(),
(char*)output_buff.data(),
in_shape,
axis_order,
out_shape,
sizeof(ElementValue));
ov::reference::reshape(static_cast<const char*>(p.input.data()),
reinterpret_cast<char*>(output_buff.data()),
in_shape,
axis_order,
out_shape,
sizeof(ElementValue));
EXPECT_EQ(p.output.get_vector(), output_buff);
}