blob: 6a07defd7914cbd97319a8eb2eeec18991c00deb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 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"
Giorgio Arena226e4b92018-08-23 12:00:02 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <arm_neon.h>
35#include <cstddef>
36#include <cstdint>
37
38using namespace arm_compute;
Giorgio Arena226e4b92018-08-23 12:00:02 +010039using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Georgios Pinitasd912fd82017-11-27 21:00:13 +000041namespace
42{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000044{
Anthony Barbiereaefd002018-07-20 17:49:35 +010045 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitas33843562019-12-10 13:33:18 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000047
48 // Validate configured output
49 if(output->total_size() != 0)
50 {
Giorgio Arena226e4b92018-08-23 12:00:02 +010051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, false));
Georgios Pinitasd912fd82017-11-27 21:00:13 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000054 }
55
Georgios Pinitas631c41a2017-12-06 11:53:03 +000056 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000057}
Giorgio Arena226e4b92018-08-23 12:00:02 +010058
59std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims)
60{
61 // Output auto inizialitation if not yet initialized
62 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, false)));
63
64 // Configure kernel window
65 Window win = calculate_max_window(*input, Steps());
66
67 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
68 Coordinates coord;
69 coord.set_num_dimensions(output->num_dimensions());
70 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
71
72 return std::make_pair(Status{}, win);
73}
Georgios Pinitasd912fd82017-11-27 21:00:13 +000074} // namespace
75
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076template <typename T>
77void NECol2ImKernel::run_col2im(const Window &window)
78{
79 const int output_stride_x = _output->info()->strides_in_bytes().x();
80 const int output_stride_y = _output->info()->strides_in_bytes().y();
81 const int output_stride_z = _output->info()->strides_in_bytes().z();
82
83 Window window_out(window);
84 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
85 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
86 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
87
88 // Create iterators
89 Iterator in(_input, window);
90 Iterator out(_output, window_out);
91
92 execute_window_loop(window, [&](const Coordinates & id)
93 {
94 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000095 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 +010096
97 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
98 },
99 in, out);
100}
101
102NECol2ImKernel::NECol2ImKernel()
103 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
104{
105}
106
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000107void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000109 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000110 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
Giorgio Arena226e4b92018-08-23 12:00:02 +0100133 auto win_config = validate_and_configure_window(input->info(), output->info(), convolved_dims);
134 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
135 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136}
137
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000138Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000139{
140 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100141 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims).first);
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000142 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000143}
144
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100145void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100147 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
149 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
150
151 (this->*_func)(window);
152}