Compute Library
 21.11
CpuScaleKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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 
29 #include "src/core/CPP/Validate.h"
38 #include "support/Rounding.h"
39 
40 #include <arm_neon.h>
41 #include <map>
42 
43 namespace arm_compute
44 {
45 namespace cpu
46 {
47 namespace kernels
48 {
49 namespace
50 {
51 struct ScaleSelectorData
52 {
54  const CPUInfo &ci;
55 };
57 using ScaleKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ITensor *, const ITensor *, const ITensor *,
58  InterpolationPolicy, BorderMode, PixelValue, float, bool, const Window &)>::type;
59 struct ScaleKernel
60 {
61  const char *name;
62  const ScaleSelectorPtr is_selected;
63  ScaleKernelPtr ukernel;
64 };
65 
66 static const ScaleKernel available_kernels[] =
67 {
68 #if defined(ARM_COMPUTE_ENABLE_SVE)
69  {
70  "sve_fp16_scale",
71  [](const ScaleSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); },
73  },
74  {
75  "sve_fp32_scale",
76  [](const ScaleSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); },
78  },
79  {
80  "sve_qu8_scale",
81  [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8 && data.ci.has_sve(); },
83  },
84  {
85  "sve_qs8_scale",
86  [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.ci.has_sve(); },
88  },
89  {
90  "sve_u8_scale",
91  [](const ScaleSelectorData & data) { return data.dt == DataType::U8 && data.ci.has_sve(); },
93  },
94  {
95  "sve_s16_scale",
96  [](const ScaleSelectorData & data) { return data.dt == DataType::S16 && data.ci.has_sve(); },
98  },
99 #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
100 #if defined(ARM_COMPUTE_ENABLE_NEON)
101 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
102  {
103  "neon_fp16_scale",
104  [](const ScaleSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_fp16(); },
105  REGISTER_FP16_NEON(arm_compute::cpu::common_neon_scale<float16_t>)
106  },
107 #endif /* !defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
108  {
109  "neon_fp32_scale",
110  [](const ScaleSelectorData & data) { return data.dt == DataType::F32; },
111  REGISTER_FP32_NEON(arm_compute::cpu::common_neon_scale<float>)
112  },
113  {
114  "neon_qu8_scale",
115  [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8; },
117  },
118  {
119  "neon_qs8_scale",
120  [](const ScaleSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
122  },
123  {
124  "neon_u8_scale",
125  [](const ScaleSelectorData & data) { return data.dt == DataType::U8; },
127  },
128  {
129  "neon_s16_scale",
130  [](const ScaleSelectorData & data) { return data.dt == DataType::S16; },
132  },
133 #endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
134 };
135 
136 /** Micro-kernel selector
137  *
138  * @param[in] data Selection data passed to help pick the appropriate micro-kernel
139  *
140  * @return A matching micro-kernel else nullptr
141  */
142 const ScaleKernel *get_implementation(const ScaleSelectorData &data)
143 {
144  for(const auto &uk : available_kernels)
145  {
146  if(uk.is_selected(data))
147  {
148  return &uk;
149  }
150  }
151  return nullptr;
152 }
153 
154 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy,
155  const ITensorInfo *offsets, ITensorInfo *dst, const ScaleKernelInfo &info)
156 {
157  const auto *uk = get_implementation(ScaleSelectorData{ src->data_type(), CPUInfo::get() });
158  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
159 
162  ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
163  ARM_COMPUTE_RETURN_ERROR_ON(info.sampling_policy != SamplingPolicy::CENTER && info.sampling_policy != SamplingPolicy::TOP_LEFT);
164  ARM_COMPUTE_UNUSED(info.constant_border_value);
165  ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.use_padding, "Padding is not supported");
166 
167  const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
168  const auto width_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
169  const auto height_index = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
170  const auto output_width = dst->dimension(width_index);
171  const auto output_height = dst->dimension(height_index);
172  ARM_COMPUTE_RETURN_ERROR_ON(output_width == 0);
173  ARM_COMPUTE_RETURN_ERROR_ON(output_height == 0);
174 
175  if(info.interpolation_policy == InterpolationPolicy::NEAREST_NEIGHBOR)
176  {
178  }
179 
180  if(info.interpolation_policy == InterpolationPolicy::BILINEAR)
181  {
183  if(dx != nullptr && dy != nullptr)
184  {
187  }
188  }
189 
190  ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
191 
192  if(info.interpolation_policy == InterpolationPolicy::AREA)
193  {
196  }
197 
198  return Status{};
199 }
200 } // namespace
201 
202 void CpuScaleKernel::configure(const ITensorInfo *src, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets,
204 {
205  ARM_COMPUTE_UNUSED(dx, dy, offsets);
207  // Perform validation step
208  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src,
209  dx,
210  dy,
211  offsets,
212  dst,
213  info));
214 
215  const auto *uk = get_implementation(ScaleSelectorData{ src->data_type(), CPUInfo::get() });
217 
218  _run_method = uk->ukernel;
219  _name = std::string("CpuScaleKernel").append("/").append(uk->name).append("_").append(string_from_interpolation_policy(info.interpolation_policy));
220 
221  // Get data layout and width/height indices
222  _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
225 
226  _policy = info.interpolation_policy;
227  _border_mode = info.border_mode;
228  _constant_border_value = info.constant_border_value;
229  _align_corners = info.align_corners;
230 
232  {
233  _sampling_offset = 0.5f;
234  }
235 
236  // Compute the ratio between source width/height and destination width/height
237  const auto wr = scale_utils::calculate_resize_ratio(src->dimension(idx_width), dst->dimension(idx_width), _align_corners);
238  const auto hr = scale_utils::calculate_resize_ratio(src->dimension(idx_height), dst->dimension(idx_height), _align_corners);
239 
240  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
241  _policy = (_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) ? InterpolationPolicy::NEAREST_NEIGHBOR : _policy;
242 
243  if(_border_mode == BorderMode::UNDEFINED)
244  {
245  _border_mode = BorderMode::CONSTANT;
246  _constant_border_value = PixelValue();
247  }
248 
249 #ifdef ENABLE_NCHW_KERNELS
250  // Configure scale function to run
251  if(_data_layout == DataLayout::NCHW)
252  {
253  std::string function_to_call("scale_");
254  function_to_call += string_from_data_type(src->data_type()) + "_";
255  function_to_call += string_from_data_layout(_data_layout) + "_";
256  function_to_call += string_from_interpolation_policy(_policy);
257 
258  static std::map<std::string, ScaleFunctionPtr> map_function =
259  {
260  { "scale_U8_NCHW_AREA_CONSTANT", &CpuScaleKernel::scale_area_nchw_u8 },
261 
262  { "scale_U8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<uint8_t> },
263  { "scale_U8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
264 
265  { "scale_QASYMM8_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<uint8_t> },
266  { "scale_QASYMM8_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<uint8_t> },
267 
268  { "scale_QASYMM8_SIGNED_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_qasymm<int8_t> },
269  { "scale_QASYMM8_SIGNED_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int8_t> },
270 
271  { "scale_S16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<int16_t> },
272  { "scale_S16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<int16_t> },
273 
274 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
275  { "scale_F16_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float16_t> },
276  { "scale_F16_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float16_t> },
277 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
278 
279  { "scale_F32_NCHW_BILINEAR", &CpuScaleKernel::scale_bilinear_nchw<float> },
280  { "scale_F32_NCHW_NEAREST_NEIGHBOUR", &CpuScaleKernel::scale_nearest_nchw<float> },
281  };
282  auto it = map_function.find(function_to_call);
283  if(it != map_function.end())
284  {
285  _func = it->second;
286  }
287  }
288 #endif // ENABLE_NCHW_KERNELS
289 
290  // Configure window
291  Window win = calculate_max_window(*dst, Steps());
292  ICpuKernel::configure(win);
293 }
294 
295 #ifdef ENABLE_NCHW_KERNELS
296 template <typename T>
297 void CpuScaleKernel::scale_nearest_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
298 {
299  ARM_COMPUTE_UNUSED(dx, dy);
300  const size_t in_stride_x = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
301 
302  // Compute the ratio between source height and destination height
303  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
304 
305  // Don't increment in X and Y direction for the input tensor
306  // A pointer to the start of this plane is needed as base for the precomputed offsets
307  Window win_in(window);
308  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
309  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
310 
311  // Set offsets window
312  Window win_off;
313  win_off.set(Window::DimX, window[Window::DimX]);
314  win_off.set(Window::DimY, window[Window::DimY]);
315  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
316  {
317  win_off.set(d, Window::Dimension(0, 0, 0));
318  }
319 
320  // Create iterators
321  Iterator src_i(src, win_in);
322  Iterator dst_i(dst, window);
323  Iterator offsets_i(offsets, win_off);
324  execute_window_loop(window, [&](const Coordinates & id)
325  {
326  const auto offsets_ptr = reinterpret_cast<const int32_t *>(offsets_i.ptr());
327  const auto in_yi = static_cast<int32_t>(_align_corners ? utils::rounding::round_half_away_from_zero((id.y() + _sampling_offset) * hr) : std::floor((
328  id.y() + _sampling_offset)
329  * hr));
330  const int32_t offset_row = in_yi * in_stride_x;
331  *reinterpret_cast<T *>(dst_i.ptr()) = *(reinterpret_cast<const T *>(src_i.ptr()) + offsets_ptr[0] + offset_row);
332  },
333  src_i, offsets_i, dst_i);
334 }
335 
336 template <typename T>
337 void CpuScaleKernel::scale_bilinear_nchw(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
338 {
339  // Compute the ratio between source height and destination height
340  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
341  Window win_off;
342  win_off.set(Window::DimX, window.x());
343  win_off.set(Window::DimY, window.y());
344 
345  // Don't increment in X and Y direction for the input tensor
346  // A pointer to the start of this plane is needed as base for the precomputed offsets
347  Window win_in(window);
348  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
349  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
350 
351  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
352  {
353  win_off.set(d, Window::Dimension(0, 0, 0));
354  }
355 
356  Iterator src_i(src, win_in);
357  Iterator dst_i(dst, window);
358  Iterator offsets_i(offsets, win_off);
359  Iterator dx_i(dx, win_off);
360  Iterator dy_i(dy, win_off);
361 
362  const int32_t in_dim_w = src->info()->dimension(0);
363  const int32_t in_dim_h = src->info()->dimension(1);
364  const int32_t in_stride_w = in_dim_w + src->info()->padding().left + src->info()->padding().right;
365 
366  if(_border_mode == BorderMode::CONSTANT)
367  {
368 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
369  using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
370 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
371  using ConstType = T;
372 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
373  const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
374  execute_window_loop(window, [&](const Coordinates & id)
375  {
376  const int32_t index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
377  const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
378  const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
379  const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
380  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
381 
382  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;
383  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;
384  const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h
385  && index_h < in_dim_h - 1) ?
386  (*(pixel_row_ptr + index_w + index_h * in_stride_w + in_stride_w)) :
387  const_border_value;
388  const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h
389  && index_h < in_dim_h - 1) ?
390  (*(pixel_row_ptr + index_w + 1 + index_h * in_stride_w + in_stride_w)) :
391  const_border_value;
392 
393  *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
394  },
395  src_i, offsets_i, dx_i, dy_i, dst_i);
396  }
397  else if(_border_mode == BorderMode::REPLICATE)
398  {
399  execute_window_loop(window, [&](const Coordinates & id)
400  {
401  const int index_h = std::floor((id.y() + _sampling_offset) * hr - _sampling_offset);
402  const auto index_w = *(reinterpret_cast<const int32_t *>(offsets_i.ptr()));
403  const auto dx_val = *(reinterpret_cast<const float *>(dx_i.ptr()));
404  const auto dy_val = *(reinterpret_cast<const float *>(dy_i.ptr()));
405  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
406 
407  auto clamped_x = utility::clamp<int>(index_w, 0, in_dim_w - 1);
408  auto clamped_x1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
409  auto clamped_y = utility::clamp<int>(index_h, 0, in_dim_h - 1);
410  auto clamped_y1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
411 
412  const auto a00 = *(pixel_row_ptr + clamped_x + clamped_y * in_stride_w);
413  const auto a01 = *(pixel_row_ptr + clamped_x1 + clamped_y * in_stride_w);
414  const auto a10 = *(pixel_row_ptr + clamped_x + clamped_y1 * in_stride_w);
415  const auto a11 = *(pixel_row_ptr + clamped_x1 + clamped_y1 * in_stride_w);
416 
417  *reinterpret_cast<T *>(dst_i.ptr()) = static_cast<T>(scale_helpers::delta_bilinear(a00, a01, a10, a11, dx_val, dy_val));
418  },
419  src_i, offsets_i, dx_i, dy_i, dst_i);
420  }
421  else
422  {
423  ARM_COMPUTE_ERROR("Not implemented");
424  }
425 }
426 
427 void CpuScaleKernel::scale_area_nchw_u8(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
428 {
429  ARM_COMPUTE_UNUSED(dx, dy, offsets);
430  using namespace scale_helpers;
431 
433 
434  // Don't increment in width/height/channels for the input tensor
435  // A pointer to the start of this plane is needed as base for the precomputed offsets
436  Window win_in(window);
437  win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
438  win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
439  win_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
440 
441  Iterator src_i(src, win_in);
442  Iterator dst_i(dst, window);
443 
444  const auto wr = scale_utils::calculate_resize_ratio(src->info()->dimension(0), dst->info()->dimension(0), _align_corners);
445  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), _align_corners);
446  const auto w = src->info()->dimension(0);
447  const auto h = src->info()->dimension(1);
448  const size_t in_stride = src->info()->strides_in_bytes()[1];
449 
450  execute_window_loop(window, [&](const Coordinates & id)
451  {
452  const auto in_ptr = reinterpret_cast<const uint8_t *>(src_i.ptr());
453 
454  uint8x8_t tmp0 = vdup_n_u8(0);
455  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x(), id.y()), tmp0, 0);
456  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 1, id.y()), tmp0, 1);
457  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 2, id.y()), tmp0, 2);
458  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 3, id.y()), tmp0, 3);
459  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 4, id.y()), tmp0, 4);
460  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 5, id.y()), tmp0, 5);
461  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 6, id.y()), tmp0, 6);
462  tmp0 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 7, id.y()), tmp0, 7);
463 
464  uint8x8_t tmp1 = vdup_n_u8(0);
465  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 8, id.y()), tmp1, 0);
466  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 9, id.y()), tmp1, 1);
467  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 10, id.y()), tmp1, 2);
468  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 11, id.y()), tmp1, 3);
469  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 12, id.y()), tmp1, 4);
470  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 13, id.y()), tmp1, 5);
471  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 14, id.y()), tmp1, 6);
472  tmp1 = vset_lane_u8(pixel_area_c1u8_clamp(in_ptr, in_stride, w, h, wr, hr, id.x() + 15, id.y()), tmp1, 7);
473 
474  vst1q_u8(dst_i.ptr(), vcombine_u8(tmp0, tmp1));
475  },
476  src_i, dst_i);
477 }
478 
479 template <typename T>
480 void CpuScaleKernel::scale_bilinear_qasymm(const ITensor *src, ITensor *dst, const ITensor *dx, const ITensor *dy, const ITensor *offsets, const Window &window)
481 {
482  // Get data layout and width/height indices
485 
486  // Compute the ratio between source height and destination height
487  const auto hr = scale_utils::calculate_resize_ratio(src->info()->dimension(idx_height), dst->info()->dimension(idx_height), _align_corners);
488  Window win_off;
489  win_off.set(Window::DimX, Window::Dimension(0, 0, 0));
490  win_off.set(Window::DimY, Window::Dimension(0, 0, 0));
491 
492  // Don't increment in X and Y direction for the input tensor
493  // A pointer to the start of this plane is needed as base for the precomputed offsets
494  Window win_in(window);
495  win_in.set(idx_width, Window::Dimension(0, 0, 0));
496  win_in.set(idx_height, Window::Dimension(0, 0, 0));
497 
498  for(size_t d = Window::DimZ; d < offsets->info()->num_dimensions(); ++d)
499  {
500  win_off.set(d, Window::Dimension(0, 0, 0));
501  }
502 
503  Iterator src_i(src, win_in);
504  Iterator dst_i(dst, window);
505 
506  const int32_t in_dim_w = src->info()->dimension(idx_width);
507  const int32_t in_dim_h = src->info()->dimension(idx_height);
508  const int32_t stride_w = src->info()->strides_in_bytes()[idx_width];
509  const int32_t stride_h = src->info()->strides_in_bytes()[idx_height];
510 
511  const UniformQuantizationInfo iq_info = src->info()->quantization_info().uniform();
512  const UniformQuantizationInfo oq_info = dst->info()->quantization_info().uniform();
513 
514  if(_border_mode == BorderMode::CONSTANT)
515  {
516 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
517  using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type;
518 #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
519  using ConstType = T;
520 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
521  const T const_border_value = static_cast<T>(_constant_border_value.get<ConstType>());
522  execute_window_loop(window, [&](const Coordinates & id)
523  {
524  const int32_t index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
525  const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
526  const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
527  const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
528  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
529 
530  const auto a00 = (0 <= index_w && index_w < in_dim_w && 0 <= index_h && index_h < in_dim_h) ?
531  (*(pixel_row_ptr + index_w * stride_w + index_h * stride_h)) :
532  const_border_value;
533  const auto a01 = (-1 <= index_w && index_w < in_dim_w - 1 && 0 <= index_h && index_h < in_dim_h) ?
534  (*(pixel_row_ptr + (index_w + 1) * stride_w + index_h * stride_h)) :
535  const_border_value;
536  const auto a10 = (0 <= index_w && index_w < in_dim_w && -1 <= index_h && index_h < in_dim_h - 1) ?
537  (*(pixel_row_ptr + index_w * stride_w + (index_h + 1) * stride_h)) :
538  const_border_value;
539  const auto a11 = (-1 <= index_w && index_w < in_dim_w - 1 && -1 <= index_h && index_h < in_dim_h - 1) ?
540  (*(pixel_row_ptr + (index_w + 1) * stride_w + (index_h + 1) * stride_h)) :
541  const_border_value;
542 
543  const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
544  const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
545  const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
546  const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
547  *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
548  },
549  src_i, dst_i);
550  }
551  else if(_border_mode == BorderMode::REPLICATE)
552  {
553  execute_window_loop(window, [&](const Coordinates & id)
554  {
555  const int index_h = std::floor((id[idx_height] + _sampling_offset) * hr - _sampling_offset);
556  const int32_t index_w = *(reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
557  const auto dx_val = *(reinterpret_cast<const float *>(dx->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
558  const auto dy_val = *(reinterpret_cast<const float *>(dy->ptr_to_element(Coordinates(id[idx_width], id[idx_height]))));
559  const auto pixel_row_ptr = reinterpret_cast<const T *>(src_i.ptr());
560 
561  auto clamped_w = utility::clamp<int>(index_w, 0, in_dim_w - 1);
562  auto clamped_w1 = utility::clamp<int>(index_w + 1, 0, in_dim_w - 1);
563  auto clamped_h = utility::clamp<int>(index_h, 0, in_dim_h - 1);
564  auto clamped_h1 = utility::clamp<int>(index_h + 1, 0, in_dim_h - 1);
565 
566  const auto a00 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h * stride_h);
567  const auto a01 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h * stride_h);
568  const auto a10 = *(pixel_row_ptr + clamped_w * stride_w + clamped_h1 * stride_h);
569  const auto a11 = *(pixel_row_ptr + clamped_w1 * stride_w + clamped_h1 * stride_h);
570 
571  const float inp00 = Qasymm8QuantizationHelper<T>::dequantize(a00, iq_info);
572  const float inp01 = Qasymm8QuantizationHelper<T>::dequantize(a01, iq_info);
573  const float inp10 = Qasymm8QuantizationHelper<T>::dequantize(a10, iq_info);
574  const float inp11 = Qasymm8QuantizationHelper<T>::dequantize(a11, iq_info);
575  *reinterpret_cast<T *>(dst_i.ptr()) = Qasymm8QuantizationHelper<T>::quantize(scale_helpers::delta_bilinear(inp00, inp01, inp10, inp11, dx_val, dy_val), oq_info);
576  },
577  src_i, dst_i);
578  }
579  else
580  {
581  ARM_COMPUTE_ERROR("Not implemented");
582  }
583 }
584 #endif // ENABLE_NCHW_KERNELS
585 
587  const ITensorInfo *offsets, ITensorInfo *output, const ScaleKernelInfo &info)
588 {
589  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, dx, dy, offsets, output, info));
590  return Status{};
591 }
592 
593 void CpuScaleKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
594 {
595  ARM_COMPUTE_UNUSED(info);
598  ARM_COMPUTE_ERROR_ON(_func == nullptr && _data_layout == DataLayout::NCHW);
599  ARM_COMPUTE_ERROR_ON(_run_method == nullptr && _data_layout == DataLayout::NHWC);
600 
601  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
602  auto dst = tensors.get_tensor(TensorType::ACL_DST);
603  const auto dx = tensors.get_const_tensor(TensorType::ACL_INT_0);
604  const auto dy = tensors.get_const_tensor(TensorType::ACL_INT_1);
605  const auto offsets = tensors.get_const_tensor(TensorType::ACL_INT_2);
606 
607  if(_data_layout == DataLayout::NCHW)
608  {
609  (this->*_func)(src, dst, dx, dy, offsets, window);
610  }
611  else
612  {
613  _run_method(src, dst, offsets, dx, dy, _policy, _border_mode, _constant_border_value, _sampling_offset, _align_corners, window);
614  }
615 }
616 
617 const char *CpuScaleKernel::name() const
618 {
619  return _name.c_str();
620 }
621 } // namespace kernels
622 } // namespace cpu
623 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:261
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)
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)
InterpolationPolicy
Interpolation method.
Definition: Types.h:398
SimpleTensor< float > w
Definition: DFT.cpp:156
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
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)
InterpolationPolicy interpolation_policy
Interpolation type to use.
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:42
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
1 channel, 1 U8 per channel
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:61
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)
#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:62
Output values are defined by bilinear interpolation between the pixels.
#define REGISTER_QASYMM8_SVE(func_name)
Definition: Registrars.h:91
#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:76
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:77
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...
#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-2021 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
DataType dt
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:52
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:124
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
#define REGISTER_QASYMM8_SIGNED_SVE(func_name)
Definition: Registrars.h:77
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
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
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)
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:90
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:187
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)
UniformQuantizationInfo uniform() const
Return per layer quantization info.
#define REGISTER_INTEGER_SVE(func_name)
Definition: Registrars.h:125
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
ScaleKernelPtr ukernel
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:380
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:378
#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:265
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:786
const CPUInfo & ci
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:158
const ScaleSelectorPtr is_selected
#define REGISTER_FP16_SVE(func_name)
Definition: Registrars.h:43
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:154
#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:279
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:27
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
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:39
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
DataType
Available data types.
Definition: Types.h:79
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
const char * name() const override
Name of the kernel.
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:131
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
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:145
const char * name