• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 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 "src/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 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
33 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 #include <arm_neon.h>
37 #include <cstddef>
38 #include <cstdint>
39 
40 using namespace arm_compute;
41 using namespace misc::shape_calculator;
42 
43 namespace
44 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const Size2D & convolved_dims)45 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
46 {
47     //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
48     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
49 
50     // Validate configured output
51     if(output->total_size() != 0)
52     {
53         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_col2im_shape(*input, convolved_dims, false));
54         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
55         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
56     }
57 
58     return Status{};
59 }
60 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output,const Size2D & convolved_dims)61 std::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 }
76 } // namespace
77 
78 template <typename T>
run_col2im(const Window & window)79 void 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();
97         const int idx  = id.x() * output_stride_z + (hidx / _convolved_dims.width) * output_stride_y + (hidx % _convolved_dims.width) * output_stride_x;
98 
99         *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
100     },
101     in, out);
102 }
103 
NECol2ImKernel()104 NECol2ImKernel::NECol2ImKernel()
105     : _func(), _input(nullptr), _output(nullptr), _convolved_dims()
106 {
107 }
108 
configure(const ITensor * input,ITensor * output,const Size2D & convolved_dims)109 void NECol2ImKernel::configure(const ITensor *input, ITensor *output, const Size2D &convolved_dims)
110 {
111     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
112     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), convolved_dims));
113 
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
135     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);
138 }
139 
validate(const ITensorInfo * input,const ITensorInfo * output,const Size2D & convolved_dims)140 Status NECol2ImKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &convolved_dims)
141 {
142     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, convolved_dims));
143     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), convolved_dims).first);
144     return Status{};
145 }
146 
run(const Window & window,const ThreadInfo & info)147 void NECol2ImKernel::run(const Window &window, const ThreadInfo &info)
148 {
149     ARM_COMPUTE_UNUSED(info);
150     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
151     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
152 
153     (this->*_func)(window);
154 }
155