xref: /aosp_15_r20/external/ComputeLibrary/src/dynamic_fusion/sketch/gpu/operators/GpuCast.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 #include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuCast.h"
25 
26 #include "src/core/helpers/AutoConfiguration.h"
27 #include "src/dynamic_fusion/sketch/ArgumentPack.h"
28 #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
29 #include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentCast.h"
30 
31 #include "src/common/utils/Log.h"
32 
33 namespace arm_compute
34 {
35 namespace experimental
36 {
37 namespace dynamic_fusion
38 {
39 namespace
40 {
is_supported_op_helper(const GpuWorkloadContext & context,const ITensorInfo * src,const ITensorInfo * dst,const CastAttributes & attributes)41 Status is_supported_op_helper(const GpuWorkloadContext &context,
42                               const ITensorInfo        *src,
43                               const ITensorInfo        *dst,
44                               const CastAttributes     &attributes)
45 {
46     ARM_COMPUTE_RETURN_ERROR_ON(src == dst);
47     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48 
49     TensorInfo         dst_info_to_validate;
50     const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
51 
52     if(dst != nullptr)
53     {
54         dst_info_to_validate_ptr = dst;
55     }
56 
57     auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type()));
58 
59     // Check support level
60     // Data Type
61     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src,
62                                                          1,
63                                                          DataType::U8, DataType::S8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S16,
64                                                          DataType::U16, DataType::U32, DataType::S32, DataType::F16,
65                                                          DataType::F32);
66     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst_info_to_validate_ptr,
67                                                          1,
68                                                          DataType::U8, DataType::S8, DataType::QASYMM8, DataType::S16,
69                                                          DataType::U16, DataType::U32, DataType::S32, DataType::F16,
70                                                          DataType::F32);
71 
72     if(context.gpu_language() == GpuLanguage::OpenCL)
73     {
74         const auto cl_compile_ctx = context.cl_compile_context();
75         ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
76         // Validate Cast Component
77         {
78             const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
79             auto       settings   = ClComponentCast::Settings();
80 
81             ArgumentPack<ITensorInfo> arguments;
82             arguments.add_const_tensor(ACL_SRC_0, src);
83             arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
84             ARM_COMPUTE_RETURN_ON_ERROR(ClComponentCast::validate(properties, arguments, attributes, settings));
85         }
86     }
87     else
88     {
89         ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
90     }
91 
92     return Status{};
93 }
94 constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
95 } // namespace
96 
is_supported_op(const GpuWorkloadContext & context,const ITensorInfo * src,const CastAttributes & attributes)97 Status GpuCast::is_supported_op(const GpuWorkloadContext &context,
98                                 const ITensorInfo        *src,
99                                 const CastAttributes     &attributes)
100 {
101     return is_supported_op_helper(context, src, nullptr, attributes);
102 }
103 
validate_op(const GpuWorkloadSketch & sketch,const ITensorInfo * src,const CastAttributes & attributes)104 Status GpuCast::validate_op(const GpuWorkloadSketch &sketch,
105                             const ITensorInfo       *src,
106                             const CastAttributes    &attributes)
107 {
108     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
109     ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
110 
111     // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
112     TensorInfo dst_info_to_validate;
113 
114     // Auto initialize dst tensor info
115     auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type()));
116 
117     // Perform fusion test
118     // Pack tensor infos
119     ArgumentPack<ITensorInfo> tensors;
120     tensors.add_const_tensor(ACL_SRC_0, src);
121     tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
122     const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
123     ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
124                                     "Operator fusion test failed. This operator cannot be fused into the workload");
125 
126     // Check if configuration is supported
127     return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
128 }
129 
create_op(GpuWorkloadSketch & sketch,ITensorInfo * src,const CastAttributes & attributes)130 ITensorInfo *GpuCast::create_op(GpuWorkloadSketch    &sketch,
131                                 ITensorInfo          *src,
132                                 const CastAttributes &attributes)
133 {
134     ARM_COMPUTE_ERROR_ON_NULLPTR(src);
135     ARM_COMPUTE_LOG_PARAMS(src, attributes);
136     ARM_COMPUTE_ERROR_THROW_ON(GpuCast::validate_op(sketch, src, attributes));
137 
138     ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
139     ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
140 
141     // Auto initialize dst tensor info if empty
142     auto_init_if_empty(*dst, src->clone()->set_data_type(attributes.data_type()));
143 
144     // Translate into components and add to component graph
145     GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
146     const auto              *sketch_ctx = sketch.implementation().context();
147 
148     if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
149     {
150         ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr);
151 
152         // Add Depthwise Conv2d Component
153         {
154             const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
155             auto       settings   = ClComponentCast::Settings();
156 
157             ArgumentPack<ITensorInfo> arguments;
158             arguments.add_const_tensor(ACL_SRC_0, src);
159             arguments.add_const_tensor(ACL_DST_0, dst);
160             comp_graph.add_new_component<ClComponentCast>(properties, arguments, attributes, settings);
161         }
162     }
163     else
164     {
165         ARM_COMPUTE_ERROR("Unimplemented Gpu language");
166     }
167 
168     // Set up fusion test by adding to the Operator Group
169     // Note this has to be performed after all the components have been successfully added to the component graph
170 
171     // Pack tensor infos
172     ArgumentPack<ITensorInfo> tensors;
173     tensors.add_const_tensor(ACL_SRC_0, src);
174     tensors.add_const_tensor(ACL_DST_0, dst);
175 
176     const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
177     sketch.implementation().operator_group().add_operator(op);
178 
179     return dst;
180 }
181 
182 } // namespace dynamic_fusion
183 } // namespace experimental
184 } // namespace arm_compute
185