Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / ResizeBilinearLayer.cc
1 /*
2  * Copyright (c) 2018 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 #include "OperationUtils.h"
17 #include "ResizeBilinearLayer.h"
18 #include "cker/operation/ResizeBilinear.h"
19 #include <cker/Types.h>
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27 namespace ops
28 {
29
30 ResizeBilinearLayer::ResizeBilinearLayer()
31   : _input(nullptr), _output(nullptr), _size(nullptr), _output_height(0), _output_width(0),
32     _align_corners(false), _half_pixel_centers(false)
33 {
34   // DO NOTHING
35 }
36
37 void ResizeBilinearLayer::configure(const IPortableTensor *input, IPortableTensor *output,
38                                     const IPortableTensor *size, bool align_corners,
39                                     bool half_pixel_centers)
40 {
41   assert(!size->is_constant());
42   _input = input;
43   _output = output;
44   _size = size;
45   _align_corners = align_corners;
46   _half_pixel_centers = half_pixel_centers;
47 }
48
49 void ResizeBilinearLayer::configure(const IPortableTensor *input, IPortableTensor *output,
50                                     int32_t output_height, int32_t output_width, bool align_corners,
51                                     bool half_pixel_centers)
52 {
53   assert(_size == nullptr);
54   if (output_height < 0)
55   {
56     throw std::runtime_error{"ResizeBilinear: size value must be positive value, output_height = " +
57                              std::to_string(output_height)};
58   }
59   if (output_width < 0)
60   {
61     throw std::runtime_error{"ResizeBilinear: size value must be positive value, output_width = " +
62                              std::to_string(output_width)};
63   }
64   _input = input;
65   _output = output;
66   _output_height = output_height;
67   _output_width = output_width;
68   _align_corners = align_corners;
69   _half_pixel_centers = half_pixel_centers;
70 }
71
72 void ResizeBilinearLayer::run()
73 {
74   nnfw::cker::ResizeBilinearParams params;
75   if (_size == nullptr)
76   {
77     params.output_height = _output_height;
78     params.output_width = _output_width;
79   }
80   else
81   {
82     const auto size_buf = getBuffer<int32_t>(_size);
83     params.output_height = size_buf[0];
84     params.output_width = size_buf[1];
85   }
86   params.align_corners = _align_corners;
87   params.half_pixel_centers = _half_pixel_centers;
88
89   switch (_input->data_type())
90   {
91     case OperandType::FLOAT32:
92       nnfw::cker::ResizeBilinear(params, getShape(_input), getBuffer<float>(_input),
93                                  getShape(_output), getBuffer<float>(_output));
94       break;
95
96     case OperandType::QUANT_UINT8_ASYMM:
97       nnfw::cker::ResizeBilinear(params, getShape(_input), getBuffer<uint8_t>(_input),
98                                  getShape(_output), getBuffer<uint8_t>(_output));
99       break;
100
101     case OperandType::QUANT_INT8_ASYMM:
102       nnfw::cker::ResizeBilinear(params, getShape(_input), getBuffer<int8_t>(_input),
103                                  getShape(_output), getBuffer<int8_t>(_output));
104       break;
105
106     case OperandType::UINT8:
107     case OperandType::BOOL8:
108     case OperandType::FLOAT16:
109     case OperandType::INT32:
110     case OperandType::INT64:
111     case OperandType::QUANT_INT8_SYMM:
112       std::runtime_error("ResizeBilinear NYI");
113       break;
114     default:
115       std::runtime_error("ResizeBilinear unsupported data type");
116   }
117 }
118
119 } // namespace ops
120 } // namespace cpu
121 } // namespace backend
122 } // namespace onert