Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +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/GpuReshape.h" |
| 25 | #include "arm_compute/core/Error.h" |
| 26 | #include "src/common/utils/Log.h" |
| 27 | #include "src/core/helpers/AutoConfiguration.h" |
| 28 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
| 29 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
| 30 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentReshape.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace experimental |
| 35 | { |
| 36 | namespace dynamic_fusion |
| 37 | { |
| 38 | namespace |
| 39 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 40 | Status is_supported_op_helper(const GpuWorkloadContext &context, |
| 41 | const ITensorInfo *src, |
| 42 | const ITensorInfo *dst, |
| 43 | const ReshapeAttributes &attributes) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 44 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 46 | |
| 47 | TensorInfo dst_info_to_validate; |
| 48 | const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate; |
| 49 | |
| 50 | if(dst != nullptr) |
| 51 | { |
| 52 | dst_info_to_validate_ptr = dst; |
| 53 | } |
| 54 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 55 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape())); |
| 56 | |
| 57 | // Check components |
| 58 | if(context.gpu_language() == GpuLanguage::OpenCL) |
| 59 | { |
| 60 | const auto cl_compile_ctx = context.cl_compile_context(); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr); |
| 62 | |
| 63 | // Validate GpuReshape Component |
| 64 | ArgumentPack<ITensorInfo> arguments; |
| 65 | arguments.add_const_tensor(ACL_SRC_0, src); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 66 | arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 67 | |
| 68 | ARM_COMPUTE_RETURN_ON_ERROR(ClComponentReshape::validate(arguments)); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language"); |
| 73 | } |
| 74 | |
| 75 | return Status{}; |
| 76 | } |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 77 | |
| 78 | GpuOperatorType operator_type = GpuOperatorType::Complex; |
| 79 | } // namespace |
| 80 | |
| 81 | Status GpuReshape::is_supported_op(const GpuWorkloadContext &context, |
| 82 | const ITensorInfo *src, |
| 83 | const Attributes &attributes) |
| 84 | { |
| 85 | return is_supported_op_helper(context, src, nullptr, attributes); |
| 86 | } |
| 87 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 88 | Status GpuReshape::validate_op(const GpuWorkloadSketch &sketch, |
| 89 | const ITensorInfo *src, |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 90 | const Attributes &attributes) |
| 91 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 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; |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 97 | |
| 98 | // Auto initialize dst tensor info |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 99 | auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(attributes.shape())); |
| 100 | |
| 101 | // Perform fusion test |
| 102 | // Pack tensor infos |
| 103 | ArgumentPack<ITensorInfo> tensors; |
| 104 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 105 | tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate); |
| 106 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op), |
| 108 | "Operator fusion test failed. This operator cannot be fused into the workload"); |
| 109 | |
| 110 | // Check if configuration is supported |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 111 | 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] | 112 | } |
| 113 | |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 114 | ITensorInfo *GpuReshape::create_op(GpuWorkloadSketch &sketch, |
| 115 | ITensorInfo *src, |
| 116 | const Attributes &attributes) |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 117 | { |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON_NULLPTR(src); |
| 119 | ARM_COMPUTE_LOG_PARAMS(src, attributes.shape()); |
| 120 | ARM_COMPUTE_ERROR_THROW_ON(GpuReshape::validate_op(sketch, src, attributes)); |
| 121 | |
| 122 | ITensorInfo *dst = sketch.implementation().create_virtual_tensor(); |
| 123 | ARM_COMPUTE_ERROR_ON_NULLPTR(dst); |
| 124 | |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 125 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(attributes.shape())); |
| 126 | |
| 127 | // Translate into components and add to component graph |
| 128 | auto &comp_graph = sketch.implementation().component_graph(); |
| 129 | const auto sketch_ctx = sketch.implementation().context(); |
| 130 | if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
| 131 | { |
| 132 | const auto cl_compile_ctx = sketch_ctx->cl_compile_context(); |
Omar Al Khatib | 3c7c1fa | 2023-03-07 09:57:49 +0000 | [diff] [blame] | 133 | ARM_COMPUTE_UNUSED(cl_compile_ctx); |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 134 | ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr); |
| 135 | |
| 136 | // Add ElementwiseBinary Component |
| 137 | { |
| 138 | auto properties = IGpuKernelComponent::Properties(); |
| 139 | properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run }); |
| 140 | |
| 141 | ArgumentPack<ITensorInfo> arguments; |
| 142 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 143 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 144 | comp_graph.add_new_component<ClComponentReshape>(properties, arguments); |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 150 | } |
| 151 | // Set up fusion test by adding to the Operator Group |
| 152 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 153 | |
| 154 | // Pack tensor infos |
| 155 | ArgumentPack<ITensorInfo> tensors; |
| 156 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 157 | tensors.add_tensor(ACL_DST_0, dst); |
| 158 | |
| 159 | const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
| 160 | sketch.implementation().operator_group().add_operator(op); |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 161 | |
| 162 | return dst; |
Ramy Elgammal | f800adf | 2022-12-14 15:39:29 +0000 | [diff] [blame] | 163 | } |
| 164 | } // namespace dynamic_fusion |
| 165 | } // namespace experimental |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 166 | } // namespace arm_compute |