blob: bb8e758b78110a0fb20e1e4468da4ec37834d587 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/NECol2ImKernel.h"
25
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/Types.h"
31#include "arm_compute/core/Validate.h"
32
33#include <arm_neon.h>
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
Georgios Pinitasd912fd82017-11-27 21:00:13 +000039namespace
40{
41TensorShape get_output_shape(const ITensorInfo *input, const Size2D &convolved_dims)
42{
43 TensorShape output_shape = input->tensor_shape();
44 output_shape.set(0, convolved_dims.width);
45 output_shape.set(1, convolved_dims.height);
46 output_shape.set(2, input->tensor_shape()[0]);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010047 output_shape.set(3, input->tensor_shape()[3]); // For NEON the batch size is on the fourth dimension of the input tensor
Georgios Pinitasd912fd82017-11-27 21:00:13 +000048
49 return output_shape;
50}
51
Georgios Pinitas631c41a2017-12-06 11:53:03 +000052Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000053{
Anthony Barbiereaefd002018-07-20 17:49:35 +010054 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
56 DataType::U16, DataType::S16,
Georgios Pinitasd912fd82017-11-27 21:00:13 +000057 DataType::U32, DataType::S32,
58 DataType::F16, DataType::F32);
59
60 // Validate configured output
61 if(output->total_size() != 0)
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, convolved_dims));
64 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000065 }
66
Georgios Pinitas631c41a2017-12-06 11:53:03 +000067 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000068}
69} // namespace
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071template <typename T>
72void NECol2ImKernel::run_col2im(const Window &window)
73{
74 const int output_stride_x = _output->info()->strides_in_bytes().x();
75 const int output_stride_y = _output->info()->strides_in_bytes().y();
76 const int output_stride_z = _output->info()->strides_in_bytes().z();
77
78 Window window_out(window);
79 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
80 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
81 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
82
83 // Create iterators
84 Iterator in(_input, window);
85 Iterator out(_output, window_out);
86
87 execute_window_loop(window, [&](const Coordinates & id)
88 {
89 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000090 const int idx = id.x() * output_stride_z + (hidx / _convolved_dims.width) * output_stride_y + (hidx % _convolved_dims.width) * output_stride_x;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
92 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
93 },
94 in, out);
95}
96
97NECol2ImKernel::NECol2ImKernel()
98 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
99{
100}
101
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000102void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000104 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100106 // Output auto inizialitation if not yet initialized
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000107 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), convolved_dims)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000109 // Perform validation step
110 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
112 _input = input;
113 _output = output;
114 _convolved_dims = convolved_dims;
115
116 switch(input->info()->element_size())
117 {
118 case 1:
119 _func = &NECol2ImKernel::run_col2im<uint8_t>;
120 break;
121 case 2:
122 _func = &NECol2ImKernel::run_col2im<uint16_t>;
123 break;
124 case 4:
125 _func = &NECol2ImKernel::run_col2im<uint32_t>;
126 break;
127 default:
128 ARM_COMPUTE_ERROR("Element size not supported");
129 break;
130 }
131
132 // Configure kernel window
133 Window win = calculate_max_window(*input->info(), Steps());
134
135 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
136 Coordinates coord;
137 coord.set_num_dimensions(output->info()->num_dimensions());
138 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
139
140 INEKernel::configure(win);
141}
142
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000143Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000144{
145 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000146 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000147}
148
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100149void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100151 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
154
155 (this->*_func)(window);
156}