blob: ffef6115d615b931c451444315f8beeaa961ffb1 [file] [log] [blame]
Jakub Sujak32741722022-11-25 16:43:18 +00001/*
2 * Copyright (c) 2022 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
36namespace arm_compute
37{
38namespace experimental
39{
40namespace dynamic_fusion
41{
42namespace
43{
44constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
45} // namespace
46
47Status GpuClamp::is_supported_op(const GpuWorkloadContext &context,
48 const ITensorInfo *src,
49 const ITensorInfo *dst,
50 const ClampAttributes &attributes)
51{
52 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
54 ARM_COMPUTE_RETURN_ERROR_ON_MSG(attributes.max_val() < attributes.min_val(), "Maximum clamp value cannot be lower than minimum value");
55
56 // Auto initialize dst tensor info
57 TensorInfo dst_info_to_validate = *dst;
58 auto_init_if_empty(dst_info_to_validate, *src->clone());
59
60 // CLAMP operator is implemented as LU_BOUNDED_RELU with the alpha and beta variables swapped
61 const ClComponentActivation::Attributes act_info{ ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, attributes.max_val(), attributes.min_val() };
62
63 // Check components
64 if(context.gpu_language() == GpuLanguage::OpenCL)
65 {
66 // Validate Activation Component
67 const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
68
69 ArgumentPack<ITensorInfo> arguments;
70 arguments.add_const_tensor(ACL_SRC, src);
71 arguments.add_const_tensor(ACL_DST, &dst_info_to_validate);
72 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentActivation::validate(properties, arguments, act_info));
73 }
74 else
75 {
76 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
77 }
78 return Status{};
79}
80
81Status GpuClamp::validate_op(const GpuWorkloadSketch &sketch,
82 const ITensorInfo *src,
83 const ITensorInfo *dst,
84 const ClampAttributes &attributes)
85{
86 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
87
88 // Check if tensors have valid id, i.e. they are created from a sketch
89 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !dst->has_valid_id());
90
91 // Auto initialize dst tensor info
92 TensorInfo dst_info_to_validate = *dst;
93 auto_init_if_empty(dst_info_to_validate, *src->clone());
94
95 // Perform fusion test to check if the operator meets fusion constraints
96 ArgumentPack<ITensorInfo> tensors;
97 tensors.add_const_tensor(ACL_SRC, src);
98 tensors.add_const_tensor(ACL_DST, &dst_info_to_validate);
99 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
100 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
101 "Operator fusion test failed. This operator cannot be fused into the workload");
102
103 // Check if configuration is supported
104 return is_supported_op(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
105}
106
107void GpuClamp::create_op(GpuWorkloadSketch &sketch,
108 ITensorInfo *src,
109 ITensorInfo *dst,
110 const ClampAttributes &attributes)
111{
112 // Assert validation
113 ARM_COMPUTE_ERROR_THROW_ON(GpuClamp::validate_op(sketch, src, dst, attributes));
114 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
115 ARM_COMPUTE_LOG_PARAMS(src, dst, attributes);
116
117 // Auto initialize dst tensor
118 auto_init_if_empty(*dst, *src->clone());
119
120 // Translate into components and add to component graph
121 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
122
123 // CLAMP operator is implemented as LU_BOUNDED_RELU with the alpha and beta variables swapped
124 const ClComponentActivation::Attributes act_info{ ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, attributes.max_val(), attributes.min_val() };
125
126 const auto *const sketch_ctx = sketch.implementation().context();
127
128 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
129 {
130 // Add Activation Component
131 auto properties = IGpuKernelComponent::Properties();
132 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
133
134 ArgumentPack<ITensorInfo> arguments;
135 arguments.add_const_tensor(ACL_SRC, src);
136 arguments.add_const_tensor(ACL_DST, dst);
137 comp_graph.add_new_component<ClComponentActivation>(properties, arguments, act_info);
138 }
139 else
140 {
141 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
142 }
143
144 // Set up fusion test by adding to the Operator Group
145 // Note this has to be performed after all the components have been successfully added to the component graph
146
147 // Pack tensor infos
148 ArgumentPack<ITensorInfo> tensors;
149 tensors.add_const_tensor(ACL_SRC, src);
150 tensors.add_const_tensor(ACL_DST, dst);
151
152 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
153 sketch.implementation().operator_group().add_operator(op);
154}
155
156} // namespace dynamic_fusion
157} // namespace experimental
158} // namespace arm_compute