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 "src/dynamic_fusion/sketch/gpu/operators/internal/GpuElementwiseBinaryCommon.h"
25 #include "src/common/utils/Log.h"
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/ClComponentElementwiseBinary.h"
30
31 namespace arm_compute
32 {
33 namespace experimental
34 {
35 namespace dynamic_fusion
36 {
37 namespace
38 {
calculate_and_init_dst_if_empty(ITensorInfo * dst,const ITensorInfo * lhs,const ITensorInfo * rhs)39 void calculate_and_init_dst_if_empty(ITensorInfo *dst, const ITensorInfo *lhs, const ITensorInfo *rhs)
40 {
41 if(dst->total_size() == 0U)
42 {
43 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*lhs, *rhs);
44 auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(broadcast_pair.first));
45 }
46 }
47
is_supported_op_helper(const GpuWorkloadContext & context,const ITensorInfo * lhs,const ITensorInfo * rhs,const ITensorInfo * dst,const ElementwiseBinaryCommonAttributes & attributes)48 Status is_supported_op_helper(const GpuWorkloadContext &context,
49 const ITensorInfo *lhs,
50 const ITensorInfo *rhs,
51 const ITensorInfo *dst,
52 const ElementwiseBinaryCommonAttributes &attributes)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs);
55
56 TensorInfo dst_info_to_validate;
57 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
58
59 if(dst != nullptr)
60 {
61 dst_info_to_validate_ptr = dst;
62 }
63
64 calculate_and_init_dst_if_empty(&dst_info_to_validate, lhs, rhs);
65
66 // Check components
67 if(context.gpu_language() == GpuLanguage::OpenCL)
68 {
69 const auto cl_compile_ctx = context.cl_compile_context();
70 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
71
72 // Validate ElementwiseBinary Component
73 {
74 ArgumentPack<ITensorInfo> arguments;
75 arguments.add_const_tensor(ACL_SRC_0, lhs);
76 arguments.add_const_tensor(ACL_SRC_1, rhs);
77 arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
78
79 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentElementwiseBinary::validate(arguments, attributes));
80 }
81 }
82 else
83 {
84 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
85 }
86
87 return Status{};
88 }
89
90 GpuOperatorType operator_type = GpuOperatorType::Simple;
91 } // namespace
92
operation(const ElementwiseBinaryCommonAttributes::ElementwiseOp & operation)93 ElementwiseBinaryCommonAttributes &ElementwiseBinaryCommonAttributes::operation(const ElementwiseBinaryCommonAttributes::ElementwiseOp &operation)
94 {
95 _operation = operation;
96 return *this;
97 }
98
operation() const99 ElementwiseBinaryCommonAttributes::ElementwiseOp ElementwiseBinaryCommonAttributes::operation() const
100 {
101 return _operation;
102 }
103
is_supported_op(const GpuWorkloadContext & context,const ITensorInfo * lhs,const ITensorInfo * rhs,const ElementwiseBinaryCommonAttributes & attributes)104 Status GpuElementwiseBinaryCommon::is_supported_op(const GpuWorkloadContext &context,
105 const ITensorInfo *lhs,
106 const ITensorInfo *rhs,
107 const ElementwiseBinaryCommonAttributes &attributes)
108 {
109 return is_supported_op_helper(context, lhs, rhs, nullptr, attributes);
110 }
111
validate_op(const GpuWorkloadSketch & sketch,const ITensorInfo * lhs,const ITensorInfo * rhs,const ElementwiseBinaryCommonAttributes & attributes)112 Status GpuElementwiseBinaryCommon::validate_op(const GpuWorkloadSketch &sketch,
113 const ITensorInfo *lhs,
114 const ITensorInfo *rhs,
115 const ElementwiseBinaryCommonAttributes &attributes)
116 {
117 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs);
118 ARM_COMPUTE_RETURN_ERROR_ON(!lhs->has_valid_id() || !rhs->has_valid_id());
119
120 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
121 TensorInfo dst_info_to_validate;
122
123 // Auto initialize dst tensor info
124 calculate_and_init_dst_if_empty(&dst_info_to_validate, lhs, rhs);
125
126 // Perform fusion test
127 // Pack tensor infos
128 ArgumentPack<ITensorInfo> tensors;
129 tensors.add_const_tensor(ACL_SRC_0, lhs);
130 tensors.add_const_tensor(ACL_SRC_1, rhs);
131 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
132 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
134 "Operator fusion test failed. This operator cannot be fused into the workload");
135
136 // Check if configuration is supported
137 return is_supported_op_helper(*sketch.gpu_context(), lhs, rhs, &dst_info_to_validate, attributes);
138 }
139
create_op(GpuWorkloadSketch & sketch,ITensorInfo * lhs,ITensorInfo * rhs,const ElementwiseBinaryCommonAttributes & attributes)140 ITensorInfo *GpuElementwiseBinaryCommon::create_op(GpuWorkloadSketch &sketch,
141 ITensorInfo *lhs,
142 ITensorInfo *rhs,
143 const ElementwiseBinaryCommonAttributes &attributes)
144 {
145 ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs);
146 ARM_COMPUTE_LOG_PARAMS(lhs, rhs);
147 ARM_COMPUTE_ERROR_THROW_ON(GpuElementwiseBinaryCommon::validate_op(sketch, lhs, rhs, attributes));
148
149 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
150 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
151
152 // Auto initialize dst tensor
153 calculate_and_init_dst_if_empty(dst, lhs, rhs);
154
155 // Translate into components and add to component graph
156 auto &comp_graph = sketch.implementation().component_graph();
157
158 const auto sketch_ctx = sketch.implementation().context();
159
160 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
161 {
162 ARM_COMPUTE_ERROR_ON_NULLPTR(sketch_ctx->cl_compile_context());
163
164 // Add ElementwiseBinary Component
165 {
166 auto properties = IGpuKernelComponent::Properties();
167 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
168
169 ArgumentPack<ITensorInfo> arguments;
170 arguments.add_const_tensor(ACL_SRC_0, lhs);
171 arguments.add_const_tensor(ACL_SRC_1, rhs);
172 arguments.add_const_tensor(ACL_DST_0, dst);
173 comp_graph.add_new_component<ClComponentElementwiseBinary>(properties, arguments, attributes);
174 }
175 }
176 else
177 {
178 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
179 }
180
181 // Set up fusion test by adding to the Operator Group
182 // Note this has to be performed after all the components have been successfully added to the component graph
183
184 // Pack tensor infos
185 ArgumentPack<ITensorInfo> tensors;
186 tensors.add_const_tensor(ACL_SRC_0, lhs);
187 tensors.add_const_tensor(ACL_SRC_1, rhs);
188 tensors.add_tensor(ACL_DST_0, dst);
189 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
190 sketch.implementation().operator_group().add_operator(op);
191
192 return dst;
193 }
194
195 } // namespace dynamic_fusion
196 } // namespace experimental
197 } // namespace arm_compute
198