blob: 02cf1334ac3bdaad03780a2d212fed54c87d7dbd [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 */
24#include "arm_compute/core/NEON/kernels/NEUpsampleLayerKernel.h"
25
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000026#include "arm_compute/core/CPP/Validate.h"
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
Manuel Bottini238580f2020-06-02 12:33:51 +010030#include "arm_compute/core/NEON/wrapper/wrapper.h"
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010031#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
38namespace arm_compute
39{
40namespace
41{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000042template <typename T, int S>
43inline T get_data_out(T data, int offset)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010044{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000045 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 Spyrou7c9541c2018-09-20 17:40:04 +010052}
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010053} // namespace
54NEUpsampleLayerKernel::NEUpsampleLayerKernel()
Manuel Bottini238580f2020-06-02 12:33:51 +010055 : _func(nullptr), _input(nullptr), _output(nullptr), _info()
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010056{
57}
58
59Status 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 Pinitas8f5802f2019-02-22 11:08:32 +000068 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Luca Foschiani5c09ae82020-01-21 16:24:16 +000069 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 +010070 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 Gottardi0a1090a2019-02-14 18:07:36 +000080 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010081 }
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010082 return Status{};
83}
84
Luca Foschiani5c09ae82020-01-21 16:24:16 +000085template <typename T, int S>
86void NEUpsampleLayerKernel::upsample_nchw(const arm_compute::Window &window)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010087{
Luca Foschiani5c09ae82020-01-21 16:24:16 +000088 using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
89
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010090 Window window_in(window);
Manuel Bottini238580f2020-06-02 12:33:51 +010091 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010092
93 Window window_out(window);
Manuel Bottini238580f2020-06-02 12:33:51 +010094 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +010095 window_out.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), _info.y()));
96
Manuel Bottini238580f2020-06-02 12:33:51 +010097 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 Spyrou7c9541c2018-09-20 17:40:04 +0100101 Iterator input(_input, window_in);
102 Iterator output(_output, window_out);
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000103 const int offset_y_out = _output->info()->strides_in_bytes().y() / sizeof(T);
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100104
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100105 execute_window_loop(window_out, [&](const Coordinates &)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100106 {
Manuel Bottini238580f2020-06-02 12:33:51 +0100107 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
108 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100109
Manuel Bottini238580f2020-06-02 12:33:51 +0100110 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 Spyrou7c9541c2018-09-20 17:40:04 +0100131 },
132 input, output);
133}
134
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000135template <typename T, int S>
136void NEUpsampleLayerKernel::upsample_nhwc(const arm_compute::Window &window)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100137{
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000138 using VectorType = typename wrapper::traits::neon_vector<T, S>::type;
139
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100140 Window window_out(window);
Manuel Bottini238580f2020-06-02 12:33:51 +0100141 window_out.set(Window::DimX, Window::Dimension(0, 1, 1));
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100142 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 Bottini238580f2020-06-02 12:33:51 +0100145 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 Spyrou7c9541c2018-09-20 17:40:04 +0100153 Iterator output(_output, window_out);
154
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000155 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 Bottini238580f2020-06-02 12:33:51 +0100157
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100158 execute_window_loop(window_out, [&](const Coordinates &)
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100159 {
Manuel Bottini238580f2020-06-02 12:33:51 +0100160 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
161 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100162
Manuel Bottini238580f2020-06-02 12:33:51 +0100163 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 Spyrou7c9541c2018-09-20 17:40:04 +0100182 },
183 input, output);
184}
185
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100186void 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 Spyrou7c9541c2018-09-20 17:40:04 +0100204 switch(data_layout)
205 {
206 case DataLayout::NCHW:
207 {
208 switch(input->info()->data_type())
209 {
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000210 case DataType::QASYMM8_SIGNED:
211 _func = &NEUpsampleLayerKernel::upsample_nchw<int8_t, 16>;
212 break;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100213 case DataType::QASYMM8:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000214 _func = &NEUpsampleLayerKernel::upsample_nchw<uint8_t, 16>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100215 break;
216 case DataType::F32:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000217 _func = &NEUpsampleLayerKernel::upsample_nchw<float, 4>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100218 break;
219#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
220 case DataType::F16:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000221 _func = &NEUpsampleLayerKernel::upsample_nchw<float16_t, 8>;
222 ;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100223 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 Foschiani5c09ae82020-01-21 16:24:16 +0000234 case DataType::QASYMM8_SIGNED:
235 _func = &NEUpsampleLayerKernel::upsample_nhwc<int8_t, 16>;
236 break;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100237 case DataType::QASYMM8:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000238 _func = &NEUpsampleLayerKernel::upsample_nhwc<uint8_t, 16>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100239 break;
240 case DataType::F32:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000241 _func = &NEUpsampleLayerKernel::upsample_nhwc<float, 4>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100242 break;
243#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
244 case DataType::F16:
Luca Foschiani5c09ae82020-01-21 16:24:16 +0000245 _func = &NEUpsampleLayerKernel::upsample_nhwc<float16_t, 8>;
Michalis Spyrou7c9541c2018-09-20 17:40:04 +0100246 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 Bottini238580f2020-06-02 12:33:51 +0100258 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 Spyrou7c9541c2018-09-20 17:40:04 +0100263}
264
265void 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