blob: 66efee084442c041b23f934078726509de8c1367 [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"
Eric Kunzee5e26762020-10-13 16:11:07 -070018
TatWai Chongf7326092022-06-08 12:17:14 -070019#include <type_traits>
20
Eric Kunzee5e26762020-10-13 16:11:07 -070021using namespace TosaReference;
22using namespace Eigen;
23using namespace tosa;
24
TatWai Chongf7326092022-06-08 12:17:14 -070025template <DType InDtype, DType OutDtype, typename resize_t>
26OpResize<InDtype, OutDtype, resize_t>::OpResize(SubgraphTraverser* sgt_,
27 TosaAttributeBase* attribute_,
28 uint64_t id_)
Kevin Chengacb550f2021-06-29 15:32:19 -070029 : GraphNode(sgt_, Op_RESIZE, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070030{
31 setRequiredOperands(1, 1);
32 setRequiredRank(4, 4);
33
34 INIT_ATTRIBUTE(Resize);
35}
36
TatWai Chongf7326092022-06-08 12:17:14 -070037template <DType InDtype, DType OutDtype, typename resize_t>
38OpResize<InDtype, OutDtype, resize_t>::~OpResize()
Eric Kunzee5e26762020-10-13 16:11:07 -070039{
40 if (attribute)
41 delete attribute;
42}
43
TatWai Chongf7326092022-06-08 12:17:14 -070044template <DType InDtype, DType OutDtype, typename resize_t>
45int OpResize<InDtype, OutDtype, resize_t>::checkTensorAttributes()
Eric Kunzee5e26762020-10-13 16:11:07 -070046{
47 if (validateRequiredOperands())
48 return 1;
49
50 if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]))
51 return 1;
52
TatWai Chongf7326092022-06-08 12:17:14 -070053 if (this->attribute->scale().size() != 4)
54 {
55 printNodeValidationError("OpResize: illegal size for attribute scale");
56 return 1;
57 }
Eric Kunzee5e26762020-10-13 16:11:07 -070058
TatWai Chongf7326092022-06-08 12:17:14 -070059 scale = this->attribute->scale();
60 offset = this->attribute->offset();
61 border = this->attribute->border();
62 mode = this->attribute->mode();
Eric Kunzee5e26762020-10-13 16:11:07 -070063
64 if (this->mode == ResizeMode_BILINEAR)
65 {
James Ward24dbc422022-10-19 12:20:31 +010066 if (OutDtype != DType_INT32 && OutDtype != DType_INT48 && OutDtype != DType_FP32 && OutDtype != DType_FP16 && OutDtype != DType_BF16)
Eric Kunzee5e26762020-10-13 16:11:07 -070067 {
68 printNodeValidationError("OpResize: invalid data type for BILINEAR");
69 return 1;
70 }
71 }
72 else
73 {
James Ward24dbc422022-10-19 12:20:31 +010074 if (OutDtype != DType_INT8 && OutDtype != DType_INT16 && OutDtype != DType_FP32 && OutDtype != DType_FP16 && OutDtype != DType_BF16)
Eric Kunzee5e26762020-10-13 16:11:07 -070075 {
76 printNodeValidationError("OpResize: invalid data type for NEAREST");
77 return 1;
78 }
79 }
80
Eric Kunzee5e26762020-10-13 16:11:07 -070081 in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
82 out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
83
84 ASSERT_MEM(in && out);
85
86 return 0;
87}
88
TatWai Chongf7326092022-06-08 12:17:14 -070089template <DType InDtype, DType OutDtype, typename resize_t>
90int OpResize<InDtype, OutDtype, resize_t>::eval()
Eric Kunzee5e26762020-10-13 16:11:07 -070091{
92 int in_batch = in->getShape()[0];
93 int in_height = in->getShape()[1];
94 int in_width = in->getShape()[2];
95 int in_channels = in->getShape()[3];
96
97 int out_batch = out->getShape()[0];
98 int out_height = out->getShape()[1];
99 int out_width = out->getShape()[2];
100 int out_channels = out->getShape()[3];
101
TatWai Chongf7326092022-06-08 12:17:14 -0700102 int16_t scale_y_n = scale[0];
103 int16_t scale_y_d = scale[1];
104 int16_t scale_x_n = scale[2];
105 int16_t scale_x_d = scale[3];
106
107 int16_t offset_y = offset[0];
108 int16_t offset_x = offset[1];
109
110 int16_t border_y = border[0];
111 int16_t border_x = border[1];
112
Kevin Cheng1918e8a2021-09-15 10:17:39 -0700113 ERROR_IF(std::max<int>({ in_height, in_width, out_height, out_width }) >= 16384,
114 "OpResize: exceeds maximum dimension");
Kevin Chengacb550f2021-06-29 15:32:19 -0700115 ERROR_IF(in_batch != out_batch, "OpResize: output tensor batch mismatch");
116 ERROR_IF(in_channels != out_channels, "OpResize: output tensor channel mismatch");
TatWai Chongf7326092022-06-08 12:17:14 -0700117 ERROR_IF(scale_y_n <= 0 || scale_y_d <= 0 || scale_x_n <= 0 || scale_x_d <= 0,
118 "OpResize: attribute scale must not be negative");
119 // If data type is int8_t then ensure that an int32_t accumulator can be used.
120 ERROR_IF(scale_y_n > (1 << 11) || scale_x_n > (1 << 11), "OpResize: invalid attribute scale");
121 // Set a consistent lower limit of 1/16 downscale to simplify implementations
122 ERROR_IF((scale_y_d >= 16 * scale_y_n) || (scale_x_d >= 16 * scale_x_n), "OpResize: invalid attribute scale");
123 ERROR_IF((offset_y < -scale_y_n) || (offset_y >= 16 * scale_y_n),
124 "OpResize: invalid attribute offset height dimension");
125 ERROR_IF((offset_x < -scale_x_n) || (offset_x >= 16 * scale_x_n),
126 "OpResize: invalid attribute offset width dimension");
127 ERROR_IF((border_y < -16 * scale_y_n || border_y >= scale_y_n),
128 "OpResize: invalid attribute border height dimension");
129 ERROR_IF((border_x < -16 * scale_x_n || border_x >= scale_x_n),
130 "OpResize: invalid attribute border width dimension");
131
132 int32_t res_height = 0;
133 int32_t res_width = 0;
134
135 if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height))
136 return 1;
137
138 if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width))
139 return 1;
140
141 ERROR_IF(out_height != res_height + 1,
142 "OpResize: mismatch between output height dimension provided and expected shape");
143 ERROR_IF(out_width != res_width + 1,
144 "OpResize: mismatch between output width dimension provided and expected shape");
Eric Kunzee5e26762020-10-13 16:11:07 -0700145
146 for (int b = 0; b < out_batch; b++)
147 for (int c = 0; c < out_channels; c++)
148 for (int oy = 0; oy < out_height; oy++)
149 for (int ox = 0; ox < out_width; ox++)
150 {
TatWai Chongf7326092022-06-08 12:17:14 -0700151 int32_t y = oy * scale_y_d + offset_y;
152 int32_t x = ox * scale_x_d + offset_x;
Eric Kunzee5e26762020-10-13 16:11:07 -0700153
TatWai Chongf7326092022-06-08 12:17:14 -0700154 float fy = static_cast<float>(y) / static_cast<float>(scale_y_n);
155 float fx = static_cast<float>(x) / static_cast<float>(scale_x_n);
156
157 int32_t iy = floor(fy);
158 int32_t ix = floor(fx);
159
160 resize_t dy;
161 resize_t dx;
James Ward24dbc422022-10-19 12:20:31 +0100162 if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)))
TatWai Chongf7326092022-06-08 12:17:14 -0700163 {
James Ward24dbc422022-10-19 12:20:31 +0100164 dy = (resize_t)(fy - iy);
165 dx = (resize_t)(fx - ix);
TatWai Chongf7326092022-06-08 12:17:14 -0700166 }
167 else
168 {
James Ward24dbc422022-10-19 12:20:31 +0100169 dy = (resize_t)(y - (iy * scale_y_n));
170 dx = (resize_t)(x - (ix * scale_x_n));
TatWai Chongf7326092022-06-08 12:17:14 -0700171 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700172
Kevin Cheng77d0f762020-11-24 10:26:32 -0800173 int32_t iy0 = MAX(iy, 0);
174 int32_t iy1 = MIN(iy + 1, in_height - 1);
175 int32_t ix0 = MAX(ix, 0);
176 int32_t ix1 = MIN(ix + 1, in_width - 1);
Eric Kunzee5e26762020-10-13 16:11:07 -0700177
Eric Kunzee5e26762020-10-13 16:11:07 -0700178 OutEigenType acc;
179 if (mode == ResizeMode_BILINEAR)
180 {
Kevin Cheng77d0f762020-11-24 10:26:32 -0800181 InEigenType v00 = in->getTensor()(b, iy0, ix0, c);
182 InEigenType v01 = in->getTensor()(b, iy0, ix1, c);
183 InEigenType v10 = in->getTensor()(b, iy1, ix0, c);
184 InEigenType v11 = in->getTensor()(b, iy1, ix1, c);
185
TatWai Chongf7326092022-06-08 12:17:14 -0700186 if (std::is_floating_point<resize_t>::value)
187 {
188 acc = (OutEigenType)v00 * (1.0 - dy) * (1.0 - dx);
189 acc += (OutEigenType)v01 * (1.0 - dy) * dx;
190 acc += (OutEigenType)v10 * dy * (1.0 - dx);
191 acc += (OutEigenType)v11 * dy * dx;
192 }
James Ward24dbc422022-10-19 12:20:31 +0100193 else if ((typeid(resize_t) == typeid(Eigen::bfloat16)))
194 {
195 Eigen::bfloat16 bf16_acc;
196 bf16_acc = (Eigen::bfloat16)v00 * (Eigen::bfloat16)(1.0 - dy) * (Eigen::bfloat16)(1.0 - dx);
197 bf16_acc += (Eigen::bfloat16)v01 * (Eigen::bfloat16)(1.0 - dy) * (Eigen::bfloat16)dx;
198 bf16_acc += (Eigen::bfloat16)v10 * (Eigen::bfloat16)dy * (Eigen::bfloat16)(1.0 - dx);
199 bf16_acc += (Eigen::bfloat16)v11 * (Eigen::bfloat16)dy * (Eigen::bfloat16)dx;
200 acc = (float)bf16_acc;
201 }
TatWai Chongf7326092022-06-08 12:17:14 -0700202 else
203 {
204 acc = (OutEigenType)v00 * (scale_y_n - dy) * (scale_x_n - dx);
205 acc += (OutEigenType)v01 * (scale_y_n - dy) * dx;
206 acc += (OutEigenType)v10 * dy * (scale_x_n - dx);
207 acc += (OutEigenType)v11 * dy * dx;
208 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700209 }
210 else
211 {
TatWai Chongf7326092022-06-08 12:17:14 -0700212 ASSERT_MSG(mode == ResizeMode_NEAREST, "OpResize: invalid mode");
James Ward24dbc422022-10-19 12:20:31 +0100213 if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)))
TatWai Chongf7326092022-06-08 12:17:14 -0700214 {
215 iy = (dy >= 0.5) ? iy1 : iy0;
216 ix = (dx >= 0.5) ? ix1 : ix0;
217 }
218 else
219 {
220 iy = (2 * dy >= scale_y_n) ? iy1 : iy0;
221 ix = (2 * dx >= scale_x_n) ? ix1 : ix0;
222 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700223 acc = in->getTensor()(b, iy, ix, c);
224 }
James Ward24dbc422022-10-19 12:20:31 +0100225 if ((typeid(resize_t) == typeid(Eigen::bfloat16))) {
226 ASSERT_MSG(checkValidBFloat(acc), "Resize accumulator float value is not a valid bfloat16 value.");
227 }
Kevin Cheng77d0f762020-11-24 10:26:32 -0800228 out->getTensor()(b, oy, ox, c) = acc;
229 }
230
231 return GraphNode::eval();
232}
233
Eric Kunzee5e26762020-10-13 16:11:07 -0700234// template explicit instantiation
TatWai Chongf7326092022-06-08 12:17:14 -0700235DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT32, int16_t);
236DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT8, int16_t);
237DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT48, int16_t);
238DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT16, int16_t);
James Ward8b390432022-08-12 20:48:56 +0100239DEF_INSTANTIATE_THREE_TYPE(OpResize, FP16, FP16, float);
James Ward24dbc422022-10-19 12:20:31 +0100240DEF_INSTANTIATE_THREE_TYPE(OpResize, BF16, BF16, Eigen::bfloat16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100241DEF_INSTANTIATE_THREE_TYPE(OpResize, FP32, FP32, float);