1 /*
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 #include "ClComponentPool2d.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
29 #include "arm_compute/dynamic_fusion/sketch/attributes/Pool2dAttributes.h"
30 #include "src/core/CL/CLValidate.h"
31 #include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplatePool2d.h"
32 #include "src/dynamic_fusion/utils/Utils.h"
33 #include <memory>
34
35 namespace arm_compute
36 {
37 namespace experimental
38 {
39 namespace dynamic_fusion
40 {
validate(const Properties & properties,const ArgumentPack<ITensorInfo> & tensors,const Attributes & attributes,const Settings & settings)41 Status ClComponentPool2d::validate(
42 const Properties &properties,
43 const ArgumentPack<ITensorInfo> &tensors,
44 const Attributes &attributes,
45 const Settings &settings)
46 {
47 ARM_COMPUTE_UNUSED(properties);
48 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
49 const auto dst = tensors.get_const_tensor(TensorType::ACL_DST_0);
50
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
52 ARM_COMPUTE_ERROR_ON_MSG((attributes.pool_type() != PoolingType::AVG && attributes.pool_type() != PoolingType::MAX), "Unsupported Pooling type");
53
54 // 1. Check validity
55 // Check if pooling is valid
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_region_entirely_outside_input(convert_pool_attr_to_pool_info(attributes, settings.mixed_precision())),
57 "Pooling region that is entirely outside input tensor is unsupported");
58
59 // Matching data type
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
61
62 // Matching data layout
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
64
65 // All tensor infos are initialized
66 ARM_COMPUTE_RETURN_ERROR_ON(src->tensor_shape().total_size() == 0);
67 ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0);
68
69 // Device requirements are met
70 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
71
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
73 misc::shape_calculator::compute_pool_shape(*src, convert_pool_attr_to_pool_info(attributes, settings.mixed_precision())));
74
75 // 2. Check support level
76 // Data type
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
78
79 // Data layout
80 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
81
82 return Status{};
83 }
84
ClComponentPool2d(ComponentId id,const Properties & properties,const ArgumentPack<ITensorInfo> & tensors,const Attributes & attributes,const Settings & settings)85 ClComponentPool2d::ClComponentPool2d(
86 ComponentId id,
87 const Properties &properties,
88 const ArgumentPack<ITensorInfo> &tensors,
89 const Attributes &attributes,
90 const Settings &settings)
91 : IGpuKernelComponent{ id, properties, tensors },
92 _component_writer{ std::make_unique<ClTemplatePool2d>(id, tensors, attributes, settings) }
93 {
94 }
~ClComponentPool2d()95 ClComponentPool2d::~ClComponentPool2d()
96 {
97 }
template_writer() const98 const IGpuTemplateComponentWriter *ClComponentPool2d::template_writer() const
99 {
100 return _component_writer.get();
101 }
102 } // namespace dynamic_fusion
103 } // namespace experimental
104 } // namespace arm_compute
105