Elffuss BraidHub

kikocis/kleidiai public

main · 32.2 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#ifndef _GNU_SOURCE
8#define _GNU_SOURCE
9#define NO_LONG_OPT
10#endif
11
12#include <inttypes.h>
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/time.h>
18
19#ifndef NO_LONG_OPT
20#include <getopt.h>
21#else
22#include <unistd.h> // for standard getopt()
23#endif
24
25#include <algorithm>
26#include <atomic>
27#include <memory>
28#include <thread>
29#include <type_traits>
30#include <vector>
31
32#include "kai/ops/bfloat.hpp"
33
34#define restrict __restrict
35
36#ifdef THREADS
37#include <atomic>
38#include <thread>
39#ifdef BIND_THREADS
40#include <sched.h>
41#endif
42#endif
43
44#include "src/gemm_lib.hpp"
45
46// Test mode for the teams code - not normally needed.
47#undef TEST_TEAMS
48
49#if defined(BARE_METAL)
50// Make sure SVE is enabled on bare metal builds (needed on certain
51// compilers but harmless even if unnecessary).
52__attribute__((constructor)) void setup_cptr() {
53 {
54 uint64_t bit12 = 0x1100;
55 uint64_t temp;
56 __asm __volatile(
57 "mrs %[temp], CPTR_EL3\n"
58 "orr %[temp], %[temp], %[bit12]\n"
59 "msr CPTR_EL3, %[temp]\n"
60 : [temp] "=&r"(temp)
61 : [bit12] "r"(bit12)
62 : "memory");
63 }
64}
65#endif
66
67// Default cache sizes, set here so they can be overridden on the command line.
68int l1_cache_size = 32768;
69int l2_cache_size = 131072;
70
71int force_cpu = 0;
72
73// Functions to benchmark and test kernels. These are compiled externally.
74template <typename T, bool legacy, enum QuantizationType quantized, bool perchannel>
75void benchmark(GemmProblem* p, int iterations, int nthreads, int run_delay, mapfn cpuid_map, const char* kernel_name);
76
77template <typename T_ref, typename T_test, bool per_channel = false>
78void test_quantized(GemmProblem* p, int iterations, int nthreads, const char* kernel_name, FILE*);
79
80template <typename T_ref, typename T_test>
81void test_dequantized(GemmProblem* p, int iterations, int nthreads, const char* kernel_name, FILE*);
82
83template <typename T_ref, typename T_test, bool legacy>
84void test(GemmProblem* p, int iterations, int nthreads, const char* kernel_name, FILE*);
85
86template <typename T, bool legacy>
87void print_kernels(GemmProblem* p, unsigned int nthreads);
88
89/*
90 * Various type names are used in the macros below to construct the kernel list.
91 *
92 * All code actually depending on these types is compiled elsewhere, so we
93 * just need to forward declare them all.
94 */
95
96// Reference GEMM class
97template <typename To, typename Tr>
98class gemm_transposeB;
99
100// kai_ops wrapper classes
101template <typename Tlop, typename Trop, typename Tret, enum QuantizationType quantized = QuantizationType::NONE>
102class kai_ops_wrapper;
103template <typename Tlop, typename Trop, typename Tret>
104using kai_ops_quantized = kai_ops_wrapper<Tlop, Trop, Tret, QuantizationType::INTEGER>;
105
106template <typename Ta, typename Tb, typename Tref, enum QuantizationType quantized = QuantizationType::NONE>
107class depthwise_wrapper;
108template <typename Top, typename Tret>
109class winograd_wrapper;
110
111// Instantiations of kai_ops wrapper classes.
112// FP types.
113typedef kai_ops_wrapper<float, float, float> sgemm_new;
114typedef kai_ops_wrapper<__fp16, __fp16, __fp16> hgemm_new;
115typedef kai_ops_wrapper<bfloat16, bfloat16, float> gemm_bf16_new;
116typedef kai_ops_wrapper<bfloat16, bfloat16, bfloat16> gemm_bf16bf16;
117typedef kai_ops_wrapper<__fp16, __fp16, float> gemm_fp16fp32;
118typedef depthwise_wrapper<float, float, float> depthwise_fp32;
119typedef depthwise_wrapper<__fp16, __fp16, __fp16> depthwise_fp16;
120typedef winograd_wrapper<float, float> winograd_fp32;
121typedef winograd_wrapper<__fp16, __fp16> winograd_fp16;
122
123// int types.
124typedef kai_ops_wrapper<uint16_t, uint16_t, uint32_t> gemm_u16;
125typedef kai_ops_wrapper<int16_t, int16_t, int32_t> gemm_s16;
126typedef kai_ops_wrapper<uint8_t, uint8_t, uint32_t> gemm_u8_new;
127typedef kai_ops_wrapper<int8_t, int8_t, int32_t> gemm_s8_new;
128typedef kai_ops_wrapper<int8_t, int8_t, float, QuantizationType::FLOAT> gemm_s8fp32;
129typedef kai_ops_wrapper<int8_t, int8_t, __fp16, QuantizationType::FLOAT> gemm_s8fp16;
130typedef kai_ops_wrapper<uint8_t, int8_t, float, QuantizationType::FLOAT> gemm_u8s8fp32;
131typedef kai_ops_quantized<uint8_t, uint8_t, uint8_t> gemm_u8_quant;
132typedef kai_ops_quantized<int8_t, int8_t, int8_t> gemm_s8_quant;
133typedef kai_ops_quantized<uint8_t, int8_t, uint8_t> gemm_u8s8_quant;
134typedef depthwise_wrapper<int8_t, int8_t, int8_t, QuantizationType::INTEGER> depthwise_s8q;
135typedef depthwise_wrapper<uint8_t, uint8_t, uint8_t, QuantizationType::INTEGER> depthwise_u8q;
136
137/*
138 * Infrastructure to allow the kernel to be tested to be selected.
139 *
140 * Here are some function pointers to a benchmark function and a test
141 * function (which should be specializations of the templated functions
142 * defined above).
143 */
144typedef void (*benchfn)(GemmProblem*, int, int, int, mapfn, const char*);
145typedef void (*testfn)(GemmProblem*, int, int, const char*, FILE*);
146typedef void (*printfn)(GemmProblem*, unsigned int);
147
148/*
149 * This structure declares a list entry consisting of a name and the corresponding functions.
150 */
151struct kernentry {
152 const char* name;
153 benchfn bench;
154 testfn test;
155 printfn print;
156 int flags;
157};
158
159#define KERN_GEMV 0x1 /* GEMV kernel (force M=1) */
160#define KERN_LEGACY 0x2 /* Legacy kernel (M,N,K interface w/no transpose, alpha, beta) */
161#define KERN_ACTOK 0x4 /* Kernel which supports activations */
162#define KERN_WGOK 0x8 /* Kernel of a type that supports Winograd */
163#define KERN_QUANTIZED 0x10 /* Quantized kernel */
164#define KERN_CONV 0x20 /* Convolution supporting kernel */
165#define KERN_GEMV_N 0x40 /* GEMV kernel with M/N swapped (N=1) */
166#define KERN_CONV_ONLY 0x80 /* Convolution-only kernel (i.e. force -I conv2d) */
167#define KERN_DEPTHWISE 0x100 /* Depthwise kernel */
168#define KERN_ADD 0x200 /* Add kernel */
169#define KERN_NOACC 0x400 /* No accumulate support */
170#define KERN_FAST_ONLY 0x800 /* Force fast mode */
171
172/*
173 * This macro generates a structure declaration, based on the kernel name,
174 * operand type and result type.
175 *
176 * It uses the type parameters to specialize test() with an appropriately
177 * specialized reference implementation.
178 */
179#define KERN(name, optype, restype, flags) \
180 {#name, benchmark<name, !!(((flags) & KERN_LEGACY)), QuantizationType::NONE, false>, \
181 test<gemm_transposeB<optype, restype>, name, !!(((flags) & KERN_LEGACY))>, \
182 print_kernels<name, !!(((flags) & KERN_LEGACY))>, flags},
183
184#define KERN_DEQUANT(name, comp) \
185 {#name, benchmark<name, false, QuantizationType::FLOAT, false>, test_dequantized<comp, name>, \
186 print_kernels<name, false>, KERN_ACTOK | KERN_CONV},
187
188#define KERN_QUANT_COMP(name, comp, flags) \
189 {#name, benchmark<name, false, QuantizationType::INTEGER, false>, test_quantized<comp, name>, \
190 print_kernels<name, false>, KERN_ACTOK | KERN_QUANTIZED | flags},
191
192#define KERN_QUANT(name, flags) KERN_QUANT_COMP(name, sgemm_new, flags)
193
194#define KERN_QUANT_PERCHANNEL_COMP(name, comp, flags) \
195 {#name "_perchannel", benchmark<name, false, QuantizationType::INTEGER, true>, test_quantized<comp, name, true>, \
196 print_kernels<name, false>, KERN_ACTOK | KERN_QUANTIZED | flags},
197
198#define KERN_QUANT_PERCHANNEL(name, flags) KERN_QUANT_PERCHANNEL_COMP(name, sgemm_new, flags)
199
200// clang-format off
201/*
202 * The list of supported kernels. This consists of a call to the
203 KERN() macro for each kernel included above.
204 */
205kernentry thekerns[] = {
206/* START OF KERNEL LIST */
207
208KERN(sgemm_new, float, float, KERN_WGOK | KERN_ACTOK | KERN_CONV )
209KERN(gemm_bf16_new, bfloat16, float, KERN_WGOK | KERN_ACTOK | KERN_CONV)
210KERN(gemm_bf16bf16, bfloat16, bfloat16, KERN_WGOK | KERN_ACTOK | KERN_CONV)
211
212KERN(winograd_fp32, float, float, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_NOACC )
213
214#ifdef __aarch64__
215KERN(gemm_fp16fp32, __fp16, float, KERN_WGOK | KERN_ACTOK | KERN_CONV)
216
217KERN(hgemm_new, __fp16, __fp16, KERN_ACTOK | KERN_CONV )
218
219KERN(winograd_fp16, __fp16, __fp16, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_NOACC )
220
221KERN(depthwise_fp32, float, float, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE)
222KERN(depthwise_fp16, __fp16, __fp16, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE | KERN_FAST_ONLY )
223
224KERN(gemm_s16, int16_t, int32_t, KERN_CONV)
225KERN(gemm_u16, uint16_t, uint32_t, KERN_CONV)
226KERN(gemm_s8_new, int8_t, int32_t, KERN_CONV)
227KERN(gemm_u8_new, uint8_t, uint32_t, KERN_CONV)
228KERN_DEQUANT(gemm_s8fp32, sgemm_new)
229KERN_DEQUANT(gemm_s8fp16, hgemm_new)
230KERN_DEQUANT(gemm_u8s8fp32, sgemm_new)
231KERN_QUANT(gemm_u8_quant,KERN_CONV)
232KERN_QUANT(gemm_s8_quant,KERN_CONV)
233KERN_QUANT(gemm_u8s8_quant,KERN_CONV)
234KERN_QUANT_PERCHANNEL(gemm_s8_quant,KERN_CONV)
235
236KERN_QUANT_COMP(depthwise_s8q, depthwise_fp32, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE)
237KERN_QUANT_PERCHANNEL_COMP(depthwise_s8q, depthwise_fp32, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE)
238KERN_QUANT_COMP(depthwise_u8q, depthwise_fp32, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE)
239KERN_QUANT_PERCHANNEL_COMP(depthwise_u8q, depthwise_fp32, KERN_CONV | KERN_CONV_ONLY | KERN_ACTOK | KERN_DEPTHWISE)
240#endif
241/* END OF KERNEL LIST */
242{ "", nullptr, nullptr, nullptr, 0 } };
243
244/*
245 * Similar to above, infrastructure to allow selection of thread binding
246 * strategies.
247 */
248struct threadmapentry {
249 const char *name;
250 mapfn fn;
251};
252
253#define THREADMAP(name) \
254{ \
255 #name, \
256 threadmap_##name \
257},
258
259/* Here are the actual strategies. Included inline as they are so short. */
260
261/* Linear mapping, wrap at maxcpus */
262int threadmap_linear(int i, int maxcpus) {
263 return i % maxcpus;
264}
265
266/* Map to even CPUs, then odd CPUs, e.g. 0,2,4,6,1,3,5,7 */
267int threadmap_evenodd(int i, int maxcpus) {
268 i %= maxcpus; /* Restrict range to 0-(maxcpus-1) initially */
269
270 return ((i % (maxcpus/2)) * 2) + ((i >= maxcpus/2) ? 1 : 0);
271}
272
273/* Cycle between 4 and 5 */
274int threadmap_a57(int i, int maxcpus) {
275 return (i % 2) + 4;
276}
277
278/* Cycle between 1 and 2 */
279int threadmap_a72(int i, int maxcpus) {
280 return (i % 2) + 1;
281}
282
283/* Cycle 0, 3, 4, 5 */
284int threadmap_a53(int i, int maxcpus) {
285 i %= 4;
286 return i + (i ? 2 : 0);
287}
288
289/* Cycle between 4 and maxcpus */
290int threadmap_start_at_4(int i, int maxcpus) {
291 int numcpus = maxcpus - 4;
292 return (i % numcpus) + 4;
293}
294
295/* Cycle between 6 and maxcpus */
296int threadmap_start_at_6(int i, int maxcpus) {
297 int numcpus = maxcpus - 6;
298 return (i % numcpus) + 6;
299}
300
301/* Cycle through all but start at 4 */
302int threadmap_loop_from_4(int i, int maxcpus) {
303 return (i + 4) % maxcpus;
304}
305
306/* Reverse order */
307int threadmap_reverse(int i, int maxcpus) {
308 return maxcpus - (i % maxcpus) - 1;
309}
310
311threadmapentry themaps[] = {
312/* START OF THREAD STRATEGY LIST */
313THREADMAP(linear)
314THREADMAP(evenodd)
315THREADMAP(a57)
316THREADMAP(a72)
317THREADMAP(a53)
318THREADMAP(start_at_4)
319THREADMAP(start_at_6)
320THREADMAP(loop_from_4)
321THREADMAP(reverse)
322/* END OF THREAD STRATEGY LIST */
323{ "", NULL } };
324
325// This is used to allow specification of a strategy on the command line.
326strategy_name strategies_list[] = {
327 { "fail", ConvStrategy::fail },
328 { "im2row", ConvStrategy::im2row },
329 { "indirect", ConvStrategy::indirect },
330 { "convolution", ConvStrategy::convolution } };
331
332ConvStrategy str2strat(std::string name) {
333 for (auto &&i : strategies_list) {
334 if (i.name == name) {
335 return i.strat;
336 }
337 }
338
339 return ConvStrategy::fail;
340}
341
342void printhelp(const char *progname) {
343 printf("Usage: %s [-M Msize] [-N Nsize] [-K Ksize] [-A] [-a fn] [-b] [-c] [-t nthreads] [-f] [-m mapfn] [-i iterations] [-d delay] [-l L1_size] [-L L2_size] [-s a_stride] [-S b_stride] [-C cpu_code] [-p] [-n testID] [-q] [-F filter] [-u features] [-U features] [-o|-O dump_file] [-P] [-h] <kernel>\n"
344 " where:\n"
345 " -M, -N, -K: Set the problem size (M = height of output, N = width of output, K = depth of dot product).\n"
346 " Default size is M=N=K=768 in benchmark mode, or M=N=K=100 in check mode.\n"
347 " -a: Specify activation function (relu or relu6).\n"
348 " -b: Enable random bias.\n"
349 " -c: Turns on checking functionality (default: benchmark mode).\n"
350#ifdef THREADS
351 " -t: Sets the number of execution threads to use.\n"
352 " -T: Sets the number of thread teams (of size T) to create.\n"
353 " -G: Enables dynamic scheduling and specifies maximum number of granules (0=unlimited).\n"
354 " -w: For 2D scheduling, force a particular tile width.\n"
355#ifdef BIND_THREADS
356 " -m: Enables thread binding and specifies the mapping function to use.\n"
357#else
358 " -m: Configure thread binding (not enabled on this build).\n"
359#endif // BIND_THREADS
360#else
361 " -t, -m, -G: Configure threading (not enabled on this build).\n"
362#endif
363 " -f: Enable \"fast mode\" lower precision computation.\n"
364 " -i: Configure how many iterations to run in benchmark mode.\n"
365 " -d: Configures the delay (in seconds) between each iteration.\n"
366 " -l: Configure L1 cache size (bytes).\n"
367 " -L: Configure L2 cache size (bytes).\n"
368 " -s, -S: Set the stride of the A and B matrix respectively.\n"
369 " -C: Overrides the detected CPU type.\n"
370 " -y: Specifies number of batches (same B, multiple A, C).\n"
371 " -z: Specifies number of multis (multiple A, B, C).\n"
372 " -D: Select test suite name (see list below).\n"
373 " -n: Select test ID (overrides M,N,K,batch,multi settings).\n"
374 " -I: Select test implementation strategy (see list below, default is suite dependent).\n"
375 " -q: List valid kernel implementations for the given problem.\n"
376 " -F: Substring filter to select kernel to use.\n"
377 " -j: Override inner block size (kernel dependent).\n"
378 " -k: Override outer block size (kernel dependent).\n"
379 " -V: Specifies how to handle convolutions (im2row, indirect, convolution).\n"
380 " -r: Minimum number of ms to run for each iteration.\n"
381 " -W: Time weight transform as part of kernel run (for dynamic weights cases).\n"
382 " -A: Accumulate - add to what is already in output matrix.\n"
383 " -B: Supply RHS in transposed format (if supported).\n"
384 " -U: Force enable named CPU architecture features (comma separated list).\n"
385 " (fp16, dotprod, sve, sve2, bf16, i8mm, svebf16, svei8mm, svef32mm,\n"
386 " sme, sme_i8i32, sme_f16f32, sme_b16b32, sme_f32f32, sme2)\n"
387 " -u: Force disable named CPU architecture features (comma separated list).\n"
388 " -p: Specify convolution parameters (comma separated):\n"
389 " in_h,in_w,in_ch,kern_h,kern_w,out_ch,stride_h,stride_w,pad_top,pad_left,pad_bottom,pad_right,dilation_h,dilation_w\n"
390 " -o: Valid only in conjunction with -c; specifies dump file to create if comparison is valid.\n"
391 " -O: Specifies dump file to process; this overrides the problem specification from the dump file.\n"
392 " -Q: Gather cache statistics: Note that this option impacts performance.\n"
393 " -X: Specify additional option:\n"
394 " * after-relu: Activate the activations before running the layer.\n"
395 " -P: Request fixed format kernel.\n"
396#ifdef BARE_METAL
397 " -Z: Flush transformed weight data from cache after setup.\n"
398#endif
399 " -h: Prints this help and exits.\n"
400 " kernel: Specifies the kernel to run.\n", progname);
401
402 printf("\nList of available kernels:\n");
403 for (int i=0; thekerns[i].name[0]; i++) {
404 printf("\t%s\n", thekerns[i].name);
405 }
406
407#ifdef BIND_THREADS
408 printf("\nList of available thread binding maps:\n");
409 for (int i=0; themaps[i].name[0]; i++) {
410 printf("\t%s\n",themaps[i].name);
411 }
412#endif
413
414 print_test_suites();
415}
416
417extern const char *git_revision;
418extern const char *git_branch;
419
420int main(int argc, char **argv) {
421 /* Container for the various options */
422 GemmProblem p;
423
424 /* Options that don't go in GemmProblem */
425 bool docheck = false;
426 bool doquery = false;
427 bool manual_spec = false;
428 int nthreads = 1;
429 int iterations = 1;
430 int run_delay = 0;
431 mapfn mapfn = NULL;
432 int test_id = 0;
433 std::string test_suite = "";
434 std::string test_impl = "";
435 std::string read_dump_fname = "";
436
437 FILE *read_dump_file;
438
439#ifndef SILENT
440 printf("gemm-linux: %s (%s)\n", git_branch, git_revision);
441#endif
442
443#ifndef NO_LONG_OPT
444 struct option longopts[] = {
445 { "winograd-output-rows", 1, nullptr, 128 },
446 { "winograd-output-cols", 1, nullptr, 129 },
447 { "winograd-input-transform", 1, nullptr, 130 },
448 { "winograd-weight-transform", 1, nullptr, 131 },
449 { "winograd-output-transform", 1, nullptr, 132 },
450 { "weight-format", 1, nullptr, 133 },
451 { nullptr, 0, nullptr, 0 }, // Last element sentinel
452 };
453#endif
454
455 int opt;
456#ifndef NO_LONG_OPT
457 while ((opt = getopt_long(argc, argv, "M:N:K:a:bcd:t:T:m:i:s:S:l:L:C:y:z:n:D:I:fhF:j:k:G:w:V:r:WABu:U:p:X:PqO:o:QZ", longopts, nullptr)) >= 0) {
458#else
459 while ((opt = getopt(argc, argv, "M:N:K:a:bcd:t:T:m:i:s:S:l:L:C:y:z:n:D:I:fhF:j:k:G:w:V:r:WABu:U:p:X:PqO:o:QZ")) >= 0) {
460#endif
461 switch(opt) {
462 case 'M':
463 p.input_width=p.output_width=strtol(optarg, NULL, 0);
464 break;
465
466 case 'N':
467 p.output_channels=strtol(optarg, NULL, 0);
468 break;
469
470 case 'K':
471 p.input_channels=strtol(optarg, NULL, 0);
472 break;
473
474 case 'a':
475 if (!strcmp(optarg, "relu")) {
476 p.act=Activation(Activation::Type::ReLU);
477 } else if (!strcmp(optarg, "relu6")) {
478 p.act=Activation(Activation::Type::BoundedReLU, 6.0f);
479 } else if (!strcmp(optarg, "relu7")) {
480 p.act=Activation(Activation::Type::BoundedReLU, 7.0f);
481 } else {
482 printf("Unknown activation function: %s\n",optarg);
483 return 0;
484 }
485 break;
486
487 case 'b':
488 p.use_bias=true;
489 break;
490
491 case 'c':
492 docheck=true;
493 break;
494
495 case 'd':
496 run_delay=strtol(optarg, NULL, 0);
497 break;
498
499#ifdef THREADS
500 case 't':
501 nthreads=strtol(optarg, NULL, 0);
502 break;
503
504 case 'T':
505 p.n_teams = strtol(optarg, NULL, 0);
506 break;
507
508 case 'w':
509 p.schedule_shape_override = strtol(optarg, NULL, 0);
510 break;
511 case 'm':
512#ifdef BIND_THREADS
513 for (int i=0; themaps[i].name[0]; i++) {
514 if (!strcmp(themaps[i].name, optarg)) {
515 mapfn = themaps[i].fn;
516 break;
517 }
518 }
519
520 if (!mapfn) {
521 printf("Unknown thread ID map '%s'. Type %s -h for a list.\n", optarg, argv[0]);
522 return 1;
523 }
524#else
525 printf("Error: Thread binding not enabled on this build.\n");
526 return 1;
527#endif // BIND_THREADS
528 break;
529#else
530 case 't':
531 case 'T':
532 case 'w':
533 case 'm':
534 printf("Error: Threading not enabled on this build.\n");
535 return 1;
536#endif // THREADS
537
538 case 'i':
539 iterations=strtol(optarg, NULL, 0);
540 break;
541
542 case 's':
543 p.a_stride = strtol(optarg, NULL, 0);
544 break;
545
546 case 'S':
547 p.b_stride = strtol(optarg, NULL, 0);
548 break;
549
550 case 'l':
551 {
552 CPUInfo *ci = get_CPUInfo();
553 ci->set_L1_cache_size(strtol(optarg, NULL, 0));
554 break;
555 }
556
557 case 'L':
558 {
559 CPUInfo *ci = get_CPUInfo();
560 ci->set_L2_cache_size(strtol(optarg, NULL, 0));
561 break;
562 }
563
564 case 'C':
565 {
566 CPUInfo *ci = get_CPUInfo();
567 ci->midr_override(strtol(optarg, NULL, 0));
568 break;
569 }
570
571 case 'y':
572 p.batches = strtol(optarg, NULL, 0);
573 break;
574
575 case 'z':
576 p.multis = strtol(optarg, NULL, 0);
577 break;
578
579 case 'D':
580 test_suite = optarg;
581 break;
582
583 case 'n':
584 test_id=strtol(optarg, NULL, 0);
585 if (test_id <= 0) {
586 printf("Test ID must be at least 1.\n");
587 return 1;
588 }
589 break;
590
591 case 'I':
592 test_impl = optarg;
593 break;
594
595 case 'q':
596 doquery = true;
597 break;
598
599 case 'F':
600 p.kernel_filter = optarg;
601 break;
602
603 case 'G':
604 p.dynamic_granule_count=strtol(optarg, NULL, 0);
605 p.dynamic_scheduling=true;
606 break;
607
608 case 'j':
609 p.inner_block_size=strtol(optarg, NULL, 0);
610 break;
611
612 case 'k':
613 p.outer_block_size=strtol(optarg, NULL, 0);
614 break;
615
616 case 'V':
617 p.strategy = str2strat(optarg);
618 if (p.strategy == ConvStrategy::fail) {
619 printf("Unknown convolution strategy: %s\n",optarg);
620 return 1;
621 }
622 break;
623
624 case 'r':
625 p.min_ms=strtol(optarg, NULL, 0);
626 break;
627
628 case 'W':
629 p.time_weight_transform=true;
630 break;
631
632 case 'A':
633 p.accumulate = true;
634 break;
635
636 case 'B':
637 p.transposed_b = true;
638 break;
639
640 case 'f':
641 p.fast_mode = true;
642 break;
643
644 case 'u':
645 case 'U':
646 {
647 bool setv = false;
648 if (opt == 'U') {
649 setv = true;
650 }
651 CPUInfo *ci = get_CPUInfo();
652 char *p=optarg;
653
654 // Handle comma separation - look for next comma and blonk it out.
655 while (p) {
656 char *np = strchr(p, ',');
657 if (np) {
658 *np = '\0';
659 np++;
660 }
661
662 bool ret = ci->force_feature(p, setv);
663
664 if (!ret) {
665 printf("Warning: Unknown CPU feature %s.\n", p);
666 }
667
668 // Now process next feature.
669 p=np;
670 }
671 }
672 break;
673
674 case 'p':
675 //" -p: Specify convolution parameters (comma separated):\n"
676 //" in_h,in_w,in_ch,kern_h,kern_w,out_ch,stride_h,stride_w,pad_top,pad_left,pad_bottom,pad_right,dilation_h,dilation_w\n"
677 {
678 int64_t pad_right=0, pad_bottom=0;
679 int64_t *arg_ptrs[] = { &p.input_height, &p.input_width, &p.input_channels, &p.kernel_height, &p.kernel_width,
680 &p.output_channels, &p.out_stride_h, &p.out_stride_w, &p.padding_top, &p.padding_left, &pad_bottom, &pad_right,
681 &p.in_stride_h, &p.in_stride_w, nullptr };
682
683 char *cp = optarg;
684 int argidx=0;
685
686 while(cp) {
687 char *ncp = strchr(cp, ',');
688 if (ncp) {
689 *ncp = '\0';
690 ncp++;
691 }
692
693 if (arg_ptrs[argidx] == nullptr) {
694 printf("Warning: Too many parameters to -p\n");
695 break;
696 }
697
698 *arg_ptrs[argidx] = strtoull(cp, nullptr, 0);
699
700 argidx++;
701 cp=ncp;
702 }
703
704 // Re-derive output width/height
705 auto eff_kernel_w = (p.kernel_width - 1)*p.in_stride_w;
706 auto eff_kernel_h = (p.kernel_height - 1)*p.in_stride_h;
707 p.output_width = (p.input_width + p.padding_left + pad_right - eff_kernel_w + (p.out_stride_w-1)) / p.out_stride_w;
708 p.output_height = (p.input_height + p.padding_top + pad_bottom - eff_kernel_h + (p.out_stride_h-1)) / p.out_stride_h;
709
710 manual_spec = true;
711 }
712 break;
713
714 case 'o':
715 p.dump_file = optarg;
716 break;
717
718 case 'O':
719 read_dump_fname = optarg;
720 break;
721
722 case 'Q':
723 p.cache_stats = true;
724 break;
725
726 case 'X':
727 // Switch on the following argument
728 if (!strcmp(optarg, "after-relu")) {
729 // Indicate that the incoming activations should be passed
730 // through a ReLU before being passed into the
731 // GEMM/Depthwise/Winograd layer. This can enable some
732 // optimisations for some layers.
733 p.after_relu = true;
734 } else {
735 printf("Unknown -X argument '%s'\n", optarg);
736 return -1;
737 }
738 break;
739
740 case 128:
741 p.winograd_args.output_tile_rows = strtoul(optarg, nullptr, 0);
742 break;
743
744 case 129:
745 p.winograd_args.output_tile_cols = strtoul(optarg, nullptr, 0);
746 break;
747
748 case 130:
749 p.winograd_args.input_transform_filter = optarg;
750 break;
751
752 case 131:
753 p.winograd_args.weight_transform_filter = optarg;
754 break;
755
756 case 132:
757 p.winograd_args.output_transform_filter = optarg;
758 break;
759
760 case 133:
761 p.weight_format=static_cast<kai::ops::WeightFormat>(strtoul(optarg, nullptr, 0));
762 break;
763
764 case 'P':
765 p.fixed_format = true;
766 break;
767
768 case 'Z':
769#ifdef BARE_METAL
770 p.cache_flush = true;
771#else
772 printf("Warning: -Z option ignored for non-bare metal builds.");
773#endif
774 break;
775
776 case 'h':
777 printhelp(argv[0]);
778 return 0;
779
780 case '?':
781 printhelp(argv[0]);
782 return 1;
783
784 default:
785 fprintf(stderr,
786 "Internal error: getopt returned unhandled option %d. "
787 "The getopt option list and switch cases are out of sync.\n",
788 opt);
789 return 1;
790 }
791 }
792
793 kernentry *mykern = NULL;
794
795 if (optind==argc) {
796 /* No kernel name specified. */
797 printf("Error: No kernel name specified.\n");
798 } else {
799 for (int i=0; thekerns[i].name[0]; i++) {
800 if (!strcmp(thekerns[i].name, argv[optind])) {
801 mykern = &(thekerns[i]);
802 break;
803 }
804 }
805
806 if (!mykern) {
807 printf("Unknown kernel: %s\n", argv[optind]);
808 }
809 }
810
811 /* Print help and exit if kernel name was not specified or not found. */
812 if (!mykern) {
813 printhelp(argv[0]);
814 return 0;
815 }
816
817 /* Set default sizes if needed, based on check mode */
818 if (docheck) {
819 if (!p.input_width) p.input_width=p.output_width=100;
820 if (!p.output_channels) p.output_channels=100;
821 if (!p.input_channels) p.input_channels=100;
822 } else {
823 if (!p.input_width) p.input_width=p.output_width=768;
824 if (!p.output_channels) p.output_channels=768;
825 if (!p.input_channels) p.input_channels=768;
826 }
827
828 if (mykern->flags & KERN_CONV_ONLY) {
829 test_impl = "conv2d";
830 }
831
832 if (mykern->flags & KERN_FAST_ONLY) {
833 p.fast_mode = true;
834 }
835
836 if (mykern->flags & KERN_ADD) {
837 p.add_problem = true;
838 p.accumulate = true;
839 p.input_channels = p.output_channels;
840 }
841
842 /* Set test config */
843 if (test_id) {
844 if (!get_test_config(&p, test_suite, test_id, test_impl, mykern->flags & KERN_WGOK, mykern->flags & KERN_CONV)) {
845 printf("Error selecting test config, aborting.\n");
846 exit(1);
847 }
848 manual_spec = false;
849 }
850
851 if (read_dump_fname != "") {
852 read_dump_file = read_dump(&p, read_dump_fname.c_str());
853 manual_spec = false;
854 } else {
855 read_dump_file = nullptr;
856 }
857
858 /* Override groups for manual depthwise problems. */
859 if (mykern->flags & KERN_DEPTHWISE && manual_spec) {
860 p.groups = p.input_channels;
861 }
862
863 /* Override M size for gemv kernels. */
864 if (mykern->flags & KERN_GEMV) {
865 p.input_width=p.output_width=1;
866 if (mykern->flags & KERN_GEMV_N) {
867 p.input_width=p.output_width=p.output_channels;
868 p.output_channels=1;
869 }
870 }
871
872 /* Warn about unsupported parameters for legacy kernels. */
873 if (mykern->flags & KERN_LEGACY) {
874 // Legacy kernels also don't support convolution - but that will be sorted out by the convolution check
875 // below. Here we just test for parameters that are legal in "pure GEMMs" but not supported by the legacy
876 // kernels.
877
878 if (p.use_bias) {
879 printf("Warning: Kernel %s does not support bias, ignoring -b\n", mykern->name);
880 p.use_bias=false;
881 }
882
883 if (p.batches != 1) {
884 printf("Warning: Kernel %s does not support batches, ignoring -y\n", mykern->name);
885 p.batches=1;
886 }
887
888 if (p.multis != 1) {
889 printf("Warning: Kernel %s does not support multis, ignoring -z\n", mykern->name);
890 p.multis=1;
891 }
892 }
893
894 if (!(mykern->flags & KERN_ACTOK)) {
895 if (p.act.type != Activation::Type::None) {
896 printf("Warning: Kernel %s does not support activation, ignoring -a\n", mykern->name);
897 p.act=Activation();
898 }
899 }
900
901 if (mykern->flags & (KERN_QUANTIZED | KERN_NOACC)) {
902 if (!(mykern->flags & KERN_ADD) && p.accumulate) {
903 printf("Warning: accumulate not supported by %s, ignoring -A.\n", mykern->name);
904 p.accumulate=false;
905 }
906 }
907
908 if (!p.is_basic_gemm() && !(mykern->flags & KERN_CONV)) {
909 printf("Error: Kernel %s does not support convolution problems, aborting.\n", mykern->name);
910 return 0;
911 }
912
913 if (doquery) {
914 mykern->print(&p, nthreads);
915 return 0;
916 }
917
918 // Delegate some checks to GemmProblem.
919 if (!p.validate()) {
920 return 0;
921 }
922
923 /* TODO: Add something to force it to error out rather than rounding up
924 * the sizes, to make efficiency calculations based on the size/runtime
925 * reliable.
926 */
927 if (docheck) {
928 mykern->test(&p, iterations, nthreads, mykern->name, read_dump_file);
929 } else {
930 mykern->bench(&p, iterations, nthreads, run_delay, mapfn, mykern->name);
931 printf("Benchmark complete (no compare).\n");
932 }
933
934 return 0;
935}