blob: c6147ee3188268cbd8bdaf154ebf6a1f0e84afa8 [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"
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);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000045 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16,
46 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
93 // Update processed elements when input is S32 (comes from quantization input)
94 if(input->data_type() == DataType::S32)
95 {
96 num_elems_processed_per_iteration = 16;
97 }
98
99 // Configure kernel window
100 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
101 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
102
103 if(output != nullptr && (output->total_size() != 0))
104 {
105 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
106
107 if(bias == nullptr)
108 {
109 window_changed = update_window_and_padding(win, input_access, output_access);
110 }
111 else
112 {
113 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
114 window_changed = update_window_and_padding(win, input_access, output_access, bias_access);
115 }
116
117 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
118 }
119 else
120 {
121 if(bias == nullptr)
122 {
123 window_changed = update_window_and_padding(win, input_access);
124 }
125 else
126 {
127 AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1));
128 window_changed = update_window_and_padding(win, input_access, bias_access);
129 }
130
131 input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape()));
132 }
133
134 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
135 return std::make_pair(err, win);
136}
137} // namespace
138
139CLDirectConvolutionLayerOutputStageKernel::CLDirectConvolutionLayerOutputStageKernel()
140 : _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0)
141{
142}
143
144void CLDirectConvolutionLayerOutputStageKernel::configure(ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
145 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift)
146{
147 ARM_COMPUTE_ERROR_ON_NULLPTR(input);
148
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000149 // Auto-initialize output if required
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000150 if(output != nullptr)
151 {
152 // Work out expected output data type
153 const DataType output_dt = (input->info()->data_type() == DataType::S32) ? DataType::QASYMM8 : input->info()->data_type();
154 // Output tensor auto initialization if not yet initialized
155 auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt));
156 }
157
158 // Perform validation step
159 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info()));
160
161 _bias = bias;
162 _input = input;
163 _output = output;
164 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
165 _result_shift = result_shift;
166 _result_offset_after_shift = result_offset_after_shift;
167
168 // Create kernel
169 CLBuildOptions build_opts;
170 build_opts.add_option_if(bias != nullptr, "-DHAS_BIAS");
Giorgio Arenad051e972018-06-20 11:46:42 +0100171 build_opts.add_option("-D" + string_from_data_layout(input->info()->data_layout()));
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000172 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("output_stage_quantized", build_opts.options()));
173
174 // Set static kernel arguments
175 int idx = 2 * num_arguments_per_3D_tensor() + ((bias != nullptr) ? num_arguments_per_1D_tensor() : 0);
176 _kernel.setArg<int>(idx++, _result_offset_after_shift);
177 _kernel.setArg<int>(idx++, _result_fixedpoint_multiplier);
178 _kernel.setArg<int>(idx++, _result_shift);
179
180 // Configure kernel window
181 auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info());
182 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
183 ICLKernel::configure(win_config.second);
184}
185
186Status CLDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output)
187{
188 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output));
189 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first);
190
191 return Status{};
192}
193
194void CLDirectConvolutionLayerOutputStageKernel::run(const Window &window, cl::CommandQueue &queue)
195{
196 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
197 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
198
199 Window slice = window.first_slice_window_3D();
200
201 // Set bias vector
202 if(_bias != nullptr)
203 {
204 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
205 Window slice_biases;
206 slice_biases.use_tensor_dimensions(_bias->info()->tensor_shape());
207 add_1D_tensor_argument(idx1, _bias, slice_biases);
208 }
209
210 // Run kernel
211 do
212 {
213 // Set arguments
214 unsigned int idx = 0;
215 add_3D_tensor_argument(idx, _input, slice);
216 add_3D_tensor_argument(idx, _output, slice);
217 enqueue(queue, *this, slice, _lws_hint);
218 }
219 while(window.slide_window_slice_3D(slice));
220}