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