| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <getopt.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <array> |
| 11 | #include <cerrno> |
| 12 | #include <cstdlib> |
| 13 | #include <cstring> |
| 14 | #include <iostream> |
| 15 | #include <optional> |
| 16 | #include <sstream> |
| 17 | #include <string> |
| 18 | #include <string_view> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "benchmark/dwconv/dwconv_registry.hpp" |
| 22 | #include "benchmark/imatmul/imatmul_registry.hpp" |
| 23 | #include "benchmark/matmul/matmul_registry.hpp" |
| 24 | #include "benchmark/pack_matmul/pack_matmul_registry.hpp" |
| 25 | #include "kai/kai_common.h" |
| 26 | |
| 27 | #ifdef __GNUC__ |
| 28 | #pragma GCC diagnostic push |
| 29 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 30 | #endif // __GNUC__ |
| 31 | |
| 32 | #include <benchmark/benchmark.h> |
| 33 | |
| 34 | #ifdef __GNUC__ |
| 35 | #pragma GCC diagnostic pop |
| 36 | #endif // __GNUC__ |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | using namespace std::string_literals; |
| 41 | |
| 42 | void print_matmul_usage(std::string_view name, bool defaulted = false) { |
| 43 | std::ostringstream oss; |
| 44 | if (defaulted) { |
| 45 | oss << "Warning: No operation specified, defaulting to 'matmul' mode.\n"; |
| 46 | oss << "If you intended to run a different operation, specify it explicitly like so:\n"; |
| 47 | oss << '\t' << name << " imatmul [options]\n\n"; |
| 48 | } |
| 49 | oss << "Matmul usage:" << '\n'; |
| 50 | oss << '\t' << name << " matmul -m <M> -n <N> -k <K> [-b <block_size>]" << '\n'; |
| 51 | oss << "Options:" << '\n'; |
| 52 | oss << "\t-m,-n,-k\tMatrix dimensions (LHS MxK, RHS KxN)" << '\n'; |
| 53 | oss << "\t-b\t\t(Optional) Block size for blockwise quantization" << '\n'; |
| 54 | std::cerr << oss.str() << '\n'; |
| 55 | } |
| 56 | |
| 57 | void print_pack_matmul_usage(std::string_view name) { |
| 58 | std::ostringstream oss; |
| 59 | oss << "PackMatmul usage:" << '\n'; |
| 60 | oss << '\t' << name << " pack_matmul -m <M> -n <N> -k <K> [-b <block_size>]" << '\n'; |
| 61 | oss << "Options:" << '\n'; |
| 62 | oss << "\t-m,-n,-k\tMatrix dimensions (LHS MxK, RHS KxN)" << '\n'; |
| 63 | oss << "\t-b\t\t(Optional) Block size for blockwise quantization" << '\n'; |
| 64 | std::cerr << oss.str() << '\n'; |
| 65 | } |
| 66 | |
| 67 | void print_imatmul_usage(std::string_view name) { |
| 68 | std::ostringstream oss; |
| 69 | oss << "IndirectMatmul usage:" << '\n'; |
| 70 | oss << '\t' << name << " imatmul -m <M> -n <N> -c <k_chunk_count> -l <k_chunk_length>" << '\n'; |
| 71 | oss << "Options:" << '\n'; |
| 72 | oss << "\t-m\tNumber of rows (LHS)" << '\n'; |
| 73 | oss << "\t-n\tNumber of columns (RHS)" << '\n'; |
| 74 | oss << "\t-c\tK chunk count" << '\n'; |
| 75 | oss << "\t-l\tK chunk length" << '\n'; |
| 76 | std::cerr << oss.str() << '\n'; |
| 77 | } |
| 78 | |
| 79 | void print_dwconv_usage(std::string_view name) { |
| 80 | std::ostringstream oss; |
| 81 | oss << "DWConv usage:" << '\n'; |
| 82 | oss << '\t' << name |
| 83 | << " dwconv --input_height <H> --input_width <W> --channels <C> [--stride <S_h,S_w>] " |
| 84 | "[--padding <P_top,P_bottom,P_left,P_right>] [--dilation <D_h,D_w>]" |
| 85 | << '\n'; |
| 86 | oss << "Options:" << '\n'; |
| 87 | oss << "\t--input_height\tInput height (required)" << '\n'; |
| 88 | oss << "\t--input_width\tInput width (required)" << '\n'; |
| 89 | oss << "\t--channels\tNumber of channels (required)" << '\n'; |
| 90 | oss << "\t--stride\t(Optional) Two positive comma-separated values for (row, col) stride (default: 1,1)" << '\n'; |
| 91 | oss << "\t--padding\t(Optional) Four non-negative comma-separated values for (top, bottom, left, right) padding " |
| 92 | "(default: 0,0,0,0)" |
| 93 | << '\n'; |
| 94 | oss << "\t--dilation\t(Optional) Two positive comma-separated values for (row, col) dilation (default: 1,1)" |
| 95 | << '\n'; |
| 96 | oss << "\nCurrent DWConv micro-kernels only support stride=1 and dilation=1.\n"; |
| 97 | std::cerr << oss.str() << '\n'; |
| 98 | } |
| 99 | |
| 100 | void print_global_usage(std::string_view name) { |
| 101 | std::ostringstream oss; |
| 102 | oss << "Usage:" << '\n'; |
| 103 | oss << '\t' << name << " <matmul|pack_matmul|imatmul|dwconv> [<options>]" << '\n'; |
| 104 | oss << "\nIf no operation is provided, defaults to: " << name << " matmul [options]" << '\n'; |
| 105 | oss << "\nBenchmark Framework options:" << '\n'; |
| 106 | oss << '\t' << name << " --help" << '\n'; |
| 107 | std::cerr << oss.str() << '\n'; |
| 108 | |
| 109 | print_matmul_usage(name); |
| 110 | print_pack_matmul_usage(name); |
| 111 | print_imatmul_usage(name); |
| 112 | print_dwconv_usage(name); |
| 113 | } |
| 114 | |
| 115 | enum class DwConvValueKind { Positive, NonNegative }; |
| 116 | |
| 117 | bool parse_size_t_arg(const char* arg, const char* name, DwConvValueKind kind, size_t& out, std::string& error) { |
| 118 | if (!arg) { |
| 119 | error = "Missing value for "s + name; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | errno = 0; |
| 124 | char* end = nullptr; |
| 125 | const unsigned long parsed = std::strtoul(arg, &end, 10); |
| 126 | if (errno != 0 || end == arg || *end != '\0') { |
| 127 | error = "Invalid value for "s + name + ": " + arg; |
| 128 | return false; |
| 129 | } |
| 130 | if (kind == DwConvValueKind::Positive && parsed == 0) { |
| 131 | error = "Value for "s + name + " must be greater than 0."; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | out = static_cast<size_t>(parsed); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | std::string_view trim_view(std::string_view sv) { |
| 140 | const size_t start = sv.find_first_not_of(" \t"); |
| 141 | if (start == std::string_view::npos) { |
| 142 | return {}; |
| 143 | } |
| 144 | const size_t end = sv.find_last_not_of(" \t"); |
| 145 | return sv.substr(start, end - start + 1); |
| 146 | } |
| 147 | |
| 148 | /// Parses a comma-separated list of `N` size_t values with optional whitespace. |
| 149 | template <size_t N> |
| 150 | bool parse_size_t_list( |
| 151 | const char* arg, const char* name, DwConvValueKind kind, std::array<size_t, N>& out, std::string& error) { |
| 152 | if (!arg) { |
| 153 | error = "Missing value for "s + name; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | std::string_view values(arg); |
| 158 | std::vector<std::string> tokens; |
| 159 | size_t start = 0; |
| 160 | while (start <= values.size()) { |
| 161 | const size_t pos = values.find(',', start); |
| 162 | const size_t len = (pos == std::string::npos) ? std::string::npos : pos - start; |
| 163 | std::string_view token = values.substr(start, len); |
| 164 | token = trim_view(token); |
| 165 | if (token.empty()) { |
| 166 | error = "Invalid value for "s + name + ": " + arg; |
| 167 | return false; |
| 168 | } |
| 169 | tokens.emplace_back(token); |
| 170 | if (pos == std::string::npos) { |
| 171 | break; |
| 172 | } |
| 173 | start = pos + 1; |
| 174 | } |
| 175 | |
| 176 | if (tokens.size() != N) { |
| 177 | error = std::string(name) + " expects " + std::to_string(N) + " comma-separated values."; |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | for (size_t i = 0; i < N; ++i) { |
| 182 | size_t parsed = 0; |
| 183 | if (!parse_size_t_arg(tokens[i].c_str(), name, kind, parsed, error)) { |
| 184 | return false; |
| 185 | } |
| 186 | out[i] = parsed; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | std::optional<kai::benchmark::DwConvShape> parse_dwconv_cli(int argc, char** argv, std::string& error) { |
| 193 | enum : int { |
| 194 | OPT_CHANNELS = 1000, |
| 195 | OPT_INPUT_HEIGHT, |
| 196 | OPT_INPUT_WIDTH, |
| 197 | OPT_STRIDE, |
| 198 | OPT_PADDING, |
| 199 | OPT_DILATION, |
| 200 | }; |
| 201 | |
| 202 | kai::benchmark::DwConvShape shape{}; |
| 203 | shape.stride = {1, 1}; |
| 204 | shape.padding = {0, 0, 0, 0}; |
| 205 | shape.dilation = {1, 1}; |
| 206 | |
| 207 | bool input_height_set = false; |
| 208 | bool input_width_set = false; |
| 209 | bool channels_set = false; |
| 210 | |
| 211 | optind = 1; |
| 212 | static const struct option long_options[] = { |
| 213 | {"channels", required_argument, nullptr, OPT_CHANNELS}, |
| 214 | {"input_height", required_argument, nullptr, OPT_INPUT_HEIGHT}, |
| 215 | {"input_width", required_argument, nullptr, OPT_INPUT_WIDTH}, |
| 216 | {"stride", required_argument, nullptr, OPT_STRIDE}, |
| 217 | {"padding", required_argument, nullptr, OPT_PADDING}, |
| 218 | {"dilation", required_argument, nullptr, OPT_DILATION}, |
| 219 | {nullptr, 0, nullptr, 0}, |
| 220 | }; |
| 221 | |
| 222 | int opt; |
| 223 | while ((opt = getopt_long(argc, argv, "", long_options, nullptr)) != -1) { |
| 224 | switch (opt) { |
| 225 | case OPT_CHANNELS: |
| 226 | if (!parse_size_t_arg(optarg, "--channels", DwConvValueKind::Positive, shape.num_channels, error)) { |
| 227 | return std::nullopt; |
| 228 | } |
| 229 | channels_set = true; |
| 230 | break; |
| 231 | case OPT_INPUT_HEIGHT: |
| 232 | if (!parse_size_t_arg(optarg, "--input_height", DwConvValueKind::Positive, shape.input_height, error)) { |
| 233 | return std::nullopt; |
| 234 | } |
| 235 | input_height_set = true; |
| 236 | break; |
| 237 | case OPT_INPUT_WIDTH: |
| 238 | if (!parse_size_t_arg(optarg, "--input_width", DwConvValueKind::Positive, shape.input_width, error)) { |
| 239 | return std::nullopt; |
| 240 | } |
| 241 | input_width_set = true; |
| 242 | break; |
| 243 | case OPT_STRIDE: |
| 244 | if (!parse_size_t_list(optarg, "--stride", DwConvValueKind::Positive, shape.stride, error)) { |
| 245 | return std::nullopt; |
| 246 | } |
| 247 | break; |
| 248 | case OPT_PADDING: |
| 249 | if (!parse_size_t_list(optarg, "--padding", DwConvValueKind::NonNegative, shape.padding, error)) { |
| 250 | return std::nullopt; |
| 251 | } |
| 252 | break; |
| 253 | case OPT_DILATION: |
| 254 | if (!parse_size_t_list(optarg, "--dilation", DwConvValueKind::Positive, shape.dilation, error)) { |
| 255 | return std::nullopt; |
| 256 | } |
| 257 | break; |
| 258 | case '?': |
| 259 | default: |
| 260 | error = "Unrecognized option for dwconv benchmark."; |
| 261 | return std::nullopt; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (!input_height_set) { |
| 266 | error = "Missing required option --input_height"; |
| 267 | return std::nullopt; |
| 268 | } |
| 269 | if (!input_width_set) { |
| 270 | error = "Missing required option --input_width"; |
| 271 | return std::nullopt; |
| 272 | } |
| 273 | if (!channels_set) { |
| 274 | error = "Missing required option --channels"; |
| 275 | return std::nullopt; |
| 276 | } |
| 277 | |
| 278 | return shape; |
| 279 | } |
| 280 | |
| 281 | } // namespace |
| 282 | |
| 283 | static std::optional<std::string> find_user_benchmark_filter(int argc, char** argv) { |
| 284 | static constexpr std::string_view benchmark_filter_eq = "--benchmark_filter="; |
| 285 | static constexpr std::string_view benchmark_filter = "--benchmark_filter"; |
| 286 | |
| 287 | for (int i = 1; i < argc; ++i) { |
| 288 | const char* arg = argv[i]; |
| 289 | if (!arg) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | // --benchmark_filter=REGEX |
| 294 | std::string_view arg_view(arg); |
| 295 | if (arg_view.substr(0, benchmark_filter_eq.length()) == benchmark_filter_eq) { |
| 296 | auto val = arg_view.substr(benchmark_filter_eq.length()); |
| 297 | return std::string(val); |
| 298 | } |
| 299 | |
| 300 | // --benchmark_filter REGEX |
| 301 | if (arg_view == benchmark_filter && i + 1 < argc) { |
| 302 | const char* val = argv[i + 1]; |
| 303 | return std::string(val ? val : ""); |
| 304 | } |
| 305 | } |
| 306 | return std::nullopt; |
| 307 | } |
| 308 | |
| 309 | static int run_matmul( |
| 310 | int argc, char** argv, bool default_to_matmul, const std::optional<std::string>& user_filter_opt) { |
| 311 | bool mflag = false, nflag = false, kflag = false, bflag = false; |
| 312 | size_t m = 1, n = 1, k = 1, bl = 32; |
| 313 | |
| 314 | optind = 1; |
| 315 | int opt; |
| 316 | while ((opt = getopt(argc, argv, "m:n:k:b:")) != -1) { |
| 317 | switch (opt) { |
| 318 | case 'm': |
| 319 | m = std::atoi(optarg); |
| 320 | mflag = true; |
| 321 | break; |
| 322 | case 'n': |
| 323 | n = std::atoi(optarg); |
| 324 | nflag = true; |
| 325 | break; |
| 326 | case 'k': |
| 327 | k = std::atoi(optarg); |
| 328 | kflag = true; |
| 329 | break; |
| 330 | case 'b': |
| 331 | bl = std::atoi(optarg); |
| 332 | bflag = true; |
| 333 | break; |
| 334 | default: |
| 335 | print_matmul_usage(argv[0], default_to_matmul); |
| 336 | return EXIT_FAILURE; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (!mflag || !nflag || !kflag) { |
| 341 | print_matmul_usage(argv[0]); |
| 342 | return EXIT_FAILURE; |
| 343 | } |
| 344 | if (!bflag) { |
| 345 | std::cerr << "Optional argument -b not specified. Defaulting to block size " << bl << "\n"; |
| 346 | } |
| 347 | |
| 348 | kai::benchmark::RegisterMatMulBenchmarks({m, n, k}, bl); |
| 349 | const std::string spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_matmul"); |
| 350 | |
| 351 | ::benchmark::RunSpecifiedBenchmarks(nullptr, nullptr, spec); |
| 352 | ::benchmark::Shutdown(); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int run_pack_matmul(int argc, char** argv, const std::optional<std::string>& user_filter_opt) { |
| 357 | bool mflag = false, nflag = false, kflag = false, bflag = false; |
| 358 | size_t m = 1, n = 1, k = 1, bl = 32; |
| 359 | |
| 360 | optind = 1; |
| 361 | int opt; |
| 362 | while ((opt = getopt(argc, argv, "m:n:k:b:")) != -1) { |
| 363 | switch (opt) { |
| 364 | case 'm': |
| 365 | m = std::atoi(optarg); |
| 366 | mflag = true; |
| 367 | break; |
| 368 | case 'n': |
| 369 | n = std::atoi(optarg); |
| 370 | nflag = true; |
| 371 | break; |
| 372 | case 'k': |
| 373 | k = std::atoi(optarg); |
| 374 | kflag = true; |
| 375 | break; |
| 376 | case 'b': |
| 377 | bl = std::atoi(optarg); |
| 378 | bflag = true; |
| 379 | break; |
| 380 | default: |
| 381 | print_pack_matmul_usage(argv[0]); |
| 382 | return EXIT_FAILURE; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (!mflag || !nflag || !kflag) { |
| 387 | print_pack_matmul_usage(argv[0]); |
| 388 | return EXIT_FAILURE; |
| 389 | } |
| 390 | if (!bflag) { |
| 391 | std::cerr << "Optional argument -b not specified. Defaulting to block size " << bl << "\n"; |
| 392 | } |
| 393 | |
| 394 | kai::benchmark::RegisterPackMatMulBenchmarks({m, n, k}, bl); |
| 395 | const std::string spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("kai_pack_matmul"); |
| 396 | |
| 397 | ::benchmark::RunSpecifiedBenchmarks(nullptr, nullptr, spec); |
| 398 | ::benchmark::Shutdown(); |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int run_imatmul(int argc, char** argv, const std::optional<std::string>& user_filter_opt) { |
| 403 | bool mflag = false, nflag = false, cflag = false, lflag = false; |
| 404 | size_t m = 1, n = 1, k_chunk_count = 1, k_chunk_length = 1; |
| 405 | |
| 406 | optind = 1; |
| 407 | int opt; |
| 408 | while ((opt = getopt(argc, argv, "m:n:c:l:")) != -1) { |
| 409 | switch (opt) { |
| 410 | case 'm': |
| 411 | m = std::atoi(optarg); |
| 412 | mflag = true; |
| 413 | break; |
| 414 | case 'n': |
| 415 | n = std::atoi(optarg); |
| 416 | nflag = true; |
| 417 | break; |
| 418 | case 'c': |
| 419 | k_chunk_count = std::atoi(optarg); |
| 420 | cflag = true; |
| 421 | break; |
| 422 | case 'l': |
| 423 | k_chunk_length = std::atoi(optarg); |
| 424 | lflag = true; |
| 425 | break; |
| 426 | default: |
| 427 | print_imatmul_usage(argv[0]); |
| 428 | return EXIT_FAILURE; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (!mflag || !nflag || !cflag || !lflag) { |
| 433 | print_imatmul_usage(argv[0]); |
| 434 | return EXIT_FAILURE; |
| 435 | } |
| 436 | |
| 437 | std::cerr << "Running imatmul benchmarks with m=" << m << ", n=" << n << ", k_chunk_count=" << k_chunk_count |
| 438 | << ", k_chunk_length=" << k_chunk_length << "\n"; |
| 439 | |
| 440 | kai::benchmark::RegisteriMatMulBenchmarks(m, n, k_chunk_count, k_chunk_length); |
| 441 | |
| 442 | // Default filter if user didn’t supply one |
| 443 | std::string spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_imatmul"); |
| 444 | |
| 445 | ::benchmark::RunSpecifiedBenchmarks(nullptr, nullptr, spec); |
| 446 | ::benchmark::Shutdown(); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int run_dwconv(int argc, char** argv, const std::optional<std::string>& user_filter_opt) { |
| 451 | std::string parse_error; |
| 452 | auto shape_opt = parse_dwconv_cli(argc, argv, parse_error); |
| 453 | if (!shape_opt) { |
| 454 | if (!parse_error.empty()) { |
| 455 | std::cerr << parse_error << '\n'; |
| 456 | } |
| 457 | print_dwconv_usage(argv[0]); |
| 458 | return EXIT_FAILURE; |
| 459 | } |
| 460 | |
| 461 | const auto inferred_dims = kai::benchmark::InferDwConvOutputDims(*shape_opt); |
| 462 | if (!inferred_dims) { |
| 463 | std::cerr << "Invalid DWConv configuration: inferred output dimensions are non-positive. " |
| 464 | << "Check stride, padding, and dilation relative to the kernel size.\n"; |
| 465 | return EXIT_FAILURE; |
| 466 | } |
| 467 | |
| 468 | const size_t inferred_out_h = inferred_dims->height; |
| 469 | const size_t inferred_out_w = inferred_dims->width; |
| 470 | |
| 471 | const auto& shape = *shape_opt; |
| 472 | const auto format_array = [](const auto& values) { |
| 473 | std::ostringstream oss; |
| 474 | oss << '['; |
| 475 | for (size_t i = 0; i < values.size(); ++i) { |
| 476 | if (i != 0) { |
| 477 | oss << ", "; |
| 478 | } |
| 479 | oss << values[i]; |
| 480 | } |
| 481 | oss << ']'; |
| 482 | return oss.str(); |
| 483 | }; |
| 484 | |
| 485 | if (!kai::benchmark::supports_unit_stride_and_dilation(shape)) { |
| 486 | std::cerr << "Configured stride=" << format_array(shape.stride) << " dilation=" << format_array(shape.dilation) |
| 487 | << " is not supported by current DWConv micro-kernels. " |
| 488 | << "Only stride=1 and dilation=1 are available.\n"; |
| 489 | return EXIT_FAILURE; |
| 490 | } |
| 491 | |
| 492 | std::cerr << "Running dwconv benchmarks with input=" << shape.input_height << 'x' << shape.input_width |
| 493 | << ", output=" << inferred_out_h << 'x' << inferred_out_w << ", channels=" << shape.num_channels |
| 494 | << ", stride=" << format_array(shape.stride) << ", padding=" << format_array(shape.padding) |
| 495 | << ", dilation=" << format_array(shape.dilation) << "\n"; |
| 496 | |
| 497 | kai::benchmark::RegisterDwConvBenchmarks(shape); |
| 498 | |
| 499 | std::string spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_dwconv"); |
| 500 | ::benchmark::RunSpecifiedBenchmarks(nullptr, nullptr, spec); |
| 501 | ::benchmark::Shutdown(); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int main(int argc, char** argv) { |
| 506 | // Detect user-provided filter BEFORE Initialize() consumes the benchmark framework flags |
| 507 | const auto user_filter_opt = find_user_benchmark_filter(argc, argv); |
| 508 | |
| 509 | // Check for --benchmark_list_tests in argv |
| 510 | bool list_tests = false; |
| 511 | for (int i = 1; i < argc; ++i) { |
| 512 | if (std::strstr(argv[i], "--benchmark_list_tests") == argv[i]) { |
| 513 | list_tests = true; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | ::benchmark::Initialize(&argc, argv); |
| 519 | |
| 520 | std::cerr << "KleidiAI version: v" << kai_get_version() << "\n"; |
| 521 | |
| 522 | // Determine subcommand (mode): matmul, pack_matmul, imatmul or dwconv. |
| 523 | enum class Mode : uint8_t { COMPAT, MATMUL, PACK_MATMUL, IMATMUL, DWCONV } mode = Mode::COMPAT; |
| 524 | |
| 525 | static constexpr std::string_view MATMUL = "matmul"; |
| 526 | static constexpr std::string_view PACK_MATMUL = "pack_matmul"; |
| 527 | static constexpr std::string_view IMATMUL = "imatmul"; |
| 528 | static constexpr std::string_view DWCONV = "dwconv"; |
| 529 | |
| 530 | std::vector<std::string_view> args(argv, argv + argc); |
| 531 | |
| 532 | if (!list_tests && argc < 2) { |
| 533 | print_global_usage(argv[0]); |
| 534 | return EXIT_FAILURE; |
| 535 | } |
| 536 | |
| 537 | if (argc >= 2 && args[1] == MATMUL) { |
| 538 | mode = Mode::MATMUL; |
| 539 | argv += 1; |
| 540 | argc -= 1; |
| 541 | } else if (argc >= 2 && args[1] == PACK_MATMUL) { |
| 542 | mode = Mode::PACK_MATMUL; |
| 543 | argv += 1; |
| 544 | argc -= 1; |
| 545 | } else if (argc >= 2 && args[1] == IMATMUL) { |
| 546 | mode = Mode::IMATMUL; |
| 547 | argv += 1; |
| 548 | argc -= 1; |
| 549 | } else if (argc >= 2 && args[1] == DWCONV) { |
| 550 | mode = Mode::DWCONV; |
| 551 | argv += 1; |
| 552 | argc -= 1; |
| 553 | } |
| 554 | |
| 555 | if (list_tests) { |
| 556 | std::string spec; |
| 557 | if (mode == Mode::COMPAT) { |
| 558 | kai::benchmark::RegisterMatMulBenchmarks({1, 1, 1}, 32); |
| 559 | kai::benchmark::RegisterPackMatMulBenchmarks({1, 1, 1}, 32); |
| 560 | kai::benchmark::RegisteriMatMulBenchmarks(1, 1, 1, 1); |
| 561 | kai::benchmark::RegisterDwConvBenchmarks({3, 3, 1}); |
| 562 | spec = user_filter_opt.value_or(""); |
| 563 | } else if (mode == Mode::MATMUL) { |
| 564 | kai::benchmark::RegisterMatMulBenchmarks({1, 1, 1}, 32); |
| 565 | spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_matmul"); |
| 566 | } else if (mode == Mode::PACK_MATMUL) { |
| 567 | kai::benchmark::RegisterPackMatMulBenchmarks({1, 1, 1}, 32); |
| 568 | spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("kai_pack_matmul"); |
| 569 | } else if (mode == Mode::IMATMUL) { |
| 570 | kai::benchmark::RegisteriMatMulBenchmarks(1, 1, 1, 1); |
| 571 | spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_imatmul"); |
| 572 | } else if (mode == Mode::DWCONV) { |
| 573 | kai::benchmark::RegisterDwConvBenchmarks({3, 3, 1}); |
| 574 | spec = user_filter_opt.has_value() ? *user_filter_opt : std::string("^kai_dwconv"); |
| 575 | } |
| 576 | ::benchmark::SetBenchmarkFilter(spec); |
| 577 | ::benchmark::RunSpecifiedBenchmarks(nullptr, nullptr, spec); |
| 578 | ::benchmark::Shutdown(); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | switch (mode) { |
| 583 | case Mode::COMPAT: |
| 584 | return run_matmul(argc, argv, true, user_filter_opt); |
| 585 | case Mode::MATMUL: |
| 586 | return run_matmul(argc, argv, false, user_filter_opt); |
| 587 | case Mode::PACK_MATMUL: |
| 588 | return run_pack_matmul(argc, argv, user_filter_opt); |
| 589 | case Mode::IMATMUL: |
| 590 | return run_imatmul(argc, argv, user_filter_opt); |
| 591 | case Mode::DWCONV: |
| 592 | return run_dwconv(argc, argv, user_filter_opt); |
| 593 | default: |
| 594 | print_global_usage(argv[0]); |
| 595 | return EXIT_FAILURE; |
| 596 | } |
| 597 | } |