Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 59e4060 | 2024-02-08 10:50:53 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2023-2024 Arm Limited. |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +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/GpuReshape.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 27 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 28 | #include "src/common/utils/Log.h" |
| 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 31 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentReshape.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 33 | |
| 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, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | const ReshapeAttributes &attributes) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 46 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 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) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 53 | { |
| 54 | dst_info_to_validate_ptr = dst; |
| 55 | } |
| 56 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 57 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape())); |
| 58 | |
| 59 | // Check components |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | if (context.gpu_language() == GpuLanguage::OpenCL) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 61 | { |
| 62 | const auto cl_compile_ctx = context.cl_compile_context(); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 64 | |
| 65 | // Validate GpuReshape Component |
| 66 | ArgumentPack<ITensorInfo> arguments; |
| 67 | arguments.add_const_tensor(ACL_SRC_0, src); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 68 | arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 69 | |
| 70 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentReshape::validate(arguments)); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 75 | } |
| 76 | |
Gunes Bayir | 59e4060 | 2024-02-08 10:50:53 +0000 | [diff] [blame^] | 77 | return Status{ErrorCode::RUNTIME_ERROR, "GpuReshape is not Supported"}; |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 78 | } |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 79 | |
| 80 | GpuOperatorType operator_type = GpuOperatorType::Complex; |
| 81 | } // namespace |
| 82 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | Status |
| 84 | GpuReshape::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const Attributes &attributes) |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 85 | { |
| 86 | return is_supported_op_helper(context, src, nullptr, attributes); |
| 87 | } |
| 88 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | Status GpuReshape::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const Attributes &attributes) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 90 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 91 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id()); |
| 93 | |
| 94 | // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object |
| 95 | TensorInfo dst_info_to_validate; |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 96 | |
| 97 | // Auto initialize dst tensor info |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 98 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape())); |
| 99 | |
| 100 | // Perform fusion test |
| 101 | // Pack tensor infos |
| 102 | ArgumentPack<ITensorInfo> tensors; |
| 103 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 104 | tensors.add_const_tensor(ACL_DST_0, &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 |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 110 | return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | ITensorInfo *GpuReshape::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, const Attributes &attributes) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 114 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_ON_NULLPTR(src); |
| 116 | ARM_COMPUTE_LOG_PARAMS(src, attributes.shape()); |
| 117 | ARM_COMPUTE_ERROR_THROW_ON(GpuReshape::validate_op(sketch, src, attributes)); |
| 118 | |
| 119 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
| 120 | ARM_COMPUTE_ERROR_ON_NULLPTR(dst); |
| 121 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 122 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(attributes.shape())); |
| 123 | |
| 124 | // Translate into components and add to component graph |
| 125 | auto &comp_graph = sketch.implementation().component_graph(); |
| 126 | const auto sketch_ctx = sketch.implementation().context(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 128 | { |
| 129 | const auto cl_compile_ctx = sketch_ctx->cl_compile_context(); |
Omar Al Khatib | 3c7c1fa | 2023-03-07 09:57:49 +0000 | [diff] [blame] | 130 | ARM_COMPUTE_UNUSED(cl_compile_ctx); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 131 | ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr); |
| 132 | |
| 133 | // Add ElementwiseBinary Component |
| 134 | { |
| 135 | auto properties = IGpuKernelComponent::Properties(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 137 | |
| 138 | ArgumentPack<ITensorInfo> arguments; |
| 139 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 140 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 141 | comp_graph.add_new_component<ClComponentReshape>(properties, arguments); |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 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_0, src); |
| 154 | tensors.add_tensor(ACL_DST_0, dst); |
| 155 | |
| 156 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 157 | sketch.implementation().operator_group().add_operator(op); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 158 | |
| 159 | return dst; |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 160 | } |
| 161 | } // namespace dynamic_fusion |
| 162 | } // namespace experimental |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 163 | } // namespace arm_compute |