Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / ResizeNearestNeighbor.cpp
1 /*
2  * Copyright (c) 2020 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 "kernels/ResizeNearestNeighbor.h"
19
20 #include "kernels/Utils.h"
21
22 #include <tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h>
23 #include "PALResizeNearestNeighbor.h"
24
25 namespace luci_interpreter
26 {
27 namespace kernels
28 {
29
30 ResizeNearestNeighbor::ResizeNearestNeighbor(const Tensor *input, const Tensor *size,
31                                              Tensor *output,
32                                              const ResizeNearestNeighborParams &params)
33   : KernelWithParams<ResizeNearestNeighborParams>({input, size}, {output}, params)
34 {
35 }
36
37 void ResizeNearestNeighbor::configure()
38 {
39   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4);
40   LUCI_INTERPRETER_CHECK(size()->shape().num_dims() == 1);
41   LUCI_INTERPRETER_CHECK(size()->element_type() == DataType::S32);
42   LUCI_INTERPRETER_CHECK(size()->shape().dim(0) == 2);
43   Shape output_shape(4);
44   output_shape.dim(0) = input()->shape().dim(0);
45   output_shape.dim(1) = getTensorData<int32_t>(size())[0];
46   output_shape.dim(2) = getTensorData<int32_t>(size())[1];
47   output_shape.dim(3) = input()->shape().dim(3);
48   output()->resize(output_shape);
49 }
50
51 void ResizeNearestNeighbor::execute() const
52 {
53   tflite::ResizeNearestNeighborParams op_params{};
54   op_params.align_corners = params().align_corners;
55   op_params.half_pixel_centers = params().half_pixel_centers;
56   switch (output()->element_type())
57   {
58     case DataType::FLOAT32:
59       tflite::reference_ops::ResizeNearestNeighbor(
60         op_params, getTensorShape(input()), getTensorData<int32_t>(input()), getTensorShape(size()),
61         getTensorData<int32_t>(size()), getTensorShape(output()), getTensorData<int32_t>(output()));
62       break;
63     case DataType::U8:
64       luci_interpreter_pal::ResizeNearestNeighbor(
65         op_params, getTensorShape(input()), getTensorData<uint8_t>(input()), getTensorShape(size()),
66         getTensorData<int32_t>(size()), getTensorShape(output()), getTensorData<uint8_t>(output()));
67       break;
68     default:
69       throw std::runtime_error("Unsupported type.");
70   }
71 }
72
73 } // namespace kernels
74 } // namespace luci_interpreter