Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEDerivativeKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 46 | NEDerivativeKernel::NEDerivativeKernel() |
| 47 | : _func(nullptr), _input(nullptr), _output_x(nullptr), _output_y(nullptr) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | BorderSize NEDerivativeKernel::border_size() const |
| 52 | { |
| 53 | return BorderSize(1); |
| 54 | } |
| 55 | |
| 56 | void NEDerivativeKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 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 | { |
| 66 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16); |
| 67 | } |
| 68 | |
| 69 | if(run_der_y) |
| 70 | { |
| 71 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16); |
| 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); |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 85 | |
Michele Di Giorgio | 29258e8 | 2018-08-13 16:49:30 +0100 | [diff] [blame] | 86 | // TODO(COMPMID-1503) Fix x-access input bug in NEON kernel instead of '+2' |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 87 | AccessWindowHorizontal in_x_access(input->info(), -border_size().left, num_elems_processed_per_iteration + 2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | AccessWindowRectangle in_y_access(input->info(), 0, -border_size().left, num_elems_processed_per_iteration, num_rows_read_per_iteration); |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 89 | |
Michele Di Giorgio | 29258e8 | 2018-08-13 16:49:30 +0100 | [diff] [blame] | 90 | // TODO(COMPMID-1503) Fix x-access input bug in NEON kernel instead of '+2' |
John Richardson | 384a43e | 2017-12-14 15:41:13 +0000 | [diff] [blame] | 91 | AccessWindowRectangle in_xy_access(input->info(), -border_size().left, -border_size().top, num_elems_processed_per_iteration + 2, num_rows_read_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 */ |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 129 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 */ |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 158 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 */ |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 188 | execute_window_loop(window, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 223 | void NEDerivativeKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 225 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 226 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 227 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 228 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 229 | |
| 230 | (this->*_func)(window); |
| 231 | } |