blob: 2d04f756100ef91d3249699d11f92a4c057e8d27 [file] [log] [blame]
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +00001/*
Gunes Bayir2b9fa592024-01-17 16:07:03 +00002 * Copyright (c) 2023-2024 Arm Limited.
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuPool2d.h"
26
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000027#include "arm_compute/core/CL/CLCompileContext.h"
Gunes Bayir2b9fa592024-01-17 16:07:03 +000028#include "arm_compute/core/Error.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000029#include "arm_compute/core/experimental/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/core/Validate.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000032#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000033#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000035#include "src/common/utils/Log.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/dynamic_fusion/sketch/ArgumentPack.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentPool2d.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000039#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
40#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSourceCode.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000041#include "src/dynamic_fusion/utils/Utils.h"
42
43namespace arm_compute
44{
45namespace experimental
46{
47namespace dynamic_fusion
48{
49namespace
50{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051void calculate_and_init_dst_if_empty(ITensorInfo *dst,
52 const ITensorInfo *src,
53 const Pool2dAttributes &attributes,
54 const GpuPool2dSettings &settings)
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010055{
Gunes Bayir2b9fa592024-01-17 16:07:03 +000056 ARM_COMPUTE_UNUSED(settings);
57
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 if (dst->total_size() == 0U)
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010059 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 auto shape = misc::shape_calculator::compute_pool_shape(
Gunes Bayir2b9fa592024-01-17 16:07:03 +000061 *src, convert_pool_attr_to_pool_info(attributes, /* mixed_precision */ true));
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010062 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape));
63 }
64}
65
66constexpr GpuOperatorType operator_type = GpuOperatorType::Complex;
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000067} // namespace
68
Adnan AlSinan227db8d2023-02-14 14:24:09 +000069GpuPool2dSettings GpuPool2dSettings::use_inf_as_limit(bool use_inf_as_limit)
70{
71 _use_inf_as_limit = use_inf_as_limit;
72 return *this;
73}
74
75bool GpuPool2dSettings::use_inf_as_limit() const
76{
77 return _use_inf_as_limit;
78}
79
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000080Status GpuPool2d::validate_op(const GpuWorkloadSketch &sketch,
81 const ITensorInfo *src,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 const Pool2dAttributes &attributes,
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000083 const GpuPool2dSettings &settings)
84{
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010085 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
86 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000087
88 // Auto initialize dst tensor info
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010089 TensorInfo dst_info_to_validate;
90
91 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000092
93 // Perform fusion test
94 // Pack tensor infos
95 ArgumentPack<ITensorInfo> tensors;
96 tensors.add_const_tensor(ACL_SRC_0, src);
97 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
98
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
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100104 return is_supported_op(*sketch.gpu_context(), src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000105}
106
107Status GpuPool2d::is_supported_op(const GpuWorkloadContext &context,
108 const ITensorInfo *src,
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000109 const Pool2dAttributes &attributes,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 const GpuPool2dSettings &settings)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000111{
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100112 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000113 // Data type
114 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
115 // Data layout
116 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
117 // Check exclude padding is not false
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!attributes.exclude_padding(),
119 "Exclude padding must be set to true in Attributes!");
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000120
121 // Auto initialize dst tensor info
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100122 TensorInfo dst_info_to_validate;
123
124 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000125
126 // Check components
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127 if (context.gpu_language() == GpuLanguage::OpenCL)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000128 {
129 const auto cl_compile_ctx = context.cl_compile_context();
130 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
131
132 // Validate Component
133 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 const KernelProperties properties =
135 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000136
137 ArgumentPack<ITensorInfo> arguments;
138 arguments.add_const_tensor(ACL_SRC_0, src);
139 arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
140 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentPool2d::validate(properties, arguments, attributes, settings));
141 }
142 }
143 else
144 {
145 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
146 }
147 return Status{};
148}
149
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150ITensorInfo *GpuPool2d::create_op(GpuWorkloadSketch &sketch,
151 ITensorInfo *src,
152 const Pool2dAttributes &attributes,
153 const GpuPool2dSettings &settings)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000154{
155 // Assert validation
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100156 ARM_COMPUTE_ERROR_THROW_ON(GpuPool2d::validate_op(sketch, src, attributes, settings));
157 ARM_COMPUTE_LOG_PARAMS(src, attributes, settings);
158
159 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
160 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000161
162 // Auto initialize dst tensor
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100163 calculate_and_init_dst_if_empty(dst, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000164
165 // Translate into components and add to component graph
166 auto &comp_graph = sketch.implementation().component_graph();
167
168 const auto sketch_ctx = sketch.implementation().context();
169
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000171 {
172 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000173 ARM_COMPUTE_UNUSED(cl_compile_ctx);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000174 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
175
176 // Add Component
177 {
178 auto properties = IGpuKernelComponent::Properties();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000180
181 ArgumentPack<ITensorInfo> arguments;
182 arguments.add_const_tensor(ACL_SRC_0, src);
183 arguments.add_const_tensor(ACL_DST_0, dst);
184 comp_graph.add_new_component<ClComponentPool2d>(properties, arguments, attributes, settings);
185 }
186 }
187 else
188 {
189 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
190 }
191
192 // Set up fusion test by adding to the Operator Group
193 // Note this has to be performed after all the components have been successfully added to the component graph
194
195 // Pack tensor infos
196 ArgumentPack<ITensorInfo> tensors;
197 tensors.add_const_tensor(ACL_SRC_0, src);
198 tensors.add_tensor(ACL_DST_0, dst);
199
200 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
201 sketch.implementation().operator_group().add_operator(op);
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100202
203 return dst;
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000204}
205
206} // namespace dynamic_fusion
207} // namespace experimental
208} // namespace arm_compute