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