blob: 190b35432b9816f0542273d676f5724cfe05427f [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
James Ward8b390432022-08-12 20:48:56 +01002// Copyright (c) 2020-2022, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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 "image.h"
17#include "arith_util.h"
James Wardee256692022-11-15 11:36:47 +000018#include "half.hpp"
Eric Kunzee5e26762020-10-13 16:11:07 -070019
TatWai Chongf7326092022-06-08 12:17:14 -070020#include <type_traits>
21
Eric Kunzee5e26762020-10-13 16:11:07 -070022using namespace TosaReference;
23using namespace Eigen;
24using namespace tosa;
25
TatWai Chongf7326092022-06-08 12:17:14 -070026template <DType InDtype, DType OutDtype, typename resize_t>
27OpResize<InDtype, OutDtype, resize_t>::OpResize(SubgraphTraverser* sgt_,
28 TosaAttributeBase* attribute_,
29 uint64_t id_)
Kevin Chengacb550f2021-06-29 15:32:19 -070030 : GraphNode(sgt_, Op_RESIZE, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070031{
32 setRequiredOperands(1, 1);
33 setRequiredRank(4, 4);
34
35 INIT_ATTRIBUTE(Resize);
36}
37
TatWai Chongf7326092022-06-08 12:17:14 -070038template <DType InDtype, DType OutDtype, typename resize_t>
39OpResize<InDtype, OutDtype, resize_t>::~OpResize()
Eric Kunzee5e26762020-10-13 16:11:07 -070040{
41 if (attribute)
42 delete attribute;
43}
44
TatWai Chongf7326092022-06-08 12:17:14 -070045template <DType InDtype, DType OutDtype, typename resize_t>
46int OpResize<InDtype, OutDtype, resize_t>::checkTensorAttributes()
Eric Kunzee5e26762020-10-13 16:11:07 -070047{
48 if (validateRequiredOperands())
49 return 1;
50
51 if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]))
52 return 1;
53
TatWai Chongf7326092022-06-08 12:17:14 -070054 if (this->attribute->scale().size() != 4)
55 {
56 printNodeValidationError("OpResize: illegal size for attribute scale");
57 return 1;
58 }
Eric Kunzee5e26762020-10-13 16:11:07 -070059
TatWai Chongf7326092022-06-08 12:17:14 -070060 scale = this->attribute->scale();
61 offset = this->attribute->offset();
62 border = this->attribute->border();
63 mode = this->attribute->mode();
Eric Kunzee5e26762020-10-13 16:11:07 -070064
65 if (this->mode == ResizeMode_BILINEAR)
66 {
James Ward24dbc422022-10-19 12:20:31 +010067 if (OutDtype != DType_INT32 && OutDtype != DType_INT48 && OutDtype != DType_FP32 && OutDtype != DType_FP16 && OutDtype != DType_BF16)
Eric Kunzee5e26762020-10-13 16:11:07 -070068 {
69 printNodeValidationError("OpResize: invalid data type for BILINEAR");
70 return 1;
71 }
72 }
73 else
74 {
James Ward24dbc422022-10-19 12:20:31 +010075 if (OutDtype != DType_INT8 && OutDtype != DType_INT16 && OutDtype != DType_FP32 && OutDtype != DType_FP16 && OutDtype != DType_BF16)
Eric Kunzee5e26762020-10-13 16:11:07 -070076 {
77 printNodeValidationError("OpResize: invalid data type for NEAREST");
78 return 1;
79 }
80 }
81
Eric Kunzee5e26762020-10-13 16:11:07 -070082 in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
83 out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
84
85 ASSERT_MEM(in && out);
86
87 return 0;
88}
89
TatWai Chongf7326092022-06-08 12:17:14 -070090template <DType InDtype, DType OutDtype, typename resize_t>
91int OpResize<InDtype, OutDtype, resize_t>::eval()
Eric Kunzee5e26762020-10-13 16:11:07 -070092{
93 int in_batch = in->getShape()[0];
94 int in_height = in->getShape()[1];
95 int in_width = in->getShape()[2];
96 int in_channels = in->getShape()[3];
97
98 int out_batch = out->getShape()[0];
99 int out_height = out->getShape()[1];
100 int out_width = out->getShape()[2];
101 int out_channels = out->getShape()[3];
102
TatWai Chongf7326092022-06-08 12:17:14 -0700103 int16_t scale_y_n = scale[0];
104 int16_t scale_y_d = scale[1];
105 int16_t scale_x_n = scale[2];
106 int16_t scale_x_d = scale[3];
107
108 int16_t offset_y = offset[0];
109 int16_t offset_x = offset[1];
110
111 int16_t border_y = border[0];
112 int16_t border_x = border[1];
113
Jerry Gea793f462023-04-11 00:05:02 +0000114 // Check Tosa Level
115 auto tosa_level = g_func_config.tosa_level;
116 LEVEL_CHECK(scale_y_n / scale_y_d <= tosa_level.MAX_SCALE, "scale_y_n / scale_y_d should be smaller than or equal to MAX_SCALE");
117 LEVEL_CHECK(scale_x_n / scale_x_d <= tosa_level.MAX_SCALE, "scale_x_n / scale_x_d should be smaller than or equal to MAX_SCALE");
118
Kevin Cheng1918e8a2021-09-15 10:17:39 -0700119 ERROR_IF(std::max<int>({ in_height, in_width, out_height, out_width }) >= 16384,
120 "OpResize: exceeds maximum dimension");
Kevin Chengacb550f2021-06-29 15:32:19 -0700121 ERROR_IF(in_batch != out_batch, "OpResize: output tensor batch mismatch");
122 ERROR_IF(in_channels != out_channels, "OpResize: output tensor channel mismatch");
TatWai Chongf7326092022-06-08 12:17:14 -0700123 ERROR_IF(scale_y_n <= 0 || scale_y_d <= 0 || scale_x_n <= 0 || scale_x_d <= 0,
124 "OpResize: attribute scale must not be negative");
125 // If data type is int8_t then ensure that an int32_t accumulator can be used.
126 ERROR_IF(scale_y_n > (1 << 11) || scale_x_n > (1 << 11), "OpResize: invalid attribute scale");
127 // Set a consistent lower limit of 1/16 downscale to simplify implementations
128 ERROR_IF((scale_y_d >= 16 * scale_y_n) || (scale_x_d >= 16 * scale_x_n), "OpResize: invalid attribute scale");
129 ERROR_IF((offset_y < -scale_y_n) || (offset_y >= 16 * scale_y_n),
130 "OpResize: invalid attribute offset height dimension");
131 ERROR_IF((offset_x < -scale_x_n) || (offset_x >= 16 * scale_x_n),
132 "OpResize: invalid attribute offset width dimension");
133 ERROR_IF((border_y < -16 * scale_y_n || border_y >= scale_y_n),
134 "OpResize: invalid attribute border height dimension");
135 ERROR_IF((border_x < -16 * scale_x_n || border_x >= scale_x_n),
136 "OpResize: invalid attribute border width dimension");
137
138 int32_t res_height = 0;
139 int32_t res_width = 0;
140
141 if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height))
142 return 1;
143
144 if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width))
145 return 1;
146
147 ERROR_IF(out_height != res_height + 1,
148 "OpResize: mismatch between output height dimension provided and expected shape");
149 ERROR_IF(out_width != res_width + 1,
150 "OpResize: mismatch between output width dimension provided and expected shape");
Eric Kunzee5e26762020-10-13 16:11:07 -0700151
152 for (int b = 0; b < out_batch; b++)
153 for (int c = 0; c < out_channels; c++)
154 for (int oy = 0; oy < out_height; oy++)
155 for (int ox = 0; ox < out_width; ox++)
156 {
TatWai Chongf7326092022-06-08 12:17:14 -0700157 int32_t y = oy * scale_y_d + offset_y;
158 int32_t x = ox * scale_x_d + offset_x;
Eric Kunzee5e26762020-10-13 16:11:07 -0700159
TatWai Chongf7326092022-06-08 12:17:14 -0700160 float fy = static_cast<float>(y) / static_cast<float>(scale_y_n);
161 float fx = static_cast<float>(x) / static_cast<float>(scale_x_n);
162
163 int32_t iy = floor(fy);
164 int32_t ix = floor(fx);
165
166 resize_t dy;
167 resize_t dx;
James Wardee256692022-11-15 11:36:47 +0000168 if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)) ||
169 (typeid(resize_t) == typeid(half_float::half)))
TatWai Chongf7326092022-06-08 12:17:14 -0700170 {
James Ward24dbc422022-10-19 12:20:31 +0100171 dy = (resize_t)(fy - iy);
172 dx = (resize_t)(fx - ix);
TatWai Chongf7326092022-06-08 12:17:14 -0700173 }
174 else
175 {
James Ward24dbc422022-10-19 12:20:31 +0100176 dy = (resize_t)(y - (iy * scale_y_n));
177 dx = (resize_t)(x - (ix * scale_x_n));
TatWai Chongf7326092022-06-08 12:17:14 -0700178 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700179
Kevin Cheng77d0f762020-11-24 10:26:32 -0800180 int32_t iy0 = MAX(iy, 0);
181 int32_t iy1 = MIN(iy + 1, in_height - 1);
182 int32_t ix0 = MAX(ix, 0);
183 int32_t ix1 = MIN(ix + 1, in_width - 1);
Eric Kunzee5e26762020-10-13 16:11:07 -0700184
Eric Kunzee5e26762020-10-13 16:11:07 -0700185 OutEigenType acc;
186 if (mode == ResizeMode_BILINEAR)
187 {
Kevin Cheng77d0f762020-11-24 10:26:32 -0800188 InEigenType v00 = in->getTensor()(b, iy0, ix0, c);
189 InEigenType v01 = in->getTensor()(b, iy0, ix1, c);
190 InEigenType v10 = in->getTensor()(b, iy1, ix0, c);
191 InEigenType v11 = in->getTensor()(b, iy1, ix1, c);
192
TatWai Chongf7326092022-06-08 12:17:14 -0700193 if (std::is_floating_point<resize_t>::value)
194 {
195 acc = (OutEigenType)v00 * (1.0 - dy) * (1.0 - dx);
196 acc += (OutEigenType)v01 * (1.0 - dy) * dx;
197 acc += (OutEigenType)v10 * dy * (1.0 - dx);
198 acc += (OutEigenType)v11 * dy * dx;
199 }
James Wardee256692022-11-15 11:36:47 +0000200 else if ((typeid(resize_t) == typeid(Eigen::bfloat16)) ||
201 (typeid(resize_t) == typeid(half_float::half)))
James Ward24dbc422022-10-19 12:20:31 +0100202 {
James Wardee256692022-11-15 11:36:47 +0000203 resize_t f16_acc;
204 f16_acc = (resize_t)v00 * (resize_t)(1.0 - dy) * (resize_t)(1.0 - dx);
205 f16_acc += (resize_t)v01 * (resize_t)(1.0 - dy) * (resize_t)dx;
206 f16_acc += (resize_t)v10 * (resize_t)dy * (resize_t)(1.0 - dx);
207 f16_acc += (resize_t)v11 * (resize_t)dy * (resize_t)dx;
208 acc = (float)f16_acc;
James Ward24dbc422022-10-19 12:20:31 +0100209 }
TatWai Chongf7326092022-06-08 12:17:14 -0700210 else
211 {
212 acc = (OutEigenType)v00 * (scale_y_n - dy) * (scale_x_n - dx);
213 acc += (OutEigenType)v01 * (scale_y_n - dy) * dx;
214 acc += (OutEigenType)v10 * dy * (scale_x_n - dx);
215 acc += (OutEigenType)v11 * dy * dx;
216 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700217 }
218 else
219 {
TatWai Chongf7326092022-06-08 12:17:14 -0700220 ASSERT_MSG(mode == ResizeMode_NEAREST, "OpResize: invalid mode");
James Wardee256692022-11-15 11:36:47 +0000221 if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)) ||
222 (typeid(resize_t) == typeid(half_float::half)))
TatWai Chongf7326092022-06-08 12:17:14 -0700223 {
224 iy = (dy >= 0.5) ? iy1 : iy0;
225 ix = (dx >= 0.5) ? ix1 : ix0;
226 }
227 else
228 {
229 iy = (2 * dy >= scale_y_n) ? iy1 : iy0;
230 ix = (2 * dx >= scale_x_n) ? ix1 : ix0;
231 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700232 acc = in->getTensor()(b, iy, ix, c);
233 }
James Ward24dbc422022-10-19 12:20:31 +0100234 if ((typeid(resize_t) == typeid(Eigen::bfloat16))) {
235 ASSERT_MSG(checkValidBFloat(acc), "Resize accumulator float value is not a valid bfloat16 value.");
236 }
Kevin Cheng77d0f762020-11-24 10:26:32 -0800237 out->getTensor()(b, oy, ox, c) = acc;
238 }
239
240 return GraphNode::eval();
241}
242
Eric Kunzee5e26762020-10-13 16:11:07 -0700243// template explicit instantiation
James Wardd34b3fc2023-01-18 14:51:25 +0000244DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT8, INT32, int16_t);
245DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT8, INT8, int16_t);
246DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT16, INT48, int16_t);
247DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT16, INT16, int16_t);
248DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, FP16, FP16, half_float::half);
249DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, BF16, BF16, Eigen::bfloat16);
250DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, FP32, FP32, float);