blob: e3661eef3088b2ff8f5983463b5e4ab2db3f0c94 [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.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
47 DataType::U16, DataType::S16,
Georgios Pinitasd912fd82017-11-27 21:00:13 +000048 DataType::U32, DataType::S32,
49 DataType::F16, DataType::F32);
50
51 // Validate configured output
52 if(output->total_size() != 0)
53 {
Giorgio Arena226e4b92018-08-23 12:00:02 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, false));
Georgios Pinitasd912fd82017-11-27 21:00:13 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000057 }
58
Georgios Pinitas631c41a2017-12-06 11:53:03 +000059 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000060}
Giorgio Arena226e4b92018-08-23 12:00:02 +010061
62std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims)
63{
64 // Output auto inizialitation if not yet initialized
65 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, false)));
66
67 // Configure kernel window
68 Window win = calculate_max_window(*input, Steps());
69
70 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
71 Coordinates coord;
72 coord.set_num_dimensions(output->num_dimensions());
73 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
74
75 return std::make_pair(Status{}, win);
76}
Georgios Pinitasd912fd82017-11-27 21:00:13 +000077} // namespace
78
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079template <typename T>
80void NECol2ImKernel::run_col2im(const Window &window)
81{
82 const int output_stride_x = _output->info()->strides_in_bytes().x();
83 const int output_stride_y = _output->info()->strides_in_bytes().y();
84 const int output_stride_z = _output->info()->strides_in_bytes().z();
85
86 Window window_out(window);
87 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
88 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
89 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
90
91 // Create iterators
92 Iterator in(_input, window);
93 Iterator out(_output, window_out);
94
95 execute_window_loop(window, [&](const Coordinates & id)
96 {
97 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000098 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 +010099
100 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
101 },
102 in, out);
103}
104
105NECol2ImKernel::NECol2ImKernel()
106 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
107{
108}
109
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000110void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000112 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000113 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115 _input = input;
116 _output = output;
117 _convolved_dims = convolved_dims;
118
119 switch(input->info()->element_size())
120 {
121 case 1:
122 _func = &NECol2ImKernel::run_col2im<uint8_t>;
123 break;
124 case 2:
125 _func = &NECol2ImKernel::run_col2im<uint16_t>;
126 break;
127 case 4:
128 _func = &NECol2ImKernel::run_col2im<uint32_t>;
129 break;
130 default:
131 ARM_COMPUTE_ERROR("Element size not supported");
132 break;
133 }
134
135 // Configure kernel window
Giorgio Arena226e4b92018-08-23 12:00:02 +0100136 auto win_config = validate_and_configure_window(input->info(), output->info(), convolved_dims);
137 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
138 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}
140
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000141Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000142{
143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100144 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 +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}