Skip to content

Commit

Permalink
[onert-micro] Reduce duplicate code in PadCommon.cpp (Samsung#12484)
Browse files Browse the repository at this point in the history
This commit reduces duplicate code in PadCommon.cpp.

ONE-DCO-1.0-Signed-off-by: ragmani <[email protected]>
  • Loading branch information
ragmani authored Jan 25, 2024
1 parent c101827 commit 158647a
Showing 1 changed file with 95 additions and 68 deletions.
163 changes: 95 additions & 68 deletions onert-micro/luci-interpreter/src/kernels/PadCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,92 +22,119 @@

namespace luci_interpreter
{
namespace
{
class PadKernel
{
public:
PadKernel() = delete;

explicit PadKernel(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
: _runtime_graph(runtime_graph)
{
const auto num_inputs = cur_op->inputs()->size();

const auto input1_index = cur_op->inputs()->operator[](0);
const auto input2_index = cur_op->inputs()->operator[](1);
const auto input3_index = num_inputs == 3 ? cur_op->inputs()->operator[](2) : -1;
const auto output_index = cur_op->outputs()->operator[](0);

assert(input1_index != -1);
assert(input2_index != -1);
assert(input3_index != -1 or num_inputs == 2);
assert(output_index != -1);

_input1_tensor = _runtime_graph->getCircleTensorByIndex(input1_index);
_input2_tensor = _runtime_graph->getCircleTensorByIndex(input2_index);
_input3_tensor =
num_inputs == 3 ? runtime_graph->getCircleTensorByIndex(input3_index) : nullptr;
_output_tensor = _runtime_graph->getCircleTensorByIndex(output_index);

assert(_input1_tensor != nullptr);
assert(_input2_tensor != nullptr);
assert(_input3_tensor != nullptr or num_inputs == 2);
assert(_output_tensor != nullptr);
}

luci_interpreter_pal::PadParams getPadParams()
{
luci_interpreter_pal::PadParams pad_params;
const int num_input_dimensions = Tensor::num_dims(_input1_tensor);
pad_params.left_padding_count = num_input_dimensions;
pad_params.right_padding_count = num_input_dimensions;

const int32_t *paddings_data =
kernels::getTensorData<int32_t>(_runtime_graph->getConstDataByTensor(_input2_tensor));
for (int idx = num_input_dimensions - 1; idx >= 0; --idx)
{
pad_params.left_padding[idx] = paddings_data[idx * 2];
pad_params.right_padding[idx] = paddings_data[idx * 2 + 1];
}

return pad_params;
}

const circle::Tensor *input1() const { return _input1_tensor; }
const circle::Tensor *input2() const { return _input2_tensor; }
const circle::Tensor *input3() const { return _input3_tensor; }
const circle::Tensor *output() const { return _output_tensor; }

BaseRuntimeGraph *runtime_graph() const { return _runtime_graph; }

private:
const circle::Tensor *_input1_tensor;
const circle::Tensor *_input2_tensor;
const circle::Tensor *_input3_tensor;
const circle::Tensor *_output_tensor;

BaseRuntimeGraph *_runtime_graph;
};
} // namespace

void configure_kernel_CirclePadCommon(const circle::Operator *cur_op,
BaseRuntimeGraph *runtime_graph)
{
const auto num_inputs = cur_op->inputs()->size();

const auto input1_index = cur_op->inputs()->operator[](0);
const auto input2_index = cur_op->inputs()->operator[](1);
const auto input3_index = num_inputs == 3 ? cur_op->inputs()->operator[](2) : -1;
const auto output_index = cur_op->outputs()->operator[](0);

assert(input1_index != -1);
assert(input2_index != -1);
assert(input3_index != -1 or num_inputs == 2);
assert(output_index != -1);

const auto input1_tensor = runtime_graph->getCircleTensorByIndex(input1_index);
const auto input2_tensor = runtime_graph->getCircleTensorByIndex(input2_index);
const auto input3_tensor =
num_inputs == 3 ? runtime_graph->getCircleTensorByIndex(input3_index) : nullptr;
const auto output_tensor = runtime_graph->getCircleTensorByIndex(output_index);

assert(input1_tensor != nullptr);
assert(input2_tensor != nullptr);
assert(input3_tensor != nullptr or num_inputs == 2);
assert(output_tensor != nullptr);

LUCI_INTERPRETER_CHECK(Tensor::element_type(input2_tensor) == DataType::S32);
LUCI_INTERPRETER_CHECK(Tensor::element_type(input1_tensor) ==
Tensor::element_type(output_tensor));
if (input3_tensor != nullptr)
PadKernel kernel(cur_op, runtime_graph);

LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input2()) == DataType::S32);
LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
Tensor::element_type(kernel.output()));
if (kernel.input3() != nullptr)
PadKernel kernel(cur_op, runtime_graph);

LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input2()) == DataType::S32);
LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input1()) ==
Tensor::element_type(kernel.output()));
if (kernel.input3() != nullptr)
{
LUCI_INTERPRETER_CHECK(Tensor::element_type(input3_tensor) ==
Tensor::element_type(input1_tensor));
LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input3()) ==
Tensor::element_type(kernel.input1()));
// Value is scalar
LUCI_INTERPRETER_CHECK(Tensor::num_elements(input3_tensor) == 1);
LUCI_INTERPRETER_CHECK(Tensor::num_elements(kernel.input3()) == 1);
}

// Check shapes
const int32_t *paddings_data =
kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(input2_tensor));
for (int i = 0; i < Tensor::num_dims(output_tensor); i++)
kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(kernel.input2()));
for (int i = 0; i < Tensor::num_dims(kernel.output()); i++)
{
int output_dim = Tensor::dim(output_tensor, i);
int output_dim = Tensor::dim(kernel.output(), i);
int expected_dim =
Tensor::dim(input1_tensor, i) + paddings_data[i * 2] + paddings_data[i * 2 + 1];
Tensor::dim(kernel.input1(), i) + paddings_data[i * 2] + paddings_data[i * 2 + 1];
LUCI_INTERPRETER_CHECK(output_dim == expected_dim);
}
}

void execute_kernel_CirclePadCommon(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
{
const auto num_inputs = cur_op->inputs()->size();

const auto input1_index = cur_op->inputs()->operator[](0);
const auto input2_index = cur_op->inputs()->operator[](1);
const auto input3_index = num_inputs == 3 ? cur_op->inputs()->operator[](2) : -1;
const auto output_index = cur_op->outputs()->operator[](0);

assert(input1_index != -1);
assert(input2_index != -1);
assert(input3_index != -1 or num_inputs == 2);
assert(output_index != -1);

const auto input1_tensor = runtime_graph->getCircleTensorByIndex(input1_index);
const auto input2_tensor = runtime_graph->getCircleTensorByIndex(input2_index);
const auto input3_tensor =
num_inputs == 3 ? runtime_graph->getCircleTensorByIndex(input3_index) : nullptr;
const auto output_tensor = runtime_graph->getCircleTensorByIndex(output_index);

assert(input1_tensor != nullptr);
assert(input2_tensor != nullptr);
assert(input3_tensor != nullptr or num_inputs == 2);
assert(output_tensor != nullptr);

luci_interpreter_pal::PadParams pad_params;
const int num_input_dimensions = Tensor::num_dims(input1_tensor);
pad_params.left_padding_count = num_input_dimensions;
pad_params.right_padding_count = num_input_dimensions;
PadKernel kernel(cur_op, runtime_graph);

const int32_t *paddings_data =
kernels::getTensorData<int32_t>(runtime_graph->getConstDataByTensor(input2_tensor));
for (int idx = num_input_dimensions - 1; idx >= 0; --idx)
{
pad_params.left_padding[idx] = paddings_data[idx * 2];
pad_params.right_padding[idx] = paddings_data[idx * 2 + 1];
}
const auto input1_tensor = kernel.input1();
const auto input2_tensor = kernel.input2();
const auto input3_tensor = kernel.input3();
const auto output_tensor = kernel.output();

auto pad_params = kernel.getPadParams();

auto *input1_data = runtime_graph->getDataByTensor(input1_tensor);
if (input1_data == nullptr)
Expand Down

0 comments on commit 158647a

Please sign in to comment.