blob: c602f451646539d8c340f7337fdab165fcca4989 [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
25#include "arm_compute/core/CL/CLCompileContext.h"
26#include "arm_compute/core/Validate.h"
27#include "arm_compute/core/experimental/Types.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h"
30
31#include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadSketch.h"
32#include "arm_compute/dynamic_fusion/sketch/gpu/operators/GpuPool2d.h"
33#include "src/common/utils/Log.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/dynamic_fusion/sketch/ArgumentPack.h"
36#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSketchImpl.h"
37#include "src/dynamic_fusion/sketch/gpu/GpuWorkloadSourceCode.h"
38#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentPool2d.h"
39#include "src/dynamic_fusion/utils/Utils.h"
40
41namespace arm_compute
42{
43namespace experimental
44{
45namespace dynamic_fusion
46{
47namespace
48{
49constexpr GpuOperatorType operator_type = GpuOperatorType::Unfusable;
50} // namespace
51
52GpuPool2dSettings &GpuPool2dSettings::mixed_precision(bool mixed_precision)
53{
54 _mixed_precision = mixed_precision;
55 return *this;
56}
57
58bool GpuPool2dSettings::mixed_precision() const
59{
60 return _mixed_precision;
61}
62
Adnan AlSinan227db8d2023-02-14 14:24:09 +000063GpuPool2dSettings GpuPool2dSettings::use_inf_as_limit(bool use_inf_as_limit)
64{
65 _use_inf_as_limit = use_inf_as_limit;
66 return *this;
67}
68
69bool GpuPool2dSettings::use_inf_as_limit() const
70{
71 return _use_inf_as_limit;
72}
73
Mohammed Suhail Munshia18d85c2023-01-03 10:16:16 +000074Status GpuPool2d::validate_op(const GpuWorkloadSketch &sketch,
75 const ITensorInfo *src,
76 const ITensorInfo *dst,
77 const Pool2dAttributes &attributes,
78 const GpuPool2dSettings &settings)
79{
80 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
81 ARM_COMPUTE_RETURN_ERROR_ON(!src->has_valid_id() || !dst->has_valid_id());
82
83 // Auto initialize dst tensor info
84 TensorInfo dst_info_to_validate = *dst;
85 {
86 auto shape = misc::shape_calculator::compute_pool_shape(*src, convert_pool_attr_to_pool_info(attributes, settings.mixed_precision()));
87 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(shape));
88 }
89
90 // Perform fusion test
91 // Pack tensor infos
92 ArgumentPack<ITensorInfo> tensors;
93 tensors.add_const_tensor(ACL_SRC_0, src);
94 tensors.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
95
96 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
97 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!sketch.implementation().operator_group().try_add_operator(op),
98 "Operator fusion test failed. This operator cannot be fused into the workload");
99
100 // Check if configuration is supported
101 return is_supported_op(*sketch.gpu_context(), src, &dst_info_to_validate, attributes, settings);
102}
103
104Status GpuPool2d::is_supported_op(const GpuWorkloadContext &context,
105 const ITensorInfo *src,
106 const ITensorInfo *dst,
107 const Pool2dAttributes &attributes,
108 const GpuPool2dSettings &settings)
109{
110 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
111 // Data type
112 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
113 // Data layout
114 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
115 // Check exclude padding is not false
116 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!attributes.exclude_padding(), "Exclude padding must be set to true in Attributes!");
117
118 // Auto initialize dst tensor info
119 TensorInfo dst_info_to_validate = *dst;
120 {
121 auto shape = misc::shape_calculator::compute_pool_shape(*src, convert_pool_attr_to_pool_info(attributes, settings.mixed_precision()));
122 auto_init_if_empty(dst_info_to_validate, src->clone()->set_tensor_shape(shape));
123 }
124
125 // Check components
126 if(context.gpu_language() == GpuLanguage::OpenCL)
127 {
128 const auto cl_compile_ctx = context.cl_compile_context();
129 ARM_COMPUTE_RETURN_ERROR_ON(cl_compile_ctx == nullptr);
130
131 // Validate Component
132 {
133 const KernelProperties properties = IGpuKernelComponent::Properties().stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
134
135 ArgumentPack<ITensorInfo> arguments;
136 arguments.add_const_tensor(ACL_SRC_0, src);
137 arguments.add_const_tensor(ACL_DST_0, &dst_info_to_validate);
138 ARM_COMPUTE_RETURN_ON_ERROR(ClComponentPool2d::validate(properties, arguments, attributes, settings));
139 }
140 }
141 else
142 {
143 ARM_COMPUTE_RETURN_ERROR_MSG("Unimplemented Gpu language");
144 }
145 return Status{};
146}
147
148void GpuPool2d::create_op(GpuWorkloadSketch &sketch,
149 ITensorInfo *src,
150 ITensorInfo *dst,
151 const Pool2dAttributes &attributes,
152 const GpuPool2dSettings &settings)
153{
154 // Assert validation
155 ARM_COMPUTE_ERROR_THROW_ON(GpuPool2d::validate_op(sketch, src, dst, attributes, settings));
156 ARM_COMPUTE_LOG_PARAMS(src, dst, attributes, settings);
157
158 // Auto initialize dst tensor
159 {
160 auto shape = misc::shape_calculator::compute_pool_shape(*src, convert_pool_attr_to_pool_info(attributes, settings.mixed_precision())); // use the default DimensionRoundingType
161 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(shape));
162 }
163
164 // Translate into components and add to component graph
165 auto &comp_graph = sketch.implementation().component_graph();
166
167 const auto sketch_ctx = sketch.implementation().context();
168
169 if(sketch_ctx->gpu_language() == GpuLanguage::OpenCL)
170 {
171 const auto cl_compile_ctx = sketch_ctx->cl_compile_context();
172 ARM_COMPUTE_ERROR_ON(cl_compile_ctx == nullptr);
173
174 // Add Component
175 {
176 auto properties = IGpuKernelComponent::Properties();
177 properties.stage(UnitWorkloadStage{ UnitWorkloadStage::Stage::Run });
178
179 ArgumentPack<ITensorInfo> arguments;
180 arguments.add_const_tensor(ACL_SRC_0, src);
181 arguments.add_const_tensor(ACL_DST_0, dst);
182 comp_graph.add_new_component<ClComponentPool2d>(properties, arguments, attributes, settings);
183 }
184 }
185 else
186 {
187 ARM_COMPUTE_ERROR("Unimplemented Gpu language");
188 }
189
190 // Set up fusion test by adding to the Operator Group
191 // Note this has to be performed after all the components have been successfully added to the component graph
192
193 // Pack tensor infos
194 ArgumentPack<ITensorInfo> tensors;
195 tensors.add_const_tensor(ACL_SRC_0, src);
196 tensors.add_tensor(ACL_DST_0, dst);
197
198 const auto op = sketch.implementation().operator_group().new_operator(operator_type, tensors);
199 sketch.implementation().operator_group().add_operator(op);
200}
201
202} // namespace dynamic_fusion
203} // namespace experimental
204} // namespace arm_compute