Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEGaussianPyramidKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/INEKernel.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | #include <tuple> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | NEGaussianPyramidHorKernel::NEGaussianPyramidHorKernel() |
| 44 | : _border_size(0), _l2_load_offset(0) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | BorderSize NEGaussianPyramidHorKernel::border_size() const |
| 49 | { |
| 50 | return _border_size; |
| 51 | } |
| 52 | |
| 53 | void NEGaussianPyramidHorKernel::configure(const ITensor *input, ITensor *output, bool border_undefined) |
| 54 | { |
| 55 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 56 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S16); |
| 57 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != 2 * output->info()->dimension(0)); |
| 58 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1)); |
| 59 | |
| 60 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 61 | { |
| 62 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 63 | } |
| 64 | |
| 65 | _input = input; |
| 66 | _output = output; |
| 67 | _border_size = BorderSize(border_undefined ? 0 : 2, 2); |
| 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 | constexpr float scale_x = 0.5f; |
| 74 | |
| 75 | Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size()); |
| 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 | update_window_and_padding(win, |
| 101 | AccessWindowHorizontal(input->info(), _l2_load_offset, num_elems_read_per_iteration), |
| 102 | output_access); |
| 103 | |
| 104 | ValidRegion valid_region = input->info()->valid_region(); |
| 105 | valid_region.anchor.set(0, std::ceil((valid_region.anchor[0] + (border_undefined ? border_size().left : 0)) / 2.f)); |
| 106 | valid_region.shape.set(0, (valid_region.shape[0] - (border_undefined ? border_size().right : 0)) / 2 - valid_region.anchor[0]); |
| 107 | |
| 108 | output_access.set_valid_region(win, valid_region); |
| 109 | |
| 110 | INEKernel::configure(win); |
| 111 | } |
| 112 | |
| 113 | void NEGaussianPyramidHorKernel::run(const Window &window) |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 116 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 117 | ARM_COMPUTE_ERROR_ON(window.x().step() % 2); |
| 118 | |
| 119 | static const int16x8_t six = vdupq_n_s16(6); |
| 120 | static const int16x8_t four = vdupq_n_s16(4); |
| 121 | |
| 122 | Window win_in(window); |
| 123 | win_in.shift(Window::DimX, _l2_load_offset); |
| 124 | |
| 125 | Iterator in(_input, win_in); |
| 126 | |
| 127 | // The output is half the width of the input |
| 128 | Window win_out(window); |
| 129 | win_out.scale(Window::DimX, 0.5f); |
| 130 | |
| 131 | Iterator out(_output, win_out); |
| 132 | |
| 133 | execute_window_loop(window, [&](const Coordinates & id) |
| 134 | { |
| 135 | const uint8x16x2_t data_2q = vld2q_u8(in.ptr()); |
| 136 | const uint8x16_t &data_even = data_2q.val[0]; |
| 137 | const uint8x16_t &data_odd = data_2q.val[1]; |
| 138 | |
| 139 | const int16x8_t data_l2 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data_even))); |
| 140 | const int16x8_t data_l1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data_odd))); |
| 141 | const int16x8_t data_m = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_even, data_even, 1)))); |
| 142 | const int16x8_t data_r1 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_odd, data_odd, 1)))); |
| 143 | const int16x8_t data_r2 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vextq_u8(data_even, data_even, 2)))); |
| 144 | |
| 145 | int16x8_t out_val = vaddq_s16(data_l2, data_r2); |
| 146 | out_val = vmlaq_s16(out_val, data_l1, four); |
| 147 | out_val = vmlaq_s16(out_val, data_m, six); |
| 148 | out_val = vmlaq_s16(out_val, data_r1, four); |
| 149 | |
| 150 | vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()), out_val); |
| 151 | }, |
| 152 | in, out); |
| 153 | } |
| 154 | |
| 155 | NEGaussianPyramidVertKernel::NEGaussianPyramidVertKernel() |
| 156 | : _t2_load_offset(0) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | BorderSize NEGaussianPyramidVertKernel::border_size() const |
| 161 | { |
| 162 | return BorderSize(2, 0); |
| 163 | } |
| 164 | |
| 165 | void NEGaussianPyramidVertKernel::configure(const ITensor *input, ITensor *output, bool border_undefined) |
| 166 | { |
| 167 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16); |
| 168 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 169 | |
| 170 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0)); |
| 171 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != 2 * output->info()->dimension(1)); |
| 172 | |
| 173 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 174 | { |
| 175 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 176 | } |
| 177 | |
| 178 | _input = input; |
| 179 | _output = output; |
| 180 | |
| 181 | // Configure kernel window |
| 182 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 183 | constexpr unsigned int num_rows_processed_per_iteration = 2; |
| 184 | |
| 185 | constexpr unsigned int num_elems_written_per_iteration = 16; |
| 186 | constexpr unsigned int num_rows_written_per_iteration = 1; |
| 187 | |
| 188 | constexpr unsigned int num_elems_read_per_iteration = 16; |
| 189 | constexpr unsigned int num_rows_read_per_iteration = 5; |
| 190 | |
| 191 | constexpr float scale_y = 0.5f; |
| 192 | |
| 193 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration, num_rows_processed_per_iteration), border_undefined, border_size()); |
| 194 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_written_per_iteration, 1.f, scale_y); |
| 195 | |
| 196 | // Determine whether we need to load even or odd rows. See above for a |
| 197 | // detailed explanation. |
| 198 | _t2_load_offset = -border_size().top; |
| 199 | |
| 200 | if((_input->info()->valid_region().anchor[1] + _input->info()->valid_region().shape[1]) % 2 == 0) |
| 201 | { |
| 202 | _t2_load_offset += 1; |
| 203 | } |
| 204 | |
| 205 | update_window_and_padding(win, |
| 206 | AccessWindowRectangle(input->info(), 0, _t2_load_offset, num_elems_read_per_iteration, num_rows_read_per_iteration), |
| 207 | output_access); |
| 208 | |
| 209 | ValidRegion valid_region = input->info()->valid_region(); |
| 210 | valid_region.anchor.set(1, std::ceil((valid_region.anchor[1] + (border_undefined ? border_size().top : 0)) / 2.f)); |
| 211 | valid_region.shape.set(1, (valid_region.shape[1] - (border_undefined ? border_size().bottom : 0)) / 2 - valid_region.anchor[1]); |
| 212 | |
| 213 | output_access.set_valid_region(win, valid_region); |
| 214 | |
| 215 | INEKernel::configure(win); |
| 216 | } |
| 217 | |
| 218 | void NEGaussianPyramidVertKernel::run(const Window &window) |
| 219 | { |
| 220 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 221 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 222 | ARM_COMPUTE_ERROR_ON(window.x().step() != 16); |
| 223 | ARM_COMPUTE_ERROR_ON(window.y().step() % 2); |
| 224 | ARM_COMPUTE_ERROR_ON(_input->buffer() == nullptr); |
| 225 | |
| 226 | static const uint16x8_t six = vdupq_n_u16(6); |
| 227 | static const uint16x8_t four = vdupq_n_u16(4); |
| 228 | |
| 229 | Window win_in(window); |
| 230 | // Need to load two times 8 values instead of 16 values once |
| 231 | win_in.set_dimension_step(Window::DimX, 8); |
| 232 | win_in.shift(Window::DimY, _t2_load_offset); |
| 233 | |
| 234 | Iterator in(_input, win_in); |
| 235 | |
| 236 | // Output's height is half of input's |
| 237 | Window win_out(window); |
| 238 | win_out.scale(Window::DimY, 0.5f); |
| 239 | |
| 240 | Iterator out(_output, win_out); |
| 241 | |
| 242 | const uint8_t *input_top2_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 0)); |
| 243 | const uint8_t *input_top_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 1)); |
| 244 | const uint8_t *input_mid_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 2)); |
| 245 | const uint8_t *input_low_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 3)); |
| 246 | const uint8_t *input_low2_ptr = _input->buffer() + _input->info()->offset_element_in_bytes(Coordinates(0, 4)); |
| 247 | |
| 248 | execute_window_loop(window, [&](const Coordinates & id) |
| 249 | { |
| 250 | // Low data |
| 251 | const uint16x8_t data_low_t2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + in.offset()))); |
| 252 | const uint16x8_t data_low_t1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + in.offset()))); |
| 253 | const uint16x8_t data_low_m = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + in.offset()))); |
| 254 | const uint16x8_t data_low_b1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + in.offset()))); |
| 255 | const uint16x8_t data_low_b2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + in.offset()))); |
| 256 | |
| 257 | uint16x8_t out_low = vaddq_u16(data_low_t2, data_low_b2); |
| 258 | out_low = vmlaq_u16(out_low, data_low_t1, four); |
| 259 | out_low = vmlaq_u16(out_low, data_low_m, six); |
| 260 | out_low = vmlaq_u16(out_low, data_low_b1, four); |
| 261 | |
| 262 | in.increment(Window::DimX); |
| 263 | |
| 264 | // High data |
| 265 | const uint16x8_t data_high_t2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top2_ptr + in.offset()))); |
| 266 | const uint16x8_t data_high_t1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_top_ptr + in.offset()))); |
| 267 | const uint16x8_t data_high_m = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_mid_ptr + in.offset()))); |
| 268 | const uint16x8_t data_high_b1 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low_ptr + in.offset()))); |
| 269 | const uint16x8_t data_high_b2 = vreinterpretq_u16_s16(vld1q_s16(reinterpret_cast<const int16_t *>(input_low2_ptr + in.offset()))); |
| 270 | |
| 271 | uint16x8_t out_high = vaddq_u16(data_high_t2, data_high_b2); |
| 272 | out_high = vmlaq_u16(out_high, data_high_t1, four); |
| 273 | out_high = vmlaq_u16(out_high, data_high_m, six); |
| 274 | out_high = vmlaq_u16(out_high, data_high_b1, four); |
| 275 | |
| 276 | vst1q_u8(out.ptr(), vcombine_u8(vqshrn_n_u16(out_low, 8), vqshrn_n_u16(out_high, 8))); |
| 277 | }, |
| 278 | in, out); |
| 279 | } |