blob: f23ecf3ad0eefa9ab4860abeba3fe5da3035af64 [file] [log] [blame]
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +00001/*
2 * Copyright (c) 2018 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 "arm_compute/core/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#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);
44 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16,
45 DataType::F32);
46
47 if(bias != nullptr)
48 {
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32, DataType::F16, DataType::F32);
50
51 if(is_data_type_quantized_asymmetric(input->data_type()))
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
54 }
55 else
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
58 }
59
60 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
61 }
62 else
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_float(input->data_type()),
65 "Calling output stage kernel with floating point arguments");
66 }
67
68 // Checks performed on output
69 if(input->data_type() == DataType::S32)
70 {
71 // Quantized configuration checks
72 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
73 }
74 else
75 {
76 // In case of out-of-place computation (supported for non-quantized configurations)
77 if((output != nullptr) && (output->total_size() != 0))
78 {
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
80 }
81 }
82
83 return Status{};
84}
85
86std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output)
87{
88 bool window_changed = false;
89 unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->data_type());
90
91 // Update processed elements when input is S32 (comes from quantization input)
92 if(input->data_type() == DataType::S32)
93 {
94 num_elems_processed_per_iteration = 16;
95 }
96
97 // Configure kernel window
98 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
99 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
100
101 if(output != nullptr && (output->total_size() != 0))
102 {
103 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
104
105 if(bias == nullptr)
106 {
107 window_changed = update_window_and_padding(win, input_access, output_access);
108 }
109 else
110 {
111 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
112 window_changed = update_window_and_padding(win, input_access, output_access, bias_access);
113 }
114
115 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
116 }
117 else
118 {
119 if(bias == nullptr)
120 {
121 window_changed = update_window_and_padding(win, input_access);
122 }
123 else
124 {
125 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
126 window_changed = update_window_and_padding(win, input_access, bias_access);
127 }
128
129 input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape()));
130 }
131
132 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
133 return std::make_pair(err, win);
134}
135} // namespace
136
137CLDirectConvolutionLayerOutputStageKernel::CLDirectConvolutionLayerOutputStageKernel()
138 : _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0)
139{
140}
141
142void CLDirectConvolutionLayerOutputStageKernel::configure(ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
143 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift)
144{
145 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
146
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000147 // Auto-initialize output if required
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000148 if(output != nullptr)
149 {
150 // Work out expected output data type
151 const DataType output_dt = (input->info()->data_type() == DataType::S32) ? DataType::QASYMM8 : input->info()->data_type();
152 // Output tensor auto initialization if not yet initialized
153 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt));
154 }
155
156 // Perform validation step
157 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info()));
158
159 _bias = bias;
160 _input = input;
161 _output = output;
162 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
163 _result_shift = result_shift;
164 _result_offset_after_shift = result_offset_after_shift;
165
166 // Create kernel
167 CLBuildOptions build_opts;
168 build_opts.add_option_if(bias != nullptr, "-DHAS_BIAS");
169 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("output_stage_quantized", build_opts.options()));
170
171 // Set static kernel arguments
172 int idx = 2 * num_arguments_per_3D_tensor() + ((bias != nullptr) ? num_arguments_per_1D_tensor() : 0);
173 _kernel.setArg<int>(idx++, _result_offset_after_shift);
174 _kernel.setArg<int>(idx++, _result_fixedpoint_multiplier);
175 _kernel.setArg<int>(idx++, _result_shift);
176
177 // Configure kernel window
178 auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info());
179 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
180 ICLKernel::configure(win_config.second);
181}
182
183Status CLDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output)
184{
185 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output));
186 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first);
187
188 return Status{};
189}
190
191void CLDirectConvolutionLayerOutputStageKernel::run(const Window &window, cl::CommandQueue &queue)
192{
193 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
194 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
195
196 Window slice = window.first_slice_window_3D();
197
198 // Set bias vector
199 if(_bias != nullptr)
200 {
201 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
202 Window slice_biases;
203 slice_biases.use_tensor_dimensions(_bias->info()->tensor_shape());
204 add_1D_tensor_argument(idx1, _bias, slice_biases);
205 }
206
207 // Run kernel
208 do
209 {
210 // Set arguments
211 unsigned int idx = 0;
212 add_3D_tensor_argument(idx, _input, slice);
213 add_3D_tensor_argument(idx, _output, slice);
214 enqueue(queue, *this, slice, _lws_hint);
215 }
216 while(window.slide_window_slice_3D(slice));
217}