Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [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" |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 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 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 53 | } // namespace |
| 54 | NEUpsampleLayerKernel::NEUpsampleLayerKernel() |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 55 | : _func(nullptr), _input(nullptr), _output(nullptr), _info() |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | Status NEUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy policy) |
| 60 | { |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 62 | ARM_COMPUTE_UNUSED(policy); |
| 63 | |
| 64 | const DataLayout data_layout = input->data_layout(); |
| 65 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 66 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 67 | |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 69 | 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] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported"); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported"); |
| 72 | |
| 73 | // Check output if configured |
| 74 | if(output->total_size() != 0) |
| 75 | { |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width)); |
| 79 | 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] | 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 81 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 82 | return Status{}; |
| 83 | } |
| 84 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 85 | template <typename T, int S> |
| 86 | void NEUpsampleLayerKernel::upsample_nchw(const arm_compute::Window &window) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 87 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 88 | using VectorType = typename wrapper::traits::neon_vector<T, S>::type; |
| 89 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 90 | Window window_in(window); |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 91 | window_in.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 92 | |
| 93 | Window window_out(window); |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 94 | window_out.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 95 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y())); |
| 96 | |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 97 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 98 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 99 | const int window_step_x = S; |
| 100 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 101 | Iterator input(_input, window_in); |
| 102 | Iterator output(_output, window_out); |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 103 | 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] | 104 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 105 | execute_window_loop(window_out, [&](const Coordinates &) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 106 | { |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 107 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 108 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 109 | |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 110 | int x = window_start_x; |
| 111 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 112 | { |
| 113 | const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input_ptr + x)); |
| 114 | const VectorType data_out1 = get_data_out<VectorType, S>(data, 0); |
| 115 | const VectorType data_out2 = get_data_out<VectorType, S>(data, S / 2); |
| 116 | |
| 117 | wrapper::vstore(output_ptr + 2 * x, data_out1); |
| 118 | wrapper::vstore(output_ptr + 2 * x + S, data_out2); |
| 119 | wrapper::vstore(output_ptr + 2 * x + offset_y_out, data_out1); |
| 120 | wrapper::vstore(output_ptr + 2 * x + offset_y_out + S, data_out2); |
| 121 | } |
| 122 | |
| 123 | // Compute left-over elements |
| 124 | for(; x < window_end_x; ++x) |
| 125 | { |
| 126 | *(output_ptr + 2 * x) = *(input_ptr + x); |
| 127 | *(output_ptr + 2 * x + 1) = *(input_ptr + x); |
| 128 | *(output_ptr + 2 * x + offset_y_out) = *(input_ptr + x); |
| 129 | *(output_ptr + 2 * x + offset_y_out + 1) = *(input_ptr + x); |
| 130 | } |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 131 | }, |
| 132 | input, output); |
| 133 | } |
| 134 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 135 | template <typename T, int S> |
| 136 | void NEUpsampleLayerKernel::upsample_nhwc(const arm_compute::Window &window) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 137 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 138 | using VectorType = typename wrapper::traits::neon_vector<T, S>::type; |
| 139 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 140 | Window window_out(window); |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 141 | window_out.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 142 | window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x())); |
| 143 | window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y())); |
| 144 | |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 145 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 146 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 147 | const int window_step_x = S; |
| 148 | |
| 149 | Window window_in{ window }; |
| 150 | window_in.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 151 | |
| 152 | Iterator input(_input, window_in); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 153 | Iterator output(_output, window_out); |
| 154 | |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 155 | const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T); |
| 156 | const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(T); |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 157 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 158 | execute_window_loop(window_out, [&](const Coordinates &) |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 159 | { |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 160 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 161 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 162 | |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 163 | int x = window_start_x; |
| 164 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 165 | { |
| 166 | const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input_ptr + x)); |
| 167 | |
| 168 | wrapper::vstore(output_ptr + x, data); |
| 169 | wrapper::vstore(output_ptr + x + offset_y_out, data); |
| 170 | wrapper::vstore(output_ptr + x + offset_z_out, data); |
| 171 | wrapper::vstore(output_ptr + x + offset_y_out + offset_z_out, data); |
| 172 | } |
| 173 | |
| 174 | // Compute left-over elements |
| 175 | for(; x < window_end_x; ++x) |
| 176 | { |
| 177 | *(output_ptr + x) = *(input_ptr + x); |
| 178 | *(output_ptr + x + offset_y_out) = *(input_ptr + x); |
| 179 | *(output_ptr + x + offset_z_out) = *(input_ptr + x); |
| 180 | *(output_ptr + x + offset_y_out + offset_z_out) = *(input_ptr + x); |
| 181 | } |
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 | |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 204 | switch(data_layout) |
| 205 | { |
| 206 | case DataLayout::NCHW: |
| 207 | { |
| 208 | switch(input->info()->data_type()) |
| 209 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 210 | case DataType::QASYMM8_SIGNED: |
| 211 | _func = &NEUpsampleLayerKernel::upsample_nchw<int8_t, 16>; |
| 212 | break; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 213 | case DataType::QASYMM8: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 214 | _func = &NEUpsampleLayerKernel::upsample_nchw<uint8_t, 16>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 215 | break; |
| 216 | case DataType::F32: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 217 | _func = &NEUpsampleLayerKernel::upsample_nchw<float, 4>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 218 | break; |
| 219 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 220 | case DataType::F16: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 221 | _func = &NEUpsampleLayerKernel::upsample_nchw<float16_t, 8>; |
| 222 | ; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 223 | break; |
| 224 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 225 | default: |
| 226 | ARM_COMPUTE_ERROR("Not implemented"); |
| 227 | } |
| 228 | break; |
| 229 | } |
| 230 | case DataLayout::NHWC: |
| 231 | { |
| 232 | switch(input->info()->data_type()) |
| 233 | { |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 234 | case DataType::QASYMM8_SIGNED: |
| 235 | _func = &NEUpsampleLayerKernel::upsample_nhwc<int8_t, 16>; |
| 236 | break; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 237 | case DataType::QASYMM8: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 238 | _func = &NEUpsampleLayerKernel::upsample_nhwc<uint8_t, 16>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 239 | break; |
| 240 | case DataType::F32: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 241 | _func = &NEUpsampleLayerKernel::upsample_nhwc<float, 4>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 242 | break; |
| 243 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 244 | case DataType::F16: |
Luca Foschiani | 5c09ae8 | 2020-01-21 16:24:16 +0000 | [diff] [blame] | 245 | _func = &NEUpsampleLayerKernel::upsample_nhwc<float16_t, 8>; |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 246 | break; |
| 247 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 248 | default: |
| 249 | ARM_COMPUTE_ERROR("Not implemented"); |
| 250 | } |
| 251 | break; |
| 252 | } |
| 253 | default: |
| 254 | ARM_COMPUTE_ERROR("Not implemented"); |
| 255 | } |
| 256 | |
| 257 | // Configure window |
Manuel Bottini | 238580f | 2020-06-02 12:33:51 +0100 | [diff] [blame] | 258 | Window win = calculate_max_window(*input->info(), Steps()); |
| 259 | Coordinates coord; |
| 260 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 261 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 262 | INEKernel::configure(win); |
Michalis Spyrou | 7c9541c | 2018-09-20 17:40:04 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void NEUpsampleLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 266 | { |
| 267 | ARM_COMPUTE_UNUSED(info); |
| 268 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 269 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 270 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 271 | |
| 272 | (this->*_func)(window); |
| 273 | } |
| 274 | } // namespace arm_compute |