SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 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/GpuConv2d.h" |
| 25 | |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/KernelDescriptors.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLScheduler.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 30 | |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 31 | #include "src/common/utils/Log.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
| 34 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 35 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentDirectConv2d.h" |
| 36 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 37 | #include "src/runtime/heuristics/direct_conv/ClDirectConvKernelConfig.h" |
| 38 | #include "src/runtime/heuristics/direct_conv/IClDirectConvKernelConfig.h" |
Ramy Elgammal | 404462a | 2022-11-08 02:14:46 +0000 | [diff] [blame] | 39 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace experimental |
| 43 | { |
| 44 | namespace dynamic_fusion |
| 45 | { |
| 46 | namespace |
| 47 | { |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 48 | DirectConvComputeKernelInfo config_direct_convolution_nhwc(const ITensorInfo *src, const ITensorInfo *weights, const PadStrideInfo &conv_info) |
| 49 | { |
| 50 | // Get GPU target |
| 51 | GPUTarget gpu_target = CLScheduler::get().target(); |
| 52 | |
| 53 | std::unique_ptr<arm_compute::cl_direct_conv::IClDirectConvKernelConfig> t = arm_compute::cl_direct_conv::ClDirectConvKernelConfigurationFactory::create(gpu_target); |
| 54 | |
| 55 | return t->configure(src, weights, conv_info); |
| 56 | } |
| 57 | |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 58 | void calculate_and_init_dst_if_empty(ITensorInfo *dst, const ITensorInfo *src, const ITensorInfo *wei, const Conv2dAttributes &attributes) |
| 59 | { |
| 60 | if(dst->total_size() == 0U) |
| 61 | { |
| 62 | const auto shape = misc::shape_calculator::compute_deep_convolution_shape(src->tensor_shape(), src->data_layout(), wei->tensor_shape(), |
| 63 | PadStrideInfo(attributes.stride().x(), attributes.stride().y(), attributes.pad().left, |
| 64 | attributes.pad().right, |
| 65 | attributes.pad().top, attributes.pad().bottom, DimensionRoundingType::FLOOR)); // use the default DimensionRoundingType |
| 66 | |
| 67 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape)); |
| 68 | } |
| 69 | } |
| 70 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 71 | /* A helper method to reduce the duplication in dst tensor initialization |
| 72 | * when calling validate() |
| 73 | */ |
| 74 | Status is_supported_op_helper(const GpuWorkloadContext &context, |
| 75 | const ITensorInfo *src, |
| 76 | const ITensorInfo *wei, |
| 77 | const ITensorInfo *bia, |
| 78 | const ITensorInfo *dst, |
| 79 | const Conv2dAttributes &attributes) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 80 | { |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 82 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 83 | TensorInfo dst_info_to_validate; |
| 84 | const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate; |
| 85 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 86 | if(dst != nullptr) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 87 | { |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 88 | dst_info_to_validate_ptr = dst; |
| 89 | } |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 90 | |
| 91 | calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 92 | |
| 93 | // Check support level |
| 94 | // Data type |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
| 96 | // Data layout |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC); |
| 98 | |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 99 | // Check components |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 100 | const auto gpu_target = context.gpu_target(); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 101 | if(context.gpu_language() == GpuLanguage::OpenCL) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 102 | { |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 103 | const auto cl_compile_ctx = context.cl_compile_context(); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 104 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 105 | // Validate Direct Conv2d Component |
| 106 | { |
| 107 | const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run }); |
| 108 | auto settings = ClComponentDirectConv2d::Settings(); |
| 109 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 110 | settings.fast_relaxed_math( |
| 111 | (gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 112 | && (dst_info_to_validate_ptr->data_type() == DataType::F32 || dst_info_to_validate_ptr->data_type() == DataType::F16)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 113 | |
| 114 | ArgumentPack<ITensorInfo> arguments; |
| 115 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 116 | arguments.add_const_tensor(ACL_SRC_1, wei); |
| 117 | arguments.add_const_tensor(ACL_SRC_2, bia); |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 118 | arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentDirectConv2d::validate(properties, arguments, attributes, settings)); |
| 120 | } |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 125 | } |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 129 | constexpr GpuOperatorType operator_type = GpuOperatorType::Complex; |
| 130 | } // namespace |
| 131 | |
| 132 | Status GpuConv2d::is_supported_op(const GpuWorkloadContext &context, |
| 133 | const ITensorInfo *src, |
| 134 | const ITensorInfo *wei, |
| 135 | const ITensorInfo *bia, |
| 136 | const Conv2dAttributes &attributes) |
| 137 | { |
| 138 | return is_supported_op_helper(context, src, wei, bia, nullptr, attributes); |
| 139 | } |
| 140 | |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 141 | Status GpuConv2d::validate_op(const GpuWorkloadSketch &sketch, |
| 142 | const ITensorInfo *src, |
| 143 | const ITensorInfo *wei, |
| 144 | const ITensorInfo *bia, |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 145 | const Conv2dAttributes &attributes) |
| 146 | { |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 147 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 148 | |
| 149 | // Check if tensors have valid id. I.e. they are created from a sketch |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !wei->has_valid_id()); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 151 | if(bia != nullptr) |
| 152 | { |
| 153 | ARM_COMPUTE_RETURN_ERROR_ON(!bia->has_valid_id()); |
| 154 | } |
| 155 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 156 | // This tensor info will have invalid id but because all the existing tensors in the |
| 157 | // sketch have valid ids and the DependencyGraph implementation has no notion of validness |
| 158 | // regarding tensor ids, it'll be just another tensor id and will validate |
| 159 | // Additionally, a new dst id is added every time in create_op, thus there's no need to validate it |
| 160 | TensorInfo dst_info_to_validate; |
| 161 | |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 162 | // Auto initialize dst tensor info |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 163 | calculate_and_init_dst_if_empty(&dst_info_to_validate, src, wei, attributes); |
| 164 | |
| 165 | // Perform fusion test |
| 166 | // Check if operator meets fusion constraints |
| 167 | ArgumentPack<ITensorInfo> tensors; |
| 168 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 169 | tensors.add_const_tensor(ACL_SRC_1, wei); |
| 170 | tensors.add_const_tensor(ACL_SRC_2, bia); |
| 171 | tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 172 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 173 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op), |
| 174 | "Operator fusion test failed. This operator cannot be fused into the workload"); |
| 175 | |
| 176 | // Check if configuration is supported |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 177 | return is_supported_op_helper(*sketch.gpu_context(), src, wei, bia, &dst_info_to_validate, attributes); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 180 | ITensorInfo *GpuConv2d::create_op(GpuWorkloadSketch &sketch, |
| 181 | ITensorInfo *src, |
| 182 | ITensorInfo *wei, |
| 183 | ITensorInfo *bia, |
| 184 | const Conv2dAttributes &attributes) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 185 | { |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 186 | ARM_COMPUTE_LOG_PARAMS(src, wei, bia, attributes); |
Ramy Elgammal | df6a3b0 | 2022-11-30 16:23:10 +0000 | [diff] [blame] | 187 | PadStrideInfo conv_info(attributes.stride().x(), attributes.stride().y(), attributes.pad().left, |
| 188 | attributes.pad().right, |
| 189 | attributes.pad().top, attributes.pad().bottom, DimensionRoundingType::FLOOR); |
| 190 | // Initialize the direct convolution descriptor |
| 191 | const DirectConvComputeKernelInfo desc = config_direct_convolution_nhwc(src, wei, conv_info); |
| 192 | |
SiCong Li | 5a2bc01 | 2023-01-12 12:54:49 +0000 | [diff] [blame] | 193 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 194 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 195 | // Assert validation |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 196 | ARM_COMPUTE_ERROR_THROW_ON(GpuConv2d::validate_op(sketch, src, wei, bia, attributes)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 197 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, wei, dst); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 198 | |
| 199 | // Auto initialize dst tensor |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 200 | calculate_and_init_dst_if_empty(dst, src, wei, attributes); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 201 | |
| 202 | // Translate into components and add to component graph |
| 203 | auto &comp_graph = sketch.implementation().component_graph(); |
| 204 | |
| 205 | const auto sketch_ctx = sketch.implementation().context(); |
| 206 | |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 207 | const auto gpu_target = sketch_ctx->gpu_target(); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 208 | |
| 209 | if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
| 210 | { |
| 211 | const auto cl_compile_ctx = sketch_ctx->cl_compile_context(); |
| 212 | ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr); |
| 213 | |
| 214 | // Add Direct Conv2d Component |
| 215 | { |
| 216 | auto properties = IGpuKernelComponent::Properties(); |
| 217 | properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run }); |
| 218 | |
| 219 | auto settings = ClComponentDirectConv2d::Settings(); |
| 220 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 221 | settings.fast_relaxed_math( |
| 222 | (gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) |
| 223 | && (dst->data_type() == DataType::F32 || dst->data_type() == DataType::F16)); |
| 224 | |
Viet-Hoa Do | e2e6d74 | 2023-03-01 15:46:10 +0000 | [diff] [blame^] | 225 | settings.direct_conv_descriptor(desc); |
| 226 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 227 | if(settings.export_to_cl_image()) |
| 228 | { |
| 229 | arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(wei); |
| 230 | } |
| 231 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 232 | ArgumentPack<ITensorInfo> arguments; |
| 233 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 234 | arguments.add_const_tensor(ACL_SRC_1, wei); |
| 235 | arguments.add_const_tensor(ACL_SRC_2, bia); |
| 236 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 237 | comp_graph.add_new_component<ClComponentDirectConv2d>(properties, arguments, attributes, settings); |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 243 | } |
| 244 | |
| 245 | // Set up fusion test by adding to the Operator Group |
| 246 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 247 | |
| 248 | // Pack tensor infos |
| 249 | ArgumentPack<ITensorInfo> tensors; |
| 250 | tensors.add_const_tensor(ACL_SRC_0, src); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 251 | tensors.add_const_tensor(ACL_SRC_1, wei); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 252 | tensors.add_const_tensor(ACL_SRC_2, bia); |
SiCong Li | 81fdadd | 2022-11-23 09:58:18 +0000 | [diff] [blame] | 253 | tensors.add_const_tensor(ACL_DST_0, dst); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 254 | |
| 255 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 256 | sketch.implementation().operator_group().add_operator(op); |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 257 | |
| 258 | return dst; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | } // namespace dynamic_fusion |
| 262 | } // namespace experimental |
| 263 | } // namespace arm_compute |