2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <cker/operation/Pad.h>
19 #include "OperationUtil.h"
21 #include "interp/Registration.h"
22 #include "ir/operation/Pad.h"
31 void preparePad(ExecEnv *env, const ir::Operation &node)
33 const auto input_index = node.getInputs().at(ir::operation::Pad::INPUT);
34 const auto output_index = node.getOutputs().at(0);
36 const auto input_tensor = env->tensorAt(input_index);
38 const auto output_info = env->graph().operands().at(output_index).info();
40 // Check shape and type lhs is same with rhs
41 // TODO Util function to compare TensorInfo
42 if (output_info.total_size() == 0)
44 throw std::runtime_error{"Interp(Pad): NYI unspecified output shape"};
48 env->allocateIfNeeded(output_index, output_info);
51 const auto output_tensor = env->tensorAt(output_index);
52 if (input_tensor->data_type() != output_tensor->data_type())
54 throw std::runtime_error{"Interp(Pad): Invalid output type"};
58 void invoke(const ITensor *input_tensor, const ITensor *pad_tensor, const ITensor *output_tensor)
60 const auto input_buffer = input_tensor->bufferRO();
61 const auto pad_buffer = pad_tensor->bufferRO();
62 auto output_buffer = output_tensor->buffer();
64 int32_t pad_rank = pad_tensor->dimension(0);
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);
72 nnfw::cker::Pad<float>(pad_ptr, pad_rank, cker_input_shape, input_ptr, cker_output_shape,
76 void invokePad(const ExecEnv *env, const ir::Operation &node)
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);
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);
86 const auto data_type = input_tensor->data_type();
88 if (data_type == ir::DataType::FLOAT32)
90 invoke(input_tensor, pad_tensor, output_tensor);
94 throw std::runtime_error{"Interp(Pad): NYI - Unsupported data type"};
101 static OpKernel kernel = {preparePad, invokePad};
105 } // namespace interp