1 /*
2 * Copyright (c) 2022-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 "ClComponentDirectConv2d.h"
25
26 #include "arm_compute/core/Validate.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h"
29 #include "src/core/CL/CLValidate.h"
30 #include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateDirectConv2d.h"
31
32 namespace arm_compute
33 {
34 namespace experimental
35 {
36 namespace dynamic_fusion
37 {
export_to_cl_image(bool cl_image)38 ClComponentDirectConv2dSettings &ClComponentDirectConv2dSettings::export_to_cl_image(bool cl_image)
39 {
40 _export_to_cl_image = cl_image;
41 return *this;
42 }
43
export_to_cl_image() const44 bool ClComponentDirectConv2dSettings::export_to_cl_image() const
45 {
46 return _export_to_cl_image;
47 }
48
fast_relaxed_math(bool fast_relaxed_math)49 ClComponentDirectConv2dSettings &ClComponentDirectConv2dSettings::fast_relaxed_math(bool fast_relaxed_math)
50 {
51 _fast_relaxed_math = fast_relaxed_math;
52 return *this;
53 }
54
fast_relaxed_math() const55 bool ClComponentDirectConv2dSettings::fast_relaxed_math() const
56 {
57 return _fast_relaxed_math;
58 }
59
direct_conv_descriptor(const DirectConvComputeKernelInfo & desc)60 ClComponentDirectConv2dSettings &ClComponentDirectConv2dSettings::direct_conv_descriptor(const DirectConvComputeKernelInfo &desc)
61 {
62 _desc = desc;
63 return *this;
64 }
65
direct_conv_descriptor() const66 DirectConvComputeKernelInfo ClComponentDirectConv2dSettings::direct_conv_descriptor() const
67 {
68 return _desc;
69 }
70
validate(const Properties & properties,const ArgumentPack<ITensorInfo> & tensors,const Attributes & attributes,const Settings & settings)71 Status ClComponentDirectConv2d::validate(
72 const Properties &properties,
73 const ArgumentPack<ITensorInfo> &tensors,
74 const Attributes &attributes,
75 const Settings &settings)
76 {
77 ARM_COMPUTE_UNUSED(properties);
78 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
79 const auto wei = tensors.get_const_tensor(TensorType::ACL_SRC_1);
80 const auto bia = tensors.get_const_tensor(TensorType::ACL_SRC_2);
81 const auto dst = tensors.get_const_tensor(TensorType::ACL_DST_0);
82
83 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, wei, dst);
84
85 // 1. Check validity
86 // Matching data type
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, wei);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
89 if(bia != nullptr)
90 {
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, bia);
92 }
93
94 // Matching data layout
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, wei);
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
97 if(bia != nullptr)
98 {
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, bia);
100 }
101
102 // All tensor infos are initialized
103 ARM_COMPUTE_RETURN_ERROR_ON(src->tensor_shape().total_size() == 0);
104 ARM_COMPUTE_RETURN_ERROR_ON(wei->tensor_shape().total_size() == 0);
105 ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0);
106 if(bia != nullptr)
107 {
108 ARM_COMPUTE_RETURN_ERROR_ON(bia->tensor_shape().total_size() == 0);
109 }
110 // Device requirements are met
111 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
112 // wei shape is correct
113 const DataLayout data_layout = src->data_layout();
114 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
115 ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->dimension(channel_idx) != src->dimension(channel_idx), "Weights feature map dimension should match the respective src's one");
116 ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->num_dimensions() > 4, "Weights can be at most 4 dimensional");
117
118 // dst shape is correct
119 PadStrideInfo legacy_pad_stride(attributes.stride().x(), attributes.stride().y(), attributes.pad().left, attributes.pad().right, attributes.pad().top,
120 attributes.pad().bottom, DimensionRoundingType{});
121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
122 misc::shape_calculator::compute_deep_convolution_shape(*src, *wei, legacy_pad_stride));
123
124 // bia shape is correct
125 if(bia != nullptr)
126 {
127 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->dimension(0) != wei->dimension(3),
128 "Biases size and number of dst feature maps should match");
129 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->num_dimensions() > 1,
130 "Biases should be one dimensional");
131 }
132
133 // 2. Check support level
134 // Data type
135 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
136 // Data layout
137 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
138
139 const auto desc = settings.direct_conv_descriptor();
140 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 && desc.n0 != 16,
141 "N0 can only be: 1, 2, 3, 4, 8, and 16");
142 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16,
143 "K0 can only be: 1, 2, 3, 4, 8, and 16");
144 return Status{};
145 }
146
ClComponentDirectConv2d(ComponentId id,const Properties & properties,const ArgumentPack<ITensorInfo> & tensors,const Attributes & attributes,const Settings & settings)147 ClComponentDirectConv2d::ClComponentDirectConv2d(
148 ComponentId id,
149 const Properties &properties,
150 const ArgumentPack<ITensorInfo> &tensors,
151 const Attributes &attributes,
152 const Settings &settings)
153 : IGpuKernelComponent{ id, properties, tensors },
154 _component_writer{ std::make_unique<ClTemplateDirectConv2d>(id, tensors, attributes, settings) }
155 {
156 }
~ClComponentDirectConv2d()157 ClComponentDirectConv2d::~ClComponentDirectConv2d()
158 {
159 }
template_writer() const160 const IGpuTemplateComponentWriter *ClComponentDirectConv2d::template_writer() const
161 {
162 return _component_writer.get();
163 }
164 } // namespace dynamic_fusion
165 } // namespace experimental
166 } // namespace arm_compute
167