blob: 9fda65feb4404e001afa8bf68c1a3942587d0d81 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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/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]);
47
48 return output_shape;
49}
50
Georgios Pinitas631c41a2017-12-06 11:53:03 +000051Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000052{
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8,
54 DataType::U16, DataType::S16, DataType::QS16,
55 DataType::U32, DataType::S32,
56 DataType::F16, DataType::F32);
57
58 // Validate configured output
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, convolved_dims));
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
64 }
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000067}
68} // namespace
69
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070template <typename T>
71void NECol2ImKernel::run_col2im(const Window &window)
72{
73 const int output_stride_x = _output->info()->strides_in_bytes().x();
74 const int output_stride_y = _output->info()->strides_in_bytes().y();
75 const int output_stride_z = _output->info()->strides_in_bytes().z();
76
77 Window window_out(window);
78 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
79 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
80 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
81
82 // Create iterators
83 Iterator in(_input, window);
84 Iterator out(_output, window_out);
85
86 execute_window_loop(window, [&](const Coordinates & id)
87 {
88 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000089 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 +010090
91 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
92 },
93 in, out);
94}
95
96NECol2ImKernel::NECol2ImKernel()
97 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
98{
99}
100
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000101void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100105 // Output auto inizialitation if not yet initialized
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000106 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 +0100107
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000108 // Perform validation step
109 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
111 _input = input;
112 _output = output;
113 _convolved_dims = convolved_dims;
114
115 switch(input->info()->element_size())
116 {
117 case 1:
118 _func = &NECol2ImKernel::run_col2im<uint8_t>;
119 break;
120 case 2:
121 _func = &NECol2ImKernel::run_col2im<uint16_t>;
122 break;
123 case 4:
124 _func = &NECol2ImKernel::run_col2im<uint32_t>;
125 break;
126 default:
127 ARM_COMPUTE_ERROR("Element size not supported");
128 break;
129 }
130
131 // Configure kernel window
132 Window win = calculate_max_window(*input->info(), Steps());
133
134 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
135 Coordinates coord;
136 coord.set_num_dimensions(output->info()->num_dimensions());
137 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
138
139 INEKernel::configure(win);
140}
141
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000142Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000143{
144 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000145 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000146}
147
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100148void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100150 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
152 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
153
154 (this->*_func)(window);
155}