blob: 732d768308602fbc1f2ed97e75d520993d3d4d5a [file] [log] [blame]
Gian Marco Iodice9285adb2019-09-05 16:10:27 +01001/*
Gunes Bayirfdb53422022-05-25 10:09:39 +01002 * Copyright (c) 2019-2022 Arm Limited.
Gian Marco Iodice9285adb2019-09-05 16:10:27 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h"
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010025
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010028#include "arm_compute/core/CL/ICLTensor.h"
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010031#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michele Di Giorgioa046e162019-10-08 09:36:26 +010033#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Gian Marco Iodice8155c022021-04-16 15:08:59 +010034#include "src/core/CL/CLUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CL/CLValidate.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036#include "src/core/CL/ICLKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010039#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000040#include "support/StringSupport.h"
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010041
42namespace arm_compute
43{
44namespace
45{
Gian Marco Iodice8155c022021-04-16 15:08:59 +010046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const DWCComputeKernelInfo &dwc_info,
47 const ConvolutionInfo &conv_info, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010048{
Gian Marco Iodicec63b7222021-06-30 08:39:44 +000049 ARM_COMPUTE_UNUSED(dwc_info);
SiCongLibc4e3112021-06-29 13:18:30 +010050 bool in_place = false;
51 if(output == nullptr || output == input)
52 {
53 in_place = true;
54 output = input;
55 }
56 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights);
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010057 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
58 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
Michele Di Giorgio4cd4cde2020-01-06 14:07:44 +000059 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Gian Marco Iodice8155c022021-04-16 15:08:59 +010060 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.depth_multiplier > 1 && dwc_info.n0 != 1);
61 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_stride_info.stride().first > 1 && dwc_info.m0 != 1);
62 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.dilation.x() > 1 && dwc_info.m0 != 1);
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG((dwc_info.export_weights_to_cl_image == true) && (export_weights_to_cl_image(weights) == false), "Export to cl_image not supported!");
64 ARM_COMPUTE_RETURN_ERROR_ON((dwc_info.export_weights_to_cl_image == true) && (conv_info.depth_multiplier > 1));
65 ARM_COMPUTE_RETURN_ERROR_ON((dwc_info.export_weights_to_cl_image == true) && ((dwc_info.n0 % 4) != 0));
66 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_stride_info.stride().first < 1);
67 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_stride_info.stride().second < 1);
68 ARM_COMPUTE_RETURN_ERROR_ON((conv_info.dilation.x() < 1) || (conv_info.dilation.y() < 1));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010069 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
70 ARM_COMPUTE_UNUSED(idx_c);
Gian Marco Iodice8155c022021-04-16 15:08:59 +010071 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_c) != (input->dimension(idx_c) * conv_info.depth_multiplier));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010072
SiCongLibc4e3112021-06-29 13:18:30 +010073 // In place restrictions
74 if(in_place)
75 {
76 const int weights_width_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH);
77 const int weights_height_idx = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::HEIGHT);
78 ARM_COMPUTE_RETURN_ERROR_ON(weights->tensor_shape()[weights_width_idx] != 1U || weights->tensor_shape()[weights_height_idx] != 1U);
Gian Marco Iodice8155c022021-04-16 15:08:59 +010079 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.depth_multiplier != 1U);
80 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_stride_info.stride() != std::make_pair(1U, 1U));
81 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.dilation != Size2D(1U, 1U));
82 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.pad_stride_info.has_padding()); // Note that in princple padding can be supported with in_place but we choose not to support it
SiCongLibc4e3112021-06-29 13:18:30 +010083 }
84
Gian Marco Iodice8155c022021-04-16 15:08:59 +010085 const ConvolutionInfo info{ conv_info.pad_stride_info, conv_info.depth_multiplier, ActivationLayerInfo(), conv_info.dilation };
Gian Marco Iodice5e281812021-07-06 13:19:41 +010086 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010087
88 const bool is_quantized = is_data_type_quantized(input->data_type());
89
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010090 if(biases != nullptr)
91 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010092 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != output_shape[idx_c]);
Gian Marco Iodice9285adb2019-09-05 16:10:27 +010093 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Michele Di Giorgioa046e162019-10-08 09:36:26 +010094
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010095 if(is_quantized)
Michele Di Giorgioa046e162019-10-08 09:36:26 +010096 {
97 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
98 }
99 else
100 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100102 }
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100103 }
104
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100105 if(is_quantized)
106 {
107 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output_multipliers, output_shifts);
108 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_multipliers, 1, DataType::S32);
109 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_shifts, 1, DataType::S32);
110 ARM_COMPUTE_RETURN_ERROR_ON(output_multipliers->num_dimensions() > 1);
111 ARM_COMPUTE_RETURN_ERROR_ON(output_shifts->num_dimensions() > 1);
112
113 if(is_data_type_quantized_per_channel(weights->data_type()))
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
116 ARM_COMPUTE_RETURN_ERROR_ON(output_shape[idx_c] != output_multipliers->dimension(0));
117 ARM_COMPUTE_RETURN_ERROR_ON(output_shape[idx_c] != output_shifts->dimension(0));
118 }
119 else
120 {
121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
122 ARM_COMPUTE_RETURN_ERROR_ON(1 != output_multipliers->dimension(0));
123 ARM_COMPUTE_RETURN_ERROR_ON(1 != output_shifts->dimension(0));
124 }
125 }
126 else
127 {
128 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
129 }
130
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100131 if(output->total_size() != 0)
132 {
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100133 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Michele Di Giorgio4cd4cde2020-01-06 14:07:44 +0000134 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100135 }
136
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100137 if(is_data_type_quantized(input->data_type()))
138 {
139 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
140 const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
141 const UniformQuantizationInfo oq_info = (output->total_size() != 0) ? output->quantization_info().uniform() : iq_info;
142
143 float multiplier = iq_info.scale * wq_info.scale / oq_info.scale;
144 int output_multiplier = 0;
145 int output_shift = 0;
146 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
147 }
148
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100149 return Status{};
150}
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100151} // namespace
152
153CLDepthwiseConvolutionLayerNativeKernel::CLDepthwiseConvolutionLayerNativeKernel()
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100154 : _input(nullptr),
155 _weights(nullptr),
156 _biases(nullptr),
157 _output(nullptr),
158 _depth_multiplier(1),
159 _output_multipliers(nullptr),
160 _output_shifts(nullptr),
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100161 _export_to_cl_image(false),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100162 _is_quantized(false)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100163{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100164 _type = CLKernelType::DEPTHWISE;
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100165}
166
SiCongLibc4e3112021-06-29 13:18:30 +0100167void CLDepthwiseConvolutionLayerNativeKernel::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100168 const DWCComputeKernelInfo &dwc_info, const ConvolutionInfo &conv_info,
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100169 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100170{
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100171 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, dwc_info, conv_info, output_multipliers, output_shifts);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100172}
173
SiCongLibc4e3112021-06-29 13:18:30 +0100174void CLDepthwiseConvolutionLayerNativeKernel::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100175 const DWCComputeKernelInfo &dwc_info, const ConvolutionInfo &conv_info,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100176 const ICLTensor *output_multipliers, const ICLTensor *output_shifts)
177{
SiCongLibc4e3112021-06-29 13:18:30 +0100178 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
179 if(output == nullptr)
180 {
181 // In-place
182 output = input;
183 }
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100184 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(),
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100185 dwc_info, conv_info, (output_multipliers != nullptr) ? output_multipliers->info() : nullptr, (output_shifts != nullptr) ? output_shifts->info() : nullptr));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100186
Giorgio Arena79acd772020-10-22 14:29:50 +0100187 auto padding_info = get_padding_info({ input, output });
188
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100189 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_depthwise_convolution_shape(*(input->info()), *(weights->info()), conv_info);
Giorgio Arena79acd772020-10-22 14:29:50 +0100190 auto_init_if_empty(*(output->info()), input->info()->clone()->set_tensor_shape(output_shape).set_quantization_info(output->info()->quantization_info()));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100191
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100192 _input = input;
193 _output = output;
194 _weights = weights;
195 _biases = biases;
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100196 _depth_multiplier = conv_info.depth_multiplier;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100197 _output_multipliers = output_multipliers;
198 _output_shifts = output_shifts;
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100199 _export_to_cl_image = dwc_info.export_weights_to_cl_image;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100200 _is_quantized = is_data_type_quantized(input->info()->data_type());
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100201
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100202 const unsigned int n0 = adjust_vec_size(dwc_info.n0, input->info()->dimension(0));
203 const unsigned int m0 = std::min(dwc_info.m0, (unsigned int)output->info()->dimension(1));
204 std::string kernel_name = "";
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100205
206 CLBuildOptions build_opts;
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000207
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100208 // Update the padding for the weights tensor if we can export to cl_image
209 if(_export_to_cl_image)
210 {
211 arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(weights->info());
212 }
213
Gian Marco Iodice12571312022-08-25 12:25:44 +0100214 // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324
Gunes Bayirfdb53422022-05-25 10:09:39 +0100215 const GPUTarget gpu_target = get_target();
216 const auto act_function = conv_info.act_info.activation();
217 const auto dst_data_type = _output->info()->data_type();
218
219 if((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST)
220 && (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU || act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
221 && (dst_data_type == DataType::F32 || dst_data_type == DataType::F16))
222 {
223 // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations
224 // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations
225 build_opts.add_option("-cl-unsafe-math-optimizations");
226 }
227 else
228 {
229 build_opts.add_option("-cl-fast-relaxed-math");
230 }
231
232 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function)));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100233 build_opts.add_option("-DDEPTH_MULTIPLIER=" + support::cpp11::to_string(conv_info.depth_multiplier));
234 build_opts.add_option("-DSRC_TENSOR_TYPE=BUFFER");
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100235 // Note: SRC_DATA_TYPE must have the same data type of WEI_DATA_TYPE. In quantized, we could
236 // have a case where the data types for the activation and weights are different. However, since the implementation
237 // only works when both have same data type, we have to change the offset to take into account this aspect
238 build_opts.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(_input->info()->data_type()));
239 build_opts.add_option("-DDST_TENSOR_TYPE=BUFFER");
Gunes Bayirfdb53422022-05-25 10:09:39 +0100240 build_opts.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst_data_type));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100241 build_opts.add_option_if_else(_export_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER");
242 build_opts.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(_weights->info()->dimension(1)));
243 build_opts.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(_weights->info()->dimension(2)));
244 build_opts.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(_weights->info()->data_type()));
245 build_opts.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_stride_info.pad_top()));
246 build_opts.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_stride_info.pad_left()));
247 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_info.pad_stride_info.stride().first));
248 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_info.pad_stride_info.stride().second));
249 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(conv_info.dilation.x()));
250 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(conv_info.dilation.y()));
251 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
252 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100253 build_opts.add_option("-DM0_A=" + support::cpp11::to_string(_weights->info()->dimension(1) + m0 - 1));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100254 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(_input->info()->dimension(0) % n0));
255 build_opts.add_option_if(_input->info()->num_dimensions() > 3, "-DBATCHED_EXECUTION");
Gian Marco Iodice5e281812021-07-06 13:19:41 +0100256
257 // Force unroll with pragma when any of the following values exceed the maximum number of manual unroll
258 set_unroll_with_pragma(build_opts, { static_cast<int>(_weights->info()->dimension(1) + m0 - 1),
259 static_cast<int>(_weights->info()->dimension(1)),
260 static_cast<int>(_weights->info()->dimension(2))
261 });
262
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100263 if(biases != nullptr)
264 {
265 build_opts.add_option(std::string("-DHAS_BIAS"));
266 build_opts.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->info()->data_type())));
267 }
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100268
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100269 if(_is_quantized)
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100270 {
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100271 kernel_name = "dwc_native_quantized_nhwc";
272 const UniformQuantizationInfo iqinfo = input->info()->quantization_info().uniform();
273 const UniformQuantizationInfo wqinfo = weights->info()->quantization_info().uniform();
274 const UniformQuantizationInfo oqinfo = output->info()->quantization_info().uniform();
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100275
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100276 PixelValue zero_value = PixelValue(0, input->info()->data_type(), input->info()->quantization_info());
277 int zero_value_s32;
278 zero_value.get(zero_value_s32);
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100279
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100280 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100281 int output_multiplier = 0;
282 int output_shift = 0;
283 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100284 build_opts.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
285 build_opts.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
286 build_opts.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
287 build_opts.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
288 build_opts.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
289 build_opts.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
290 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
291 build_opts.add_option("-DDST_MULTIPLIERS_DATA_TYPE=" + get_cl_type_from_data_type(_output_multipliers->info()->data_type()));
292 build_opts.add_option("-DDST_SHIFTS_DATA_TYPE=" + get_cl_type_from_data_type(_output_shifts->info()->data_type()));
293 build_opts.add_option_if_else(weights->info()->data_type() == DataType::QSYMM8_PER_CHANNEL, "-DQUANTIZATION_TYPE=PER_CHANNEL", "-DQUANTIZATION_TYPE=PER_TENSOR");
294 // Note: We expect the input and output tensors to always adopt a per-tensor quantization approach
295 int a_val{};
296 int b_val{};
297 std::tie(b_val, a_val) = get_quantized_activation_min_max(conv_info.act_info, input->info()->data_type(), oqinfo);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100298
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100299 build_opts.add_option_if(conv_info.act_info.enabled(), "-DA_VAL=" + support::cpp11::to_string(a_val));
300 build_opts.add_option_if(conv_info.act_info.enabled(), "-DB_VAL=" + support::cpp11::to_string(b_val));
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100301 }
302 else
303 {
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100304 kernel_name = "dwc_native_fp_nhwc";
305 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100306 build_opts.add_option_if(conv_info.act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(conv_info.act_info.a()));
307 build_opts.add_option_if(conv_info.act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(conv_info.act_info.b()));
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100308 }
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100309
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100310 Window win = calculate_max_window(*(output->info()), Steps(n0, m0));
Giorgio Arena79acd772020-10-22 14:29:50 +0100311 ICLKernel::configure_internal(win);
312
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100313 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100314
Giorgio Arena79acd772020-10-22 14:29:50 +0100315 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
316
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100317 // Set config_id for enabling LWS tuning
318 _config_id = kernel_name;
319 _config_id += "_";
320 _config_id += support::cpp11::to_string(input->info()->dimension(0));
321 _config_id += "_";
322 _config_id += support::cpp11::to_string(input->info()->dimension(1));
323 _config_id += "_";
324 _config_id += support::cpp11::to_string(input->info()->dimension(2));
325 _config_id += "_";
326 _config_id += support::cpp11::to_string(output->info()->dimension(0));
327 _config_id += "_";
328 _config_id += support::cpp11::to_string(output->info()->dimension(1));
329 _config_id += "_";
330 _config_id += support::cpp11::to_string(output->info()->dimension(2));
331 _config_id += "_";
332 _config_id += string_from_data_type(input->info()->data_type());
333}
334
335Status CLDepthwiseConvolutionLayerNativeKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100336 const DWCComputeKernelInfo &dwc_info, const ConvolutionInfo &conv_info, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts)
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100337{
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100338 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, dwc_info, conv_info, output_multipliers, output_shifts));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100339 return Status{};
340}
341
342void CLDepthwiseConvolutionLayerNativeKernel::run(const Window &window, cl::CommandQueue &queue)
343{
344 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
345 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
346
347 // Collapse window
348 Window window_collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100349
350 Window slice = window_collapsed.first_slice_window_4D();
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100351
352 if(_depth_multiplier != 1)
353 {
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100354 // If the depth multiplier > 1, we need to use the input channels rather than the output channels
355 ARM_COMPUTE_ERROR_ON(slice.x().step() != 1);
356 slice.set(Window::DimX, Window::Dimension(0, _input->info()->tensor_shape()[0], 1));
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100357 }
358
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100359 cl::Image2D weights_cl_image;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100360
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100361 if(_export_to_cl_image)
362 {
363 const size_t image_w = _weights->info()->dimension(0) / 4;
364 const size_t image_h = _weights->info()->dimension(1) * _weights->info()->dimension(2) * _weights->info()->dimension(3);
365 const TensorShape shape2d(image_w, image_h);
366 const size_t image_row_pitch = _weights->info()->strides_in_bytes()[1];
367
368 // Export cl_buffer to cl_image
369 weights_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), _weights->cl_buffer(), shape2d, _weights->info()->data_type(), image_row_pitch);
370 }
371
372 unsigned int idx = 0;
Sheri Zhange3b19742021-11-10 13:08:40 +0000373 add_4d_tensor_nhwc_argument(idx, _input);
374 add_4d_tensor_nhwc_argument(idx, _output);
375
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100376 if(_export_to_cl_image)
377 {
378 _kernel.setArg(idx++, weights_cl_image);
379 }
380 add_4D_tensor_argument(idx, _weights, slice);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100381 if(_is_quantized)
382 {
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100383 add_1D_tensor_argument(idx, _output_multipliers, slice);
384 add_1D_tensor_argument(idx, _output_shifts, slice);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100385 }
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100386 if(_biases != nullptr)
387 {
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100388 add_1D_tensor_argument(idx, _biases, slice);
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100389 }
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100390 enqueue(queue, *this, slice, lws_hint());
Gian Marco Iodice9285adb2019-09-05 16:10:27 +0100391}
392} // namespace arm_compute