blob: bf0f274c5c998a94a42cd0937b823fe0e7e66aba [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/GpuTanh.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000027#include "arm_compute/core/experimental/Types.h"
28
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000029#include "src/common/utils/Log.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/dynamic_fusion/sketch/ArgumentPack.h"
32#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentActivation.h"
33#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
34#include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateActivation.h"
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000035
36namespace arm_compute
37{
38namespace experimental
39{
40namespace dynamic_fusion
41{
42namespace
43{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044Status is_supported_op_helper(const GpuWorkloadContext &context, const ITensorInfo *src, const ITensorInfo *dst)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000045{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
48
49 TensorInfo dst_info_to_validate;
50 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (dst != nullptr)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000053 {
54 dst_info_to_validate_ptr = dst;
55 }
56
57 auto_init_if_empty(dst_info_to_validate, *src->clone());
58
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 const ClComponentActivation::Attributes act_info{ActivationLayerInfo::ActivationFunction::TANH};
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000060
61 // Check components
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 if (context.gpu_language() == GpuLanguage::OpenCL)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000063 {
64 // Validate Activation Component
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 const auto properties =
66 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000067
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083Status GpuTanh::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000084{
85 return is_supported_op_helper(context, src, nullptr);
86}
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088Status GpuTanh::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +000089{
90 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
91
92 // Check if tensors have valid id, i.e. they are created from a sketch
93 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
94
95 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
96 TensorInfo dst_info_to_validate;
97
98 // Auto initialize dst tensor info
99 auto_init_if_empty(dst_info_to_validate, *src->clone());
100
101 // Perform fusion test to check if the operator meets fusion constraints
102 ArgumentPack<ITensorInfo> tensors;
103 tensors.add_const_tensor(ACL_SRC, src);
104 tensors.add_const_tensor(ACL_DST, &dst_info_to_validate);
105 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
106 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
107 "Operator fusion test failed. This operator cannot be fused into the workload");
108
109 // Check if configuration is supported
110 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate);
111}
112
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113ITensorInfo *GpuTanh::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +0000114{
115 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
116 ARM_COMPUTE_LOG_PARAMS(src);
117 ARM_COMPUTE_ERROR_THROW_ON(GpuTanh::validate_op(sketch, src));
118
119 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
120 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
121
122 // Auto initialize dst tensor
123 auto_init_if_empty(*dst, *src->clone());
124
125 // Translate into components and add to component graph
126 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
127
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 const ClComponentActivation::Attributes act_info{ActivationLayerInfo::ActivationFunction::TANH};
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +0000129
130 const auto *const sketch_ctx = sketch.implementation().context();
131
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +0000133 {
134 // Add Activation Component
135 auto properties = IGpuKernelComponent::Properties();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Viet-Hoa Do98aca0f2023-03-02 17:43:45 +0000137
138 ArgumentPack<ITensorInfo> arguments;
139 arguments.add_const_tensor(ACL_SRC, src);
140 arguments.add_const_tensor(ACL_DST, dst);
141 comp_graph.add_new_component<ClComponentActivation>(properties, arguments, act_info);
142 }
143 else
144 {
145 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
146 }
147
148 // Set up fusion test by adding to the Operator Group
149 // Note this has to be performed after all the components have been successfully added to the component graph
150
151 // Pack tensor infos
152 ArgumentPack<ITensorInfo> tensors;
153 tensors.add_const_tensor(ACL_SRC, src);
154 tensors.add_const_tensor(ACL_DST, dst);
155
156 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
157 sketch.implementation().operator_group().add_operator(op);
158
159 return dst;
160}
161
162} // namespace dynamic_fusion
163} // namespace experimental
164} // namespace arm_compute