// Copyright (c) 2020-2022, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "image.h" #include "arith_util.h" #include using namespace TosaReference; using namespace Eigen; using namespace tosa; template OpResize::OpResize(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) : GraphNode(sgt_, Op_RESIZE, id_) { setRequiredOperands(1, 1); setRequiredRank(4, 4); INIT_ATTRIBUTE(Resize); } template OpResize::~OpResize() { if (attribute) delete attribute; } template int OpResize::checkTensorAttributes() { if (validateRequiredOperands()) return 1; if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) return 1; if (this->attribute->scale().size() != 4) { printNodeValidationError("OpResize: illegal size for attribute scale"); return 1; } scale = this->attribute->scale(); offset = this->attribute->offset(); border = this->attribute->border(); mode = this->attribute->mode(); if (this->mode == ResizeMode_BILINEAR) { if (OutDtype != DType_INT32 && OutDtype != DType_INT48 && OutDtype != DType_FP32 && OutDtype != DType_FP16) { printNodeValidationError("OpResize: invalid data type for BILINEAR"); return 1; } } else { if (OutDtype != DType_INT8 && OutDtype != DType_INT16 && OutDtype != DType_FP32 && OutDtype != DType_FP16) { printNodeValidationError("OpResize: invalid data type for NEAREST"); return 1; } } in = dynamic_cast*>(inputs[0]); out = dynamic_cast*>(outputs[0]); ASSERT_MEM(in && out); return 0; } template int OpResize::eval() { int in_batch = in->getShape()[0]; int in_height = in->getShape()[1]; int in_width = in->getShape()[2]; int in_channels = in->getShape()[3]; int out_batch = out->getShape()[0]; int out_height = out->getShape()[1]; int out_width = out->getShape()[2]; int out_channels = out->getShape()[3]; int16_t scale_y_n = scale[0]; int16_t scale_y_d = scale[1]; int16_t scale_x_n = scale[2]; int16_t scale_x_d = scale[3]; int16_t offset_y = offset[0]; int16_t offset_x = offset[1]; int16_t border_y = border[0]; int16_t border_x = border[1]; ERROR_IF(std::max({ in_height, in_width, out_height, out_width }) >= 16384, "OpResize: exceeds maximum dimension"); ERROR_IF(in_batch != out_batch, "OpResize: output tensor batch mismatch"); ERROR_IF(in_channels != out_channels, "OpResize: output tensor channel mismatch"); ERROR_IF(scale_y_n <= 0 || scale_y_d <= 0 || scale_x_n <= 0 || scale_x_d <= 0, "OpResize: attribute scale must not be negative"); // If data type is int8_t then ensure that an int32_t accumulator can be used. ERROR_IF(scale_y_n > (1 << 11) || scale_x_n > (1 << 11), "OpResize: invalid attribute scale"); // Set a consistent lower limit of 1/16 downscale to simplify implementations ERROR_IF((scale_y_d >= 16 * scale_y_n) || (scale_x_d >= 16 * scale_x_n), "OpResize: invalid attribute scale"); ERROR_IF((offset_y < -scale_y_n) || (offset_y >= 16 * scale_y_n), "OpResize: invalid attribute offset height dimension"); ERROR_IF((offset_x < -scale_x_n) || (offset_x >= 16 * scale_x_n), "OpResize: invalid attribute offset width dimension"); ERROR_IF((border_y < -16 * scale_y_n || border_y >= scale_y_n), "OpResize: invalid attribute border height dimension"); ERROR_IF((border_x < -16 * scale_x_n || border_x >= scale_x_n), "OpResize: invalid attribute border width dimension"); int32_t res_height = 0; int32_t res_width = 0; if (idiv_check((in_height - 1) * scale_y_n - offset_y + border_y, scale_y_d, res_height)) return 1; if (idiv_check((in_width - 1) * scale_x_n - offset_x + border_x, scale_x_d, res_width)) return 1; ERROR_IF(out_height != res_height + 1, "OpResize: mismatch between output height dimension provided and expected shape"); ERROR_IF(out_width != res_width + 1, "OpResize: mismatch between output width dimension provided and expected shape"); for (int b = 0; b < out_batch; b++) for (int c = 0; c < out_channels; c++) for (int oy = 0; oy < out_height; oy++) for (int ox = 0; ox < out_width; ox++) { int32_t y = oy * scale_y_d + offset_y; int32_t x = ox * scale_x_d + offset_x; float fy = static_cast(y) / static_cast(scale_y_n); float fx = static_cast(x) / static_cast(scale_x_n); int32_t iy = floor(fy); int32_t ix = floor(fx); resize_t dy; resize_t dx; if (std::is_floating_point::value) { dy = fy - iy; dx = fx - ix; } else { dy = y - (iy * scale_y_n); dx = x - (ix * scale_x_n); } int32_t iy0 = MAX(iy, 0); int32_t iy1 = MIN(iy + 1, in_height - 1); int32_t ix0 = MAX(ix, 0); int32_t ix1 = MIN(ix + 1, in_width - 1); OutEigenType acc; if (mode == ResizeMode_BILINEAR) { InEigenType v00 = in->getTensor()(b, iy0, ix0, c); InEigenType v01 = in->getTensor()(b, iy0, ix1, c); InEigenType v10 = in->getTensor()(b, iy1, ix0, c); InEigenType v11 = in->getTensor()(b, iy1, ix1, c); if (std::is_floating_point::value) { acc = (OutEigenType)v00 * (1.0 - dy) * (1.0 - dx); acc += (OutEigenType)v01 * (1.0 - dy) * dx; acc += (OutEigenType)v10 * dy * (1.0 - dx); acc += (OutEigenType)v11 * dy * dx; } else { acc = (OutEigenType)v00 * (scale_y_n - dy) * (scale_x_n - dx); acc += (OutEigenType)v01 * (scale_y_n - dy) * dx; acc += (OutEigenType)v10 * dy * (scale_x_n - dx); acc += (OutEigenType)v11 * dy * dx; } } else { ASSERT_MSG(mode == ResizeMode_NEAREST, "OpResize: invalid mode"); if (std::is_floating_point::value) { iy = (dy >= 0.5) ? iy1 : iy0; ix = (dx >= 0.5) ? ix1 : ix0; } else { iy = (2 * dy >= scale_y_n) ? iy1 : iy0; ix = (2 * dx >= scale_x_n) ? ix1 : ix0; } acc = in->getTensor()(b, iy, ix, c); } out->getTensor()(b, oy, ox, c) = acc; } return GraphNode::eval(); } // template explicit instantiation DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT32, int16_t); DEF_INSTANTIATE_THREE_TYPE(OpResize, INT8, INT8, int16_t); DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT48, int16_t); DEF_INSTANTIATE_THREE_TYPE(OpResize, INT16, INT16, int16_t); DEF_INSTANTIATE_THREE_TYPE(OpResize, FP16, FP16, float); DEF_INSTANTIATE_THREE_TYPE(OpResize, FP32, FP32, float);