blob: 95adfae70c2ebc00923b2a709db665072addc8e5 [file] [log] [blame]
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +00001/*
2 * Copyright (c) 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
25#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuSigmoid.h"
Matthew Bentham1d062042023-07-06 13:13:59 +000026#include "arm_compute/core/ActivationLayerInfo.h"
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000027#include "arm_compute/core/experimental/Types.h"
28
SiCong Li19844f62023-05-16 16:46:34 +010029#include "src/common/utils/Log.h"
30#include "src/core/helpers/AutoConfiguration.h"
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000031#include "src/dynamic_fusion/sketch/ArgumentPack.h"
32#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
33#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentActivation.h"
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000034
35namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41namespace
42{
43Status is_supported_op_helper(const GpuWorkloadContext &context,
44 const ITensorInfo *src,
45 const ITensorInfo *dst)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
49
50 TensorInfo dst_info_to_validate;
51 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
52
53 if(dst != nullptr)
54 {
55 dst_info_to_validate_ptr = dst;
56 }
57
58 auto_init_if_empty(dst_info_to_validate, *src->clone());
59
60 const ClComponentActivation::Attributes act_info{ ActivationLayerInfo::ActivationFunction::LOGISTIC };
61
62 // Check components
63 if(context.gpu_language() == GpuLanguage::OpenCL)
64 {
65 // Validate Activation Component
66 const auto properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
67
68 ArgumentPack<ITensorInfo> arguments;
69 arguments.add_const_tensor(ACL_SRC, src);
70 arguments.add_const_tensor(ACL_DST, dst_info_to_validate_ptr);
71 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentActivation::validate(properties, arguments, act_info));
72 }
73 else
74 {
75 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
76 }
77 return Status{};
78}
79
80constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
81} // namespace
82
83Status GpuSigmoid::is_supported_op(const GpuWorkloadContext &context,
SiCong Li19844f62023-05-16 16:46:34 +010084 const ITensorInfo *src)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000085{
86 return is_supported_op_helper(context, src, nullptr);
87}
88
89Status GpuSigmoid::validate_op(const GpuWorkloadSketch &sketch,
90 const ITensorInfo *src)
91{
92 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
93
94 // Check if tensors have valid id, i.e. they are created from a sketch
95 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
96
97 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
98 TensorInfo dst_info_to_validate;
99
100 // Auto initialize dst tensor info
101 auto_init_if_empty(dst_info_to_validate, *src->clone());
102
103 // Perform fusion test to check if the operator meets fusion constraints
104 ArgumentPack<ITensorInfo> tensors;
105 tensors.add_const_tensor(ACL_SRC, src);
106 tensors.add_const_tensor(ACL_DST, &dst_info_to_validate);
107 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
108 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
109 "Operator fusion test failed. This operator cannot be fused into the workload");
110
111 // Check if configuration is supported
112 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate);
113}
114
SiCong Li19844f62023-05-16 16:46:34 +0100115ITensorInfo *GpuSigmoid::create_op(GpuWorkloadSketch &sketch,
116 ITensorInfo *src)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +0000117{
118 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
119 ARM_COMPUTE_LOG_PARAMS(src);
120 ARM_COMPUTE_ERROR_THROW_ON(GpuSigmoid::validate_op(sketch, src));
121
122 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
123 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
124
125 // Auto initialize dst tensor
126 auto_init_if_empty(*dst, *src->clone());
127
128 // Translate into components and add to component graph
129 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
130
131 const ClComponentActivation::Attributes act_info{ ActivationLayerInfo::ActivationFunction::LOGISTIC };
132
133 const auto *const sketch_ctx = sketch.implementation().context();
134
135 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
136 {
137 // Add Activation Component
138 auto properties = IGpuKernelComponent::Properties();
139 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
140
141 ArgumentPack<ITensorInfo> arguments;
142 arguments.add_const_tensor(ACL_SRC, src);
143 arguments.add_const_tensor(ACL_DST, dst);
144 comp_graph.add_new_component<ClComponentActivation>(properties, arguments, act_info);
145 }
146 else
147 {
148 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
149 }
150
151 // Set up fusion test by adding to the Operator Group
152 // Note this has to be performed after all the components have been successfully added to the component graph
153
154 // Pack tensor infos
155 ArgumentPack<ITensorInfo> tensors;
156 tensors.add_const_tensor(ACL_SRC, src);
157 tensors.add_const_tensor(ACL_DST, dst);
158
159 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
160 sketch.implementation().operator_group().add_operator(op);
161
162 return dst;
163}
164
165} // namespace dynamic_fusion
166} // namespace experimental
167} // namespace arm_compute