blob: 98f3d6a882c125fbca5f5078702ed4c42504e5d9 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
SiCong Li5a63d1e2023-01-06 16:28:57 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
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
SiCong Lif44bbc52022-08-29 18:25:51 +010026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027#include "arm_compute/core/Validate.h"
SiCong Li5a63d1e2023-01-06 16:28:57 +000028#include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h"
Jakub Sujake1c96e72023-07-31 13:36:58 +010029
SiCong Lif44bbc52022-08-29 18:25:51 +010030#include "src/core/CL/CLValidate.h"
Jakub Sujake1c96e72023-07-31 13:36:58 +010031
32#ifndef ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +010033#include "src/dynamic_fusion/sketch/gpu/template_writer/cl/ClTemplateDirectConv2d.h"
Jakub Sujake1c96e72023-07-31 13:36:58 +010034#else // ACL_INTERNAL_TEST_CKW_IN_DF
35#include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/GpuCkwDirectConv2d.h"
36#endif // ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +010037
38namespace arm_compute
39{
40namespace experimental
41{
42namespace dynamic_fusion
43{
Gunes Bayir7dc02342022-11-21 21:46:50 +000044bool ClComponentDirectConv2dSettings::export_to_cl_image() const
SiCong Lif44bbc52022-08-29 18:25:51 +010045{
Viet-Hoa Doe2e6d742023-03-01 15:46:10 +000046 return _desc.export_weights_to_cl_image;
SiCong Lif44bbc52022-08-29 18:25:51 +010047}
48
Gunes Bayir7dc02342022-11-21 21:46:50 +000049ClComponentDirectConv2dSettings &ClComponentDirectConv2dSettings::fast_relaxed_math(bool fast_relaxed_math)
SiCong Lif44bbc52022-08-29 18:25:51 +010050{
51 _fast_relaxed_math = fast_relaxed_math;
52 return *this;
53}
54
Gunes Bayir7dc02342022-11-21 21:46:50 +000055bool ClComponentDirectConv2dSettings::fast_relaxed_math() const
SiCong Lif44bbc52022-08-29 18:25:51 +010056{
57 return _fast_relaxed_math;
58}
59
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060ClComponentDirectConv2dSettings &
61ClComponentDirectConv2dSettings::direct_conv_descriptor(const DirectConvComputeKernelInfo &desc)
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000062{
63 _desc = desc;
64 return *this;
65}
66
67DirectConvComputeKernelInfo ClComponentDirectConv2dSettings::direct_conv_descriptor() const
68{
69 return _desc;
70}
71
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072Status ClComponentDirectConv2d::validate(const Properties &properties,
73 const ArgumentPack<ITensorInfo> &tensors,
74 const Attributes &attributes,
75 const Settings &settings)
SiCong Lif44bbc52022-08-29 18:25:51 +010076{
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000077 ARM_COMPUTE_UNUSED(properties);
SiCong Lif44bbc52022-08-29 18:25:51 +010078 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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 if (bia != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +010090 {
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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 if (bia != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +010098 {
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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 if (bia != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100107 {
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);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->dimension(channel_idx) != src->dimension(channel_idx),
116 "Weights feature map dimension should match the respective src's one");
SiCong Lif44bbc52022-08-29 18:25:51 +0100117 ARM_COMPUTE_RETURN_ERROR_ON_MSG(wei->num_dimensions() > 4, "Weights can be at most 4 dimensional");
118
119 // dst shape is correct
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 PadStrideInfo legacy_pad_stride(attributes.stride().x(), attributes.stride().y(), attributes.pad().left,
121 attributes.pad().right, attributes.pad().top, attributes.pad().bottom,
122 DimensionRoundingType{});
123 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(
124 dst->tensor_shape(), misc::shape_calculator::compute_deep_convolution_shape(*src, *wei, legacy_pad_stride));
SiCong Lif44bbc52022-08-29 18:25:51 +0100125
126 // bia shape is correct
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100127 if (bia != nullptr)
SiCong Lif44bbc52022-08-29 18:25:51 +0100128 {
129 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->dimension(0) != wei->dimension(3),
130 "Biases size and number of dst feature maps should match");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 ARM_COMPUTE_RETURN_ERROR_ON_MSG(bia->num_dimensions() > 1, "Biases should be one dimensional");
SiCong Lif44bbc52022-08-29 18:25:51 +0100132 }
133
134 // 2. Check support level
135 // Data type
136 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
137 // Data layout
138 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
139
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000140 const auto desc = settings.direct_conv_descriptor();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 &&
142 desc.n0 != 16,
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000143 "N0 can only be: 1, 2, 3, 4, 8, and 16");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 &&
145 desc.k0 != 16,
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000146 "K0 can only be: 1, 2, 3, 4, 8, and 16");
SiCong Lif44bbc52022-08-29 18:25:51 +0100147 return Status{};
148}
149
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100150ClComponentDirectConv2d::ClComponentDirectConv2d(ComponentId id,
151 const Properties &properties,
152 const ArgumentPack<ITensorInfo> &tensors,
153 const Attributes &attributes,
154 const Settings &settings)
155 : IGpuKernelComponent{id, properties, tensors},
Jakub Sujake1c96e72023-07-31 13:36:58 +0100156#ifndef ACL_INTERNAL_TEST_CKW_IN_DF
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 _component_writer{std::make_unique<ClTemplateDirectConv2d>(id, tensors, attributes, settings)}
158#else // ACL_INTERNAL_TEST_CKW_IN_DF
159 _component_writer{std::make_unique<GpuCkwDirectConv2d>(id, tensors, attributes, settings)}
Jakub Sujake1c96e72023-07-31 13:36:58 +0100160#endif // ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +0100161{
162}
Jakub Sujake1c96e72023-07-31 13:36:58 +0100163
SiCong Lif44bbc52022-08-29 18:25:51 +0100164ClComponentDirectConv2d::~ClComponentDirectConv2d()
165{
166}
Jakub Sujake1c96e72023-07-31 13:36:58 +0100167
168#ifndef ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +0100169const IGpuTemplateComponentWriter *ClComponentDirectConv2d::template_writer() const
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170#else // ACL_INTERNAL_TEST_CKW_IN_DF
Jakub Sujake1c96e72023-07-31 13:36:58 +0100171const IGpuCkwComponentDriver *ClComponentDirectConv2d::ckw_component_driver() const
172#endif // ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +0100173{
174 return _component_writer.get();
175}
Jakub Sujake1c96e72023-07-31 13:36:58 +0100176
SiCong Lif44bbc52022-08-29 18:25:51 +0100177} // namespace dynamic_fusion
178} // namespace experimental
179} // namespace arm_compute