xref: /aosp_15_r20/external/ComputeLibrary/src/dynamic_fusion/sketch/gpu/operators/GpuReshape.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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 {
is_supported_op_helper(const GpuWorkloadContext & context,const ITensorInfo * src,const ITensorInfo * dst,const ReshapeAttributes & attributes)40 Status is_supported_op_helper(const GpuWorkloadContext &context,
41                               const ITensorInfo        *src,
42                               const ITensorInfo        *dst,
43                               const ReshapeAttributes &attributes)
44 {
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 
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);
66         arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
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 }
77 
78 GpuOperatorType operator_type = GpuOperatorType::Complex;
79 } // namespace
80 
is_supported_op(const GpuWorkloadContext & context,const ITensorInfo * src,const Attributes & attributes)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 
validate_op(const GpuWorkloadSketch & sketch,const ITensorInfo * src,const Attributes & attributes)88 Status GpuReshape::validate_op(const GpuWorkloadSketch &sketch,
89                                const ITensorInfo       *src,
90                                const Attributes        &attributes)
91 {
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;
97 
98     // Auto initialize dst tensor info
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
111     return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
112 }
113 
create_op(GpuWorkloadSketch & sketch,ITensorInfo * src,const Attributes & attributes)114 ITensorInfo *GpuReshape::create_op(GpuWorkloadSketch &sketch,
115                                    ITensorInfo       *src,
116                                    const Attributes &attributes)
117 {
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 
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();
133         ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
134 
135         // Add ElementwiseBinary Component
136         {
137             auto properties = IGpuKernelComponent::Properties();
138             properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
139 
140             ArgumentPack<ITensorInfo> arguments;
141             arguments.add_const_tensor(ACL_SRC_0, src);
142             arguments.add_const_tensor(ACL_DST_0, dst);
143             comp_graph.add_new_component<ClComponentReshape>(properties, arguments);
144         }
145     }
146     else
147     {
148         ARM_COMPUTE_ERROR("Unimplemented Gpu language");
149     }
150     // Set up fusion test by adding to the Operator Group
151     // Note this has to be performed after all the components have been successfully added to the component graph
152 
153     // Pack tensor infos
154     ArgumentPack<ITensorInfo> tensors;
155     tensors.add_const_tensor(ACL_SRC_0, src);
156     tensors.add_tensor(ACL_DST_0, dst);
157 
158     const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
159     sketch.implementation().operator_group().add_operator(op);
160 
161     return dst;
162 }
163 } // namespace dynamic_fusion
164 } // namespace experimental
165 } // namespace arm_compute
166