ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Pad.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 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 "kernels/Pad.h"
18
19#include "kernels/Utils.h"
20
21#include <tensorflow/lite/kernels/internal/reference/pad.h>
22
23#include <limits>
24
25namespace luci_interpreter
26{
27namespace kernels
28{
29
30Pad::Pad(const Tensor *input, const Tensor *paddings, Tensor *output)
31 : Kernel({input, paddings}, {output})
32{
33}
34
36{
37 const Shape &input_shape = input()->shape();
38 const int num_dims = input_shape.num_dims();
39
40 if (num_dims > 4)
41 throw std::runtime_error("Unsupported number of dimensions.");
42
43 assert(output()->element_type() == input()->element_type());
44 assert(paddings()->element_type() == DataType::S32);
45 // Paddings shape should be [N, 2].
46 assert(paddings()->shape().num_dims() == 2);
47 assert(paddings()->shape().dim(0) == num_dims);
48 assert(paddings()->shape().dim(1) == 2);
49
50 Shape output_shape(num_dims);
51 const auto *paddings_data = getTensorData<int32_t>(paddings());
52 for (int i = 0; i < num_dims; ++i)
53 {
54 const int32_t padding_before = paddings_data[i * 2];
55 const int32_t padding_after = paddings_data[i * 2 + 1];
56 assert(padding_before >= 0 && padding_after >= 0);
57 output_shape.dim(i) = input_shape.dim(i) + padding_before + padding_after;
58 }
59
61}
62
63void Pad::execute() const
64{
65 const int num_dims = input()->shape().num_dims();
66
67 tflite::PadParams params{};
68 params.left_padding_count = num_dims;
69 params.right_padding_count = num_dims;
70
71 const auto *paddings_data = getTensorData<int32_t>(paddings());
72 for (int i = num_dims - 1; i >= 0; --i)
73 {
74 params.left_padding[i] = paddings_data[i * 2];
75 params.right_padding[i] = paddings_data[i * 2 + 1];
76 }
77
78 switch (input()->element_type())
79 {
80 case DataType::FLOAT32:
81 {
82 const float pad_value = 0.0f;
83 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<float>(input()),
84 &pad_value, getTensorShape(output()),
85 getTensorData<float>(output()));
86 break;
87 }
88 case DataType::U8:
89 {
90 assert(output()->zero_point() >= std::numeric_limits<uint8_t>::min());
91 assert(output()->zero_point() <= std::numeric_limits<uint8_t>::max());
92 const auto pad_value = static_cast<uint8_t>(output()->zero_point());
93 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
94 &pad_value, getTensorShape(output()),
95 getTensorData<uint8_t>(output()));
96 break;
97 }
98 case DataType::S8:
99 {
100 assert(output()->zero_point() >= std::numeric_limits<int8_t>::min());
101 assert(output()->zero_point() <= std::numeric_limits<int8_t>::max());
102 const auto pad_value = static_cast<int8_t>(output()->zero_point());
103 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<int8_t>(input()),
104 &pad_value, getTensorShape(output()),
105 getTensorData<int8_t>(output()));
106 break;
107 }
108 default:
109 throw std::runtime_error("luci-intp Pad Unsupported type.");
110 }
111}
112
113} // namespace kernels
114} // namespace luci_interpreter
int32_t dim(int i) const
Definition Tensor.h:41
int num_dims() const
Definition Tensor.h:39
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
int32_t zero_point() const
Definition Tensor.h:115
void execute() const override
Definition Pad.cpp:63
const Tensor * paddings() const
Definition Pad.h:33
void configure() override
Definition Pad.cpp:35
const Tensor * input() const
Definition Pad.h:32
Pad(const Tensor *input, const Tensor *paddings, Tensor *output)
Definition Pad.cpp:30
Tensor * output() const
Definition Pad.h:34
const luci_interpreter::RuntimeShape output_shape
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194