Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +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 | |
| 25 | #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h" |
| 26 | |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 27 | #include "src/common/utils/Log.h" |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 28 | #include "src/core/helpers/AutoConfiguration.h" |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 29 | #include "src/dynamic_fusion/sketch/ArgumentPack.h" |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 30 | #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentStore.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h" |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 32 | #include "src/dynamic_fusion/utils/Utils.h" |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace experimental |
| 37 | { |
| 38 | namespace dynamic_fusion |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | constexpr GpuOperatorType operator_type = GpuOperatorType::Simple; |
Gunes Bayir | cc28773 | 2023-01-19 15:56:00 +0000 | [diff] [blame] | 43 | } // namespace |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 44 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | Status GpuOutput::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const ITensorInfo *dst) |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 48 | |
| 49 | // Initialize the destination tensor info. |
| 50 | TensorInfo dst_to_validate = *dst; |
| 51 | auto_init_if_empty(dst_to_validate, *src); |
| 52 | |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate); |
| 55 | |
| 56 | ARM_COMPUTE_UNUSED(context); |
| 57 | return Status{}; |
| 58 | } |
| 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | Status GpuOutput::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const ITensorInfo *dst) |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id()); |
SiCong Li | 5a2bc01 | 2023-01-12 12:54:49 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON(!is_alloc_tensor(dst)); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 65 | |
| 66 | // Initialize the destination tensor info. |
| 67 | TensorInfo dst_to_validate = *dst; |
| 68 | auto_init_if_empty(dst_to_validate, *src); |
| 69 | |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate); |
| 72 | |
| 73 | // Perform fusion test. |
| 74 | ArgumentPack<ITensorInfo> tensors; |
| 75 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 76 | tensors.add_const_tensor(ACL_DST_0, &dst_to_validate); |
| 77 | |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 78 | const auto group = sketch.implementation().operator_group(); |
| 79 | const auto op = group.new_operator(operator_type, tensors); |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 80 | const auto success = group.try_add_operator(op, true); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 81 | |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!success, "This operator cannot be fused into the workload."); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 83 | |
| 84 | const auto status = is_supported_op(*sketch.gpu_context(), src, dst); |
| 85 | return status; |
| 86 | } |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | void GpuOutput::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, ITensorInfo *dst) |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 89 | { |
| 90 | ARM_COMPUTE_LOG_PARAMS(src, dst); |
| 91 | ARM_COMPUTE_ERROR_THROW_ON(GpuOutput::validate_op(sketch, src, dst)); |
| 92 | |
| 93 | // Auto initialize dst tensor info if empty |
| 94 | auto_init_if_empty(*dst, *src); |
| 95 | |
| 96 | // Translate into components and add to component graph |
Gunes Bayir | 3a1e125 | 2023-01-03 21:26:09 +0000 | [diff] [blame] | 97 | auto &comp_graph = sketch.implementation().component_graph(); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 98 | const auto sketch_ctx = sketch.implementation().context(); |
| 99 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL) |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 101 | { |
| 102 | ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr); |
| 103 | |
| 104 | // Add store component |
| 105 | { |
| 106 | IGpuKernelComponent::Properties properties; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run}); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 108 | |
| 109 | ArgumentPack<ITensorInfo> arguments; |
| 110 | arguments.add_const_tensor(ACL_SRC_0, src); |
| 111 | arguments.add_const_tensor(ACL_DST_0, dst); |
| 112 | comp_graph.add_new_component<ClComponentStore>(properties, arguments); |
| 113 | } |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | ARM_COMPUTE_ERROR("Unimplemented Gpu language"); |
| 118 | } |
| 119 | |
| 120 | // Set up fusion test by adding to the Operator Group |
| 121 | // Note this has to be performed after all the components have been successfully added to the component graph |
| 122 | |
| 123 | // Pack tensor infos |
| 124 | ArgumentPack<ITensorInfo> tensors; |
| 125 | tensors.add_const_tensor(ACL_SRC_0, src); |
| 126 | tensors.add_const_tensor(ACL_DST_0, dst); |
| 127 | |
| 128 | const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors); |
Viet-Hoa Do | 04f4620 | 2022-12-14 14:49:56 +0000 | [diff] [blame] | 129 | sketch.implementation().operator_group().add_operator(op, true); |
Viet-Hoa Do | b84e253 | 2022-12-13 13:09:10 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace dynamic_fusion |
| 133 | } // namespace experimental |
| 134 | } // namespace arm_compute |