Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2023-2024 Arm Limited. |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +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 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuPool2d.h" |
| 26 | |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLCompileContext.h" |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/experimental/Types.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 32 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 33 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 35 | #include "src/common/utils/Log.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 38 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentPool2d.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 39 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
| 40 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSourceCode.h" |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 41 | #include "src/dynamic_fusion/utils/Utils.h" |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace experimental |
| 46 | { |
| 47 | namespace dynamic_fusion |
| 48 | { |
| 49 | namespace |
| 50 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | void calculate_and_init_dst_if_empty(ITensorInfo *dst, |
| 52 | const ITensorInfo *src, |
| 53 | const Pool2dAttributes &attributes, |
| 54 | const GpuPool2dSettings &settings) |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 55 | { |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_UNUSED(settings); |
| 57 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | if (dst->total_size() == 0U) |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 59 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | auto shape = misc::shape_calculator::compute_pool_shape( |
Gunes Bayir | 2b9fa59 | 2024-01-17 16:07:03 +0000 | [diff] [blame] | 61 | *src, convert_pool_attr_to_pool_info(attributes, /* mixed_precision */ true)); |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 62 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | constexpr GpuOperatorType operator_type = GpuOperatorType::Complex; |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 67 | } // namespace |
| 68 | |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 69 | GpuPool2dSettings GpuPool2dSettings::use_inf_as_limit(bool use_inf_as_limit) |
| 70 | { |
| 71 | _use_inf_as_limit = use_inf_as_limit; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | bool GpuPool2dSettings::use_inf_as_limit() const |
| 76 | { |
| 77 | return _use_inf_as_limit; |
| 78 | } |
| 79 | |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 80 | Status GpuPool2d::validate_op(const GpuWorkloadSketch &sketch, |
| 81 | const ITensorInfo *src, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | const Pool2dAttributes &attributes, |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 83 | const GpuPool2dSettings &settings) |
| 84 | { |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id()); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 87 | |
| 88 | // Auto initialize dst tensor info |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 89 | TensorInfo dst_info_to_validate; |
| 90 | |
| 91 | calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 92 | |
| 93 | // Perform fusion test |
| 94 | // Pack tensor infos |
| 95 | ArgumentPack<ITensorInfo> tensors; |
| 96 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 97 | tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 98 | |
| 99 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op), |
| 101 | "Operator fusion test failed. This operator cannot be fused into the workload"); |
| 102 | |
| 103 | // Check if configuration is supported |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 104 | return is_supported_op(*sketch.gpu_context(), src, attributes, settings); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Status GpuPool2d::is_supported_op(const GpuWorkloadContext &context, |
| 108 | const ITensorInfo *src, |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 109 | const Pool2dAttributes &attributes, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | const GpuPool2dSettings &settings) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 111 | { |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 113 | // Data type |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
| 115 | // Data layout |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC); |
| 117 | // Check exclude padding is not false |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!attributes.exclude_padding(), |
| 119 | "Exclude padding must be set to true in Attributes!"); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 120 | |
| 121 | // Auto initialize dst tensor info |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 122 | TensorInfo dst_info_to_validate; |
| 123 | |
| 124 | calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 125 | |
| 126 | // Check components |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | if (context.gpu_language() == GpuLanguage::OpenCL) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 128 | { |
| 129 | const auto cl_compile_ctx = context.cl_compile_context(); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 131 | |
| 132 | // Validate Component |
| 133 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 134 | const KernelProperties properties = |
| 135 | IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 136 | |
| 137 | ArgumentPack<ITensorInfo> arguments; |
| 138 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 139 | arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 140 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentPool2d::validate(properties, arguments, attributes, settings)); |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 146 | } |
| 147 | return Status{}; |
| 148 | } |
| 149 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 150 | ITensorInfo *GpuPool2d::create_op(GpuWorkloadSketch &sketch, |
| 151 | ITensorInfo *src, |
| 152 | const Pool2dAttributes &attributes, |
| 153 | const GpuPool2dSettings &settings) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 154 | { |
| 155 | // Assert validation |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_THROW_ON(GpuPool2d::validate_op(sketch, src, attributes, settings)); |
| 157 | ARM_COMPUTE_LOG_PARAMS(src, attributes, settings); |
| 158 | |
| 159 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
| 160 | ARM_COMPUTE_ERROR_ON_NULLPTR(dst); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 161 | |
| 162 | // Auto initialize dst tensor |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 163 | calculate_and_init_dst_if_empty(dst, src, attributes, settings); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 164 | |
| 165 | // Translate into components and add to component graph |
| 166 | auto &comp_graph = sketch.implementation().component_graph(); |
| 167 | |
| 168 | const auto sketch_ctx = sketch.implementation().context(); |
| 169 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 170 | if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 171 | { |
| 172 | const auto cl_compile_ctx = sketch_ctx->cl_compile_context(); |
Omar Al Khatib | 3c7c1fa | 2023-03-07 09:57:49 +0000 | [diff] [blame] | 173 | ARM_COMPUTE_UNUSED(cl_compile_ctx); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 174 | ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr); |
| 175 | |
| 176 | // Add Component |
| 177 | { |
| 178 | auto properties = IGpuKernelComponent::Properties(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 179 | properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 180 | |
| 181 | ArgumentPack<ITensorInfo> arguments; |
| 182 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 183 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 184 | comp_graph.add_new_component<ClComponentPool2d>(properties, arguments, attributes, settings); |
| 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 190 | } |
| 191 | |
| 192 | // Set up fusion test by adding to the Operator Group |
| 193 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 194 | |
| 195 | // Pack tensor infos |
| 196 | ArgumentPack<ITensorInfo> tensors; |
| 197 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 198 | tensors.add_tensor(ACL_DST_0, dst); |
| 199 | |
| 200 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 201 | sketch.implementation().operator_group().add_operator(op); |
Adnan AlSinan | 2e6d659 | 2023-08-21 13:54:27 +0100 | [diff] [blame] | 202 | |
| 203 | return dst; |
Mohammed Suhail Munshi | a18d85c | 2023-01-03 10:16:16 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace dynamic_fusion |
| 207 | } // namespace experimental |
| 208 | } // namespace arm_compute |