blob: b08af61d8fab31467feaf93e7173d084bdae7aa9 [file] [log] [blame]
Gunes Bayir7dc02342022-11-21 21:46:50 +00001/*
2 * Copyright (c) 2022 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/dynamic_fusion/sketch/gpu/operators/GpuDepthwiseConv2d.h"
25
26#include "arm_compute/core/utils/misc/ShapeCalculator.h"
27
28#include "src/common/utils/Log.h"
29#include "src/core/helpers/AutoConfiguration.h"
30#include "src/dynamic_fusion/sketch/ArgumentPack.h"
31#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
32#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentDepthwiseConv2d.h"
33#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Gian Marco Iodice9d3bd412022-12-30 09:45:00 +000034#include "src/runtime/heuristics/dwc_native/ClDWCNativeKernelConfig.h"
35#include "src/runtime/heuristics/dwc_native/IClDWCNativeKernelConfig.h"
Gunes Bayir7dc02342022-11-21 21:46:50 +000036
37namespace arm_compute
38{
39namespace experimental
40{
41namespace dynamic_fusion
42{
43namespace
44{
Gunes Bayir7dc02342022-11-21 21:46:50 +000045void calculate_and_init_dst_if_empty(ITensorInfo *dst, const ITensorInfo *src, const ITensorInfo *wei, const DepthwiseConv2dAttributes &attributes)
46{
47 if(dst->total_size() == 0U)
48 {
49 const PadStrideInfo pad_stride_info(attributes.stride().x(),
50 attributes.stride().y(),
51 attributes.pad().left,
52 attributes.pad().right,
53 attributes.pad().top,
54 attributes.pad().bottom,
55 attributes.dimension_rounding_type());
56
57 const ConvolutionInfo conv_info{ pad_stride_info, attributes.depth_multiplier(), ActivationLayerInfo(), attributes.dilation() };
58 const TensorShape shape = misc::shape_calculator::compute_depthwise_convolution_shape(*src, *wei, conv_info);
59
60 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape));
61 }
62}
63
64constexpr GpuOperatorType operator_type = GpuOperatorType::Complex;
65} // namespace
66
67Status GpuDepthwiseConv2d::is_supported_op(const GpuWorkloadContext &context,
68 const ITensorInfo *src,
69 const ITensorInfo *wei,
70 const ITensorInfo *bia,
71 const ITensorInfo *dst,
72 const DepthwiseConv2dAttributes &attributes)
73{
74 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei, dst);
75
76 // Auto initialize dst tensor info
77 TensorInfo dst_info_to_validate = *dst;
78 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes);
79
80 // Check support level
81 // Data type
82 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
83 // Data layout
84 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
85
86 const GpuTarget gpu_target = context.gpu_target();
87
88 if(context.gpu_language() == GpuLanguage::OpenCL)
89 {
90 const CLCompileContext *cl_compile_ctx = context.cl_compile_context();
91 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
92
93 // Validate Depthwise Conv2d Component
94 {
95 const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
96 auto settings = ClComponentDepthwiseConv2d::Settings();
97
Gian Marco Iodice9d3bd412022-12-30 09:45:00 +000098 const PadStrideInfo legacy_conv_info(attributes.stride().x(), attributes.stride().y(), attributes.pad().left,
99 attributes.pad().right,
100 attributes.pad().top, attributes.pad().bottom, DimensionRoundingType::FLOOR);
101
102 // Get the depthwise convolution compute parameters
103 auto t = arm_compute::cl_dwc::ClDWCNativeKernelConfigurationFactory::create(gpu_target);
104 const DWCComputeKernelInfo dwc_info = t->configure(src, wei, legacy_conv_info, attributes.dilation(), attributes.depth_multiplier());
Gunes Bayir7dc02342022-11-21 21:46:50 +0000105
106 settings.fast_relaxed_math(
107 (gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST)
108 && (dst_info_to_validate.data_type() == DataType::F32 || dst_info_to_validate.data_type() == DataType::F16));
109
110 settings.is_fma_available(get_arch_from_target(gpu_target) == GPUTarget::MIDGARD)
111 .m0(dwc_info.m0)
112 .n0(dwc_info.n0)
113 .export_input_to_cl_image(dwc_info.export_input_to_cl_image)
114 .export_weights_to_cl_image(dwc_info.export_weights_to_cl_image);
115
116 ArgumentPack<ITensorInfo> arguments;
117 arguments.add_const_tensor(ACL_SRC_0, src);
118 arguments.add_const_tensor(ACL_SRC_1, wei);
119 arguments.add_const_tensor(ACL_SRC_2, bia);
120 arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
121 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentDepthwiseConv2d::validate(properties, arguments, attributes, settings));
122 }
123 }
124 else
125 {
126 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
127 }
128
129 return Status{};
130}
131
132Status GpuDepthwiseConv2d::validate_op(const GpuWorkloadSketch &sketch,
133 const ITensorInfo *src,
134 const ITensorInfo *wei,
135 const ITensorInfo *bia,
136 const ITensorInfo *dst,
137 const DepthwiseConv2dAttributes &attributes)
138{
139 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei, dst);
140 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !wei->has_valid_id() || !dst->has_valid_id());
141
142 if(bia != nullptr)
143 {
144 ARM_COMPUTE_RETURN_ERROR_ON(!bia->has_valid_id());
145 }
146
147 // Auto initialize dst tensor info
148 TensorInfo dst_info_to_validate = *dst;
149 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes);
150
151 // Perform fusion test
152 // Pack tensor infos
153 ArgumentPack<ITensorInfo> tensors;
154 tensors.add_const_tensor(ACL_SRC_0, src);
155 tensors.add_const_tensor(ACL_SRC_1, wei);
156 tensors.add_const_tensor(ACL_SRC_2, bia);
157 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
158 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
159
160 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
161 "Operator fusion test failed. This operator cannot be fused into the workload");
162
163 // Check if configuration is supported
164 return is_supported_op(*sketch.gpu_context(), src, wei, bia, &dst_info_to_validate, attributes);
165}
166
167void GpuDepthwiseConv2d::create_op(GpuWorkloadSketch &sketch,
168 ITensorInfo *src,
169 ITensorInfo *wei,
170 ITensorInfo *bia,
171 ITensorInfo *dst,
172 const DepthwiseConv2dAttributes &attributes)
173{
174 // Assert validation
175 ARM_COMPUTE_ERROR_THROW_ON(GpuDepthwiseConv2d::validate_op(sketch, src, wei, bia, dst, attributes));
176 ARM_COMPUTE_ERROR_ON_NULLPTR(src, wei, dst);
177 ARM_COMPUTE_LOG_PARAMS(src, wei, bia, dst, attributes);
178
179 calculate_and_init_dst_if_empty(dst, src, wei, attributes);
180
181 // Translate into components and add to component graph
182 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
183 const auto *sketch_ctx = sketch.implementation().context();
184 const GpuTarget gpu_target = sketch_ctx->gpu_target();
185
186 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
187 {
Viet-Hoa Do81f796b2022-12-23 14:42:55 +0000188 ARM_COMPUTE_ERROR_ON_NULLPTR(sketch_ctx->cl_compile_context());
Gunes Bayir7dc02342022-11-21 21:46:50 +0000189
190 // Add Depthwise Conv2d Component
191 {
192 const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
193 auto settings = ClComponentDepthwiseConv2d::Settings();
194
Gian Marco Iodice9d3bd412022-12-30 09:45:00 +0000195 const PadStrideInfo legacy_conv_info(attributes.stride().x(), attributes.stride().y(), attributes.pad().left,
196 attributes.pad().right,
197 attributes.pad().top, attributes.pad().bottom, DimensionRoundingType::FLOOR);
198
199 // Get the depthwise convolution compute parameters
200 auto t = arm_compute::cl_dwc::ClDWCNativeKernelConfigurationFactory::create(gpu_target);
201 const DWCComputeKernelInfo dwc_info = t->configure(src, wei, legacy_conv_info, attributes.dilation(), attributes.depth_multiplier());
Gunes Bayir7dc02342022-11-21 21:46:50 +0000202
203 settings.is_fma_available(get_arch_from_target(gpu_target) != GPUTarget::MIDGARD)
204 .m0(dwc_info.m0)
205 .n0(dwc_info.n0)
206 .export_input_to_cl_image(dwc_info.export_input_to_cl_image)
207 .export_weights_to_cl_image(dwc_info.export_weights_to_cl_image);
208
209 if(settings.export_input_to_cl_image())
210 {
211 arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(src);
212 }
213
214 if(settings.export_weights_to_cl_image())
215 {
216 arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(wei);
217 }
218
219 ArgumentPack<ITensorInfo> arguments;
220 arguments.add_const_tensor(ACL_SRC_0, src);
221 arguments.add_const_tensor(ACL_SRC_1, wei);
222 arguments.add_const_tensor(ACL_SRC_2, bia);
223 arguments.add_const_tensor(ACL_DST_0, dst);
224 comp_graph.add_new_component<ClComponentDepthwiseConv2d>(properties, arguments, attributes, settings);
225 }
226 }
227 else
228 {
229 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
230 }
231
232 // Set up fusion test by adding to the Operator Group
233 // Note this has to be performed after all the components have been successfully added to the component graph
234
235 // Pack tensor infos
236 ArgumentPack<ITensorInfo> tensors;
237 tensors.add_const_tensor(ACL_SRC_0, src);
238 tensors.add_const_tensor(ACL_SRC_1, wei);
239 tensors.add_const_tensor(ACL_SRC_2, bia);
240 tensors.add_const_tensor(ACL_DST_0, dst);
241
242 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
243 sketch.implementation().operator_group().add_operator(op);
244}
245
246} // namespace dynamic_fusion
247} // namespace experimental
248} // namespace arm_compute