Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 0ee13af | 2024-02-07 15:34:45 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2022-2024 Arm Limited. |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +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/GpuClamp.h" |
| 25 | |
| 26 | #include "arm_compute/core/experimental/Types.h" |
| 27 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "src/common/utils/Log.h" |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 31 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentActivation.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 33 | |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace experimental |
| 37 | { |
| 38 | namespace dynamic_fusion |
| 39 | { |
| 40 | namespace |
| 41 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 42 | Status is_supported_op_helper(const GpuWorkloadContext &context, |
| 43 | const ITensorInfo *src, |
| 44 | const ITensorInfo *dst, |
| 45 | const ClampAttributes &attributes) |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(attributes.max_val() < attributes.min_val(), |
| 50 | "Maximum clamp value cannot be lower than minimum value"); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 51 | |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 52 | TensorInfo dst_info_to_validate; |
| 53 | const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate; |
| 54 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | if (dst != nullptr) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 56 | { |
| 57 | dst_info_to_validate_ptr = dst; |
| 58 | } |
| 59 | |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 60 | auto_init_if_empty(dst_info_to_validate, *src->clone()); |
| 61 | |
| 62 | // CLAMP operator is implemented as LU_BOUNDED_RELU with the alpha and beta variables swapped |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | const ClComponentActivation::Attributes act_info{ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
| 64 | attributes.max_val(), attributes.min_val()}; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 65 | |
| 66 | // Check components |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | if (context.gpu_language() == GpuLanguage::OpenCL) |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 68 | { |
| 69 | // Validate Activation Component |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | const auto properties = |
| 71 | IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 72 | |
| 73 | ArgumentPack<ITensorInfo> arguments; |
| 74 | arguments.add_const_tensor(ACL_SRC, src); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 75 | arguments.add_const_tensor(ACL_DST, dst_info_to_validate_ptr); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentActivation::validate(properties, arguments, act_info)); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 81 | } |
| 82 | return Status{}; |
| 83 | } |
| 84 | |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 85 | constexpr GpuOperatorType operator_type = GpuOperatorType::Simple; |
| 86 | } // namespace |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | Status |
| 89 | GpuClamp::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const ClampAttributes &attributes) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 90 | { |
| 91 | return is_supported_op_helper(context, src, nullptr, attributes); |
| 92 | } |
| 93 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | Status GpuClamp::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const ClampAttributes &attributes) |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 95 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 96 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 97 | |
| 98 | // Check if tensors have valid id, i.e. they are created from a sketch |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id()); |
| 100 | |
| 101 | // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object |
| 102 | TensorInfo dst_info_to_validate; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 103 | |
| 104 | // Auto initialize dst tensor info |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 105 | auto_init_if_empty(dst_info_to_validate, *src->clone()); |
| 106 | |
| 107 | // Perform fusion test to check if the operator meets fusion constraints |
| 108 | ArgumentPack<ITensorInfo> tensors; |
| 109 | tensors.add_const_tensor(ACL_SRC, src); |
| 110 | tensors.add_const_tensor(ACL_DST, &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); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | ITensorInfo *GpuClamp::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, const ClampAttributes &attributes) |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +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(GpuClamp::validate_op(sketch, src, attributes)); |
| 124 | |
| 125 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
| 126 | ARM_COMPUTE_ERROR_ON_NULLPTR(dst); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 127 | |
| 128 | // Auto initialize dst tensor |
| 129 | auto_init_if_empty(*dst, *src->clone()); |
| 130 | |
| 131 | // Translate into components and add to component graph |
| 132 | GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph(); |
| 133 | |
| 134 | // CLAMP operator is implemented as LU_BOUNDED_RELU with the alpha and beta variables swapped |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | const ClComponentActivation::Attributes act_info{ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
| 136 | attributes.max_val(), attributes.min_val()}; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 137 | |
| 138 | const auto *const sketch_ctx = sketch.implementation().context(); |
| 139 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 140 | if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 141 | { |
| 142 | // Add Activation Component |
| 143 | auto properties = IGpuKernelComponent::Properties(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 145 | |
| 146 | ArgumentPack<ITensorInfo> arguments; |
| 147 | arguments.add_const_tensor(ACL_SRC, src); |
| 148 | arguments.add_const_tensor(ACL_DST, dst); |
| 149 | comp_graph.add_new_component<ClComponentActivation>(properties, arguments, act_info); |
| 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, src); |
| 162 | tensors.add_const_tensor(ACL_DST, dst); |
| 163 | |
| 164 | const auto 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; |
Jakub Sujak | 3274172 | 2022-11-25 16:43:18 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace dynamic_fusion |
| 171 | } // namespace experimental |
| 172 | } // namespace arm_compute |