blob: 22149b4ea4403ae560a36bbc2c5424be790040f2 [file] [log] [blame]
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +00001/*
Manuel Bottini581c8982019-02-07 10:31:57 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +00003 *
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/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010027#include "arm_compute/core/CL/CLValidate.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Types.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000032#include "arm_compute/core/Window.h"
33
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010044 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini581c8982019-02-07 10:31:57 +000045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32, DataType::F16,
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000046 DataType::F32);
47
48 if(bias != nullptr)
49 {
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010050 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(bias);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000051 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32, DataType::F16, DataType::F32);
52
53 if(is_data_type_quantized_asymmetric(input->data_type()))
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
56 }
57 else
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
60 }
61
62 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
63 }
64 else
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_float(input->data_type()),
67 "Calling output stage kernel with floating point arguments");
68 }
69
70 // Checks performed on output
71 if(input->data_type() == DataType::S32)
72 {
73 // Quantized configuration checks
74 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
75 }
76 else
77 {
78 // In case of out-of-place computation (supported for non-quantized configurations)
79 if((output != nullptr) && (output->total_size() != 0))
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
82 }
83 }
84
85 return Status{};
86}
87
88std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output)
89{
90 bool window_changed = false;
91 unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->data_type());
92
Georgios Pinitas5b52fe32018-07-12 12:42:35 +010093 // Configure kernel window
94 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
95
96 // Input window
97 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
98 window_changed = window_changed || update_window_and_padding(win, input_access);
99
100 // Bias window
101 if(bias != nullptr)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000102 {
Georgios Pinitas5b52fe32018-07-12 12:42:35 +0100103 AccessWindowStatic bias_access(bias, 0, 0, ceil_to_multiple(bias->dimension(0), num_elems_processed_per_iteration), bias->dimension(1));
104 window_changed = window_changed || update_window_and_padding(win, bias_access);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000105 }
106
Georgios Pinitas5b52fe32018-07-12 12:42:35 +0100107 // Output window
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000108 if(output != nullptr && (output->total_size() != 0))
109 {
110 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
Georgios Pinitas5b52fe32018-07-12 12:42:35 +0100111 window_changed = window_changed || update_window_and_padding(win, output_access);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000112 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
113 }
114 else
115 {
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000116 input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape()));
117 }
118
119 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
120 return std::make_pair(err, win);
121}
122} // namespace
123
124CLDirectConvolutionLayerOutputStageKernel::CLDirectConvolutionLayerOutputStageKernel()
125 : _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0)
126{
127}
128
129void CLDirectConvolutionLayerOutputStageKernel::configure(ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
130 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift)
131{
132 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
133
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000134 // Auto-initialize output if required
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000135 if(output != nullptr)
136 {
137 // Work out expected output data type
138 const DataType output_dt = (input->info()->data_type() == DataType::S32) ? DataType::QASYMM8 : input->info()->data_type();
139 // Output tensor auto initialization if not yet initialized
140 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt));
141 }
142
143 // Perform validation step
144 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info()));
145
146 _bias = bias;
147 _input = input;
148 _output = output;
149 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
150 _result_shift = result_shift;
151 _result_offset_after_shift = result_offset_after_shift;
152
Georgios Pinitas5b52fe32018-07-12 12:42:35 +0100153 const unsigned int num_elems_accessed_per_iteration = 16 / element_size_from_data_type(input->info()->data_type());
154
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000155 // Create kernel
156 CLBuildOptions build_opts;
157 build_opts.add_option_if(bias != nullptr, "-DHAS_BIAS");
Giorgio Arenad051e972018-06-20 11:46:42 +0100158 build_opts.add_option("-D" + string_from_data_layout(input->info()->data_layout()));
Georgios Pinitas5b52fe32018-07-12 12:42:35 +0100159 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_accessed_per_iteration));
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000160 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("output_stage_quantized", build_opts.options()));
161
162 // Set static kernel arguments
163 int idx = 2 * num_arguments_per_3D_tensor() + ((bias != nullptr) ? num_arguments_per_1D_tensor() : 0);
164 _kernel.setArg<int>(idx++, _result_offset_after_shift);
165 _kernel.setArg<int>(idx++, _result_fixedpoint_multiplier);
166 _kernel.setArg<int>(idx++, _result_shift);
167
168 // Configure kernel window
169 auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info());
170 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100171 ICLKernel::configure_internal(win_config.second);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000172}
173
174Status CLDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output)
175{
176 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output));
Anthony Barbierde014682018-07-03 15:10:48 +0100177 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias == nullptr ? nullptr : bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000178
179 return Status{};
180}
181
182void CLDirectConvolutionLayerOutputStageKernel::run(const Window &window, cl::CommandQueue &queue)
183{
184 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
185 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
186
187 Window slice = window.first_slice_window_3D();
188
189 // Set bias vector
190 if(_bias != nullptr)
191 {
192 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
193 Window slice_biases;
194 slice_biases.use_tensor_dimensions(_bias->info()->tensor_shape());
195 add_1D_tensor_argument(idx1, _bias, slice_biases);
196 }
197
198 // Run kernel
199 do
200 {
201 // Set arguments
202 unsigned int idx = 0;
203 add_3D_tensor_argument(idx, _input, slice);
204 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100205 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000206 }
207 while(window.slide_window_slice_3D(slice));
208}