Compute Library
 21.02
NEBox3x3Kernel.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 
34 
35 #include <arm_neon.h>
36 
37 using namespace arm_compute;
38 
39 int16x8_t calculate_kernel(const uint8x16_t &top_data, const uint8x16_t &mid_data, const uint8x16_t &bot_data)
40 {
41  const int16x8x2_t top_s16 =
42  {
43  {
44  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
45  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
46  }
47  };
48  const int16x8x2_t mid_s16 =
49  {
50  {
51  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
52  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
53  }
54  };
55  const int16x8x2_t bot_s16 =
56  {
57  {
58  vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
59  vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
60  }
61  };
62 
63  //top left
64  int16x8_t out = top_s16.val[0];
65  //top mid
66  out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1));
67  //top right
68  out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
69  //mid left
70  out = vaddq_s16(out, mid_s16.val[0]);
71  //mid mid
72  out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1));
73  //mid right
74  out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2));
75  //bot left
76  out = vaddq_s16(out, bot_s16.val[0]);
77  //bot mid
78  out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1));
79  //bot right
80  out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
81  return out;
82 }
83 
84 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
85 void NEBox3x3FP16Kernel::run(const Window &window, const ThreadInfo &info)
86 {
87  ARM_COMPUTE_UNUSED(info);
90 
91  Iterator input(_input, window);
92  Iterator output(_output, window);
93 
94  unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
95  unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
96  unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
97 
98  const float16x8_t oneovernine = vdupq_n_f16(1.0f / 9.0f);
99 
100  execute_window_loop(window, [&](const Coordinates &)
101  {
102  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
103  const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
104  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
105 
106  int16x8_t out = calculate_kernel(top_data, mid_data, bot_data);
107 
108  float16x8_t outfloat = vcvtq_f16_s16(out);
109  outfloat = vmulq_f16(outfloat, oneovernine);
110 
111  vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(outfloat)));
112  },
113  input, output);
114 }
115 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
116 
118 {
119  return BorderSize(1);
120 }
121 
122 void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
123 {
124  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
125 
126  set_shape_if_empty(*output->info(), input->info()->tensor_shape());
127 
130 
135 
136  _input = input;
137  _output = output;
138 
139  constexpr unsigned int num_elems_processed_per_iteration = 8;
140  constexpr unsigned int num_elems_read_per_iteration = 16;
141  constexpr unsigned int num_elems_written_per_iteration = 8;
142  constexpr unsigned int num_rows_read_per_iteration = 3;
143  constexpr int rect_offset_xy = -1;
144 
145  // Configure kernel window
146  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
147  AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
148 
149  update_window_and_padding(win, AccessWindowRectangle(input->info(), rect_offset_xy, rect_offset_xy, num_elems_read_per_iteration, num_rows_read_per_iteration), output_access);
150 
151  output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
152 
153  INEKernel::configure(win);
154 }
155 
156 void NEBox3x3Kernel::run(const Window &window, const ThreadInfo &info)
157 {
158  ARM_COMPUTE_UNUSED(info);
161 
162  Iterator input(_input, window);
163  Iterator output(_output, window);
164 
165  unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
166  unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
167  unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
168 
169  const int shift = 19;
170  int value = (1 << shift) / 9 + 1; //58255 / (2^19) ~= 1/9
171  const int32x4_t oneovernine = vdupq_n_s32(value);
172 
173  execute_window_loop(window, [&](const Coordinates &)
174  {
175  const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
176  const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
177  const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
178 
179  int16x8_t out = calculate_kernel(top_data, mid_data, bot_data);
180 
181  int32x4_t outfloathigh = vmovl_s16(vget_high_s16(out));
182  int32x4_t outfloatlow = vmovl_s16(vget_low_s16(out));
183 
184  outfloathigh = vmulq_s32(outfloathigh, oneovernine);
185  outfloatlow = vmulq_s32(outfloatlow, oneovernine);
186  outfloathigh = vshrq_n_s32(outfloathigh, shift);
187  outfloatlow = vshrq_n_s32(outfloatlow, shift);
188  out = vcombine_s16(vqmovn_s32((outfloatlow)),
189  vqmovn_s32((outfloathigh)));
190 
191  vst1_u8(output.ptr(), vqmovun_s16(out));
192  },
193  input, output);
194 }
bool set_format_if_unknown(ITensorInfo &info, Format format)
Set the format, data type and number of channels to the specified value if the current data type is u...
int16x8_t calculate_kernel(const uint8x16_t &top_data, const uint8x16_t &mid_data, const uint8x16_t &bot_data)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
uint16x8_t vcvtq_f16_s16(float16x8_t)
Definition: clang-tidy.h:118
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
float16x8_t vmulq_f16(float16x8_t, float16x8_t)
Definition: clang-tidy.h:78
Container for 2D border size.
Definition: Types.h:273
1 channel, 1 U8 per channel
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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.
void configure(const ITensor *input, ITensor *output, bool border_undefined)
Set the source, destination and border mode of the kernel.
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
#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.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:441
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
unsigned int num_elems_processed_per_iteration
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
int16x8_t vcvtq_s16_f16(float16x8_t)
Definition: clang-tidy.h:63
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
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
BorderSize border_size() const override
The size of the border for that kernel.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205