xref: /aosp_15_r20/external/ComputeLibrary/src/dynamic_fusion/sketch/gpu/operators/GpuOutput.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2022-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 
25 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuOutput.h"
26 
27 #include "src/common/utils/Log.h"
28 #include "src/core/helpers/AutoConfiguration.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 #include "src/dynamic_fusion/utils/Utils.h"
34 
35 namespace arm_compute
36 {
37 namespace experimental
38 {
39 namespace dynamic_fusion
40 {
41 namespace
42 {
43 constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
44 } // namespace
45 
is_supported_op(const GpuWorkloadContext & context,const ITensorInfo * src,const ITensorInfo * dst)46 Status GpuOutput::is_supported_op(const GpuWorkloadContext &context,
47                                   const ITensorInfo        *src,
48                                   const ITensorInfo        *dst)
49 {
50     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
51 
52     // Initialize the destination tensor info.
53     TensorInfo dst_to_validate = *dst;
54     auto_init_if_empty(dst_to_validate, *src);
55 
56     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
57     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
58 
59     ARM_COMPUTE_UNUSED(context);
60     return Status{};
61 }
62 
validate_op(const GpuWorkloadSketch & sketch,const ITensorInfo * src,const ITensorInfo * dst)63 Status GpuOutput::validate_op(const GpuWorkloadSketch &sketch,
64                               const ITensorInfo       *src,
65                               const ITensorInfo       *dst)
66 {
67     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
68     ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
69     ARM_COMPUTE_RETURN_ERROR_ON(!is_alloc_tensor(dst));
70 
71     // Initialize the destination tensor info.
72     TensorInfo dst_to_validate = *dst;
73     auto_init_if_empty(dst_to_validate, *src);
74 
75     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, &dst_to_validate);
76     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, &dst_to_validate);
77 
78     // Perform fusion test.
79     ArgumentPack<ITensorInfo> tensors;
80     tensors.add_const_tensor(ACL_SRC_0, src);
81     tensors.add_const_tensor(ACL_DST_0, &dst_to_validate);
82 
83     const auto group   = sketch.implementation().operator_group();
84     const auto op      = group.new_operator(operator_type, tensors);
85     const auto success = group.try_add_operator(op, true);
86 
87     ARM_COMPUTE_RETURN_ERROR_ON_MSG(!success, "This operator cannot be fused into the workload.");
88 
89     const auto status = is_supported_op(*sketch.gpu_context(), src, dst);
90     return status;
91 }
92 
create_op(GpuWorkloadSketch & sketch,ITensorInfo * src,ITensorInfo * dst)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);
136     sketch.implementation().operator_group().add_operator(op, true);
137 }
138 
139 } // namespace dynamic_fusion
140 } // namespace experimental
141 } // namespace arm_compute
142