Compute Library
 22.11
CpuScaleKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
33 #include "support/Rounding.h"
34 
35 #include <arm_neon.h>
36 #include <map>
37 
38 namespace arm_compute
39 {
40 namespace cpu
41 {
42 namespace kernels
43 {
44 namespace
45 {
46 static const std::vector<CpuScaleKernel::ScaleKernel> available_kernels =
47 {
48  {
49  "sve_fp16_scale",
50  [](const ScaleKernelDataTypeISASelectorData & data)
51  {
52  return data.dt == DataType::F16 && data.isa.sve && data.isa.fp16 && data.interpolation_policy != InterpolationPolicy::BILINEAR;
53  },
55  },
56  {
57  "sve_fp32_scale",
58  [](const ScaleKernelDataTypeISASelectorData & data)
59  {
60  return data.dt == DataType::F32 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
61  },
63  },
64  {
65  "sve_qu8_scale",
66  [](const ScaleKernelDataTypeISASelectorData & data)
67  {
68  return data.dt == DataType::QASYMM8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
69  },
71  },
72  {
73  "sve_qs8_scale",
74  [](const ScaleKernelDataTypeISASelectorData & data)
75  {
76  return data.dt == DataType::QASYMM8_SIGNED && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
77  },
79  },
80  {
81  "sve_u8_scale",
82  [](const ScaleKernelDataTypeISASelectorData & data)
83  {
84  return data.dt == DataType::U8 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
85  },
87  },
88  {
89  "sve_s16_scale",
90  [](const ScaleKernelDataTypeISASelectorData & data)
91  {
92  return data.dt == DataType::S16 && data.isa.sve && data.interpolation_policy != InterpolationPolicy::BILINEAR;
93  },
95  },
96  {
97  "neon_fp16_scale",
98  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
99  REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
100  },
101  {
102  "neon_fp32_scale",
103  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::F32; },
104  REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
105  },
106  {
107  "neon_qu8_scale",
108  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
110  },
111  {
112  "neon_qs8_scale",
113  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
115  },
116  {
117  "neon_u8_scale",
118  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::U8; },
120  },
121  {
122  "neon_s8_scale",
123  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S8; },
125  },
126  {
127  "neon_s16_scale",
128  [](const ScaleKernelDataTypeISASelectorData & data) { return data.dt == DataType::S16; },
130  },
131 };
132 
133 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
134  const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
135 {
136  const auto *uk = CpuScaleKernel::get_implementation(ScaleKernelDataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa(), info.interpolation_policy });
137 
138  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
139 
142  ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
143  ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
144  ARM_COMPUTE_UNUSED(info.constant_border_value);
145  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
146 
147  const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
148  const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
149  const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
150  const auto output_width = dst->dimension(width_index);
151  const auto output_height = dst->dimension(height_index);
152  ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
153  ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
154 
155  ARM_COMPUTE_RETURN_ERROR_ON((src->data_type() == DataType::S8) && (data_layout != DataLayout::NHWC || info.interpolation_policy != InterpolationPolicy::BILINEAR
156  || info.border_mode != BorderMode::REPLICATE));
157 
158  if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR && offsets != nullptr)
159  {
161  }
162 
163  if(info.interpolation_policy == InterpolationPolicy::BILINEAR && offsets != nullptr)
164  {
166  if(dx != nullptr && dy != nullptr)
167  {
170  }
171  }
172 
173  ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
174 
175  if(info.interpolation_policy == InterpolationPolicy::AREA)
176  {
179  }
180 
181  return Status{};
182 }
183 } // namespace
184 
185 void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
186  ITensorInfo *dst, const ScaleKernelInfo &info)
187 {
188  ARM_COMPUTE_UNUSED(dx, dy, offsets);
190  // Perform validation step
192  dx,
193  dy,
194  offsets,
195  dst,
196  info));
197 
200 
201  _run_method = uk->ukernel;
202  _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
203 
204  // Get data layout and width/height indices
205  _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
208 
209  _policy = info.interpolation_policy;
210  _border_mode = info.border_mode;
211  _constant_border_value = info.constant_border_value;
212  _align_corners = info.align_corners;
213 
215  {
216  _sampling_offset = 0.5f;
217  }
218 
219  // Compute the ratio between source width/height and destination width/height
220  const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
221  const auto hr = scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), _align_corners);
222 
223  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
224  _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
225 
226  if(_border_mode == BorderMode::UNDEFINED)
227  {
228  _border_mode = BorderMode::CONSTANT;
229  _constant_border_value = PixelValue();
230  }
231 
232 #ifdef ENABLE_NCHW_KERNELS
233  // Configure scale function to run
234  if(_data_layout == DataLayout::NCHW)
235  {
236  std::string function_to_call("scale_");
237  function_to_call += string_from_data_type(src->data_type()) + "_";
238  function_to_call += string_from_data_layout(_data_layout) + "_";
239  function_to_call += string_from_interpolation_policy(_policy);
240 
241  static std::map<std::string, ScaleFunctionPtr> map_function =
242  {
243  { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
244 
245  { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
246  { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
247 
248  { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
249  { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
250 
251  { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
252  { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
253 
254  { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
255  { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
256 
257 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
258  { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
259  { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
260 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
261 
262  { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
263  { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
264  };
265  auto it = map_function.find(function_to_call);
266  if(it != map_function.end())
267  {
268  _func = it->second;
269  }
270  }
271 #endif // ENABLE_NCHW_KERNELS
272 
273  // Configure window
274  Window win = calculate_max_window(*dst, Steps());
275  ICpuKernel::configure(win);
276 }
277 
278 #ifdef ENABLE_NCHW_KERNELS
279 template <typename T>
280 void CpuScaleKernel::scale_nearest_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
281 {
282  ARM_COMPUTE_UNUSED(dx, dy);
283  const size_t in_stride_x = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
284 
285  // Compute the ratio between source height and destination height
286  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
287 
288  // Don't increment in X and Y direction for the input tensor
289  // A pointer to the start of this plane is needed as base for the precomputed offsets
290  Window win_in(window);
291  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
292  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
293 
294  // Set offsets window
295  Window win_off;
296  win_off.set(Window::DimX, window[Window::DimX]);
297  win_off.set(Window::DimY, window[Window::DimY]);
298  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
299  {
300  win_off.set(d, Window::Dimension(0, 0, 0));
301  }
302 
303  // Create iterators
304  Iterator src_i(src, win_in);
305  Iterator dst_i(dst, window);
306  Iterator offsets_i(offsets, win_off);
307  execute_window_loop(window, [&](const Coordinates & id)
308  {
309  const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
310  const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
311  id.y() + _sampling_offset)
312  * hr));
313  const int32_t offset_row = in_yi * in_stride_x;
314  *reinterpret_cast<T *>(dst_i.ptr()) = *(reinterpret_cast<const T *>(src_i.ptr()) + offsets_ptr[0] + offset_row);
315  },
316  src_i, offsets_i, dst_i);
317 }
318 
319 template <typename T>
320 void CpuScaleKernel::scale_bilinear_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
321 {
322  // Compute the ratio between source height and destination height
323  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
324  Window win_off;
325  win_off.set(Window::DimX, window.x());
326  win_off.set(Window::DimY, window.y());
327 
328  // Don't increment in X and Y direction for the input tensor
329  // A pointer to the start of this plane is needed as base for the precomputed offsets
330  Window win_in(window);
331  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
332  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
333 
334  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
335  {
336  win_off.set(d, Window::Dimension(0, 0, 0));
337  }
338 
339  Iterator src_i(src, win_in);
340  Iterator dst_i(dst, window);
341  Iterator offsets_i(offsets, win_off);
342  Iterator dx_i(dx, win_off);
343  Iterator dy_i(dy, win_off);
344 
345  const int32_t in_dim_w = src->info()->dimension(0);
346  const int32_t in_dim_h = src->info()->dimension(1);
347  const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
348 
349  if(_border_mode == BorderMode::CONSTANT)
350  {
351 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
352  using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
353 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
354  using ConstType = T;
355 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
356  const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
357  execute_window_loop(window, [&](const Coordinates & id)
358  {
359  const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
360  const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
361  const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
362  const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
363  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
364 
365  const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ? (*(pixel_row_ptr + index_w + index_h * in_stride_w)) : const_border_value;
366  const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ? (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w)) : const_border_value;
367  const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
368  && index_h < in_dim_h - 1) ?
369  (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
370  const_border_value;
371  const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
372  && index_h < in_dim_h - 1) ?
373  (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
374  const_border_value;
375 
376  *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
377  },
378  src_i, offsets_i, dx_i, dy_i, dst_i);
379  }
380  else if(_border_mode == BorderMode::REPLICATE)
381  {
382  execute_window_loop(window, [&](const Coordinates & id)
383  {
384  const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
385  const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
386  const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
387  const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
388  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
389 
390  auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
391  auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
392  auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
393  auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
394 
395  const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
396  const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
397  const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
398  const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
399 
400  *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
401  },
402  src_i, offsets_i, dx_i, dy_i, dst_i);
403  }
404  else
405  {
406  ARM_COMPUTE_ERROR("Not implemented");
407  }
408 }
409 
410 void CpuScaleKernel::scale_area_nchw_u8(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
411 {
412  ARM_COMPUTE_UNUSED(dx, dy, offsets);
413  using namespace scale_helpers;
414 
416 
417  // Don't increment in width/height/channels for the input tensor
418  // A pointer to the start of this plane is needed as base for the precomputed offsets
419  Window win_in(window);
420  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
421  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
422  win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
423 
424  Iterator src_i(src, win_in);
425  Iterator dst_i(dst, window);
426 
427  const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
428  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
429  const auto w = src->info()->dimension(0);
430  const auto h = src->info()->dimension(1);
431  const size_t in_stride = src->info()->strides_in_bytes()[1];
432 
433  execute_window_loop(window, [&](const Coordinates & id)
434  {
435  const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
436 
437  uint8x8_t tmp0 = vdup_n_u8(0);
438  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
439  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
440  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
441  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
442  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
443  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
444  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
445  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
446 
447  uint8x8_t tmp1 = vdup_n_u8(0);
448  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
449  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
450  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
451  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
452  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
453  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
454  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
455  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
456 
457  vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
458  },
459  src_i, dst_i);
460 }
461 
462 template <typename T>
463 void CpuScaleKernel::scale_bilinear_qasymm(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
464 {
465  // Get data layout and width/height indices
468 
469  // Compute the ratio between source height and destination height
470  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), _align_corners);
471  Window win_off;
472  win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
473  win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
474 
475  // Don't increment in X and Y direction for the input tensor
476  // A pointer to the start of this plane is needed as base for the precomputed offsets
477  Window win_in(window);
478  win_in.set(idx_width, Window::Dimension(0, 0, 0));
479  win_in.set(idx_height, Window::Dimension(0, 0, 0));
480 
481  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
482  {
483  win_off.set(d, Window::Dimension(0, 0, 0));
484  }
485 
486  Iterator src_i(src, win_in);
487  Iterator dst_i(dst, window);
488 
489  const int32_t in_dim_w = src->info()->dimension(idx_width);
490  const int32_t in_dim_h = src->info()->dimension(idx_height);
491  const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
492  const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
493 
494  const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
495  const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
496 
497  if(_border_mode == BorderMode::CONSTANT)
498  {
499 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
500  using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
501 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
502  using ConstType = T;
503 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
504  const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
505  execute_window_loop(window, [&](const Coordinates & id)
506  {
507  const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
508  const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
509  const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
510  const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
511  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
512 
513  const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
514  (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
515  const_border_value;
516  const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
517  (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
518  const_border_value;
519  const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
520  (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
521  const_border_value;
522  const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
523  (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
524  const_border_value;
525 
526  const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
527  const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
528  const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
529  const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
530  *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
531  },
532  src_i, dst_i);
533  }
534  else if(_border_mode == BorderMode::REPLICATE)
535  {
536  execute_window_loop(window, [&](const Coordinates & id)
537  {
538  const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
539  const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
540  const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
541  const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
542  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
543 
544  auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
545  auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
546  auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
547  auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
548 
549  const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
550  const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
551  const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
552  const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
553 
554  const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
555  const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
556  const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
557  const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
558  *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
559  },
560  src_i, dst_i);
561  }
562  else
563  {
564  ARM_COMPUTE_ERROR("Not implemented");
565  }
566 }
567 #endif // ENABLE_NCHW_KERNELS
568 
570  const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
571 {
572  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
573  return Status{};
574 }
575 
576 void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
577 {
578  ARM_COMPUTE_UNUSED(info);
581  ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
582  ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
583 
584  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
585  auto dst = tensors.get_tensor(TensorType::ACL_DST);
586  const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
587  const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
588  const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
589 
590  if(_data_layout == DataLayout::NCHW)
591  {
592  (this->*_func)(src, dst, dx, dy, offsets, window);
593  }
594  else
595  {
596  _run_method(src, dst, offsets, dx, dy, _policy, _border_mode, _constant_border_value, _sampling_offset, _align_corners, window);
597  }
598 }
599 
600 const char *CpuScaleKernel::name() const
601 {
602  return _name.c_str();
603 }
604 
605 const std::vector<CpuScaleKernel::ScaleKernel> &CpuScaleKernel::get_available_kernels()
606 {
607  return available_kernels;
608 }
609 
610 } // namespace kernels
611 } // namespace cpu
612 } // namespace arm_compute
BorderMode border_mode
Border mode policy.
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
SimpleTensor< float > w
Definition: DFT.cpp:156
void s8_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: integer.cpp:631
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
static const auto * get_implementation(const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
Micro-kernel selector.
Definition: ICpuKernel.h:53
InterpolationPolicy interpolation_policy
Interpolation type to use.
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void qasymm8_signed_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
void qasymm8_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: qasymm8.cpp:89
1 channel, 1 U8 per channel
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:244
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
half_float::half half
16-bit floating point type
Definition: Types.h:48
1 channel, 1 F32 per channel
#define REGISTER_FP32_SVE(func_name)
Definition: Registrars.h:75
Output values are defined by bilinear interpolation between the pixels.
#define REGISTER_QASYMM8_SVE(func_name)
Definition: Registrars.h:118
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
#define REGISTER_QASYMM8_SIGNED_NEON(func_name)
Definition: Registrars.h:96
bool align_corners
Align corners of input and output.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
void fp16_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Quantization info when assuming per layer quantization.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
T round_half_away_from_zero(T value)
Round floating-point value with half value rounding away from zero.
Definition: Rounding.h:106
Status class.
Definition: Error.h:52
Output values are defined to match the source pixel whose center is nearest to the sample position...
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
__kernel void scale_bilinear_nchw(__global uchar *in_ptr, uint in_stride_x, uint in_step_x, uint in_stride_y, uint in_step_y, uint in_offset_first_element_in_bytes, __global uchar *out_ptr, uint out_stride_x, uint out_step_x, uint out_stride_y, uint out_step_y, uint out_offset_first_element_in_bytes)
Performs an affine transformation on an image interpolating with the BILINEAR method.
Definition: scale.cl:158
1 channel, 1 F16 per channel
Samples are taken at pixel center.
bool is_align_corners_allowed_sampling_policy(SamplingPolicy sampling_policy)
Returns if aligned corners are allowed for the given sampling policy.
Definition: ScaleUtils.h:49
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:171
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
#define REGISTER_QASYMM8_SIGNED_SVE(func_name)
Definition: Registrars.h:97
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
void u8_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: integer.cpp:132
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
SamplingPolicy sampling_policy
Sampling policy used by the interpolation.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
uint8_t pixel_area_c1u8_clamp(const uint8_t *first_pixel_ptr, size_t stride, size_t width, size_t height, float wr, float hr, int x, int y)
Return the pixel at (x,y) using area interpolation by clamping when out of borders.
Definition: ScaleHelpers.h:126
Coordinates of an item.
Definition: Coordinates.h:37
void qasymm8_signed_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
const std::string & string_from_interpolation_policy(InterpolationPolicy policy)
Translates a given interpolation policy to a string.
Definition: Utils.cpp:189
UniformQuantizationInfo uniform() const
Return per layer quantization info.
#define REGISTER_INTEGER_SVE(func_name)
Definition: Registrars.h:172
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Samples are taken at pixel top left corner.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void s16_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: integer.cpp:147
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual PaddingSize padding() const =0
Padding of tensor.
unsigned int left
left of the border
Definition: Types.h:393
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
unsigned int right
right of the border
Definition: Types.h:391
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
1 channel, 1 S16 per channel
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
void u8_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: integer.cpp:645
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:786
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:123
Information about executing thread and CPU.
Definition: CPPTypes.h:179
#define REGISTER_FP16_SVE(func_name)
Definition: Registrars.h:49
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Borders are left undefined.
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
Pixels outside the image are assumed to have the same value as the closest image pixel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
Num samples, height, width, channels.
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:168
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
void s16_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: integer.cpp:659
void configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Initialise the kernel&#39;s inputs, output and interpolation policy.
static Status validate(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
Static function to check if given info will lead to a valid configuration.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Tensor packing service.
Definition: ITensorPack.h:39
static float dequantize(QUANTIZED_TYPE value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 8-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
PixelValue constant_border_value
Constant value to use for constant border mode policy.
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
float calculate_resize_ratio(size_t input_size, size_t output_size, bool align_corners=false)
Returns resize ratio between input and output with consideration of aligned corners.
Definition: ScaleUtils.cpp:31
quantized, asymmetric fixed-point 8-bit number signed
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:40
static const std::vector< ScaleKernel > & get_available_kernels()
DataLayout data_layout
Data layout to use.
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
float delta_bilinear(float a00, float a01, float a10, float a11, float dx_val, float dy_val)
Computes bilinear interpolation using the top-left, top-right, bottom-left, bottom-right pixels and t...
Definition: ScaleHelpers.h:186
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
const char * name() const override
Name of the kernel.
signed 8-bit number
void fp32_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: fp32.cpp:89
void qasymm8_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, bool align_corners, const Window &window)
Definition: qasymm8.cpp:310
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
cpuinfo::CpuIsaInfo get_isa() const
Gets the current cpu&#39;s ISA information.
Definition: CPPTypes.cpp:124
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:159