blob: aaeec543f8b23567c0fa4d3ceff90a077492ecc0 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Gunes Bayir3a1e1252023-01-03 21:26:09 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +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 */
24#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuConv2d.h"
25
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000026#include "arm_compute/core/KernelDescriptors.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028#include "arm_compute/core/Validate.h"
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000029#include "arm_compute/runtime/CL/CLScheduler.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010030
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000031#include "src/common/utils/Log.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/dynamic_fusion/sketch/ArgumentPack.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010034#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentDirectConv2d.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010036#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000037#include "src/runtime/heuristics/direct_conv/ClDirectConvKernelConfig.h"
38#include "src/runtime/heuristics/direct_conv/IClDirectConvKernelConfig.h"
Ramy Elgammal404462a2022-11-08 02:14:46 +000039
SiCong Lif44bbc52022-08-29 18:25:51 +010040namespace arm_compute
41{
42namespace experimental
43{
44namespace dynamic_fusion
45{
46namespace
47{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048DirectConvComputeKernelInfo
49config_direct_convolution_nhwc(const ITensorInfo *src, const ITensorInfo *weights, const PadStrideInfo &conv_info)
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000050{
51 // Get GPU target
52 GPUTarget gpu_target = CLScheduler::get().target();
53
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 std::unique_ptr<arm_compute::cl_direct_conv::IClDirectConvKernelConfig> t =
55 arm_compute::cl_direct_conv::ClDirectConvKernelConfigurationFactory::create(gpu_target);
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000056
57 return t->configure(src, weights, conv_info);
58}
59
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060void calculate_and_init_dst_if_empty(ITensorInfo *dst,
61 const ITensorInfo *src,
62 const ITensorInfo *wei,
63 const Conv2dAttributes &attributes)
SiCong Li81fdadd2022-11-23 09:58:18 +000064{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 if (dst->total_size() == 0U)
SiCong Li81fdadd2022-11-23 09:58:18 +000066 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 const auto shape = misc::shape_calculator::compute_deep_convolution_shape(
68 src->tensor_shape(), src->data_layout(), wei->tensor_shape(),
69 PadStrideInfo(attributes.stride().x(), attributes.stride().y(), attributes.pad().left,
70 attributes.pad().right, attributes.pad().top, attributes.pad().bottom,
71 DimensionRoundingType::FLOOR)); // use the default DimensionRoundingType
SiCong Li81fdadd2022-11-23 09:58:18 +000072
73 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape));
74 }
75}
76
Gunes Bayir3a1e1252023-01-03 21:26:09 +000077/* A helper method to reduce the duplication in dst tensor initialization
78* when calling validate()
79*/
80Status is_supported_op_helper(const GpuWorkloadContext &context,
81 const ITensorInfo *src,
82 const ITensorInfo *wei,
83 const ITensorInfo *bia,
84 const ITensorInfo *dst,
85 const Conv2dAttributes &attributes)
SiCong Lif44bbc52022-08-29 18:25:51 +010086{
Gunes Bayir3a1e1252023-01-03 21:26:09 +000087 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei);
SiCong Lif44bbc52022-08-29 18:25:51 +010088
Gunes Bayir3a1e1252023-01-03 21:26:09 +000089 TensorInfo dst_info_to_validate;
90 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 if (dst != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +010093 {
Gunes Bayir3a1e1252023-01-03 21:26:09 +000094 dst_info_to_validate_ptr = dst;
95 }
Gunes Bayircc287732023-01-19 15:56:00 +000096
97 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes);
SiCong Lif44bbc52022-08-29 18:25:51 +010098
99 // Check support level
100 // Data type
101 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
102 // Data layout
103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
104
SiCong Li81fdadd2022-11-23 09:58:18 +0000105 // Check components
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000106 const auto gpu_target = context.gpu_target();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (context.gpu_language() == GpuLanguage::OpenCL)
SiCong Lif44bbc52022-08-29 18:25:51 +0100108 {
SiCong Li81fdadd2022-11-23 09:58:18 +0000109 const auto cl_compile_ctx = context.cl_compile_context();
SiCong Lif44bbc52022-08-29 18:25:51 +0100110 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
111 // Validate Direct Conv2d Component
112 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113 const auto properties =
114 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
115 auto settings = ClComponentDirectConv2d::Settings();
SiCong Lif44bbc52022-08-29 18:25:51 +0100116
SiCong Lif44bbc52022-08-29 18:25:51 +0100117 settings.fast_relaxed_math(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 (gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) &&
119 (dst_info_to_validate_ptr->data_type() == DataType::F32 ||
120 dst_info_to_validate_ptr->data_type() == DataType::F16));
SiCong Lif44bbc52022-08-29 18:25:51 +0100121
122 ArgumentPack<ITensorInfo> arguments;
123 arguments.add_const_tensor(ACL_SRC_0, src);
124 arguments.add_const_tensor(ACL_SRC_1, wei);
125 arguments.add_const_tensor(ACL_SRC_2, bia);
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000126 arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
SiCong Lif44bbc52022-08-29 18:25:51 +0100127 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentDirectConv2d::validate(properties, arguments, attributes, settings));
128 }
129 }
130 else
131 {
132 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
133 }
134 return Status{};
135}
136
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000137constexpr GpuOperatorType operator_type = GpuOperatorType::Complex;
138} // namespace
139
140Status GpuConv2d::is_supported_op(const GpuWorkloadContext &context,
141 const ITensorInfo *src,
142 const ITensorInfo *wei,
143 const ITensorInfo *bia,
144 const Conv2dAttributes &attributes)
145{
146 return is_supported_op_helper(context, src, wei, bia, nullptr, attributes);
147}
148
SiCong Li81fdadd2022-11-23 09:58:18 +0000149Status GpuConv2d::validate_op(const GpuWorkloadSketch &sketch,
150 const ITensorInfo *src,
151 const ITensorInfo *wei,
152 const ITensorInfo *bia,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 const Conv2dAttributes &attributes)
SiCong Li81fdadd2022-11-23 09:58:18 +0000154{
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000155 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei);
Viet-Hoa Doedafe7f2023-05-04 17:39:30 +0100156 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!wei->are_values_constant(), "Dynamic weights are not supported");
SiCong Li81fdadd2022-11-23 09:58:18 +0000157
158 // Check if tensors have valid id. I.e. they are created from a sketch
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000159 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !wei->has_valid_id());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 if (bia != nullptr)
SiCong Li81fdadd2022-11-23 09:58:18 +0000161 {
162 ARM_COMPUTE_RETURN_ERROR_ON(!bia->has_valid_id());
163 }
164
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000165 // This tensor info will have invalid id but because all the existing tensors in the
166 // sketch have valid ids and the DependencyGraph implementation has no notion of validness
167 // regarding tensor ids, it'll be just another tensor id and will validate
168 // Additionally, a new dst id is added every time in create_op, thus there's no need to validate it
169 TensorInfo dst_info_to_validate;
170
SiCong Li81fdadd2022-11-23 09:58:18 +0000171 // Auto initialize dst tensor info
SiCong Li81fdadd2022-11-23 09:58:18 +0000172 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes);
173
174 // Perform fusion test
175 // Check if operator meets fusion constraints
176 ArgumentPack<ITensorInfo> tensors;
177 tensors.add_const_tensor(ACL_SRC_0, src);
178 tensors.add_const_tensor(ACL_SRC_1, wei);
179 tensors.add_const_tensor(ACL_SRC_2, bia);
180 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
181 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
182 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
183 "Operator fusion test failed. This operator cannot be fused into the workload");
184
185 // Check if configuration is supported
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000186 return is_supported_op_helper(*sketch.gpu_context(), src, wei, bia, &dst_info_to_validate, attributes);
SiCong Li81fdadd2022-11-23 09:58:18 +0000187}
188
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189ITensorInfo *GpuConv2d::create_op(
190 GpuWorkloadSketch &sketch, ITensorInfo *src, ITensorInfo *wei, ITensorInfo *bia, const Conv2dAttributes &attributes)
SiCong Lif44bbc52022-08-29 18:25:51 +0100191{
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000192 ARM_COMPUTE_LOG_PARAMS(src, wei, bia, attributes);
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000193 PadStrideInfo conv_info(attributes.stride().x(), attributes.stride().y(), attributes.pad().left,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 attributes.pad().right, attributes.pad().top, attributes.pad().bottom,
195 DimensionRoundingType::FLOOR);
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000196 // Initialize the direct convolution descriptor
197 const DirectConvComputeKernelInfo desc = config_direct_convolution_nhwc(src, wei, conv_info);
198
SiCong Li5a2bc012023-01-12 12:54:49 +0000199 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000200
SiCong Lif44bbc52022-08-29 18:25:51 +0100201 // Assert validation
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000202 ARM_COMPUTE_ERROR_THROW_ON(GpuConv2d::validate_op(sketch, src, wei, bia, attributes));
SiCong Lif44bbc52022-08-29 18:25:51 +0100203 ARM_COMPUTE_ERROR_ON_NULLPTR(src, wei, dst);
SiCong Lif44bbc52022-08-29 18:25:51 +0100204
205 // Auto initialize dst tensor
SiCong Li81fdadd2022-11-23 09:58:18 +0000206 calculate_and_init_dst_if_empty(dst, src, wei, attributes);
SiCong Lif44bbc52022-08-29 18:25:51 +0100207
208 // Translate into components and add to component graph
209 auto &comp_graph = sketch.implementation().component_graph();
210
211 const auto sketch_ctx = sketch.implementation().context();
212
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000213 const auto gpu_target = sketch_ctx->gpu_target();
SiCong Lif44bbc52022-08-29 18:25:51 +0100214
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100215 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
SiCong Lif44bbc52022-08-29 18:25:51 +0100216 {
217 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
218 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000219 ARM_COMPUTE_UNUSED(cl_compile_ctx);
SiCong Lif44bbc52022-08-29 18:25:51 +0100220
221 // Add Direct Conv2d Component
222 {
223 auto properties = IGpuKernelComponent::Properties();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
SiCong Lif44bbc52022-08-29 18:25:51 +0100225
226 auto settings = ClComponentDirectConv2d::Settings();
227
SiCong Lif44bbc52022-08-29 18:25:51 +0100228 settings.fast_relaxed_math(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 (gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) &&
230 (dst->data_type() == DataType::F32 || dst->data_type() == DataType::F16));
SiCong Lif44bbc52022-08-29 18:25:51 +0100231
Viet-Hoa Doe2e6d742023-03-01 15:46:10 +0000232 settings.direct_conv_descriptor(desc);
233
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 if (settings.export_to_cl_image())
SiCong Lif44bbc52022-08-29 18:25:51 +0100235 {
236 arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(wei);
237 }
238
SiCong Lif44bbc52022-08-29 18:25:51 +0100239 ArgumentPack<ITensorInfo> arguments;
240 arguments.add_const_tensor(ACL_SRC_0, src);
241 arguments.add_const_tensor(ACL_SRC_1, wei);
242 arguments.add_const_tensor(ACL_SRC_2, bia);
243 arguments.add_const_tensor(ACL_DST_0, dst);
244 comp_graph.add_new_component<ClComponentDirectConv2d>(properties, arguments, attributes, settings);
245 }
246 }
247 else
248 {
249 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
250 }
251
252 // Set up fusion test by adding to the Operator Group
253 // Note this has to be performed after all the components have been successfully added to the component graph
254
255 // Pack tensor infos
256 ArgumentPack<ITensorInfo> tensors;
257 tensors.add_const_tensor(ACL_SRC_0, src);
SiCong Li81fdadd2022-11-23 09:58:18 +0000258 tensors.add_const_tensor(ACL_SRC_1, wei);
SiCong Lif44bbc52022-08-29 18:25:51 +0100259 tensors.add_const_tensor(ACL_SRC_2, bia);
SiCong Li81fdadd2022-11-23 09:58:18 +0000260 tensors.add_const_tensor(ACL_DST_0, dst);
SiCong Lif44bbc52022-08-29 18:25:51 +0100261
262 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
263 sketch.implementation().operator_group().add_operator(op);
Gunes Bayir3a1e1252023-01-03 21:26:09 +0000264
265 return dst;
SiCong Lif44bbc52022-08-29 18:25:51 +0100266}
267
268} // namespace dynamic_fusion
269} // namespace experimental
270} // namespace arm_compute