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