1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
28*c217d954SCole Faust #include "arm_compute/core/Utils.h"
29*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
32*c217d954SCole Faust #include "src/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h"
33*c217d954SCole Faust #include "src/runtime/heuristics/dwc_native/ClDWCNativeKernelConfig.h"
34*c217d954SCole Faust #include "src/runtime/heuristics/dwc_native/IClDWCNativeKernelConfig.h"
35*c217d954SCole Faust
36*c217d954SCole Faust #include "src/common/utils/Log.h"
37*c217d954SCole Faust
38*c217d954SCole Faust namespace arm_compute
39*c217d954SCole Faust {
40*c217d954SCole Faust using namespace arm_compute::misc;
41*c217d954SCole Faust using namespace arm_compute::misc::shape_calculator;
42*c217d954SCole Faust using namespace arm_compute::cl_dwc;
43*c217d954SCole Faust
CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)44*c217d954SCole Faust CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
45*c217d954SCole Faust : _memory_group(std::move(memory_manager)),
46*c217d954SCole Faust _dwc_native_kernel(std::make_unique<CLDepthwiseConvolutionLayerNativeKernel>()),
47*c217d954SCole Faust _permute_input_to_nhwc(),
48*c217d954SCole Faust _permute_weights_to_nhwc(),
49*c217d954SCole Faust _permute_output_to_nchw(),
50*c217d954SCole Faust _permuted_input(),
51*c217d954SCole Faust _permuted_weights(),
52*c217d954SCole Faust _permuted_output(),
53*c217d954SCole Faust _output_multipliers(),
54*c217d954SCole Faust _output_shifts(),
55*c217d954SCole Faust _original_weights(),
56*c217d954SCole Faust _input(),
57*c217d954SCole Faust _output(),
58*c217d954SCole Faust _needs_permute(false),
59*c217d954SCole Faust _is_prepared(false),
60*c217d954SCole Faust _is_quantized(false)
61*c217d954SCole Faust {
62*c217d954SCole Faust }
63*c217d954SCole Faust
64*c217d954SCole Faust CLDepthwiseConvolutionLayer::~CLDepthwiseConvolutionLayer() = default;
65*c217d954SCole Faust
configure(ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,unsigned int depth_multiplier,ActivationLayerInfo act_info,const Size2D & dilation)66*c217d954SCole Faust void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
67*c217d954SCole Faust unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
68*c217d954SCole Faust {
69*c217d954SCole Faust configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
70*c217d954SCole Faust }
71*c217d954SCole Faust
configure(const CLCompileContext & compile_context,ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,unsigned int depth_multiplier,ActivationLayerInfo act_info,const Size2D & dilation)72*c217d954SCole Faust void CLDepthwiseConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases,
73*c217d954SCole Faust ICLTensor *output, const PadStrideInfo &conv_info,
74*c217d954SCole Faust unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
75*c217d954SCole Faust {
76*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
77*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(),
78*c217d954SCole Faust weights->info(),
79*c217d954SCole Faust biases != nullptr ? biases->info() : nullptr,
80*c217d954SCole Faust output != nullptr ? output->info() : input->info(),
81*c217d954SCole Faust conv_info,
82*c217d954SCole Faust depth_multiplier,
83*c217d954SCole Faust act_info,
84*c217d954SCole Faust dilation));
85*c217d954SCole Faust ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
86*c217d954SCole Faust
87*c217d954SCole Faust _is_quantized = is_data_type_quantized(input->info()->data_type());
88*c217d954SCole Faust _is_prepared = false;
89*c217d954SCole Faust _original_weights = weights;
90*c217d954SCole Faust _input = input;
91*c217d954SCole Faust _output = output;
92*c217d954SCole Faust _needs_permute = input->info()->data_layout() == DataLayout::NCHW;
93*c217d954SCole Faust
94*c217d954SCole Faust const GPUTarget gpu_target = CLScheduler::get().target();
95*c217d954SCole Faust
96*c217d954SCole Faust ICLTensor *input_to_use = input;
97*c217d954SCole Faust const ICLTensor *weights_to_use = weights;
98*c217d954SCole Faust ICLTensor *output_to_use = output;
99*c217d954SCole Faust if(_needs_permute)
100*c217d954SCole Faust {
101*c217d954SCole Faust _memory_group.manage(&_permuted_input);
102*c217d954SCole Faust _memory_group.manage(&_permuted_output);
103*c217d954SCole Faust
104*c217d954SCole Faust // Configure the function to transform the input tensor from NCHW -> NHWC
105*c217d954SCole Faust _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U));
106*c217d954SCole Faust _permuted_input.info()->set_data_layout(DataLayout::NHWC);
107*c217d954SCole Faust
108*c217d954SCole Faust // Configure the function to transform the weights tensor from IHW -> HWI
109*c217d954SCole Faust _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
110*c217d954SCole Faust _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
111*c217d954SCole Faust
112*c217d954SCole Faust // Set output quantization info before dwc kernel configure
113*c217d954SCole Faust _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
114*c217d954SCole Faust
115*c217d954SCole Faust input_to_use = &_permuted_input;
116*c217d954SCole Faust weights_to_use = &_permuted_weights;
117*c217d954SCole Faust output_to_use = &_permuted_output;
118*c217d954SCole Faust }
119*c217d954SCole Faust
120*c217d954SCole Faust CLTensor *output_multipliers_to_use = nullptr;
121*c217d954SCole Faust CLTensor *output_shifts_to_use = nullptr;
122*c217d954SCole Faust if(_is_quantized)
123*c217d954SCole Faust {
124*c217d954SCole Faust const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL);
125*c217d954SCole Faust const size_t num_filters = (is_data_type_quantized_per_channel(weights->info()->data_type())) ? weights->info()->dimension(idx_c) : 1;
126*c217d954SCole Faust
127*c217d954SCole Faust _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
128*c217d954SCole Faust _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32));
129*c217d954SCole Faust
130*c217d954SCole Faust output_multipliers_to_use = &_output_multipliers;
131*c217d954SCole Faust output_shifts_to_use = &_output_shifts;
132*c217d954SCole Faust }
133*c217d954SCole Faust
134*c217d954SCole Faust // Get the depthwise convolution compute parameters
135*c217d954SCole Faust auto t = ClDWCNativeKernelConfigurationFactory::create(gpu_target);
136*c217d954SCole Faust const DWCComputeKernelInfo dwc_native_compute_info = t->configure(input_to_use->info(), weights_to_use->info(), conv_info, dilation, depth_multiplier);
137*c217d954SCole Faust
138*c217d954SCole Faust const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation };
139*c217d954SCole Faust
140*c217d954SCole Faust _dwc_native_kernel->set_target(gpu_target);
141*c217d954SCole Faust _dwc_native_kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use,
142*c217d954SCole Faust dwc_native_compute_info, conv_kernel_info, output_multipliers_to_use, output_shifts_to_use);
143*c217d954SCole Faust
144*c217d954SCole Faust if(_needs_permute)
145*c217d954SCole Faust {
146*c217d954SCole Faust _permuted_input.allocator()->allocate();
147*c217d954SCole Faust
148*c217d954SCole Faust // Configure the function to transform the convoluted output to NCHW format
149*c217d954SCole Faust _permuted_output.info()->set_data_layout(DataLayout::NCHW);
150*c217d954SCole Faust _permute_output_to_nchw.configure(compile_context, &_permuted_output, output, PermutationVector(1U, 2U, 0U));
151*c217d954SCole Faust _permuted_output.allocator()->allocate();
152*c217d954SCole Faust }
153*c217d954SCole Faust
154*c217d954SCole Faust if(_is_quantized)
155*c217d954SCole Faust {
156*c217d954SCole Faust _output_multipliers.allocator()->allocate();
157*c217d954SCole Faust _output_shifts.allocator()->allocate();
158*c217d954SCole Faust }
159*c217d954SCole Faust }
160*c217d954SCole Faust
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & conv_info,unsigned int depth_multiplier,ActivationLayerInfo act_info,const Size2D & dilation)161*c217d954SCole Faust Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
162*c217d954SCole Faust const PadStrideInfo &conv_info,
163*c217d954SCole Faust unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
164*c217d954SCole Faust {
165*c217d954SCole Faust const bool in_place = input == output || output == nullptr;
166*c217d954SCole Faust if(in_place)
167*c217d954SCole Faust {
168*c217d954SCole Faust output = input;
169*c217d954SCole Faust }
170*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
171*c217d954SCole Faust const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
172*c217d954SCole Faust const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
173*c217d954SCole Faust
174*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (dilation.x() - 1) > input->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
175*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (dilation.y() - 1) > input->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
176*c217d954SCole Faust
177*c217d954SCole Faust const GPUTarget gpu_target = CLScheduler::get().target();
178*c217d954SCole Faust
179*c217d954SCole Faust const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation };
180*c217d954SCole Faust
181*c217d954SCole Faust const bool needs_permute = input->data_layout() == DataLayout::NCHW;
182*c217d954SCole Faust
183*c217d954SCole Faust const bool is_quantized = is_data_type_quantized(input->data_type());
184*c217d954SCole Faust
185*c217d954SCole Faust TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32));
186*c217d954SCole Faust if(is_quantized)
187*c217d954SCole Faust {
188*c217d954SCole Faust if(is_data_type_quantized_per_channel(weights->data_type()))
189*c217d954SCole Faust {
190*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL);
191*c217d954SCole Faust
192*c217d954SCole Faust const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL);
193*c217d954SCole Faust output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c)));
194*c217d954SCole Faust }
195*c217d954SCole Faust else
196*c217d954SCole Faust {
197*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
198*c217d954SCole Faust }
199*c217d954SCole Faust }
200*c217d954SCole Faust
201*c217d954SCole Faust if(needs_permute)
202*c217d954SCole Faust {
203*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(in_place, "In-place is supported only with NHWC data layout");
204*c217d954SCole Faust TensorShape permuted_input_shape = input->tensor_shape();
205*c217d954SCole Faust TensorShape permuted_weights_shape = weights->tensor_shape();
206*c217d954SCole Faust const ConvolutionInfo info{ conv_info, depth_multiplier, ActivationLayerInfo(), dilation };
207*c217d954SCole Faust TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, info);
208*c217d954SCole Faust
209*c217d954SCole Faust permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
210*c217d954SCole Faust permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
211*c217d954SCole Faust permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
212*c217d954SCole Faust
213*c217d954SCole Faust const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC);
214*c217d954SCole Faust const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC);
215*c217d954SCole Faust const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC);
216*c217d954SCole Faust
217*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
218*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
219*c217d954SCole Faust
220*c217d954SCole Faust // Get the depthwise convolution compute parameters
221*c217d954SCole Faust auto t = ClDWCNativeKernelConfigurationFactory::create(gpu_target);
222*c217d954SCole Faust const DWCComputeKernelInfo dwc_native_compute_info = t->configure(&permuted_input, &permuted_weights, conv_info, dilation, depth_multiplier);
223*c217d954SCole Faust
224*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output,
225*c217d954SCole Faust dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info, &output_multipliers_shifts_info));
226*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
227*c217d954SCole Faust }
228*c217d954SCole Faust else
229*c217d954SCole Faust {
230*c217d954SCole Faust // Get the depthwise convolution compute parameters
231*c217d954SCole Faust auto t = ClDWCNativeKernelConfigurationFactory::create(gpu_target);
232*c217d954SCole Faust const DWCComputeKernelInfo dwc_native_compute_info = t->configure(input, weights, conv_info, dilation, depth_multiplier);
233*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info,
234*c217d954SCole Faust &output_multipliers_shifts_info));
235*c217d954SCole Faust }
236*c217d954SCole Faust return Status{};
237*c217d954SCole Faust }
238*c217d954SCole Faust
run()239*c217d954SCole Faust void CLDepthwiseConvolutionLayer::run()
240*c217d954SCole Faust {
241*c217d954SCole Faust prepare();
242*c217d954SCole Faust
243*c217d954SCole Faust MemoryGroupResourceScope scope_mg(_memory_group);
244*c217d954SCole Faust
245*c217d954SCole Faust if(_needs_permute)
246*c217d954SCole Faust {
247*c217d954SCole Faust _permute_input_to_nhwc.run();
248*c217d954SCole Faust }
249*c217d954SCole Faust CLScheduler::get().enqueue(*_dwc_native_kernel);
250*c217d954SCole Faust if(_needs_permute)
251*c217d954SCole Faust {
252*c217d954SCole Faust _permute_output_to_nchw.run();
253*c217d954SCole Faust }
254*c217d954SCole Faust }
255*c217d954SCole Faust
prepare()256*c217d954SCole Faust void CLDepthwiseConvolutionLayer::prepare()
257*c217d954SCole Faust {
258*c217d954SCole Faust if(!_is_prepared)
259*c217d954SCole Faust {
260*c217d954SCole Faust if(_is_quantized)
261*c217d954SCole Faust {
262*c217d954SCole Faust _output_multipliers.map();
263*c217d954SCole Faust _output_shifts.map();
264*c217d954SCole Faust quantization::compute_quantized_multipliers_and_shifts(_input->info(),
265*c217d954SCole Faust _original_weights->info(),
266*c217d954SCole Faust _output != nullptr ? _output->info() : _input->info(),
267*c217d954SCole Faust reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))),
268*c217d954SCole Faust reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0))));
269*c217d954SCole Faust _output_multipliers.unmap();
270*c217d954SCole Faust _output_shifts.unmap();
271*c217d954SCole Faust }
272*c217d954SCole Faust
273*c217d954SCole Faust if(_needs_permute)
274*c217d954SCole Faust {
275*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
276*c217d954SCole Faust
277*c217d954SCole Faust _permuted_weights.allocator()->allocate();
278*c217d954SCole Faust _permute_weights_to_nhwc.run();
279*c217d954SCole Faust _original_weights->mark_as_unused();
280*c217d954SCole Faust }
281*c217d954SCole Faust _is_prepared = true;
282*c217d954SCole Faust }
283*c217d954SCole Faust }
284*c217d954SCole Faust } // namespace arm_compute
285