20#include "kernels/Utils.h"
38 const int num_dims = input_shape.
num_dims();
41 throw std::runtime_error(
"Unsupported number of dimensions.");
51 const auto *paddings_data = getTensorData<int32_t>(
paddings());
52 for (
int i = 0; i < num_dims; ++i)
54 const int32_t padding_before = paddings_data[i * 2];
55 const int32_t padding_after = paddings_data[i * 2 + 1];
57 output_shape.dim(i) = input_shape.
dim(i) + padding_before + padding_after;
68inline void getPadding(
const T *
data,
int offset, int64_t *left_pad, int64_t *right_pad)
70 *left_pad =
static_cast<int64_t
>(*(
data +
offset * 2));
71 *right_pad =
static_cast<int64_t
>(*(
data +
offset * 2 + 1));
76inline int getInputDimension(
int padded_dimension,
int left_pad,
int right_pad,
int input_dim_size,
81 if (padded_dimension < left_pad)
83 const int original_ind = left_pad +
offset - 1;
84 return original_ind - (std::min(padded_dimension, original_ind -
offset));
86 padded_dimension -= left_pad;
87 if (padded_dimension >= input_dim_size)
89 padded_dimension -= input_dim_size;
90 const int original_ind = input_dim_size - (1 +
offset);
91 return original_ind - std::min(padded_dimension, original_ind);
93 return padded_dimension;
98int getFlatIndex(
int index,
int num_dims,
const DataType padding_matrix_type,
99 const uint8_t *padding_matrix_data,
const int32_t *input_dims,
100 int *output_dims_num_elements,
int *input_dims_num_elements,
const int offset)
103 int64_t left_pad = 0, right_pad = 0, dimension_index, index_in_input;
105 for (
int i = 0; i < num_dims; ++i)
107 switch (padding_matrix_type)
110 getPadding(
reinterpret_cast<const int32_t *
>(padding_matrix_data), i, &left_pad,
114 getPadding(
reinterpret_cast<const int64_t *
>(padding_matrix_data), i, &left_pad,
120 dimension_index =
index / output_dims_num_elements[i];
122 index_in_input = getInputDimension(dimension_index, left_pad, right_pad, input_dims[i],
offset);
124 flat_index += index_in_input * input_dims_num_elements[i];
125 index %= output_dims_num_elements[i];
132void eval(
const DataType padding_matrix_type,
const uint8_t *padding_matrix_data,
133 const int32_t *input_dims,
int *output_dims_num_elements,
int *input_dims_num_elements,
134 const T *input_data, T *output_data,
const int offset,
const int num_dims,
135 const int output_size)
137 for (
int i = 0; i < output_size; ++i)
139 int index = getFlatIndex(i, num_dims, padding_matrix_type, padding_matrix_data, input_dims,
140 output_dims_num_elements, input_dims_num_elements,
offset);
157 int output_dims_num_elements[5];
158 int input_dims_num_elements[5];
159 int32_t input_shape_dim[5];
161 for (
int i = 0; i < input_dims; i++)
163 output_dims_num_elements[i] = 1;
164 input_dims_num_elements[i] = 1;
165 input_shape_dim[i] = t_input.
shape().
dim(i);
168 for (
int i = input_dims - 2; i >= 0; i--)
170 output_dims_num_elements[i] = output_dims_num_elements[i + 1] * t_output.
shape().
dim(i + 1);
171 input_dims_num_elements[i] = input_dims_num_elements[i + 1] * t_input.
shape().
dim(i + 1);
176 case DataType::FLOAT32:
177 eval(t_paddings.
element_type(), t_paddings.
data<uint8_t>(), input_shape_dim,
178 output_dims_num_elements, input_dims_num_elements, t_input.
data<
float>(),
179 t_output.
data<
float>(),
offset, input_dims, output_size);
183 eval(t_paddings.
element_type(), t_paddings.
data<uint8_t>(), input_shape_dim,
184 output_dims_num_elements, input_dims_num_elements, t_input.
data<uint8_t>(),
185 t_output.
data<uint8_t>(),
offset, input_dims, output_size);
189 throw std::runtime_error(
"luci-intp MirrorPad Unsupported type.");
const MirrorPadParams & params() const
int32_t num_elements() const
void resize(const Shape &new_shape)
const Shape & shape() const
DataType element_type() const
void execute() const override
MirrorPad(const Tensor *input, const Tensor *paddings, Tensor *output, const MirrorPadParams ¶ms)
const Tensor * input() const
void configure() override
const Tensor * paddings() const
#define LUCI_INTERPRETER_CHECK(cond)
__global uchar * offset(const Image *img, int x, int y)
const luci_interpreter::RuntimeShape output_shape
const T * data(const std::vector< T, Alloc > &v)
DataType
"scalar" value type
loco::GraphInputIndex index(const TFPlaceholder *node)