Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / ResizeBilinear.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "Builders.h"
19 #include "kernels/Utils.h"
20
21 #include "kernels/BinaryOpCommon.h"
22
23 #include "PALResizeBilinear.h"
24
25 namespace luci_interpreter
26 {
27
28 /*
29  * ResizeBilinear Kernel:
30  * Description: resizing input Tensor by input constants using Bilinear Interpolation
31  * 2 Inputs: Input tensor ( Shape dimensions count = 4); Input constant (Shape dimensions count = 1,
32  * Num elements =2) Parameters: align_corners; half_pixel_centers;
33  *
34  * Example:
35  *                       Input(2, 2, 2, 1)
36  *                               |
37  *                               |   Constant Input(2) [3,3] INT32
38  *                               |  /
39  *                          ResizeBilinear
40  *                                 |
41  *                         Output(2, 3, 3, 1) UINT8
42  */
43
44 void configure_kernel_CircleResizeBilinear(const circle::Operator *cur_op,
45                                            BaseRuntimeGraph *runtime_graph)
46 {
47   // Check of the size of input. Should be 2
48   assert(cur_op->inputs()->size() == 2);
49   const auto input_index = cur_op->inputs()->operator[](0);
50   const auto size_index = cur_op->inputs()->operator[](1);
51   const auto output_index = cur_op->outputs()->operator[](0);
52
53   assert(input_index != -1);
54   assert(size_index != -1);
55   assert(output_index != -1);
56   // Get tensors
57   const auto input = runtime_graph->getCircleTensorByIndex(input_index);
58   const auto size = runtime_graph->getCircleTensorByIndex(size_index);
59   const auto output = runtime_graph->getCircleTensorByIndex(output_index);
60   // Check of the Input shape
61   assert(kernels::getTensorShape(input).dimensionsCount() == 4);
62   // Check of the Const input size shape
63   assert(kernels::getTensorShape(size).dimensionsCount() == 1);
64   assert(Tensor::element_type(size) == DataType::S32);
65   assert(kernels::getTensorShape(size).dims(0) == 2);
66
67   const auto *params = cur_op->builtin_options_as_ResizeBilinearOptions();
68   if (params->half_pixel_centers() && params->align_corners())
69     assert(false && "If half_pixel_centers is True, align_corners must be False.");
70 }
71
72 void execute_kernel_CircleResizeBilinear(const circle::Operator *cur_op,
73                                          BaseRuntimeGraph *runtime_graph)
74 {
75   assert(cur_op->inputs()->size() == 2);
76   const auto input_index = cur_op->inputs()->operator[](0);
77   const auto size_index = cur_op->inputs()->operator[](1);
78   const auto output_index = cur_op->outputs()->operator[](0);
79
80   assert(input_index != -1);
81   assert(size_index != -1);
82   assert(output_index != -1);
83
84   const auto input = runtime_graph->getCircleTensorByIndex(input_index);
85   const auto size = runtime_graph->getCircleTensorByIndex(size_index);
86   const auto output = runtime_graph->getCircleTensorByIndex(output_index);
87
88   const uint8_t *input_data = runtime_graph->getDataByTensor(input);
89   const uint8_t *size_data = runtime_graph->getConstDataByTensor(size);
90   uint8_t *output_data = runtime_graph->getDataByTensor(output);
91
92   assert(input_data != nullptr);
93   assert(size_data != nullptr);
94   assert(output_data != nullptr);
95
96   // Get parameters
97   const auto *op_params = cur_op->builtin_options_as_ResizeBilinearOptions();
98
99   switch (Tensor::element_type(output))
100   {
101     case DataType::FLOAT32:
102       luci_interpreter_pal::ResizeBilinear(
103         op_params, kernels::getTensorShape(input), kernels::getTensorData<float>(input_data),
104         kernels::getTensorShape(size), kernels::getTensorData<int32_t>(size_data),
105         kernels::getTensorShape(output), kernels::getTensorData<float>(output_data));
106       break;
107     case DataType::U8:
108       luci_interpreter_pal::ResizeBilinear(
109         op_params, kernels::getTensorShape(input), kernels::getTensorData<uint8_t>(input_data),
110         kernels::getTensorShape(size), kernels::getTensorData<int32_t>(size_data),
111         kernels::getTensorShape(output), kernels::getTensorData<uint8_t>(output_data));
112       break;
113     default:
114       assert(false && "Unsupported type.");
115   }
116 }
117
118 } // namespace luci_interpreter