3131Increase verbosity
3232 python xegpu_kernel_bench.py -l 1 -b 1 -vv
3333
34- Run a single benchmark in debug mode (no timeout, exceptions raised
35- immediately)
36- python xegpu_kernel_bench.py -l 1 -b 1 -vv --debug
37-
3834Dump the kernel at a specific stage of the pipeline, does not execute the
3935benchmark
4036 python xegpu_kernel_bench.py -l 1 -b 1 --dump-kernel bufferized
7369from lighthouse .ingress .torch import gpu_backend , TargetDialect
7470from lighthouse .ingress .torch .compile import TorchMemoryManager
7571from tune_matmul_costmodel import optimize_payload
76- from tune_utils import run_with_timeout
7772from csv_logger import CSVLogger
7873
7974
@@ -246,15 +241,12 @@ def copy_module(module: ir.Module) -> ir.Module:
246241
247242
248243def is_caused_by_pipeline_interrupt (exc : BaseException ) -> bool :
249- """Return True if PipelineInterrupt appears anywhere in the exception chain."""
250244 pending = [exc ]
251245 visited = set ()
252246
253247 while pending :
254248 current = pending .pop ()
255- if current is None :
256- continue
257- if current in visited :
249+ if current is None or current in visited :
258250 continue
259251 visited .add (current )
260252
@@ -335,6 +327,7 @@ def infer_params_and_lower(
335327 payload_func_name : str = "main" ,
336328 stop_at_stage : str | None = None ,
337329 params_cache_json : str | None = "kb_params.json" ,
330+ dump_parameters : bool = True ,
338331 enable_tuning : bool = True ,
339332 verbose : int = 0 ,
340333) -> ir .Module :
@@ -356,6 +349,7 @@ def infer_params_and_lower(
356349 payload_func_name: Name of the payload function.
357350 stop_at_stage: Stage at which to stop the lowering pipeline.
358351 params_cache_json: Path to the JSON file for caching parameters.
352+ dump_parameters: Whether to save the applied parameters as JSON.
359353 enable_tuning: Whether to enable runtime tuning.
360354 verbose: Verbosity level.
361355 Returns:
@@ -377,7 +371,6 @@ def infer_params_and_lower(
377371 print (f"Loading cached parameters from { params_cache_json } " )
378372 schedule_params = ScheduleParameters .from_json (filename = params_cache_json )
379373 else :
380- print (f"Tuning parameters and saving to { params_cache_json } " )
381374 # construct a buffer for kernel result
382375 res_type = kernel_metadata ["inputs" ][- 1 ] # result is last input
383376 shape = res_type .shape
@@ -395,11 +388,14 @@ def infer_params_and_lower(
395388 func_metadata ["write_bytes" ],
396389 )
397390 schedule_params = ScheduleParameters ([configs [0 ][1 ]])
398- schedule_params .to_json (filename = params_cache_json , overwrite = True )
391+ if dump_parameters :
392+ print (f"Saving tuned parameters to { params_cache_json } " )
393+ schedule_params .to_json (filename = params_cache_json , overwrite = True )
399394 else :
400395 print (f"Using default parameters for { schedule_kind } schedule" )
401- print (f"Saving applied parameters to { params_cache_json } " )
402- schedule_params .to_json (filename = params_cache_json , overwrite = True )
396+ if dump_parameters :
397+ print (f"Saving applied parameters to { params_cache_json } " )
398+ schedule_params .to_json (filename = params_cache_json , overwrite = True )
403399
404400 if verbose > 2 :
405401 print ("Payload module before lowering:" )
@@ -493,7 +489,7 @@ def lower_and_execute_benchmark(
493489 verify : bool = True ,
494490 stop_at_stage : str | None = None ,
495491 verbose : int = 0 ,
496- debug : bool = False ,
492+ dump_parameters : bool = True ,
497493) -> dict :
498494 """
499495 High-level function to lower and execute a KernelBench benchmark.
@@ -509,7 +505,7 @@ def lower_and_execute_benchmark(
509505 verify: Whether to verify the result against PyTorch reference.
510506 stop_at_stage: Stage at which to stop the lowering pipeline.
511507 verbose: Verbosity level.
512- debug: Run without timeout and raise exceptions immediately .
508+ dump_parameters: Whether to save applied schedule parameters as JSON .
513509 Returns:
514510 A dictionary containing benchmark performance metrics.
515511 """
@@ -556,6 +552,7 @@ def lower_and_execute_benchmark(
556552 payload_func_name = "main" ,
557553 stop_at_stage = stop_at_stage ,
558554 params_cache_json = f"kb_params_level{ level } -{ id } .json" ,
555+ dump_parameters = dump_parameters ,
559556 enable_tuning = execute ,
560557 verbose = 2 ,
561558 )
@@ -575,8 +572,8 @@ def lower_and_execute_benchmark(
575572 gm , _ = dynamo .export (torch_model )(* torch_inputs )
576573 backend (gm , list (torch_inputs ))
577574 except dynamo .exc .BackendCompilerFailed as e :
578- if debug and not is_caused_by_pipeline_interrupt (e ):
579- raise e
575+ if not is_caused_by_pipeline_interrupt (e ):
576+ raise
580577 return {}
581578
582579 with warnings .catch_warnings ():
@@ -682,18 +679,10 @@ def lower_and_execute_benchmark(
682679 return entry
683680
684681
685- def run_experiment (use_timeout : bool = True , timeout : int = 1200 , ** kwargs ) -> dict :
686- def ctx_wrapper (* args , ** kwargs ) -> dict :
687- with ir .Context () as ctx , ir .Location .unknown ():
688- lh_dialects .register_and_load ()
689- results = lower_and_execute_benchmark (* args , ** kwargs , ctx = ctx )
690- return results
691-
692- if use_timeout :
693- exec_func = partial (ctx_wrapper , ** kwargs )
694- results = run_with_timeout (experiment_func = exec_func , timeout = timeout )
695- else :
696- results = ctx_wrapper (** kwargs )
682+ def run_experiment (** kwargs ) -> dict :
683+ with ir .Context () as ctx , ir .Location .unknown ():
684+ lh_dialects .register_and_load ()
685+ results = lower_and_execute_benchmark (** kwargs , ctx = ctx )
697686 return results
698687
699688
@@ -773,7 +762,7 @@ def parser_cli_args():
773762 "--nruns" ,
774763 type = int ,
775764 default = 500 ,
776- help = "Number of runs for benchmarking (default: 1000 )" ,
765+ help = "Number of runs for benchmarking (default: 500 )" ,
777766 )
778767 parser .add_argument (
779768 "--nwarmup" ,
@@ -782,9 +771,14 @@ def parser_cli_args():
782771 help = "Number of warmup runs (default: 500)" ,
783772 )
784773 parser .add_argument (
785- "--debug" ,
774+ "--dump-parameters" ,
775+ action = "store_true" ,
776+ help = "Store used schedule parameters to disk in JSON format." ,
777+ )
778+ parser .add_argument (
779+ "--dump-csv" ,
786780 action = "store_true" ,
787- help = "Run in single process and raise exceptions immediately ." ,
781+ help = "Dump the benchmarking results to a CSV file ." ,
788782 )
789783 parser .add_argument (
790784 "--verbose" ,
@@ -805,7 +799,7 @@ def parser_cli_args():
805799 kb_pattern = f"level{ kb_level } /*.py"
806800 bench_list = get_benchmarks (kb_pattern , include = benchmarks )
807801
808- if not stop_at_stage :
802+ if args . dump_csv and not stop_at_stage :
809803 csv_file = "out_kernelbench.csv"
810804 csv_logger = CSVLogger (csv_file , echo_stdout = False , verbose = True )
811805 else :
@@ -839,8 +833,7 @@ def parser_cli_args():
839833 nwarmup = args .nwarmup ,
840834 verbose = args .verbose ,
841835 stop_at_stage = stop_at_stage ,
842- use_timeout = not args .debug ,
843- debug = args .debug ,
836+ dump_parameters = args .dump_parameters ,
844837 )
845838 if stop_at_stage :
846839 continue
@@ -856,10 +849,9 @@ def parser_cli_args():
856849 entry ["executed" ] = 1 if err_msg == "" else 0
857850 entry ["error" ] = err_msg
858851 except Exception as e :
859- if args .debug :
860- raise e
861852 print (f"Benchmark { short_path } failed with error: { e } " , flush = True )
862853 entry ["error" ] = str (e )
854+ raise e
863855
864856 # Store intermediate results
865857 if csv_logger is not None :
0 commit comments