xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/Scale.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2020, 2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust 
25*c217d954SCole Faust #include "Scale.h"
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "Utils.h"
28*c217d954SCole Faust #include "src/core/utils/ScaleUtils.h"
29*c217d954SCole Faust #include "support/Rounding.h"
30*c217d954SCole Faust 
31*c217d954SCole Faust namespace arm_compute
32*c217d954SCole Faust {
33*c217d954SCole Faust namespace test
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace validation
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace reference
38*c217d954SCole Faust {
39*c217d954SCole Faust template <typename T>
scale_core(const SimpleTensor<T> & in,float scale_x,float scale_y,InterpolationPolicy policy,BorderMode border_mode,T constant_border_value,SamplingPolicy sampling_policy,bool ceil_policy_scale,bool align_corners)40*c217d954SCole Faust SimpleTensor<T> scale_core(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
41*c217d954SCole Faust                            SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
42*c217d954SCole Faust {
43*c217d954SCole Faust     // Add 1 if ceil_policy_scale is true
44*c217d954SCole Faust     const size_t round_value = ceil_policy_scale ? 1U : 0U;
45*c217d954SCole Faust     TensorShape  shape_scaled(in.shape());
46*c217d954SCole Faust     shape_scaled.set(0, (in.shape()[0] + round_value) * scale_x, /* apply_dim_correction = */ false);
47*c217d954SCole Faust     shape_scaled.set(1, (in.shape()[1] + round_value) * scale_y, /* apply_dim_correction = */ false);
48*c217d954SCole Faust     SimpleTensor<T> out(shape_scaled, in.data_type());
49*c217d954SCole Faust 
50*c217d954SCole Faust     // Compute the ratio between source width/height and destination width/height
51*c217d954SCole Faust     const auto wr = arm_compute::scale_utils::calculate_resize_ratio(in.shape()[0], out.shape()[0], align_corners);
52*c217d954SCole Faust     const auto hr = arm_compute::scale_utils::calculate_resize_ratio(in.shape()[1], out.shape()[1], align_corners);
53*c217d954SCole Faust 
54*c217d954SCole Faust     const auto width  = static_cast<int>(in.shape().x());
55*c217d954SCole Faust     const auto height = static_cast<int>(in.shape().y());
56*c217d954SCole Faust 
57*c217d954SCole Faust     // Determine border size
58*c217d954SCole Faust     const int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
59*c217d954SCole Faust 
60*c217d954SCole Faust     // Area interpolation behaves as Nearest Neighbour in case of up-sampling
61*c217d954SCole Faust     if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
62*c217d954SCole Faust     {
63*c217d954SCole Faust         policy = InterpolationPolicy::NEAREST_NEIGHBOR;
64*c217d954SCole Faust     }
65*c217d954SCole Faust 
66*c217d954SCole Faust     const uint32_t num_elements = out.num_elements();
67*c217d954SCole Faust     for(uint32_t element_idx = 0, count = 0; element_idx < num_elements; ++element_idx, ++count)
68*c217d954SCole Faust     {
69*c217d954SCole Faust         Coordinates id    = index2coord(out.shape(), element_idx);
70*c217d954SCole Faust         int         idx   = id.x();
71*c217d954SCole Faust         int         idy   = id.y();
72*c217d954SCole Faust         float       x_src = 0;
73*c217d954SCole Faust         float       y_src = 0;
74*c217d954SCole Faust 
75*c217d954SCole Faust         switch(policy)
76*c217d954SCole Faust         {
77*c217d954SCole Faust             case InterpolationPolicy::NEAREST_NEIGHBOR:
78*c217d954SCole Faust             {
79*c217d954SCole Faust                 switch(sampling_policy)
80*c217d954SCole Faust                 {
81*c217d954SCole Faust                     case SamplingPolicy::TOP_LEFT:
82*c217d954SCole Faust                         x_src = align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(idx * wr) : std::floor(idx * wr);
83*c217d954SCole Faust                         y_src = align_corners ? arm_compute::utils::rounding::round_half_away_from_zero(idy * hr) : std::floor(idy * hr);
84*c217d954SCole Faust                         break;
85*c217d954SCole Faust                     case SamplingPolicy::CENTER:
86*c217d954SCole Faust                         //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords
87*c217d954SCole Faust                         x_src = (idx + 0.5f) * wr;
88*c217d954SCole Faust                         y_src = (idy + 0.5f) * hr;
89*c217d954SCole Faust                         break;
90*c217d954SCole Faust                     default:
91*c217d954SCole Faust                         ARM_COMPUTE_ERROR("Unsupported sampling policy.");
92*c217d954SCole Faust                 }
93*c217d954SCole Faust 
94*c217d954SCole Faust                 id.set(0, x_src);
95*c217d954SCole Faust                 id.set(1, y_src);
96*c217d954SCole Faust 
97*c217d954SCole Faust                 // If coordinates in range of tensor's width or height
98*c217d954SCole Faust                 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
99*c217d954SCole Faust                 {
100*c217d954SCole Faust                     out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value);
101*c217d954SCole Faust                 }
102*c217d954SCole Faust                 break;
103*c217d954SCole Faust             }
104*c217d954SCole Faust             case InterpolationPolicy::BILINEAR:
105*c217d954SCole Faust             {
106*c217d954SCole Faust                 switch(sampling_policy)
107*c217d954SCole Faust                 {
108*c217d954SCole Faust                     case SamplingPolicy::TOP_LEFT:
109*c217d954SCole Faust                         x_src = idx * wr;
110*c217d954SCole Faust                         y_src = idy * hr;
111*c217d954SCole Faust                         break;
112*c217d954SCole Faust                     case SamplingPolicy::CENTER:
113*c217d954SCole Faust                         x_src = (idx + 0.5f) * wr - 0.5f;
114*c217d954SCole Faust                         y_src = (idy + 0.5f) * hr - 0.5f;
115*c217d954SCole Faust                         break;
116*c217d954SCole Faust                     default:
117*c217d954SCole Faust                         ARM_COMPUTE_ERROR("Unsupported sampling policy.");
118*c217d954SCole Faust                 }
119*c217d954SCole Faust 
120*c217d954SCole Faust                 id.set(0, std::floor(x_src));
121*c217d954SCole Faust                 id.set(1, std::floor(y_src));
122*c217d954SCole Faust                 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
123*c217d954SCole Faust                 {
124*c217d954SCole Faust                     out[element_idx] = bilinear_policy(in, id, x_src, y_src, border_mode, constant_border_value);
125*c217d954SCole Faust                 }
126*c217d954SCole Faust                 else
127*c217d954SCole Faust                 {
128*c217d954SCole Faust                     if(border_mode == BorderMode::CONSTANT)
129*c217d954SCole Faust                     {
130*c217d954SCole Faust                         out[element_idx] = constant_border_value;
131*c217d954SCole Faust                     }
132*c217d954SCole Faust                     else if(border_mode == BorderMode::REPLICATE)
133*c217d954SCole Faust                     {
134*c217d954SCole Faust                         id.set(0, utility::clamp<int>(x_src, 0, width - 1));
135*c217d954SCole Faust                         id.set(1, utility::clamp<int>(y_src, 0, height - 1));
136*c217d954SCole Faust                         out[element_idx] = in[coord2index(in.shape(), id)];
137*c217d954SCole Faust                     }
138*c217d954SCole Faust                 }
139*c217d954SCole Faust                 break;
140*c217d954SCole Faust             }
141*c217d954SCole Faust             case InterpolationPolicy::AREA:
142*c217d954SCole Faust             {
143*c217d954SCole Faust                 int       x_from = std::floor(idx * wr - 0.5f - x_src);
144*c217d954SCole Faust                 int       y_from = std::floor(idy * hr - 0.5f - y_src);
145*c217d954SCole Faust                 int       x_to   = std::ceil((idx + 1) * wr - 0.5f - x_src);
146*c217d954SCole Faust                 int       y_to   = std::ceil((idy + 1) * hr - 0.5f - y_src);
147*c217d954SCole Faust                 const int xi     = std::floor(x_src);
148*c217d954SCole Faust                 const int yi     = std::floor(y_src);
149*c217d954SCole Faust 
150*c217d954SCole Faust                 // Clamp position to borders
151*c217d954SCole Faust                 x_src = std::max(-static_cast<float>(border_size), std::min(x_src, static_cast<float>(width - 1 + border_size)));
152*c217d954SCole Faust                 y_src = std::max(-static_cast<float>(border_size), std::min(y_src, static_cast<float>(height - 1 + border_size)));
153*c217d954SCole Faust 
154*c217d954SCole Faust                 // Clamp bounding box offsets to borders
155*c217d954SCole Faust                 x_from = ((x_src + x_from) < -border_size) ? -border_size : x_from;
156*c217d954SCole Faust                 y_from = ((y_src + y_from) < -border_size) ? -border_size : y_from;
157*c217d954SCole Faust                 x_to   = ((x_src + x_to) >= (width + border_size)) ? (width - 1 + border_size) : x_to;
158*c217d954SCole Faust                 y_to   = ((y_src + y_to) >= (height + border_size)) ? (height - 1 + border_size) : y_to;
159*c217d954SCole Faust                 ARM_COMPUTE_ERROR_ON((x_to - x_from + 1) == 0 || (y_to - y_from + 1) == 0);
160*c217d954SCole Faust 
161*c217d954SCole Faust                 float sum = 0;
162*c217d954SCole Faust                 for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
163*c217d954SCole Faust                 {
164*c217d954SCole Faust                     for(int i = xi + x_from, ie = xi + x_to; i <= ie; ++i)
165*c217d954SCole Faust                     {
166*c217d954SCole Faust                         id.set(0, static_cast<int>(i));
167*c217d954SCole Faust                         id.set(1, static_cast<int>(j));
168*c217d954SCole Faust                         sum += tensor_elem_at(in, id, border_mode, constant_border_value);
169*c217d954SCole Faust                     }
170*c217d954SCole Faust                 }
171*c217d954SCole Faust                 out[element_idx] = sum / ((x_to - x_from + 1) * (y_to - y_from + 1));
172*c217d954SCole Faust 
173*c217d954SCole Faust                 break;
174*c217d954SCole Faust             }
175*c217d954SCole Faust             default:
176*c217d954SCole Faust                 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
177*c217d954SCole Faust         }
178*c217d954SCole Faust     }
179*c217d954SCole Faust 
180*c217d954SCole Faust     return out;
181*c217d954SCole Faust }
182*c217d954SCole Faust 
183*c217d954SCole Faust template <typename T>
scale(const SimpleTensor<T> & src,float scale_x,float scale_y,InterpolationPolicy policy,BorderMode border_mode,T constant_border_value,SamplingPolicy sampling_policy,bool ceil_policy_scale,bool align_corners,QuantizationInfo output_quantization_info)184*c217d954SCole Faust SimpleTensor<T> scale(const SimpleTensor<T> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
185*c217d954SCole Faust                       SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info)
186*c217d954SCole Faust {
187*c217d954SCole Faust     ARM_COMPUTE_UNUSED(output_quantization_info);
188*c217d954SCole Faust     return scale_core<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
189*c217d954SCole Faust }
190*c217d954SCole Faust 
191*c217d954SCole Faust template <>
scale(const SimpleTensor<uint8_t> & src,float scale_x,float scale_y,InterpolationPolicy policy,BorderMode border_mode,uint8_t constant_border_value,SamplingPolicy sampling_policy,bool ceil_policy_scale,bool align_corners,QuantizationInfo output_quantization_info)192*c217d954SCole Faust SimpleTensor<uint8_t> scale(const SimpleTensor<uint8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value,
193*c217d954SCole Faust                             SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info)
194*c217d954SCole Faust {
195*c217d954SCole Faust     SimpleTensor<uint8_t> dst;
196*c217d954SCole Faust     if(src.quantization_info().uniform().scale != 0.f)
197*c217d954SCole Faust     {
198*c217d954SCole Faust         SimpleTensor<float> src_tmp                 = convert_from_asymmetric(src);
199*c217d954SCole Faust         float               constant_border_value_f = dequantize_qasymm8(constant_border_value, src.quantization_info());
200*c217d954SCole Faust         SimpleTensor<float> dst_tmp                 = scale_core<float>(src_tmp, scale_x, scale_y, policy, border_mode, constant_border_value_f, sampling_policy, ceil_policy_scale, align_corners);
201*c217d954SCole Faust         dst                                         = convert_to_asymmetric<uint8_t>(dst_tmp, output_quantization_info);
202*c217d954SCole Faust     }
203*c217d954SCole Faust     else
204*c217d954SCole Faust     {
205*c217d954SCole Faust         dst = scale_core<uint8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
206*c217d954SCole Faust     }
207*c217d954SCole Faust     return dst;
208*c217d954SCole Faust }
209*c217d954SCole Faust 
210*c217d954SCole Faust template <>
scale(const SimpleTensor<int8_t> & src,float scale_x,float scale_y,InterpolationPolicy policy,BorderMode border_mode,int8_t constant_border_value,SamplingPolicy sampling_policy,bool ceil_policy_scale,bool align_corners,QuantizationInfo output_quantization_info)211*c217d954SCole Faust SimpleTensor<int8_t> scale(const SimpleTensor<int8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int8_t constant_border_value,
212*c217d954SCole Faust                            SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info)
213*c217d954SCole Faust {
214*c217d954SCole Faust     SimpleTensor<int8_t> dst;
215*c217d954SCole Faust     if(src.quantization_info().uniform().scale != 0.f)
216*c217d954SCole Faust     {
217*c217d954SCole Faust         SimpleTensor<float> src_tmp                 = convert_from_asymmetric(src);
218*c217d954SCole Faust         float               constant_border_value_f = dequantize_qasymm8_signed(constant_border_value, src.quantization_info());
219*c217d954SCole Faust         SimpleTensor<float> dst_tmp                 = scale_core<float>(src_tmp, scale_x, scale_y, policy, border_mode, constant_border_value_f, sampling_policy, ceil_policy_scale, align_corners);
220*c217d954SCole Faust         dst                                         = convert_to_asymmetric<int8_t>(dst_tmp, output_quantization_info);
221*c217d954SCole Faust     }
222*c217d954SCole Faust     else
223*c217d954SCole Faust     {
224*c217d954SCole Faust         dst = scale_core<int8_t>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, ceil_policy_scale, align_corners);
225*c217d954SCole Faust     }
226*c217d954SCole Faust     return dst;
227*c217d954SCole Faust }
228*c217d954SCole Faust 
229*c217d954SCole Faust template SimpleTensor<int16_t> scale(const SimpleTensor<int16_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int16_t constant_border_value,
230*c217d954SCole Faust                                      SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info);
231*c217d954SCole Faust template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value,
232*c217d954SCole Faust                                   SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info);
233*c217d954SCole Faust template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value,
234*c217d954SCole Faust                                    SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners, QuantizationInfo output_quantization_info);
235*c217d954SCole Faust } // namespace reference
236*c217d954SCole Faust } // namespace validation
237*c217d954SCole Faust } // namespace test
238*c217d954SCole Faust } // namespace arm_compute
239