Compute Library
 21.02
NEDerivativeKernel.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 
26 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Types.h"
34 
35 #include <arm_neon.h>
36 #include <cstddef>
37 #include <cstdint>
38 
39 using namespace arm_compute;
40 
41 namespace arm_compute
42 {
43 class Coordinates;
44 } // namespace arm_compute
45 
47  : _func(nullptr), _input(nullptr), _output_x(nullptr), _output_y(nullptr)
48 {
49 }
50 
52 {
53  return BorderSize(1);
54 }
55 
56 void NEDerivativeKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
57 {
59  ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
60 
61  const bool run_der_x = output_x != nullptr;
62  const bool run_der_y = output_y != nullptr;
63 
64  if(run_der_x)
65  {
67  }
68 
69  if(run_der_y)
70  {
72  }
73 
74  _input = input;
75  _output_x = output_x;
76  _output_y = output_y;
77 
78  constexpr unsigned int num_elems_processed_per_iteration = 16;
79  constexpr unsigned int num_rows_read_per_iteration = 3;
80 
81  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
82 
83  AccessWindowHorizontal out_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_processed_per_iteration);
84  AccessWindowHorizontal out_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_processed_per_iteration);
85 
86  // TODO(COMPMID-1503) Fix x-access input bug in Neon kernel instead of '+2'
87  AccessWindowHorizontal in_x_access(input->info(), -border_size().left, num_elems_processed_per_iteration + 2);
88  AccessWindowRectangle in_y_access(input->info(), 0, -border_size().left, num_elems_processed_per_iteration, num_rows_read_per_iteration);
89 
90  // TODO(COMPMID-1503) Fix x-access input bug in Neon kernel instead of '+2'
91  AccessWindowRectangle in_xy_access(input->info(), -border_size().left, -border_size().top, num_elems_processed_per_iteration + 2, num_rows_read_per_iteration);
92 
93  if(run_der_x && run_der_y)
94  {
95  _func = &NEDerivativeKernel::derivative_xy;
96  update_window_and_padding(win, in_xy_access, out_x_access, out_y_access);
97  out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
98  out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
99  }
100  else
101  {
102  if(run_der_x)
103  {
104  _func = &NEDerivativeKernel::derivative_x;
105  update_window_and_padding(win, in_x_access, out_x_access);
106  out_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
107  }
108  else if(run_der_y)
109  {
110  _func = &NEDerivativeKernel::derivative_y;
111  update_window_and_padding(win, in_y_access, out_y_access);
112  out_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
113  }
114  else
115  {
116  ARM_COMPUTE_ERROR("At least one output must be NOT NULL");
117  }
118  }
119 
120  INEKernel::configure(win);
121 }
122 
123 void NEDerivativeKernel::derivative_x(const Window &window)
124 {
125  Iterator in(_input, window);
126  Iterator out_x(_output_x, window);
127 
128  /* Apply 1-D centered point discrete derivative mask ([-1 0 1]) along the X direction */
129  execute_window_loop(window, [&](const Coordinates &)
130  {
131  /* Load left and right data */
132  const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
133  const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
134 
135  /* Cast to int16 and perform the subtraction between the right and left data */
136  const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
137  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
138 
139  /* Cast to int16 and perform the subtraction between the right and left data */
140  const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
141  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
142 
143  /* Store result of derivative along the X direction */
144  vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out0);
145  vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out1);
146  },
147  in, out_x);
148 }
149 
150 void NEDerivativeKernel::derivative_y(const Window &window)
151 {
152  Iterator in(_input, window);
153  Iterator out_y(_output_y, window);
154 
155  const size_t stride = _input->info()->strides_in_bytes()[1];
156 
157  /* Apply 1-D centered point discrete derivative mask ([-1 0 1]^T) along the Y direction */
158  execute_window_loop(window, [&](const Coordinates &)
159  {
160  /* Load top and bottom data */
161  const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
162  const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
163 
164  /* Cast to int16 and perform the subtraction between the bottom and top data */
165  const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
166  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
167 
168  /* Cast to int16 and perform the subtraction between the bottom and top data */
169  const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
170  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
171 
172  /* Store result of derivative along the Y direction */
173  vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
174  vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
175  },
176  in, out_y);
177 }
178 
179 void NEDerivativeKernel::derivative_xy(const Window &window)
180 {
181  Iterator in(_input, window);
182  Iterator out_x(_output_x, window);
183  Iterator out_y(_output_y, window);
184 
185  const size_t stride = _input->info()->strides_in_bytes()[1];
186 
187  /* Apply 1-D centered point discrete derivative masks ([-1 0 1] and [-1 0 1]^T) along the X and Y directions */
188  execute_window_loop(window, [&](const Coordinates &)
189  {
190  /* Load top, bottom, left and right data */
191  const uint8x16_t t_data = vld1q_u8(in.ptr() - stride);
192  const uint8x16_t b_data = vld1q_u8(in.ptr() + stride);
193  const uint8x16_t l_data = vld1q_u8(in.ptr() - 1);
194  const uint8x16_t r_data = vld1q_u8(in.ptr() + 1);
195 
196  /* Cast to int16 and perform the subtraction between the bottom and top data */
197  const int16x8_t out0 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b_data))),
198  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(t_data))));
199 
200  /* Cast to int16 and perform the subtraction between the bottom and top data */
201  const int16x8_t out1 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b_data))),
202  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(t_data))));
203 
204  /* Cast to int16 and perform the subtraction between the right and left data */
205  const int16x8_t out2 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(r_data))),
206  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(l_data))));
207 
208  /* Cast to int16 and perform the subtraction between the right and left data */
209  const int16x8_t out3 = vsubq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(r_data))),
210  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(l_data))));
211 
212  /* Store result of derivative along the Y direction */
213  vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()), out0);
214  vst1q_s16(reinterpret_cast<int16_t *>(out_y.ptr()) + 8, out1);
215 
216  /* Store result of derivative along the X direction */
217  vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()), out2);
218  vst1q_s16(reinterpret_cast<int16_t *>(out_x.ptr()) + 8, out3);
219  },
220  in, out_x, out_y);
221 }
222 
223 void NEDerivativeKernel::run(const Window &window, const ThreadInfo &info)
224 {
225  ARM_COMPUTE_UNUSED(info);
228  ARM_COMPUTE_ERROR_ON(_func == nullptr);
229 
230  (this->*_func)(window);
231 }
unsigned int top
top of the border
Definition: Types.h:375
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
Container for 2D border size.
Definition: Types.h:273
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
BorderSize border_size() const override
The size of the border for that kernel.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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
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
void configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
Initialise the kernel&#39;s sources, destination and border.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
NEDerivativeKernel()
Default constructor.
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
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
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