Compute Library
 21.02
NEGaussianPyramidKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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 
27 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Types.h"
37 
38 #include <arm_neon.h>
39 #include <cstddef>
40 #include <cstdint>
41 #include <tuple>
42 
43 using namespace arm_compute;
44 
46  : _l2_load_offset(0)
47 {
48 }
49 
51 {
52  return BorderSize{ 0, 2 };
53 }
54 
56 {
59  ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1));
60 
61  for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
62  {
63  ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i));
64  }
65 
66  _input = input;
67  _output = output;
68 
69  // Configure kernel window
70  constexpr unsigned int num_elems_processed_per_iteration = 16;
71  constexpr unsigned int num_elems_read_per_iteration = 32;
72  constexpr unsigned int num_elems_written_per_iteration = 8;
73  const float scale_x = static_cast<float>(output->info()->dimension(0)) / input->info()->dimension(0);
74 
75  Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration));
76  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration, scale_x);
77 
78  // Sub sampling selects odd pixels (1, 3, 5, ...) for images with even
79  // width and even pixels (0, 2, 4, ...) for images with odd width. (Whether
80  // a pixel is even or odd is determined based on the tensor shape not the
81  // valid region!)
82  // Thus the offset from which the first pixel (L2) for the convolution is
83  // loaded depends on the anchor and shape of the valid region.
84  // In the case of an even shape (= even image width) we need to load L2
85  // from -2 if the anchor is odd and from -1 if the anchor is even. That
86  // makes sure that L2 is always loaded from an odd pixel.
87  // On the other hand, for an odd shape (= odd image width) we need to load
88  // L2 from -1 if the anchor is odd and from -2 if the anchor is even to
89  // achieve the opposite effect.
90  // The condition can be simplified to checking whether anchor + shape is
91  // odd (-2) or even (-1) as only adding an odd and an even number will have
92  // an odd result.
93  _l2_load_offset = -border_size().left;
94 
95  if((_input->info()->valid_region().anchor[0] + _input->info()->valid_region().shape[0]) % 2 == 0)
96  {
97  _l2_load_offset += 1;
98  }
99 
100  // Replace input access with static window
102  AccessWindowHorizontal(input->info(), _l2_load_offset, num_elems_read_per_iteration),
103  output_access);
104 
105  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
106 
107  INEKernel::configure(win);
108 }
109 
111 {
112  ARM_COMPUTE_UNUSED(info);
115  ARM_COMPUTE_ERROR_ON(window.x().step() % 2);
116 
117  static const int16x8_t six = vdupq_n_s16(6);
118  static const int16x8_t four = vdupq_n_s16(4);
119 
120  Window win_in(window);
121  win_in.shift(Window::DimX, _l2_load_offset);
122 
123  Iterator in(_input, win_in);
124 
125  // The output is half the width of the input
126  Window win_out(window);
127  win_out.scale(Window::DimX, 0.5f);
128 
129  Iterator out(_output, win_out);
130 
131  execute_window_loop(window, [&](const Coordinates &)
132  {
133  const uint8x16x2_t data_2q = vld2q_u8(in.ptr());
134  const uint8x16_t &data_even = data_2q.val[0];
135  const uint8x16_t &data_odd = data_2q.val[1];
136 
137  const int16x8_t data_l2 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data_even)));
138  const int16x8_t data_l1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data_odd)));
139  const int16x8_t data_m = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_even, data_even, 1))));
140  const int16x8_t data_r1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_odd, data_odd, 1))));
141  const int16x8_t data_r2 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_even, data_even, 2))));
142 
143  int16x8_t out_val = vaddq_s16(data_l2, data_r2);
144  out_val = vmlaq_s16(out_val, data_l1, four);
145  out_val = vmlaq_s16(out_val, data_m, six);
146  out_val = vmlaq_s16(out_val, data_r1, four);
147 
148  vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()), out_val);
149  },
150  in, out);
151 }
152 
154  : _t2_load_offset(0)
155 {
156 }
157 
159 {
160  return BorderSize{ 2, 0 };
161 }
162 
164 {
167  ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0));
168 
169  for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
170  {
171  ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i));
172  }
173 
174  _input = input;
175  _output = output;
176 
177  // Configure kernel window
178  constexpr unsigned int num_elems_processed_per_iteration = 16;
179  constexpr unsigned int num_rows_processed_per_iteration = 2;
180 
181  constexpr unsigned int num_elems_written_per_iteration = 16;
182  constexpr unsigned int num_rows_written_per_iteration = 1;
183 
184  constexpr unsigned int num_elems_read_per_iteration = 16;
185  constexpr unsigned int num_rows_read_per_iteration = 5;
186 
187  const float scale_y = static_cast<float>(output->info()->dimension(1)) / input->info()->dimension(1);
188 
189  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration, num_rows_processed_per_iteration));
190  AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration, 1.f, scale_y);
191 
192  // Determine whether we need to load even or odd rows. See above for a
193  // detailed explanation.
194  _t2_load_offset = -border_size().top;
195 
196  if((_input->info()->valid_region().anchor[1] + _input->info()->valid_region().shape[1]) % 2 == 0)
197  {
198  _t2_load_offset += 1;
199  }
200 
202  AccessWindowRectangle(input->info(), 0, _t2_load_offset, num_elems_read_per_iteration, num_rows_read_per_iteration),
203  output_access);
204 
205  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
206 
207  INEKernel::configure(win);
208 }
209 
211 {
212  ARM_COMPUTE_UNUSED(info);
215  ARM_COMPUTE_ERROR_ON(window.x().step() != 16);
216  ARM_COMPUTE_ERROR_ON(window.y().step() % 2);
217  ARM_COMPUTE_ERROR_ON(_input->buffer() == nullptr);
218 
219  static const uint16x8_t six = vdupq_n_u16(6);
220  static const uint16x8_t four = vdupq_n_u16(4);
221 
222  Window win_in(window);
223  // Need to load two times 8 values instead of 16 values once
224  win_in.set_dimension_step(Window::DimX, 8);
225  win_in.shift(Window::DimY, _t2_load_offset);
226 
227  Iterator in(_input, win_in);
228 
229  // Output's height is half of input's
230  Window win_out(window);
231  win_out.scale(Window::DimY, 0.5f);
232 
233  Iterator out(_output, win_out);
234 
235  const uint8_t *input_top2_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 0));
236  const uint8_t *input_top_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 1));
237  const uint8_t *input_mid_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 2));
238  const uint8_t *input_low_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 3));
239  const uint8_t *input_low2_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 4));
240 
241  execute_window_loop(window, [&](const Coordinates &)
242  {
243  // Low data
244  const uint16x8_t data_low_t2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + in.offset())));
245  const uint16x8_t data_low_t1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + in.offset())));
246  const uint16x8_t data_low_m = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + in.offset())));
247  const uint16x8_t data_low_b1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + in.offset())));
248  const uint16x8_t data_low_b2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + in.offset())));
249 
250  uint16x8_t out_low = vaddq_u16(data_low_t2, data_low_b2);
251  out_low = vmlaq_u16(out_low, data_low_t1, four);
252  out_low = vmlaq_u16(out_low, data_low_m, six);
253  out_low = vmlaq_u16(out_low, data_low_b1, four);
254 
256 
257  // High data
258  const uint16x8_t data_high_t2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + in.offset())));
259  const uint16x8_t data_high_t1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + in.offset())));
260  const uint16x8_t data_high_m = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + in.offset())));
261  const uint16x8_t data_high_b1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + in.offset())));
262  const uint16x8_t data_high_b2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + in.offset())));
263 
264  uint16x8_t out_high = vaddq_u16(data_high_t2, data_high_b2);
265  out_high = vmlaq_u16(out_high, data_high_t1, four);
266  out_high = vmlaq_u16(out_high, data_high_m, six);
267  out_high = vmlaq_u16(out_high, data_high_b1, four);
268 
269  vst1q_u8(out.ptr(), vcombine_u8(vqshrn_n_u16(out_low, 8), vqshrn_n_u16(out_high, 8)));
270  },
271  in, out);
272 }
unsigned int top
top of the border
Definition: Types.h:375
void scale(size_t dimension, float scale_value)
Scale the values of a given dimension by the given scale_value.
Definition: Window.inl:155
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void configure(const ITensor *input, ITensor *output)
Initialise the kernel&#39;s source, destination and border mode.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
Container for 2D border size.
Definition: Types.h:273
void increment(size_t dimension)
Increment the iterator along the specified dimension of the step value associated to the dimension...
Definition: Helpers.inl:122
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
void configure(const ITensor *input, ITensor *output)
Initialise the kernel&#39;s source, destination and border mode.
1 channel, 1 U8 per channel
#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
Interface for Neon tensor.
Definition: ITensor.h:36
Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
Implementation of a rectangular access pattern.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
BorderSize border_size() const override
The size of the border for that kernel.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
unsigned int left
left of the border
Definition: Types.h:378
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
BorderSize border_size() const override
The size of the border for that kernel.
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
unsigned int num_elems_processed_per_iteration
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
constexpr size_t offset() const
Return the offset in bytes from the first element to the current position of the iterator.
Definition: Helpers.inl:134
Container for valid region of a window.
Definition: Types.h:188
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145