forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbench.py
More file actions
721 lines (647 loc) · 22 KB
/
kbench.py
File metadata and controls
721 lines (647 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
from __future__ import annotations
import gc
import logging
import math
import os
import sys
from pathlib import Path
from time import time
import click
import pandas as pd
import rich
from kbench_model import KBENCH_MODE, KbenchCache, Scheduler, Spec, SpecInstance
from rich import traceback
from rich.console import Console
from rich.logging import RichHandler
from rich.progress import (
MofNCompleteColumn,
Progress,
TextColumn,
TimeElapsedColumn,
)
from terminal_viz import render_results
from utils import (
LINE,
_get_core_count,
_get_visible_device_prefix,
_percentage,
check_gpu_clock,
check_valid_target_accelerator,
get_target_accelerator_helpstr,
pretty_exception_handler,
)
##### Utilities and configurations #####
CONSOLE = Console(width=80)
CURRENT_FILE = Path(__file__).resolve()
pd.set_option("display.float_format", str)
def configure_logging(
quiet: bool = False, verbose: bool = False, pretty_output: bool = True
) -> Console:
"""Configure logging with rich formatting."""
global CONSOLE
if pretty_output:
debug_handler = RichHandler(
show_path=False, show_time=False, console=CONSOLE
)
logging.basicConfig(format="%(message)s", handlers=[debug_handler])
else:
logging.basicConfig(format="%(levelname)s: %(message)s")
CONSOLE = Console(width=80, force_terminal=False, color_system=None)
log_level = (
logging.DEBUG if verbose else logging.WARNING if quiet else logging.INFO
)
logging.getLogger().setLevel(log_level)
if verbose and pretty_output:
traceback.install(suppress=[click, rich])
elif pretty_output:
sys.excepthook = pretty_exception_handler
return CONSOLE
def log_and_raise_error(message: str, param_hint: str | None = None) -> None:
"""Log an error and raise a Click exception.
Args:
message: The error message to log and display
param_hint: Optional parameter hint for BadParameter exception
"""
logging.error(message)
if param_hint:
raise click.BadParameter(message, param_hint=param_hint)
else:
raise click.UsageError(message)
def run(
yaml_path_list, # noqa: ANN001
obj_cache: KbenchCache,
shape: SpecInstance,
output_path: Path = Path(),
mode=KBENCH_MODE.BUILD_AND_RUN, # noqa: ANN001
param_list=None, # noqa: ANN001
filter_list=None, # noqa: ANN001
build_opts: list[str] = [], # noqa: B006
profile: str = "",
exec_prefix: list[str] = [], # noqa: B006
exec_suffix: list[str] = [], # noqa: B006
dryrun: bool = False,
verbose: bool = False,
output_dir: Path | None = None,
num_cpu: int = 1,
num_gpu: int = 1,
target_accelerator: str | None = None,
timeout_secs: int | None = None,
plot: str = "bars",
) -> None:
if yaml_path_list:
# Load specs from a list of YAML files and join them in 'spec'.
assert len(yaml_path_list), "There should be at least 1 YAML as input."
spec = Spec.load_yaml_list(yaml_path_list)
else:
# Just load an empty Spec with identical name and file as shape
spec = Spec(shape.name, shape.file)
# Set output_dir='./kbench-output' if it is not specified.
if not output_dir:
output_dir = Path("./kbench-output")
# Set output_path (for storing results) relative to output_dir
output_path = output_dir / output_path
os.makedirs(output_path.parent, exist_ok=True)
# strip output_path suffix
if output_path.suffix in [".csv", ".pkl", ".txt"]:
output_path = output_path.with_suffix("")
if shape:
spec.extend_shape_params(shape.params)
# Each shape should have its own temporary directory.
output_dir = output_dir / Path(shape.hash(with_variables=True))
logging.info(f"output-dir: [{output_dir}]")
# Expand with CLI params
if param_list:
spec.extend_params(param_list)
# Apply the filters, if any.
if filter_list:
spec.filter(filter_list)
if verbose:
for i, s in enumerate(spec):
logging.debug(f"[{i}]{s}")
logging.debug(LINE)
# Run the code over the mesh of param/values
t_start_total = time()
progress = Progress(
*Progress.get_default_columns(),
MofNCompleteColumn(),
TextColumn("|"),
TimeElapsedColumn(),
console=CONSOLE,
expand=True,
transient=True,
)
# Set num_cpu to the half of maximum number of available CPUs
if num_cpu == -1:
num_cpu = max(_get_core_count() // 2, 1)
logging.info(f"num cpu's: {num_cpu}")
# Kbench Singleton Scheduler
scheduler = Scheduler(
num_cpu=num_cpu,
num_gpu=num_gpu,
obj_cache=obj_cache,
run_only=(mode == KBENCH_MODE.RUN),
spec_list=list(spec),
output_dir=output_dir,
build_opts=build_opts,
dryrun=dryrun,
output_suffix="output.csv",
progress=progress,
)
visible_device_prefix = _get_visible_device_prefix(str(target_accelerator))
# Run the code over the mesh of param/values
t_start_total = time()
t_benchmark_total = 0.0
with progress:
try:
# Get the binary path for the unique list of build items
# Build the binary if:
# - could not find executable in the cache or cache is not active,
# - could not find executable in the unique list of scheduled build items
scheduler.build_all()
obj_cache.dump()
t_build_total = time() - t_start_total
t_benchmark_start = time()
if mode in [KBENCH_MODE.RUN, KBENCH_MODE.BUILD_AND_RUN]:
scheduler.setup_execution_pool()
num_build_items = len(scheduler.build_items)
exec_progress = scheduler.progress.add_task(
"run",
total=num_build_items,
)
# execute build items in batches of size Scheduler.EXEC_STRIDE
for lower_bound in range(
0, num_build_items, Scheduler.EXEC_STRIDE
):
upper_bound = min(
lower_bound + Scheduler.EXEC_STRIDE, num_build_items
)
tasks = []
for cnt in range(lower_bound, upper_bound):
env = {}
if visible_device_prefix and num_gpu > 1:
env[visible_device_prefix] = str(cnt % num_gpu)
tasks.append(
(
scheduler.build_items[cnt],
profile,
exec_prefix,
exec_suffix,
env,
timeout_secs,
)
)
# This is to further ensure at most `num-gpu` processes are
# running at once, none of them sharing the same device context.
for cnt in range(lower_bound, upper_bound, num_gpu):
ub_gpu = min(upper_bound, cnt + num_gpu)
current_cnt = ub_gpu - cnt
for i, b in enumerate(
scheduler.execution_pool.imap(
scheduler._pool_execute_item_wrapper,
tasks[cnt - lower_bound : ub_gpu - lower_bound],
chunksize=1,
)
):
scheduler.build_items[cnt + i] = b
logging.info(
f"running binary [{b.idx}/{num_build_items - 1}] ({_percentage(b.idx + 1, num_build_items)}%)"
)
scheduler.progress.update(
exec_progress, advance=current_cnt
)
t_benchmark_total = time() - t_benchmark_start
t_elapsed_total = time() - t_start_total
# dump results that have been executed so far
# ensure there are more than one iterations of stride loop.
if num_build_items >= Scheduler.EXEC_STRIDE:
Scheduler.dump(
scheduler.build_items[0:upper_bound],
spec,
output_path,
mode,
t_build_total,
t_benchmark_total,
t_elapsed_total,
verbose=False,
)
logging.info("finished running all binaries")
scheduler.close_execution_pool()
except KeyboardInterrupt:
scheduler.close_build_pool()
scheduler.close_execution_pool()
obj_cache.dump()
sys.exit(0)
###############################
# dump all the details
t_elapsed_total = time() - t_start_total
gc.collect()
Scheduler.dump(
scheduler.build_items,
spec,
output_path,
mode,
t_build_total,
t_benchmark_total,
t_elapsed_total,
verbose=verbose,
)
logging.info(f"output-dir: [{output_dir}]\n{LINE}")
# Render terminal visualization if requested
if plot != "none" and mode in [KBENCH_MODE.RUN, KBENCH_MODE.BUILD_AND_RUN]:
pkl_path = output_path.with_suffix(output_path.suffix + ".pkl")
if pkl_path.exists():
import pickle
with open(pkl_path, "rb") as f:
pkl_data = pickle.load(f)
if "merged_df" in pkl_data:
render_results(
pkl_data["merged_df"], mode=plot, console=CONSOLE
)
logging.info(f"Number of specs: {scheduler.num_specs}")
logging.info(
f"Number of unique build items: {scheduler.num_unique_build_items}"
)
logging.info(f"num-cpu: {scheduler.num_cpu}")
logging.info(f"num-gpu: {scheduler.num_gpu}")
def _validate_partition(partition: str) -> list[int]:
assert ":" in partition
partition_idx, num_partitions = [int(x) for x in partition.split(":")]
assert num_partitions > 0
assert 0 <= partition_idx < num_partitions, (
"Condition: 0 <= partition_idx < num_partitions"
)
return [partition_idx, num_partitions]
def set_build_opts( # noqa: ANN201
debug_level=None, # noqa: ANN001
optimization_level=None, # noqa: ANN001
use_experimental_kernels=None, # noqa: ANN001
target_accelerator=None, # noqa: ANN001
disable_warnings=None, # noqa: ANN001
):
build_opts = []
if debug_level:
build_opts.extend(["--debug-level", debug_level])
if optimization_level:
build_opts.extend([f"-O{optimization_level}"])
if use_experimental_kernels:
build_opts.extend(["-D", "USE_EXPERIMENTAL_KERNELS=1"])
if target_accelerator:
build_opts.extend(["--target-accelerator", target_accelerator])
if disable_warnings:
build_opts.extend(["--disable-warnings"])
# TODO: add num_threads to CLI
# num_threads_per_build = 1
# build_opts.extend(["--num-threads", num_threads_per_build])
return build_opts
@click.command(
help="Benchmarking toolkit for Mojo kernels", no_args_is_help=True
)
@click.option(
"--filter",
"filter",
help=(
"Define a single filter (should match a valid parameter, can have"
" multiple ones). The filters should of the format '--filter"
" PARAM=VALUE', that is, the subset of parameters that satisfy this"
" condition will be included."
),
multiple=True,
)
@click.option(
"--output",
"-o",
"output_path",
default="output.csv",
help="Path to output file.",
)
@click.option(
"--output-dir",
"output_dir",
default="kbench-output",
help="Path to output directory for all results (default='./kbench-output')",
)
@click.option(
"--build",
"build",
is_flag=True,
default=False,
help="Just build the binary and report the build time.",
)
@click.option(
"--run-only",
"run_only",
is_flag=True,
default=False,
help="Only run, do not build. Cache must exist, -c is implied",
)
@click.option(
"--param",
default=(),
help="Set extra params in the format of 'PARAM:VALUE'. Example: '--param use_vendor_blas:True'",
multiple=True,
)
@click.option(
"--debug-level", default=None, help="The debug level used during the build."
)
@click.option(
"--use-experimental-kernels",
is_flag=True,
default=False,
help="If enabled, then experimental kernels are used.",
)
@click.option(
"-O",
"--optimization-level",
default=None,
help="The optimization level used during the build.",
)
@click.option(
"--target-accelerator",
default=None,
help="Specify the mojo target accelerator. Allowed values for this option:"
+ get_target_accelerator_helpstr(),
)
@click.option(
"--disable-warnings",
is_flag=True,
default=False,
help="Disable mojo build warnings.",
)
@click.option(
"--force",
"-f",
is_flag=True,
default=False,
help="This option will be deprecated soon. See --skip-clock-check instead.",
)
@click.option(
"--skip-clock-check",
is_flag=True,
default=False,
help="Run even if accelerator clocks are not set to maximum.",
)
@click.option(
"--cached",
"-c",
is_flag=True,
default=False,
help="Enable Kbench cache (WARNING: doesn't check for source changes).",
)
@click.option(
"--clear-cache",
"-cc",
is_flag=True,
default=False,
help="Clear Kbench cache.",
)
@click.option(
"--num-cpu",
default=-1,
help="Set the total number of cpu cores for building. Set to -1 for max number of cores (default=-1).",
)
@click.option(
"--num-gpu",
default=1,
help="Set the total number of GPU devices for running, it can only be used with '--target-accelerator' (default=1).",
)
@click.option(
"--mpirun-np",
default=1,
help="Set the total number of GPU devices for running with mpirun, it cannot be combined with '--num-gpus' (default=1)."
"Make sure to call 'Bench.check_mpirun()' in mojo benchmark.",
)
@click.option(
"--dryrun",
"-dryrun",
is_flag=True,
default=False,
help="Do not execute the config, just show the parameters.",
)
@click.option(
"--verbose", "-v", is_flag=True, default=False, help="Verbose printing."
)
@click.option(
"--shapes",
default=(),
help="Set of shapes passed as extra params.",
multiple=True,
)
@click.option(
"--build-opts",
default="",
help="Any build options (treated as str and directly passed to mojo compiler.)",
multiple=False,
)
@click.option(
"--profile",
default=(),
help="Set the profiler [ncu, ncu-single, rocm, rocprof-compute].",
multiple=False,
)
@click.option(
"--exec-prefix",
default="",
help="Any prefix options (treated as str and directly passed before binary.)",
multiple=False,
)
@click.option(
"--exec-suffix",
default="",
help="Any suffix options (treated as str and directly passed after binary.)",
multiple=False,
)
@click.option(
"--timeout-secs",
default=None,
help="Timeout seconds for executing each binary. (default=None)",
multiple=False,
type=click.INT,
)
@click.option(
"--partition",
default="0:1",
help="Formatted as fraction 'm:n', divide the shapes "
"into n partitions and limit the space to m'th partition "
"(default='0:1' running everything). Note that it has no "
"effect on parameter set and is only applied to shapes.",
multiple=False,
type=click.STRING,
)
@click.option(
"--plot",
type=click.Choice(["bars", "table", "summary", "none"]),
default="bars",
help="Terminal visualization: bars (default), table, summary, or none to disable.",
)
@click.argument("files", nargs=-1, type=click.UNPROCESSED)
def cli(
files, # noqa: ANN001
filter, # noqa: ANN001
output_path, # noqa: ANN001
output_dir, # noqa: ANN001
build, # noqa: ANN001
run_only, # noqa: ANN001
param, # noqa: ANN001
debug_level, # noqa: ANN001
use_experimental_kernels, # noqa: ANN001
optimization_level, # noqa: ANN001
target_accelerator, # noqa: ANN001
disable_warnings, # noqa: ANN001
force, # noqa: ANN001
skip_clock_check, # noqa: ANN001
cached, # noqa: ANN001
clear_cache, # noqa: ANN001
num_cpu, # noqa: ANN001
num_gpu, # noqa: ANN001
mpirun_np: int,
dryrun: bool,
verbose, # noqa: ANN001
shapes, # noqa: ANN001
build_opts, # noqa: ANN001
profile, # noqa: ANN001
exec_prefix, # noqa: ANN001
exec_suffix, # noqa: ANN001
timeout_secs: int,
partition: str,
plot: str,
) -> bool:
configure_logging(verbose=verbose)
if not verbose:
sys.tracebacklimit = 1
if force:
logging.warning(
"'--force' option is deprecated and will be removed soon. Please use '--skip-clock-check' instead."
)
skip_clock_check = True
mode = KBENCH_MODE.BUILD_AND_RUN
if run_only:
mode = KBENCH_MODE.RUN
if run_only and clear_cache:
log_and_raise_error(
"Cannot clear cache when in run-only mode. Need cache to run.",
param_hint="'--clear-cache'",
)
if run_only and build_opts:
log_and_raise_error("Cannot provide build options when run-only mode")
partition_idx, num_partitions = _validate_partition(partition)
if build:
mode = KBENCH_MODE.BUILD
obj_cache = KbenchCache()
# check kbench_cache and load it if exists:
if clear_cache and run_only:
log_and_raise_error("Trying to clear cache when run_only")
elif clear_cache:
obj_cache.clear()
if cached or (mode == KBENCH_MODE.RUN):
obj_cache.load()
if len(obj_cache.data) == 0 and mode == KBENCH_MODE.RUN:
log_and_raise_error(
"Run Only requires an active cache object but the object is empty",
param_hint="'--run-only'",
)
if not len(files) and not len(shapes):
logging.info(
"Nothing more to do without parameter or shape YAML provided!"
)
return True
# Resolve YAML file paths from the input user globs
yaml_files = []
for f in files:
yaml_files.append(Path(f).resolve())
if len(yaml_files) == 0:
log_and_raise_error(
"No valid YAML files found from the input globs.",
param_hint="'FILES'",
)
if not skip_clock_check:
check_gpu_clock()
# If `shapes` is not specified, pick an empty Spec and '-o output_path'.
shape_list = list(Spec.load_yaml_list(shapes) if shapes else Spec())
shape_path_list = (
[Path(sh.hash(with_variables=True)) for sh in shape_list]
if shapes
else [Path(output_path)]
)
assert len(shape_path_list) == len(shape_list), (
"Number of shapes doesn't equal number of paths."
)
if target_accelerator and not check_valid_target_accelerator(
target_accelerator
):
log_and_raise_error(
f"Invalid target accelerator '{target_accelerator}'. "
f"Should be one of the following {get_target_accelerator_helpstr()}",
param_hint="'--target-accelerator'",
)
build_opts = build_opts.split(" ") if build_opts else []
build_opts.extend(
set_build_opts(
debug_level,
optimization_level,
use_experimental_kernels,
target_accelerator,
disable_warnings,
)
)
if num_gpu > 1 and not target_accelerator:
raise ValueError(
"Cannot use --num-gpu>1 without specifying --target-accelerator"
)
if mpirun_np > 1 and num_gpu > 1:
raise ValueError(
"Cannot use --num-gpu>1 and --mpirun-np>1 at the same time!"
)
exec_suffix = exec_suffix.split(" ") if exec_suffix else []
exec_prefix = exec_prefix.split(" ") if exec_prefix else []
if mpirun_np > 1:
exec_prefix.extend(["mpirun", "-np", str(mpirun_np)])
shapes_per_partition = math.ceil(len(shape_list) / num_partitions)
shape_idx_lb = partition_idx * shapes_per_partition
shape_idx_ub = min(shape_idx_lb + shapes_per_partition, len(shape_list))
for i in range(shape_idx_lb, shape_idx_ub):
run(
yaml_path_list=yaml_files,
obj_cache=obj_cache,
shape=shape_list[i],
output_path=shape_path_list[i],
mode=mode,
param_list=param,
filter_list=filter,
build_opts=build_opts,
profile=profile,
exec_prefix=exec_prefix,
exec_suffix=exec_suffix,
dryrun=dryrun,
verbose=verbose,
output_dir=output_dir,
num_cpu=num_cpu,
num_gpu=num_gpu,
target_accelerator=target_accelerator,
timeout_secs=timeout_secs,
plot=plot,
)
if obj_cache.is_active:
obj_cache.dump()
logging.info(f"Number of shapes: {len(shape_list)}")
return True
def main() -> None:
try:
cli()
except Exception:
CONSOLE.print_exception(suppress=[click, rich])
if __name__ == "__main__":
if directory := os.environ.get("BUILD_WORKING_DIRECTORY"):
os.chdir(directory)
main()