blob: d25a2a3153f4e0d5387bd5efa5ee65c59bab0b5d [file] [log] [blame]
Gunes Bayir1dc6ff12022-12-06 20:48:31 +00001/*
Gunes Bayir2b9fa592024-01-17 16:07:03 +00002 * Copyright (c) 2022-2024 Arm Limited.
Gunes Bayir1dc6ff12022-12-06 20:48:31 +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/GpuCast.h"
25
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026#include "src/common/utils/Log.h"
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000027#include "src/core/helpers/AutoConfiguration.h"
28#include "src/dynamic_fusion/sketch/ArgumentPack.h"
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000029#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentCast.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000031
32namespace arm_compute
33{
34namespace experimental
35{
36namespace dynamic_fusion
37{
38namespace
39{
Gunes Bayircc287732023-01-19 15:56:00 +000040Status is_supported_op_helper(const GpuWorkloadContext &context,
41 const ITensorInfo *src,
42 const ITensorInfo *dst,
43 const CastAttributes &attributes)
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000044{
45 ARM_COMPUTE_RETURN_ERROR_ON(src == dst);
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
47
Gunes Bayircc287732023-01-19 15:56:00 +000048 TensorInfo dst_info_to_validate;
49 const ITensorInfo *dst_info_to_validate_ptr = &dst_info_to_validate;
50
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 if (dst != nullptr)
Gunes Bayircc287732023-01-19 15:56:00 +000052 {
53 dst_info_to_validate_ptr = dst;
54 }
55
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000056 auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type()));
57
58 // Check support level
59 // Data Type
Gunes Bayir2b9fa592024-01-17 16:07:03 +000060 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst_info_to_validate_ptr, 1, DataType::F16, DataType::F32);
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000062
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 if (context.gpu_language() == GpuLanguage::OpenCL)
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000064 {
65 const auto cl_compile_ctx = context.cl_compile_context();
66 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
67 // Validate Cast Component
68 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 const auto properties =
70 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
71 auto settings = ClComponentCast::Settings();
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000072
73 ArgumentPack<ITensorInfo> arguments;
74 arguments.add_const_tensor(ACL_SRC_0, src);
Gunes Bayircc287732023-01-19 15:56:00 +000075 arguments.add_const_tensor(ACL_DST_0, dst_info_to_validate_ptr);
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000076 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentCast::validate(properties, arguments, attributes, settings));
77 }
78 }
79 else
80 {
81 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
82 }
83
84 return Status{};
85}
Gunes Bayircc287732023-01-19 15:56:00 +000086constexpr GpuOperatorType operator_type = GpuOperatorType::Simple;
87} // namespace
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089Status
90GpuCast::is_supported_op(const GpuWorkloadContext &context, const ITensorInfo *src, const CastAttributes &attributes)
Gunes Bayircc287732023-01-19 15:56:00 +000091{
92 return is_supported_op_helper(context, src, nullptr, attributes);
93}
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000094
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095Status GpuCast::validate_op(const GpuWorkloadSketch &sketch, const ITensorInfo *src, const CastAttributes &attributes)
Gunes Bayir1dc6ff12022-12-06 20:48:31 +000096{
Gunes Bayircc287732023-01-19 15:56:00 +000097 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
98 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
99
100 // Refer to GpuConv2d::validate_op() for id-validness of this TensorInfo object
101 TensorInfo dst_info_to_validate;
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000102
103 // Auto initialize dst tensor info
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000104 auto_init_if_empty(dst_info_to_validate, src->clone()->set_data_type(attributes.data_type()));
105
106 // Perform fusion test
107 // Pack tensor infos
108 ArgumentPack<ITensorInfo> tensors;
109 tensors.add_const_tensor(ACL_SRC_0, src);
110 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
111 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
113 "Operator fusion test failed. This operator cannot be fused into the workload");
114
115 // Check if configuration is supported
Gunes Bayircc287732023-01-19 15:56:00 +0000116 return is_supported_op_helper(*sketch.gpu_context(), src, &dst_info_to_validate, attributes);
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000117}
118
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119ITensorInfo *GpuCast::create_op(GpuWorkloadSketch &sketch, ITensorInfo *src, const CastAttributes &attributes)
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000120{
Gunes Bayircc287732023-01-19 15:56:00 +0000121 ARM_COMPUTE_ERROR_ON_NULLPTR(src);
122 ARM_COMPUTE_LOG_PARAMS(src, attributes);
123 ARM_COMPUTE_ERROR_THROW_ON(GpuCast::validate_op(sketch, src, attributes));
124
125 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
126 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000127
128 // Auto initialize dst tensor info if empty
129 auto_init_if_empty(*dst, src->clone()->set_data_type(attributes.data_type()));
130
131 // Translate into components and add to component graph
132 GpuKernelComponentGraph &comp_graph = sketch.implementation().component_graph();
133 const auto *sketch_ctx = sketch.implementation().context();
134
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000136 {
137 ARM_COMPUTE_ERROR_ON(sketch_ctx->cl_compile_context() == nullptr);
138
139 // Add Depthwise Conv2d Component
140 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 const auto properties =
142 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
143 auto settings = ClComponentCast::Settings();
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000144
145 ArgumentPack<ITensorInfo> arguments;
146 arguments.add_const_tensor(ACL_SRC_0, src);
147 arguments.add_const_tensor(ACL_DST_0, dst);
148 comp_graph.add_new_component<ClComponentCast>(properties, arguments, attributes, settings);
149 }
150 }
151 else
152 {
153 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
154 }
155
156 // Set up fusion test by adding to the Operator Group
157 // Note this has to be performed after all the components have been successfully added to the component graph
158
159 // Pack tensor infos
160 ArgumentPack<ITensorInfo> tensors;
161 tensors.add_const_tensor(ACL_SRC_0, src);
162 tensors.add_const_tensor(ACL_DST_0, dst);
163
164 const Operator op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
165 sketch.implementation().operator_group().add_operator(op);
Gunes Bayircc287732023-01-19 15:56:00 +0000166
167 return dst;
Gunes Bayir1dc6ff12022-12-06 20:48:31 +0000168}
169
170} // namespace dynamic_fusion
171} // namespace experimental
172} // namespace arm_compute