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