Ramy Elgammal | 002e653 | 2023-01-11 18:48:04 +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 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuSoftmax.h" |
| 25 | #include "arm_compute/core/Error.h" |
| 26 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DMaxShiftExpSum.h" |
| 27 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentLogits1DNorm.h" |
| 28 | |
| 29 | #include "src/common/utils/Log.h" |
| 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
| 32 | #include "src/dynamic_fusion/sketch/gpu/GpuOperatorProperties.h" |
| 33 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace experimental |
| 38 | { |
| 39 | namespace dynamic_fusion |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | GpuOperatorType operator_type = GpuOperatorType::Unfusable; |
| 44 | } // namespace |
| 45 | |
| 46 | Status GpuSoftmax::is_supported_op(const GpuWorkloadContext &context, |
| 47 | const ITensorInfo *src, |
| 48 | const ITensorInfo *dst, |
| 49 | const Attributes &attributes) |
| 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 52 | TensorInfo dst_info_to_validate; |
| 53 | |
| 54 | // Auto initialize dst tensor info |
| 55 | if(dst != nullptr) |
| 56 | { |
| 57 | dst_info_to_validate = *dst; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | auto_init_if_empty(dst_info_to_validate, *src->clone()); |
| 62 | } |
| 63 | // Check components |
| 64 | if(context.gpu_language() == GpuLanguage::OpenCL) |
| 65 | { |
| 66 | const auto cl_compile_ctx = context.cl_compile_context(); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 68 | const KernelProperties properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run }); |
| 69 | |
| 70 | TensorShape logits_sum_shape = src->tensor_shape(); |
| 71 | TensorInfo logits(src->clone()->set_tensor_shape(logits_sum_shape)); |
| 72 | |
| 73 | // The sum tensor dim0 only need one element |
| 74 | logits_sum_shape.set(0, 1); |
| 75 | TensorInfo sum(src->clone()->set_tensor_shape(logits_sum_shape)); |
| 76 | |
| 77 | // Validate Component |
| 78 | ArgumentPack<ITensorInfo> arguments_exp_sum; |
| 79 | ArgumentPack<ITensorInfo> arguments_norm; |
| 80 | |
| 81 | arguments_exp_sum.add_const_tensor(ACL_SRC_0, src); |
| 82 | arguments_exp_sum.add_const_tensor(ACL_DST_0, &sum); |
| 83 | arguments_exp_sum.add_const_tensor(ACL_DST_1, &logits); |
| 84 | |
| 85 | arguments_norm.add_const_tensor(ACL_SRC_0, &logits); |
| 86 | arguments_norm.add_const_tensor(ACL_SRC_1, &sum); |
| 87 | arguments_norm.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 88 | |
| 89 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentLogits1DMaxShiftExpSum::validate(properties, arguments_exp_sum, attributes)); |
| 90 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentLogits1DNorm::validate(properties, arguments_norm, attributes)); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 95 | } |
| 96 | |
| 97 | return Status{}; |
| 98 | } |
| 99 | |
| 100 | Status GpuSoftmax::validate_op(const GpuWorkloadSketch &sketch, |
| 101 | const ITensorInfo *src, |
| 102 | const ITensorInfo *dst, |
| 103 | const Attributes &attributes) |
| 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !dst->has_valid_id()); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() > 4, "Only up to 4 dimensions are supported"); |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON(attributes.axis() < static_cast<int32_t>(-src->num_dimensions()) || static_cast<int32_t>(src->num_dimensions()) <= attributes.axis()); |
| 109 | |
| 110 | // Auto initialize dst tensor info |
| 111 | TensorInfo dst_info_to_validate = *dst; |
| 112 | auto_init_if_empty(dst_info_to_validate, *src->clone()); |
| 113 | |
| 114 | const size_t actual_axis = static_cast<size_t>(wrap_around(attributes.axis(), static_cast<int32_t>(src->num_dimensions()))); |
| 115 | const bool needs_permute = actual_axis != 0; |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(needs_permute, "Dynamic fusion softmax on axis!=0 not supported yet."); |
| 117 | |
| 118 | // Perform fusion test and check if the operator meets the fusion constraints |
| 119 | ArgumentPack<ITensorInfo> tensors; |
| 120 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 121 | tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 122 | |
| 123 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 124 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op), |
| 125 | "Operator fusion test failed. This operator cannot be fused into the workload"); |
| 126 | |
| 127 | // Check if configuration is supported |
| 128 | return is_supported_op(*sketch.gpu_context(), src, &dst_info_to_validate, attributes); |
| 129 | } |
| 130 | |
| 131 | void GpuSoftmax::create_op(GpuWorkloadSketch &sketch, |
| 132 | ITensorInfo *src, |
| 133 | ITensorInfo *dst, |
| 134 | const Attributes &attributes) |
| 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 137 | ARM_COMPUTE_LOG_PARAMS(src, dst, attributes); |
| 138 | TensorShape logits_sum_shape = src->tensor_shape(); |
| 139 | ITensorInfo *logits = sketch.implementation().create_auxiliary_tensor(src->clone()->set_tensor_shape(logits_sum_shape)); |
| 140 | logits_sum_shape.set(0, 1); |
| 141 | ITensorInfo *sum = sketch.implementation().create_auxiliary_tensor(src->clone()->set_tensor_shape(logits_sum_shape)); |
| 142 | |
| 143 | // Auto initialize dst tensor info and the auxiliary tensor infos as well |
| 144 | auto_init_if_empty(*dst, *src->clone()); |
| 145 | |
| 146 | // Assert validation |
| 147 | ARM_COMPUTE_ERROR_THROW_ON(GpuSoftmax::validate_op(sketch, src, dst, attributes)); |
| 148 | ARM_COMPUTE_ERROR_ON_NULLPTR(logits, sum); |
| 149 | |
| 150 | // Translate into components and add to component graph |
| 151 | auto &comp_graph = sketch.implementation().component_graph(); |
| 152 | const auto sketch_ctx = sketch.implementation().context(); |
| 153 | |
| 154 | if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
| 155 | { |
| 156 | const auto cl_compile_ctx = sketch_ctx->cl_compile_context(); |
Omar Al Khatib | 3c7c1fa | 2023-03-07 09:57:49 +0000 | [diff] [blame] | 157 | ARM_COMPUTE_UNUSED(cl_compile_ctx); |
Ramy Elgammal | 002e653 | 2023-01-11 18:48:04 +0000 | [diff] [blame] | 158 | ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr); |
| 159 | |
| 160 | // Add Direct Conv2d Component |
| 161 | { |
| 162 | auto properties = IGpuKernelComponent::Properties(); |
| 163 | properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run }); |
| 164 | |
| 165 | ArgumentPack<ITensorInfo> arguments_exp_sum; |
| 166 | ArgumentPack<ITensorInfo> arguments_norm; |
| 167 | |
| 168 | arguments_exp_sum.add_const_tensor(ACL_SRC_0, src); |
| 169 | arguments_exp_sum.add_const_tensor(ACL_DST_0, sum); |
| 170 | arguments_exp_sum.add_const_tensor(ACL_DST_1, logits); |
| 171 | |
| 172 | arguments_norm.add_const_tensor(ACL_SRC_0, logits); |
| 173 | arguments_norm.add_const_tensor(ACL_SRC_1, sum); |
| 174 | arguments_norm.add_const_tensor(ACL_DST_0, dst); |
| 175 | |
| 176 | comp_graph.add_new_component<ClComponentLogits1DMaxShiftExpSum>(properties, arguments_exp_sum, attributes); |
| 177 | comp_graph.add_new_component<ClComponentLogits1DNorm>(properties, arguments_norm, attributes); |
| 178 | } |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 183 | } |
| 184 | |
| 185 | // Set up fusion test by adding to the Operator Group |
| 186 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 187 | |
| 188 | // Pack tensor infos |
| 189 | ArgumentPack<ITensorInfo> tensors; |
| 190 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 191 | tensors.add_const_tensor(ACL_DST_0, dst); |
| 192 | |
| 193 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 194 | sketch.implementation().operator_group().add_operator(op); |
| 195 | } |
| 196 | |
| 197 | } // namespace dynamic_fusion |
| 198 | } // namespace experimental |
| 199 | } // namespace arm_compute |