Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2022-2024 Arm Limited. |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [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/GpuCast.h" |
| 25 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 26 | #include "src/common/utils/Log.h" |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 27 | #include "src/core/helpers/AutoConfiguration.h" |
| 28 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 29 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentCast.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace experimental |
| 35 | { |
| 36 | namespace dynamic_fusion |
| 37 | { |
| 38 | namespace |
| 39 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 40 | Status is_supported_op_helper(const GpuWorkloadContext &context, |
| 41 | const ITensorInfo *src, |
| 42 | const ITensorInfo *dst, |
| 43 | const CastAttributes &attributes) |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON(src == dst); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 47 | |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 48 | TensorInfo dst_info_to_validate; |
| 49 | const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate; |
| 50 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | if (dst != nullptr) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 52 | { |
| 53 | dst_info_to_validate_ptr = dst; |
| 54 | } |
| 55 | |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 56 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type())); |
| 57 | |
| 58 | // Check support level |
| 59 | // Data Type |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame^] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst_info_to_validate_ptr, 1, DataType::F16, DataType::F32); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | if (context.gpu_language() == GpuLanguage::OpenCL) |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 64 | { |
| 65 | const auto cl_compile_ctx = context.cl_compile_context(); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 67 | // Validate Cast Component |
| 68 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | const auto properties = |
| 70 | IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
| 71 | auto settings = ClComponentCast::Settings(); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 72 | |
| 73 | ArgumentPack<ITensorInfo> arguments; |
| 74 | arguments.add_const_tensor(ACL_SRC_0, src); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 75 | arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentCast::validate(properties, arguments, attributes, settings)); |
| 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 82 | } |
| 83 | |
| 84 | return Status{}; |
| 85 | } |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 86 | constexpr GpuOperatorType operator_type = GpuOperatorType::Simple; |
| 87 | } // namespace |
| 88 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | Status |
| 90 | GpuCast::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const CastAttributes &attributes) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 91 | { |
| 92 | return is_supported_op_helper(context, src, nullptr, attributes); |
| 93 | } |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 94 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | Status GpuCast::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const CastAttributes &attributes) |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 96 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 97 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id()); |
| 99 | |
| 100 | // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object |
| 101 | TensorInfo dst_info_to_validate; |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 102 | |
| 103 | // Auto initialize dst tensor info |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 104 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type())); |
| 105 | |
| 106 | // Perform fusion test |
| 107 | // Pack tensor infos |
| 108 | ArgumentPack<ITensorInfo> tensors; |
| 109 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 110 | tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 111 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op), |
| 113 | "Operator fusion test failed. This operator cannot be fused into the workload"); |
| 114 | |
| 115 | // Check if configuration is supported |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 116 | return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | ITensorInfo *GpuCast::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, const CastAttributes &attributes) |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 120 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 121 | ARM_COMPUTE_ERROR_ON_NULLPTR(src); |
| 122 | ARM_COMPUTE_LOG_PARAMS(src, attributes); |
| 123 | ARM_COMPUTE_ERROR_THROW_ON(GpuCast::validate_op(sketch, src, attributes)); |
| 124 | |
| 125 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
| 126 | ARM_COMPUTE_ERROR_ON_NULLPTR(dst); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 127 | |
| 128 | // Auto initialize dst tensor info if empty |
| 129 | auto_init_if_empty(*dst, src->clone()->set_data_type(attributes.data_type())); |
| 130 | |
| 131 | // Translate into components and add to component graph |
| 132 | GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph(); |
| 133 | const auto *sketch_ctx = sketch.implementation().context(); |
| 134 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr); |
| 138 | |
| 139 | // Add Depthwise Conv2d Component |
| 140 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | const auto properties = |
| 142 | IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
| 143 | auto settings = ClComponentCast::Settings(); |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 144 | |
| 145 | ArgumentPack<ITensorInfo> arguments; |
| 146 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 147 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 148 | comp_graph.add_new_component<ClComponentCast>(properties, arguments, attributes, settings); |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 154 | } |
| 155 | |
| 156 | // Set up fusion test by adding to the Operator Group |
| 157 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 158 | |
| 159 | // Pack tensor infos |
| 160 | ArgumentPack<ITensorInfo> tensors; |
| 161 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 162 | tensors.add_const_tensor(ACL_DST_0, dst); |
| 163 | |
| 164 | const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 165 | sketch.implementation().operator_group().add_operator(op); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 166 | |
| 167 | return dst; |
Gunes Bayir | 1dc6ff1 | 2022-12-06 20:48:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace dynamic_fusion |
| 171 | } // namespace experimental |
| 172 | } // namespace arm_compute |