Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2023, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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 "image.h" |
| 17 | #include "arith_util.h" |
James Ward | ee25669 | 2022-11-15 11:36:47 +0000 | [diff] [blame] | 18 | #include "half.hpp" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 19 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 20 | #include <type_traits> |
| 21 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 22 | using namespace TosaReference; |
| 23 | using namespace Eigen; |
| 24 | using namespace tosa; |
| 25 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 26 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype, typename resize_t> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 27 | OpResize<InDtype, OutDtype, resize_t>::OpResize(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 28 | : GraphNode(sgt_, Op_RESIZE, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 29 | { |
| 30 | setRequiredOperands(1, 1); |
| 31 | setRequiredRank(4, 4); |
| 32 | |
| 33 | INIT_ATTRIBUTE(Resize); |
| 34 | } |
| 35 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 36 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype, typename resize_t> |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 37 | OpResize<InDtype, OutDtype, resize_t>::~OpResize() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 38 | { |
| 39 | if (attribute) |
| 40 | delete attribute; |
| 41 | } |
| 42 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 43 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype, typename resize_t> |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 44 | int OpResize<InDtype, OutDtype, resize_t>::checkTensorAttributes() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 45 | { |
| 46 | if (validateRequiredOperands()) |
| 47 | return 1; |
| 48 | |
| 49 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 50 | return 1; |
| 51 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 52 | if (this->attribute->scale().size() != 4) |
| 53 | { |
| 54 | printNodeValidationError("OpResize: illegal size for attribute scale"); |
| 55 | return 1; |
| 56 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 57 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 58 | scale = this->attribute->scale(); |
| 59 | offset = this->attribute->offset(); |
| 60 | border = this->attribute->border(); |
| 61 | mode = this->attribute->mode(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 62 | |
| 63 | if (this->mode == ResizeMode_BILINEAR) |
| 64 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 65 | if (OutDtype != TOSA_REF_TYPE_INT32 && OutDtype != TOSA_REF_TYPE_INT48 && OutDtype != TOSA_REF_TYPE_FP32 && |
| 66 | OutDtype != TOSA_REF_TYPE_FP16 && OutDtype != TOSA_REF_TYPE_BF16 && OutDtype != TOSA_REF_TYPE_FP64) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 67 | { |
| 68 | printNodeValidationError("OpResize: invalid data type for BILINEAR"); |
| 69 | return 1; |
| 70 | } |
| 71 | } |
| 72 | else |
| 73 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 74 | if (OutDtype != TOSA_REF_TYPE_INT8 && OutDtype != TOSA_REF_TYPE_INT16 && OutDtype != TOSA_REF_TYPE_FP32 && |
| 75 | OutDtype != TOSA_REF_TYPE_FP16 && OutDtype != TOSA_REF_TYPE_BF16 && OutDtype != TOSA_REF_TYPE_FP64) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 76 | { |
| 77 | printNodeValidationError("OpResize: invalid data type for NEAREST"); |
| 78 | return 1; |
| 79 | } |
| 80 | } |
| 81 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 82 | 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 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 90 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype, typename resize_t> |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 91 | int OpResize<InDtype, OutDtype, resize_t>::eval() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 92 | { |
| 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 Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 103 | 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 | |
Kevin Cheng | 1918e8a | 2021-09-15 10:17:39 -0700 | [diff] [blame] | 114 | ERROR_IF(std::max<int>({ in_height, in_width, out_height, out_width }) >= 16384, |
| 115 | "OpResize: exceeds maximum dimension"); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 116 | ERROR_IF(in_batch != out_batch, "OpResize: output tensor batch mismatch"); |
| 117 | ERROR_IF(in_channels != out_channels, "OpResize: output tensor channel mismatch"); |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 118 | ERROR_IF(scale_y_n <= 0 || scale_y_d <= 0 || scale_x_n <= 0 || scale_x_d <= 0, |
| 119 | "OpResize: attribute scale must not be negative"); |
| 120 | // If data type is int8_t then ensure that an int32_t accumulator can be used. |
| 121 | ERROR_IF(scale_y_n > (1 << 11) || scale_x_n > (1 << 11), "OpResize: invalid attribute scale"); |
| 122 | // Set a consistent lower limit of 1/16 downscale to simplify implementations |
| 123 | ERROR_IF((scale_y_d >= 16 * scale_y_n) || (scale_x_d >= 16 * scale_x_n), "OpResize: invalid attribute scale"); |
| 124 | ERROR_IF((offset_y < -scale_y_n) || (offset_y >= 16 * scale_y_n), |
| 125 | "OpResize: invalid attribute offset height dimension"); |
| 126 | ERROR_IF((offset_x < -scale_x_n) || (offset_x >= 16 * scale_x_n), |
| 127 | "OpResize: invalid attribute offset width dimension"); |
| 128 | ERROR_IF((border_y < -16 * scale_y_n || border_y >= scale_y_n), |
| 129 | "OpResize: invalid attribute border height dimension"); |
| 130 | ERROR_IF((border_x < -16 * scale_x_n || border_x >= scale_x_n), |
| 131 | "OpResize: invalid attribute border width dimension"); |
| 132 | |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 133 | // Check Tosa Level |
| 134 | auto tosa_level = g_func_config.tosa_level; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 135 | LEVEL_CHECK(scale_y_n / scale_y_d <= tosa_level.MAX_SCALE, |
| 136 | "scale_y_n / scale_y_d should be smaller than or equal to MAX_SCALE"); |
| 137 | LEVEL_CHECK(scale_x_n / scale_x_d <= tosa_level.MAX_SCALE, |
| 138 | "scale_x_n / scale_x_d should be smaller than or equal to MAX_SCALE"); |
Jerry Ge | 0bd4ec8 | 2023-05-01 18:36:43 +0000 | [diff] [blame] | 139 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 140 | int32_t res_height = 0; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 141 | int32_t res_width = 0; |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 142 | |
| 143 | if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height)) |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 144 | return 1; |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 145 | |
| 146 | if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width)) |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 147 | return 1; |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 148 | |
| 149 | ERROR_IF(out_height != res_height + 1, |
| 150 | "OpResize: mismatch between output height dimension provided and expected shape"); |
| 151 | ERROR_IF(out_width != res_width + 1, |
| 152 | "OpResize: mismatch between output width dimension provided and expected shape"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 153 | |
| 154 | for (int b = 0; b < out_batch; b++) |
| 155 | for (int c = 0; c < out_channels; c++) |
| 156 | for (int oy = 0; oy < out_height; oy++) |
| 157 | for (int ox = 0; ox < out_width; ox++) |
| 158 | { |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 159 | int32_t y = oy * scale_y_d + offset_y; |
| 160 | int32_t x = ox * scale_x_d + offset_x; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 161 | |
TatWai Chong | fb87982 | 2023-08-31 16:58:27 -0700 | [diff] [blame^] | 162 | int16_t iy = idiv_floor(y, scale_y_n); |
| 163 | int16_t ix = idiv_floor(x, scale_x_n); |
| 164 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 165 | resize_t dy; |
| 166 | resize_t dx; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 167 | if (std::is_same<resize_t, double>::value) |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 168 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 169 | const double fy_double = static_cast<double>(y) / static_cast<double>(scale_y_n); |
| 170 | const double fx_double = static_cast<double>(x) / static_cast<double>(scale_x_n); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 171 | |
| 172 | dy = (resize_t)(fy_double - iy); |
| 173 | dx = (resize_t)(fx_double - ix); |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 177 | const float fy = static_cast<float>(y) / static_cast<float>(scale_y_n); |
| 178 | const float fx = static_cast<float>(x) / static_cast<float>(scale_x_n); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 179 | |
| 180 | if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)) || |
| 181 | (typeid(resize_t) == typeid(half_float::half))) |
| 182 | { |
| 183 | dy = (resize_t)(fy - iy); |
| 184 | dx = (resize_t)(fx - ix); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | dy = (resize_t)(y - (iy * scale_y_n)); |
| 189 | dx = (resize_t)(x - (ix * scale_x_n)); |
| 190 | } |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 191 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 192 | |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 193 | int32_t iy0 = MAX(iy, 0); |
| 194 | int32_t iy1 = MIN(iy + 1, in_height - 1); |
| 195 | int32_t ix0 = MAX(ix, 0); |
| 196 | int32_t ix1 = MIN(ix + 1, in_width - 1); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 197 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 198 | OutEigenType acc; |
| 199 | if (mode == ResizeMode_BILINEAR) |
| 200 | { |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 201 | InEigenType v00 = in->getTensor()(b, iy0, ix0, c); |
| 202 | InEigenType v01 = in->getTensor()(b, iy0, ix1, c); |
| 203 | InEigenType v10 = in->getTensor()(b, iy1, ix0, c); |
| 204 | InEigenType v11 = in->getTensor()(b, iy1, ix1, c); |
| 205 | |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 206 | if (std::is_floating_point<resize_t>::value) |
| 207 | { |
| 208 | acc = (OutEigenType)v00 * (1.0 - dy) * (1.0 - dx); |
| 209 | acc += (OutEigenType)v01 * (1.0 - dy) * dx; |
| 210 | acc += (OutEigenType)v10 * dy * (1.0 - dx); |
| 211 | acc += (OutEigenType)v11 * dy * dx; |
| 212 | } |
James Ward | ee25669 | 2022-11-15 11:36:47 +0000 | [diff] [blame] | 213 | else if ((typeid(resize_t) == typeid(Eigen::bfloat16)) || |
| 214 | (typeid(resize_t) == typeid(half_float::half))) |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 215 | { |
James Ward | ee25669 | 2022-11-15 11:36:47 +0000 | [diff] [blame] | 216 | resize_t f16_acc; |
| 217 | f16_acc = (resize_t)v00 * (resize_t)(1.0 - dy) * (resize_t)(1.0 - dx); |
| 218 | f16_acc += (resize_t)v01 * (resize_t)(1.0 - dy) * (resize_t)dx; |
| 219 | f16_acc += (resize_t)v10 * (resize_t)dy * (resize_t)(1.0 - dx); |
| 220 | f16_acc += (resize_t)v11 * (resize_t)dy * (resize_t)dx; |
| 221 | acc = (float)f16_acc; |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 222 | } |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 223 | else |
| 224 | { |
| 225 | acc = (OutEigenType)v00 * (scale_y_n - dy) * (scale_x_n - dx); |
| 226 | acc += (OutEigenType)v01 * (scale_y_n - dy) * dx; |
| 227 | acc += (OutEigenType)v10 * dy * (scale_x_n - dx); |
| 228 | acc += (OutEigenType)v11 * dy * dx; |
| 229 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 230 | } |
| 231 | else |
| 232 | { |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 233 | ASSERT_MSG(mode == ResizeMode_NEAREST, "OpResize: invalid mode"); |
James Ward | ee25669 | 2022-11-15 11:36:47 +0000 | [diff] [blame] | 234 | if (std::is_floating_point<resize_t>::value || (typeid(resize_t) == typeid(Eigen::bfloat16)) || |
| 235 | (typeid(resize_t) == typeid(half_float::half))) |
TatWai Chong | f732609 | 2022-06-08 12:17:14 -0700 | [diff] [blame] | 236 | { |
| 237 | iy = (dy >= 0.5) ? iy1 : iy0; |
| 238 | ix = (dx >= 0.5) ? ix1 : ix0; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | iy = (2 * dy >= scale_y_n) ? iy1 : iy0; |
| 243 | ix = (2 * dx >= scale_x_n) ? ix1 : ix0; |
| 244 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 245 | acc = in->getTensor()(b, iy, ix, c); |
| 246 | } |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 247 | if ((typeid(resize_t) == typeid(Eigen::bfloat16))) |
| 248 | { |
| 249 | ASSERT_MSG(checkValidBFloat(acc), |
| 250 | "Resize accumulator float value is not a valid bfloat16 value."); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 251 | } |
Kevin Cheng | 77d0f76 | 2020-11-24 10:26:32 -0800 | [diff] [blame] | 252 | out->getTensor()(b, oy, ox, c) = acc; |
| 253 | } |
| 254 | |
| 255 | return GraphNode::eval(); |
| 256 | } |
| 257 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 258 | // template explicit instantiation |
James Ward | d34b3fc | 2023-01-18 14:51:25 +0000 | [diff] [blame] | 259 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT8, INT32, int16_t); |
| 260 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT8, INT8, int16_t); |
| 261 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT16, INT48, int16_t); |
| 262 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, INT16, INT16, int16_t); |
| 263 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, FP16, FP16, half_float::half); |
| 264 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, BF16, BF16, Eigen::bfloat16); |
| 265 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, FP32, FP32, float); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 266 | DEF_INSTANTIATE_THREE_TYPE_RESIZE(OpResize, FP64, FP64, double); |