blob: d6517ac0721f600a4ac9e39bd1cb5971ae407fc0 [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"
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);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000056 }
57
Georgios Pinitas631c41a2017-12-06 11:53:03 +000058 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000059}
Giorgio Arena226e4b92018-08-23 12:00:02 +010060
61std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &convolved_dims)
62{
63 // Output auto inizialitation if not yet initialized
64 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_col2im_shape(*input, convolved_dims, false)));
65
66 // Configure kernel window
67 Window win = calculate_max_window(*input, Steps());
68
69 // The NECol2ImKernel doesn't need padding so update_window_and_padding() can be skipped
70 Coordinates coord;
71 coord.set_num_dimensions(output->num_dimensions());
72 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
73
74 return std::make_pair(Status{}, win);
75}
Georgios Pinitasd912fd82017-11-27 21:00:13 +000076} // namespace
77
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078template <typename T>
79void NECol2ImKernel::run_col2im(const Window &window)
80{
81 const int output_stride_x = _output->info()->strides_in_bytes().x();
82 const int output_stride_y = _output->info()->strides_in_bytes().y();
83 const int output_stride_z = _output->info()->strides_in_bytes().z();
84
85 Window window_out(window);
86 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
87 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
88 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
89
90 // Create iterators
91 Iterator in(_input, window);
92 Iterator out(_output, window_out);
93
94 execute_window_loop(window, [&](const Coordinates & id)
95 {
96 const int hidx = id.y();
Georgios Pinitasd912fd82017-11-27 21:00:13 +000097 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 +010098
99 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
100 },
101 in, out);
102}
103
104NECol2ImKernel::NECol2ImKernel()
105 : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
106{
107}
108
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000109void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000111 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000112 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 _input = input;
115 _output = output;
116 _convolved_dims = convolved_dims;
117
118 switch(input->info()->element_size())
119 {
120 case 1:
121 _func = &NECol2ImKernel::run_col2im<uint8_t>;
122 break;
123 case 2:
124 _func = &NECol2ImKernel::run_col2im<uint16_t>;
125 break;
126 case 4:
127 _func = &NECol2ImKernel::run_col2im<uint32_t>;
128 break;
129 default:
130 ARM_COMPUTE_ERROR("Element size not supported");
131 break;
132 }
133
134 // Configure kernel window
Giorgio Arena226e4b92018-08-23 12:00:02 +0100135 auto win_config = validate_and_configure_window(input->info(), output->info(), convolved_dims);
136 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
137 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138}
139
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000140Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000141{
142 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100143 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 +0000144 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000145}
146
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100147void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100149 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
151 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
152
153 (this->*_func)(window);
154}