blob: 55c604aacc97abe06286fd843218838f567528b8 [file] [log] [blame]
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +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
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"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000028#include "arm_compute/core/experimental/Types.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/Validate.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000031#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000032#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000034#include "src/common/utils/Log.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/dynamic_fusion/sketch/ArgumentPack.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentPool2d.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000038#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
39#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSourceCode.h"
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000040#include "src/dynamic_fusion/utils/Utils.h"
41
42namespace arm_compute
43{
44namespace experimental
45{
46namespace dynamic_fusion
47{
48namespace
49{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050void calculate_and_init_dst_if_empty(ITensorInfo *dst,
51 const ITensorInfo *src,
52 const Pool2dAttributes &attributes,
53 const GpuPool2dSettings &settings)
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010054{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 if (dst->total_size() == 0U)
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010056 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 auto shape = misc::shape_calculator::compute_pool_shape(
58 *src, convert_pool_attr_to_pool_info(attributes, settings.mixed_precision()));
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010059 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape));
60 }
61}
62
63constexpr GpuOperatorType operator_type = GpuOperatorType::Complex;
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000064} // namespace
65
66GpuPool2dSettings &GpuPool2dSettings::mixed_precision(bool mixed_precision)
67{
68 _mixed_precision = mixed_precision;
69 return *this;
70}
71
72bool GpuPool2dSettings::mixed_precision() const
73{
74 return _mixed_precision;
75}
76
Adnan AlSinan227db8d2023-02-14 14:24:09 +000077GpuPool2dSettings GpuPool2dSettings::use_inf_as_limit(bool use_inf_as_limit)
78{
79 _use_inf_as_limit = use_inf_as_limit;
80 return *this;
81}
82
83bool GpuPool2dSettings::use_inf_as_limit() const
84{
85 return _use_inf_as_limit;
86}
87
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000088Status GpuPool2d::validate_op(const GpuWorkloadSketch &sketch,
89 const ITensorInfo *src,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 const Pool2dAttributes &attributes,
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000091 const GpuPool2dSettings &settings)
92{
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010093 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
94 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id());
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000095
96 // Auto initialize dst tensor info
Adnan AlSinan2e6d6592023-08-21 13:54:27 +010097 TensorInfo dst_info_to_validate;
98
99 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000100
101 // Perform fusion test
102 // Pack tensor infos
103 ArgumentPack<ITensorInfo> tensors;
104 tensors.add_const_tensor(ACL_SRC_0, src);
105 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
106
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
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100112 return is_supported_op(*sketch.gpu_context(), src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000113}
114
115Status GpuPool2d::is_supported_op(const GpuWorkloadContext &context,
116 const ITensorInfo *src,
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000117 const Pool2dAttributes &attributes,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 const GpuPool2dSettings &settings)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000119{
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100120 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000121 // Data type
122 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
123 // Data layout
124 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
125 // Check exclude padding is not false
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!attributes.exclude_padding(),
127 "Exclude padding must be set to true in Attributes!");
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000128
129 // Auto initialize dst tensor info
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100130 TensorInfo dst_info_to_validate;
131
132 calculate_and_init_dst_if_empty(&dst_info_to_validate, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000133
134 // Check components
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (context.gpu_language() == GpuLanguage::OpenCL)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000136 {
137 const auto cl_compile_ctx = context.cl_compile_context();
138 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
139
140 // Validate Component
141 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 const KernelProperties properties =
143 IGpuKernelComponent::Properties().stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000144
145 ArgumentPack<ITensorInfo> arguments;
146 arguments.add_const_tensor(ACL_SRC_0, src);
147 arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
148 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentPool2d::validate(properties, arguments, attributes, settings));
149 }
150 }
151 else
152 {
153 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
154 }
155 return Status{};
156}
157
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158ITensorInfo *GpuPool2d::create_op(GpuWorkloadSketch &sketch,
159 ITensorInfo *src,
160 const Pool2dAttributes &attributes,
161 const GpuPool2dSettings &settings)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000162{
163 // Assert validation
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100164 ARM_COMPUTE_ERROR_THROW_ON(GpuPool2d::validate_op(sketch, src, attributes, settings));
165 ARM_COMPUTE_LOG_PARAMS(src, attributes, settings);
166
167 ITensorInfo *dst = sketch.implementation().create_virtual_tensor();
168 ARM_COMPUTE_ERROR_ON_NULLPTR(dst);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000169
170 // Auto initialize dst tensor
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100171 calculate_and_init_dst_if_empty(dst, src, attributes, settings);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000172
173 // Translate into components and add to component graph
174 auto &comp_graph = sketch.implementation().component_graph();
175
176 const auto sketch_ctx = sketch.implementation().context();
177
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 if (sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000179 {
180 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
Omar Al Khatib3c7c1fa2023-03-07 09:57:49 +0000181 ARM_COMPUTE_UNUSED(cl_compile_ctx);
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000182 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
183
184 // Add Component
185 {
186 auto properties = IGpuKernelComponent::Properties();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 properties.stage(UnitWorkloadStage{UnitWorkloadStage::Stage::Run});
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000188
189 ArgumentPack<ITensorInfo> arguments;
190 arguments.add_const_tensor(ACL_SRC_0, src);
191 arguments.add_const_tensor(ACL_DST_0, dst);
192 comp_graph.add_new_component<ClComponentPool2d>(properties, arguments, attributes, settings);
193 }
194 }
195 else
196 {
197 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
198 }
199
200 // Set up fusion test by adding to the Operator Group
201 // Note this has to be performed after all the components have been successfully added to the component graph
202
203 // Pack tensor infos
204 ArgumentPack<ITensorInfo> tensors;
205 tensors.add_const_tensor(ACL_SRC_0, src);
206 tensors.add_tensor(ACL_DST_0, dst);
207
208 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
209 sketch.implementation().operator_group().add_operator(op);
Adnan AlSinan2e6d6592023-08-21 13:54:27 +0100210
211 return dst;
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +0000212}
213
214} // namespace dynamic_fusion
215} // namespace experimental
216} // namespace arm_compute