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