Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 1 | /* |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEUpsampleLayerKernel.h" |
| 25 | |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 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/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 34 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 35 | |
| 36 | #include <arm_neon.h> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 42 | template <typename T, int S> |
| 43 | inline T get_data_out(T data, int offset) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 44 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 45 | T out{ 0 }; |
| 46 | for(int i = 0; i < S / 2; ++i) |
| 47 | { |
| 48 | out[2 * i] = wrapper::vgetlane(data, i + offset); |
| 49 | out[2 * i + 1] = wrapper::vgetlane(data, i + offset); |
| 50 | } |
| 51 | return out; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, int num_elems_processed_per_iteration_x, const Size2D &info) |
| 55 | { |
| 56 | std::pair<Status, Window> win_config; |
| 57 | switch(input->data_layout()) |
| 58 | { |
| 59 | case DataLayout::NCHW: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 60 | { |
| 61 | const int num_elems_processed_per_iteration_x_out = num_elems_processed_per_iteration_x * info.x(); |
| 62 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x_out)); |
| 63 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, 1, 0.5f, 0.5f); |
| 64 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_x_out); |
| 65 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 66 | output_access.set_valid_region(win, output->valid_region()); |
| 67 | |
| 68 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 69 | win_config = std::make_pair(err, win); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 70 | break; |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 71 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 72 | case DataLayout::NHWC: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 73 | { |
| 74 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x)); |
| 75 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration_x); |
| 76 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration_x); |
| 77 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 78 | output_access.set_valid_region(win, output->valid_region()); |
| 79 | |
| 80 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 81 | win_config = std::make_pair(err, win); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 82 | break; |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 83 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 84 | default: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 85 | { |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 86 | win_config = std::make_pair(ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported data layout!"), Window{}); |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 87 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return win_config; |
| 91 | } |
| 92 | } // namespace |
| 93 | NEUpsampleLayerKernel::NEUpsampleLayerKernel() |
| 94 | : _func(nullptr), _input(nullptr), _output(nullptr), _info(), _num_elems_processed_per_iteration_x() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | Status NEUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy policy) |
| 99 | { |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 101 | ARM_COMPUTE_UNUSED(policy); |
| 102 | |
| 103 | const DataLayout data_layout = input->data_layout(); |
| 104 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 105 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 106 | |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 107 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 108 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 109 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported"); |
| 110 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported"); |
| 111 | |
| 112 | // Check output if configured |
| 113 | if(output->total_size() != 0) |
| 114 | { |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width)); |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height)); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 119 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | const int num_elems_processed_per_iteration_x = 16 / input->element_size(); |
| 123 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), |
| 124 | output->clone().get(), num_elems_processed_per_iteration_x, info) |
| 125 | .first); |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 129 | template <typename T, int S> |
| 130 | void NEUpsampleLayerKernel::upsample_nchw(const arm_compute::Window &window) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 131 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 132 | using VectorType = typename wrapper::traits::neon_vector<T, S>::type; |
| 133 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 134 | Window window_in(window); |
| 135 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_x)); |
| 136 | |
| 137 | Window window_out(window); |
| 138 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y())); |
| 139 | |
| 140 | Iterator input(_input, window_in); |
| 141 | Iterator output(_output, window_out); |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 142 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 143 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 144 | execute_window_loop(window_out, [&](const Coordinates &) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 145 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 146 | const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input.ptr())); |
| 147 | const VectorType data_out1 = get_data_out<VectorType, S>(data, 0); |
| 148 | const VectorType data_out2 = get_data_out<VectorType, S>(data, S / 2); |
| 149 | auto out = reinterpret_cast<T *>(output.ptr()); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 150 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 151 | wrapper::vstore(out, data_out1); |
| 152 | wrapper::vstore(out + S, data_out2); |
| 153 | wrapper::vstore(out + offset_y_out, data_out1); |
| 154 | wrapper::vstore(out + offset_y_out + S, data_out2); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 155 | }, |
| 156 | input, output); |
| 157 | } |
| 158 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 159 | template <typename T, int S> |
| 160 | void NEUpsampleLayerKernel::upsample_nhwc(const arm_compute::Window &window) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 161 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 162 | using VectorType = typename wrapper::traits::neon_vector<T, S>::type; |
| 163 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 164 | Window window_out(window); |
| 165 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x())); |
| 166 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y())); |
| 167 | |
| 168 | Iterator input(_input, window); |
| 169 | Iterator output(_output, window_out); |
| 170 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 171 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T); |
| 172 | const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(T); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 173 | execute_window_loop(window_out, [&](const Coordinates &) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 174 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 175 | const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input.ptr())); |
| 176 | auto out = reinterpret_cast<T *>(output.ptr()); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 177 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 178 | wrapper::vstore(out, data); |
| 179 | wrapper::vstore(out + offset_y_out, data); |
| 180 | wrapper::vstore(out + offset_z_out, data); |
| 181 | wrapper::vstore(out + offset_y_out + offset_z_out, data); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 182 | }, |
| 183 | input, output); |
| 184 | } |
| 185 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 186 | void NEUpsampleLayerKernel::configure(const ITensor *input, ITensor *output, const Size2D &info, const InterpolationPolicy policy) |
| 187 | { |
| 188 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 189 | ARM_COMPUTE_UNUSED(policy); |
| 190 | |
| 191 | _input = input; |
| 192 | _output = output; |
| 193 | _info = info; |
| 194 | |
| 195 | const DataLayout data_layout = input->info()->data_layout(); |
| 196 | |
| 197 | TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info); |
| 198 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 199 | output->info()->set_data_layout(data_layout); |
| 200 | |
| 201 | // Perform validation step |
| 202 | ARM_COMPUTE_ERROR_THROW_ON(NEUpsampleLayerKernel::validate(input->info(), output->info(), info, policy)); |
| 203 | |
| 204 | _num_elems_processed_per_iteration_x = 16 / output->info()->element_size(); |
| 205 | |
| 206 | switch(data_layout) |
| 207 | { |
| 208 | case DataLayout::NCHW: |
| 209 | { |
| 210 | switch(input->info()->data_type()) |
| 211 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 212 | case DataType::QASYMM8_SIGNED: |
| 213 | _func = &NEUpsampleLayerKernel::upsample_nchw<int8_t, 16>; |
| 214 | break; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 215 | case DataType::QASYMM8: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 216 | _func = &NEUpsampleLayerKernel::upsample_nchw<uint8_t, 16>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 217 | break; |
| 218 | case DataType::F32: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 219 | _func = &NEUpsampleLayerKernel::upsample_nchw<float, 4>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 220 | break; |
| 221 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 222 | case DataType::F16: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 223 | _func = &NEUpsampleLayerKernel::upsample_nchw<float16_t, 8>; |
| 224 | ; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 225 | break; |
| 226 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 227 | default: |
| 228 | ARM_COMPUTE_ERROR("Not implemented"); |
| 229 | } |
| 230 | break; |
| 231 | } |
| 232 | case DataLayout::NHWC: |
| 233 | { |
| 234 | switch(input->info()->data_type()) |
| 235 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 236 | case DataType::QASYMM8_SIGNED: |
| 237 | _func = &NEUpsampleLayerKernel::upsample_nhwc<int8_t, 16>; |
| 238 | break; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 239 | case DataType::QASYMM8: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 240 | _func = &NEUpsampleLayerKernel::upsample_nhwc<uint8_t, 16>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 241 | break; |
| 242 | case DataType::F32: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 243 | _func = &NEUpsampleLayerKernel::upsample_nhwc<float, 4>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 244 | break; |
| 245 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 246 | case DataType::F16: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame^] | 247 | _func = &NEUpsampleLayerKernel::upsample_nhwc<float16_t, 8>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 248 | break; |
| 249 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 250 | default: |
| 251 | ARM_COMPUTE_ERROR("Not implemented"); |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | default: |
| 256 | ARM_COMPUTE_ERROR("Not implemented"); |
| 257 | } |
| 258 | |
| 259 | // Configure window |
| 260 | std::pair<Status, Window> win_config = validate_and_configure_window(input->info(), |
| 261 | output->info(), |
| 262 | _num_elems_processed_per_iteration_x, |
| 263 | info); |
| 264 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 265 | INEKernel::configure(win_config.second); |
| 266 | } |
| 267 | |
| 268 | void NEUpsampleLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 269 | { |
| 270 | ARM_COMPUTE_UNUSED(info); |
| 271 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 272 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 273 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 274 | |
| 275 | (this->*_func)(window); |
| 276 | } |
| 277 | } // namespace arm_compute |