Elffuss BraidHub

kikocis/kleidiai public

main · 9.6 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#include <vector>
8#include <string>
9
10#include <inttypes.h>
11
12#include "utils.hpp"
13
14#include "gemm_lib.hpp"
15
16enum class ConvImpl {
17 DEFAULT,
18 IM2ROW,
19 IM2COL,
20 WINOGRAD,
21 CONV2D
22};
23
24struct impl_names {
25 std::string name;
26 ConvImpl impl;
27};
28
29impl_names theimpls[] = { { "im2row", ConvImpl::IM2ROW },
30 { "im2col", ConvImpl::IM2COL },
31 { "winograd", ConvImpl::WINOGRAD },
32 { "conv2d", ConvImpl::CONV2D } };
33
34const char *impl2str(ConvImpl impl) {
35 for (auto &&i : theimpls) {
36 if (i.impl == impl) {
37 return i.name.c_str();
38 }
39 }
40
41 return "UNKNOWN";
42}
43
44ConvImpl str2impl(std::string name) {
45 for (auto &&i : theimpls) {
46 if (i.name == name) {
47 return i.impl;
48 }
49 }
50
51 // 'DEFAULT' means error in this context.
52 return ConvImpl::DEFAULT;
53}
54
55class test_suite {
56public:
57 std::string shortname;
58
59 test_suite(std::string sn) : shortname(sn) { }
60
61 virtual bool get_config(GemmProblem *p, unsigned int ID, std::string implname, bool winograd_ok, bool conv_ok) = 0;
62 virtual void print_info(bool) = 0;
63
64 virtual ~test_suite() { }
65};
66
67#define RANDOM_FLAG_WINOGRAD 0x1
68#define RANDOM_FLAG_DEPTHWISE 0x2
69
70class random_suite : public test_suite {
71public:
72 random_suite(const random_suite &) = delete;
73 random_suite operator=(const random_suite &) = delete;
74
75 uint32_t flags;
76
77 random_suite(std::string name, uint32_t flags) : test_suite(name), flags(flags) { }
78
79 bool get_config(GemmProblem *p, unsigned int ID, std::string, bool, bool) override {
80 std::mt19937 eng(ID);
81
82 // Transcribed from the python used to generate the original random suite
83 // Start off by generating everything at random.
84 p->multis = 1;
85 p->batches = (eng() % 10) + 1;
86
87 p->kernel_height = (eng() % 5) * 2 + 1; // [1, 3, 5, 7, 9]
88 p->kernel_width = (eng() % 5) * 2 + 1;
89
90 p->out_stride_h = eng() % 3 + 1;
91 p->out_stride_w = eng() % 3 + 1;
92
93 p->in_stride_h = eng() % 3 + 1;
94 p->in_stride_w = eng() % 3 + 1;
95
96 p->input_channels = ((eng() % 256) + 1);
97 p->output_channels = ((eng() % 256) + 1);
98
99 const bool same_padding=eng()%2;
100
101 p->output_height = eng() % 50 + 1;
102 p->output_width = eng() % 50 + 1;
103
104 // Constrain for depthwise if needed - this should be updated as the capabilities of our depthwise kernels
105 // increases.
106 if (flags & RANDOM_FLAG_DEPTHWISE) {
107 // Make an above-average number of cases hit the optimized kernels.
108 // Remove any dilation set as it gets readded in case 6 below.
109 p->in_stride_h = p->in_stride_w = 1;
110
111 switch(eng() % 7) {
112 // 3x3s2 - versions without (0), and with (1), multiplier
113 case 0:
114 p->output_channels = p->input_channels;
115 /* fall through */
116 case 1:
117 p->kernel_height = p->kernel_width = 3;
118 p->out_stride_h = p->out_stride_w = 2;
119 break;
120 // 3x3s1 - no multipler only
121 case 2:
122 p->kernel_height = p->kernel_width = 3;
123 p->out_stride_h = p->out_stride_w = 1;
124 p->output_channels = p->input_channels;
125 break;
126 // 5x5s1 - versions without (3), and with (4), multiplier
127 case 3:
128 p->output_channels = p->input_channels;
129 /* fall through */
130 case 4:
131 p->kernel_height = p->kernel_width = 5;
132 p->out_stride_h = p->out_stride_w = 1;
133 break;
134 // 5x5s2 - no multiplier only
135 case 5:
136 p->kernel_height = p->kernel_width = 5;
137 p->out_stride_h = p->out_stride_w = 2;
138 break;
139 // Random and with dilation
140 case 6:
141 p->in_stride_h = eng() % 5 + 1;
142 p->in_stride_w = eng() % 5 + 1;
143 break;
144
145 // Let the odd random case through for the generic kernels.
146 default:
147 break;
148 }
149
150 int64_t multiplier = iceildiv(p->output_channels, p->input_channels);
151
152 p->output_channels = p->input_channels * std::max<int64_t>(1, multiplier);
153 p->groups = p->input_channels;
154 }
155
156 // Constrain for Winograd
157 if (flags & RANDOM_FLAG_WINOGRAD) {
158 // Winograd never supports a stride or dilation.
159 p->out_stride_h = p->out_stride_w = 1;
160 p->in_stride_h = p->in_stride_w = 1;
161
162 // Kernel height/width has to match a supported Winograd configuration.
163 switch(eng() % 8) {
164 // 3x3
165 case 0:
166 p->kernel_height = p->kernel_width = 3;
167 break;
168 // 3x1
169 case 1:
170 p->kernel_height = 3;
171 p->kernel_width = 1;
172 break;
173 // 1x3
174 case 2:
175 p->kernel_height = 1;
176 p->kernel_width = 3;
177 break;
178 // 5x5
179 case 3:
180 p->kernel_height = p->kernel_width = 5;
181 break;
182 // 5x1
183 case 4:
184 p->kernel_height = 5;
185 p->kernel_width = 1;
186 break;
187 // 1x5
188 case 5:
189 p->kernel_height = 1;
190 p->kernel_width = 5;
191 break;
192 // 7x1
193 case 6:
194 p->kernel_height = 7;
195 p->kernel_width = 1;
196 break;
197 // 1x7
198 case 7:
199 p->kernel_height = 1;
200 p->kernel_width = 7;
201 break;
202 default:
203 UNREACHABLE("Impossible");
204 break;
205 }
206 }
207
208 // Compute input dimensions and padding.
209 p->input_height = ((p->output_height - 1) * p->out_stride_h) + 1;
210 p->input_width = ((p->output_width - 1) * p->out_stride_w) + 1;
211
212 auto effective_kernel_height = (p->kernel_height - 1)*p->in_stride_h + 1;
213 auto effective_kernel_width = (p->kernel_width - 1)*p->in_stride_w + 1;
214
215 if (same_padding) {
216 p->padding_top = (effective_kernel_height - 1) / 2;
217 p->padding_left = (effective_kernel_width - 1) / 2;
218 } else {
219 p->input_height += effective_kernel_height - 1;
220 p->input_width += effective_kernel_width - 1;
221 }
222
223#ifndef SILENT
224 printf ("===============================================================================\n");
225 printf ("Test configuration:\n");
226 printf (" Suite: %s\n", this->shortname.c_str());
227 printf (" Test ID: %d\n", ID);
228 printf (" Convolution: filter %" PRId64 "x%" PRId64 ", stride %" PRId64 "x%" PRId64 ", ",
229 p->kernel_height, p->kernel_width, p->out_stride_h, p->out_stride_w);
230 if (p->in_stride_h != 1 || p->in_stride_w != 1)
231 {
232 printf ("dilation %" PRId64 "x%" PRId64 ", ", p->in_stride_h, p->in_stride_w);
233 }
234 printf ("output %" PRId64 "x%" PRId64 " (%" PRId64 " -> %" PRId64 " channels)\n",
235 p->output_height, p->output_width, p->input_channels, p->output_channels);
236 printf ("===============================================================================\n");
237#endif
238
239 return true;
240 }
241
242 void print_info(bool first) override {
243 // We actually support UINT_MAX configurations, but scripts might try and parse a number here so pick a more reasonable number.
244 printf("\t%s (1000000 configurations)%s\n", this->shortname.c_str(), first ? ", default" : "");
245 }
246};
247
248std::vector<std::shared_ptr<test_suite> > thesuites = {
249 std::make_shared<random_suite> ("random_conv2d", 0 ),
250 std::make_shared<random_suite> ("random_winograd", RANDOM_FLAG_WINOGRAD ),
251 std::make_shared<random_suite> ("random_depthwise", RANDOM_FLAG_DEPTHWISE ),
252 };
253
254void print_test_suites() {
255 printf ("\n");
256 printf ("List of avaliable test suites:\n");
257
258 bool first=true;
259 for (auto &&i : thesuites) {
260 i->print_info(first);
261 first=false;
262 }
263
264 printf ("\n");
265 printf ("List of available convolution implementations:\n");
266 for (auto &&i : theimpls) {
267 printf("\t%s\n", i.name.c_str());
268 }
269}
270
271bool get_test_config(GemmProblem *p, std::string suitename, unsigned int ID, std::string implname, bool wg_ok, bool conv_ok) {
272 test_suite *thesuite = thesuites[0].get();
273
274 if (suitename != "") {
275 thesuite = nullptr;
276
277 for (auto &&i : thesuites) {
278 if (suitename == i->shortname) {
279 thesuite = i.get();
280 break;
281 }
282 }
283
284 if (thesuite == nullptr) {
285 printf("Unknown test suite: %s\n", suitename.c_str());
286 return false;
287 }
288 }
289
290 return thesuite->get_config(p, ID, implname, wg_ok, conv_ok);
291}