blob: cbdec50a420ec7473ba1a4df5d93d6ea1fabe319 [file] [log] [blame]
Michalis Spyrou7c9541c2018-09-20 17:40:04 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou7c9541c2018-09-20 17:40:04 +01003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEUpsampleLayerKernel.h"
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010025
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/Validate.h"
31#include "arm_compute/core/Window.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010034#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010037
38#include <arm_neon.h>
39
40namespace arm_compute
41{
42namespace
43{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000044template <typename T, int S>
45inline T get_data_out(T data, int offset)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010046{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000047 T out{ 0 };
48 for(int i = 0; i < S / 2; ++i)
49 {
50 out[2 * i] = wrapper::vgetlane(data, i + offset);
51 out[2 * i + 1] = wrapper::vgetlane(data, i + offset);
52 }
53 return out;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010054}
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010055} // namespace
56NEUpsampleLayerKernel::NEUpsampleLayerKernel()
Manuel Bottini238580f2020-06-02 12:33:51 +010057 : _func(nullptr), _input(nullptr), _output(nullptr), _info()
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010058{
59}
60
61Status NEUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy policy)
62{
63 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
64 ARM_COMPUTE_UNUSED(policy);
65
66 const DataLayout data_layout = input->data_layout();
67 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
68 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
69
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000070 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Luca Foschiani5c09ae82020-01-21 16:24:16 +000071 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported");
73 ARM_COMPUTE_RETURN_ERROR_ON_MSG(policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported");
74
75 // Check output if configured
76 if(output->total_size() != 0)
77 {
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
80 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width));
81 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height));
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000082 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010083 }
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010084 return Status{};
85}
86
Luca Foschiani5c09ae82020-01-21 16:24:16 +000087template <typename T, int S>
88void NEUpsampleLayerKernel::upsample_nchw(const arm_compute::Window &window)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010089{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000090 using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
91
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010092 Window window_in(window);
Manuel Bottini238580f2020-06-02 12:33:51 +010093 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010094
95 Window window_out(window);
Manuel Bottini238580f2020-06-02 12:33:51 +010096 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010097 window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y()));
98
Manuel Bottini238580f2020-06-02 12:33:51 +010099 const auto window_start_x = static_cast<int>(window.x().start());
100 const auto window_end_x = static_cast<int>(window.x().end());
101 const int window_step_x = S;
102
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100103 Iterator input(_input, window_in);
104 Iterator output(_output, window_out);
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000105 const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100106
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100107 execute_window_loop(window_out, [&](const Coordinates &)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100108 {
Manuel Bottini238580f2020-06-02 12:33:51 +0100109 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
110 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100111
Manuel Bottini238580f2020-06-02 12:33:51 +0100112 int x = window_start_x;
113 for(; x <= (window_end_x - window_step_x); x += window_step_x)
114 {
115 const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input_ptr + x));
116 const VectorType data_out1 = get_data_out<VectorType, S>(data, 0);
117 const VectorType data_out2 = get_data_out<VectorType, S>(data, S / 2);
118
119 wrapper::vstore(output_ptr + 2 * x, data_out1);
120 wrapper::vstore(output_ptr + 2 * x + S, data_out2);
121 wrapper::vstore(output_ptr + 2 * x + offset_y_out, data_out1);
122 wrapper::vstore(output_ptr + 2 * x + offset_y_out + S, data_out2);
123 }
124
125 // Compute left-over elements
126 for(; x < window_end_x; ++x)
127 {
128 *(output_ptr + 2 * x) = *(input_ptr + x);
129 *(output_ptr + 2 * x + 1) = *(input_ptr + x);
130 *(output_ptr + 2 * x + offset_y_out) = *(input_ptr + x);
131 *(output_ptr + 2 * x + offset_y_out + 1) = *(input_ptr + x);
132 }
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100133 },
134 input, output);
135}
136
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000137template <typename T, int S>
138void NEUpsampleLayerKernel::upsample_nhwc(const arm_compute::Window &window)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100139{
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000140 using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
141
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100142 Window window_out(window);
Manuel Bottini238580f2020-06-02 12:33:51 +0100143 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100144 window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.x()));
145 window_out.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), _info.y()));
146
Manuel Bottini238580f2020-06-02 12:33:51 +0100147 const auto window_start_x = static_cast<int>(window.x().start());
148 const auto window_end_x = static_cast<int>(window.x().end());
149 const int window_step_x = S;
150
151 Window window_in{ window };
152 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
153
154 Iterator input(_input, window_in);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100155 Iterator output(_output, window_out);
156
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000157 const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T);
158 const int offset_z_out = _output->info()->strides_in_bytes().z() / sizeof(T);
Manuel Bottini238580f2020-06-02 12:33:51 +0100159
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100160 execute_window_loop(window_out, [&](const Coordinates &)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100161 {
Manuel Bottini238580f2020-06-02 12:33:51 +0100162 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
163 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100164
Manuel Bottini238580f2020-06-02 12:33:51 +0100165 int x = window_start_x;
166 for(; x <= (window_end_x - window_step_x); x += window_step_x)
167 {
168 const VectorType data = wrapper::vloadq(reinterpret_cast<const T *>(input_ptr + x));
169
170 wrapper::vstore(output_ptr + x, data);
171 wrapper::vstore(output_ptr + x + offset_y_out, data);
172 wrapper::vstore(output_ptr + x + offset_z_out, data);
173 wrapper::vstore(output_ptr + x + offset_y_out + offset_z_out, data);
174 }
175
176 // Compute left-over elements
177 for(; x < window_end_x; ++x)
178 {
179 *(output_ptr + x) = *(input_ptr + x);
180 *(output_ptr + x + offset_y_out) = *(input_ptr + x);
181 *(output_ptr + x + offset_z_out) = *(input_ptr + x);
182 *(output_ptr + x + offset_y_out + offset_z_out) = *(input_ptr + x);
183 }
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100184 },
185 input, output);
186}
187
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100188void NEUpsampleLayerKernel::configure(const ITensor *input, ITensor *output, const Size2D &info, const InterpolationPolicy policy)
189{
190 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
191 ARM_COMPUTE_UNUSED(policy);
192
193 _input = input;
194 _output = output;
195 _info = info;
196
197 const DataLayout data_layout = input->info()->data_layout();
198
199 TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info);
200 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
201 output->info()->set_data_layout(data_layout);
202
203 // Perform validation step
204 ARM_COMPUTE_ERROR_THROW_ON(NEUpsampleLayerKernel::validate(input->info(), output->info(), info, policy));
205
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100206 switch(data_layout)
207 {
208 case DataLayout::NCHW:
209 {
210 switch(input->info()->data_type())
211 {
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000212 case DataType::QASYMM8_SIGNED:
213 _func = &NEUpsampleLayerKernel::upsample_nchw<int8_t, 16>;
214 break;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100215 case DataType::QASYMM8:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000216 _func = &NEUpsampleLayerKernel::upsample_nchw<uint8_t, 16>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100217 break;
218 case DataType::F32:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000219 _func = &NEUpsampleLayerKernel::upsample_nchw<float, 4>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100220 break;
221#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
222 case DataType::F16:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000223 _func = &NEUpsampleLayerKernel::upsample_nchw<float16_t, 8>;
224 ;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100225 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 Foschiani5c09ae82020-01-21 16:24:16 +0000236 case DataType::QASYMM8_SIGNED:
237 _func = &NEUpsampleLayerKernel::upsample_nhwc<int8_t, 16>;
238 break;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100239 case DataType::QASYMM8:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000240 _func = &NEUpsampleLayerKernel::upsample_nhwc<uint8_t, 16>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100241 break;
242 case DataType::F32:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000243 _func = &NEUpsampleLayerKernel::upsample_nhwc<float, 4>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100244 break;
245#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
246 case DataType::F16:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000247 _func = &NEUpsampleLayerKernel::upsample_nhwc<float16_t, 8>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100248 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
Manuel Bottini238580f2020-06-02 12:33:51 +0100260 Window win = calculate_max_window(*input->info(), Steps());
261 Coordinates coord;
262 coord.set_num_dimensions(output->info()->num_dimensions());
263 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
264 INEKernel::configure(win);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100265}
266
267void NEUpsampleLayerKernel::run(const Window &window, const ThreadInfo &info)
268{
269 ARM_COMPUTE_UNUSED(info);
270 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
271 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
272 ARM_COMPUTE_ERROR_ON(_func == nullptr);
273
274 (this->*_func)(window);
275}
276} // namespace arm_compute