Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / interp / operations / Pad.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cker/operation/Pad.h>
18
19 #include "OperationUtil.h"
20
21 #include "interp/Registration.h"
22 #include "ir/operation/Pad.h"
23
24 namespace onert
25 {
26 namespace interp
27 {
28 namespace
29 {
30
31 void preparePad(ExecEnv *env, const ir::Operation &node)
32 {
33   const auto input_index = node.getInputs().at(ir::operation::Pad::INPUT);
34   const auto output_index = node.getOutputs().at(0);
35
36   const auto input_tensor = env->tensorAt(input_index);
37
38   const auto output_info = env->graph().operands().at(output_index).info();
39
40   // Check shape and type lhs is same with rhs
41   // TODO Util function to compare TensorInfo
42   if (output_info.total_size() == 0)
43   {
44     throw std::runtime_error{"Interp(Pad): NYI unspecified output shape"};
45   }
46   else
47   {
48     env->allocateIfNeeded(output_index, output_info);
49   }
50
51   const auto output_tensor = env->tensorAt(output_index);
52   if (input_tensor->data_type() != output_tensor->data_type())
53   {
54     throw std::runtime_error{"Interp(Pad): Invalid output type"};
55   }
56 }
57
58 void invoke(const ITensor *input_tensor, const ITensor *pad_tensor, const ITensor *output_tensor)
59 {
60   const auto input_buffer = input_tensor->bufferRO();
61   const auto pad_buffer = pad_tensor->bufferRO();
62   auto output_buffer = output_tensor->buffer();
63
64   int32_t pad_rank = pad_tensor->dimension(0);
65
66   const auto cker_input_shape = convertShape(input_tensor->tensorInfo().shape());
67   const auto cker_output_shape = convertShape(output_tensor->tensorInfo().shape());
68   const float *input_ptr = reinterpret_cast<const float *>(input_buffer);
69   const int32_t *pad_ptr = reinterpret_cast<const int32_t *>(pad_buffer);
70   float *output_ptr = reinterpret_cast<float *>(output_buffer);
71
72   nnfw::cker::Pad<float>(pad_ptr, pad_rank, cker_input_shape, input_ptr, cker_output_shape,
73                          output_ptr, nullptr);
74 }
75
76 void invokePad(const ExecEnv *env, const ir::Operation &node)
77 {
78   const auto input_index = node.getInputs().at(ir::operation::Pad::INPUT);
79   const auto pad_index = node.getInputs().at(ir::operation::Pad::PAD);
80   const auto output_index = node.getOutputs().at(0);
81
82   const auto input_tensor = env->tensorAt(input_index);
83   const auto pad_tensor = env->tensorAt(pad_index);
84   const auto output_tensor = env->tensorAt(output_index);
85
86   const auto data_type = input_tensor->data_type();
87
88   if (data_type == ir::DataType::FLOAT32)
89   {
90     invoke(input_tensor, pad_tensor, output_tensor);
91   }
92   else
93   {
94     throw std::runtime_error{"Interp(Pad): NYI - Unsupported data type"};
95   }
96 }
97 } // namespace
98
99 OpKernel *getPad()
100 {
101   static OpKernel kernel = {preparePad, invokePad};
102   return &kernel;
103 }
104
105 } // namespace interp
106 } // namespace onert