blob: cea8782354b0c5b876ef4c103aa7e79766ddf232 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * 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 Pinitasd912fd82017-11-27 21:00:13 +000046
47 // Validate configured output
48 if(output->total_size() != 0)
49 {
Giorgio Arena226e4b92018-08-23 12:00:02 +010050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, false));
Georgios Pinitasd912fd82017-11-27 21:00:13 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000053 }
54
Georgios Pinitas631c41a2017-12-06 11:53:03 +000055 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000056}
Giorgio Arena226e4b92018-08-23 12:00:02 +010057
58std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims)
59{
60 // Output auto inizialitation if not yet initialized
61 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, false)));
62
63 // Configure kernel window
64 Window win = calculate_max_window(*input, Steps());
65
66 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
67 Coordinates coord;
68 coord.set_num_dimensions(output->num_dimensions());
69 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
70
71 return std::make_pair(Status{}, win);
72}
Georgios Pinitasd912fd82017-11-27 21:00:13 +000073} // namespace
74
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075template <typename T>
76void NECol2ImKernel::run_col2im(const Window &window)
77{
78 const int output_stride_x = _output->info()->strides_in_bytes().x();
79 const int output_stride_y = _output->info()->strides_in_bytes().y();
80 const int output_stride_z = _output->info()->strides_in_bytes().z();
81
82 Window window_out(window);
83 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
84 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
85 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
86
87 // Create iterators
88 Iterator in(_input, window);
89 Iterator out(_output, window_out);
90
91 execute_window_loop(window, [&](const Coordinates & id)
92 {
93 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000094 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 +010095
96 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
97 },
98 in, out);
99}
100
101NECol2ImKernel::NECol2ImKernel()
102 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
103{
104}
105
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000106void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000108 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000109 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
Giorgio Arena226e4b92018-08-23 12:00:02 +0100132 auto win_config = validate_and_configure_window(input->info(), output->info(), convolved_dims);
133 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
134 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135}
136
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000137Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000138{
139 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100140 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 +0000141 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000142}
143
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100144void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100146 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
148 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
149
150 (this->*_func)(window);
151}