blob: 89b533c9b86abad028faf739c40beb080835a7d7 [file] [log] [blame]
Jakub Sujak32741722022-11-25 16:43:18 +00001/*
Gunes Bayircc287732023-01-19 15:56:00 +00002 * Copyright (c) 2022-2023 Arm Limited.
Jakub Sujak32741722022-11-25 16:43:18 +00003 *
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
36namespace arm_compute
37{
38namespace experimental
39{
40namespace dynamic_fusion
41{
42namespace
43{
Gunes Bayircc287732023-01-19 15:56:00 +000044Status is_supported_op_helper(const GpuWorkloadContext &context,
45 const ITensorInfo *src,
46 const ITensorInfo *dst,
47 const ClampAttributes &attributes)
Jakub Sujak32741722022-11-25 16:43:18 +000048{
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
Gunes Bayircc287732023-01-19 15:56:00 +000053 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
Jakub Sujak32741722022-11-25 16:43:18 +000061 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
Gunes Bayircc287732023-01-19 15:56:00 +000064 const ClComponentActivation::Attributes act_info
65 {
66 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, attributes.max_val(), attributes.min_val()
67 };
Jakub Sujak32741722022-11-25 16:43:18 +000068
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);
Gunes Bayircc287732023-01-19 15:56:00 +000077 arguments.add_const_tensor(ACL_DST, dst_info_to_validate_ptr);
Jakub Sujak32741722022-11-25 16:43:18 +000078 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
Gunes Bayircc287732023-01-19 15:56:00 +000087constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
88} // namespace
89
90Status 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
Jakub Sujak32741722022-11-25 16:43:18 +000097Status GpuClamp::validate_op(const GpuWorkloadSketch &sketch,
98 const ITensorInfo *src,
Jakub Sujak32741722022-11-25 16:43:18 +000099 const ClampAttributes &attributes)
100{
Gunes Bayircc287732023-01-19 15:56:00 +0000101 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Jakub Sujak32741722022-11-25 16:43:18 +0000102
103 // Check if tensors have valid id, i.e. they are created from a sketch
Gunes Bayircc287732023-01-19 15:56:00 +0000104 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;
Jakub Sujak32741722022-11-25 16:43:18 +0000108
109 // Auto initialize dst tensor info
Jakub Sujak32741722022-11-25 16:43:18 +0000110 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
Gunes Bayircc287732023-01-19 15:56:00 +0000121 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
Jakub Sujak32741722022-11-25 16:43:18 +0000122}
123
Gunes Bayircc287732023-01-19 15:56:00 +0000124ITensorInfo *GpuClamp::create_op(GpuWorkloadSketch &sketch,
125 ITensorInfo *src,
126 const ClampAttributes &attributes)
Jakub Sujak32741722022-11-25 16:43:18 +0000127{
Gunes Bayircc287732023-01-19 15:56:00 +0000128 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);
Jakub Sujak32741722022-11-25 16:43:18 +0000134
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
Gunes Bayircc287732023-01-19 15:56:00 +0000142 const ClComponentActivation::Attributes act_info
143 {
144 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, attributes.max_val(), attributes.min_val()
145 };
Jakub Sujak32741722022-11-25 16:43:18 +0000146
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);
Gunes Bayircc287732023-01-19 15:56:00 +0000175
176 return dst;
Jakub Sujak32741722022-11-25 16:43:18 +0000177}
178
179} // namespace dynamic_fusion
180} // namespace experimental
181} // namespace arm_compute