Skip to content

Commit af79c27

Browse files
committed
Merge branch 'main' into test_linalg_reduction_fusion_v3
2 parents 9b52059 + c2787c9 commit af79c27

32 files changed

Lines changed: 494 additions & 387 deletions

‎examples/xegpu/kernel_bench.py‎

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
Increase 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-
3834
Dump the kernel at a specific stage of the pipeline, does not execute the
3935
benchmark
4036
python xegpu_kernel_bench.py -l 1 -b 1 --dump-kernel bufferized
@@ -73,7 +69,6 @@
7369
from lighthouse.ingress.torch import gpu_backend, TargetDialect
7470
from lighthouse.ingress.torch.compile import TorchMemoryManager
7571
from tune_matmul_costmodel import optimize_payload
76-
from tune_utils import run_with_timeout
7772
from csv_logger import CSVLogger
7873

7974

@@ -246,15 +241,12 @@ def copy_module(module: ir.Module) -> ir.Module:
246241

247242

248243
def 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:

‎examples/xegpu/torch_matmul.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from lighthouse import dialects as lh_dialects
1818
from lighthouse import schedule as lh_schedule
19-
from lighthouse.schedule.parameters import ScheduleParams
19+
from lighthouse.schedule.parameters import ScheduleParameters
2020
from lighthouse.pipeline.driver import TransformDriver
2121
from lighthouse.utils.mlir import get_mlir_library_path
2222
from lighthouse.execution.runner import Runner
@@ -49,7 +49,7 @@ def shared_libs() -> list[str]:
4949

5050

5151
def schedule_modules(
52-
parameters: ScheduleParams | None = None,
52+
parameters: ScheduleParameters | None = None,
5353
device: str | None = None,
5454
benchmark: bool = False,
5555
entry_func: str = "main",
@@ -82,7 +82,7 @@ def schedule_modules(
8282

8383
def lower_to_llvm(
8484
module: ir.Module,
85-
parameters: ScheduleParams | None = None,
85+
parameters: ScheduleParameters | None = None,
8686
device: str | None = None,
8787
benchmark: bool = False,
8888
entry_func: str = "main",
@@ -216,7 +216,7 @@ def parse_cli_args(description):
216216
# Problem size
217217
m, n, k = args.sizes if args.sizes else (4096, 4096, 4096)
218218
# Set required parameters
219-
params = ScheduleParams(
219+
params = ScheduleParameters(
220220
[
221221
{
222222
"layer_kind": "matmul",

‎lighthouse/dialects/transform/smt_ext/ops/constrain_params.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ def allow_repeated_handle_operands(_op: "ConstrainParamsOp") -> bool:
7878

7979
class ConstrainParamsMemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
8080
@staticmethod
81-
def get_effects(op: "ConstrainParamsOp", effects):
81+
def get_effects(op: "ConstrainParamsOp"):
82+
effects = []
8283
if op.op_operands:
83-
transform.only_reads_handle(op.op_operands, effects)
84-
transform.produces_handle(op.results, effects)
85-
transform.only_reads_payload(effects)
84+
effects += transform.only_reads_handle(op.op_operands)
85+
effects += transform.produces_handle(op.results)
86+
effects += transform.only_reads_payload()
87+
return effects
8688

8789

8890
class MixedResultConstrainParamsOp(ConstrainParamsOp):

‎lighthouse/dialects/transform/transform_ext/ops/assign_tile_sizes.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ def allow_repeated_handle_operands(_op: "AssignTileSizesOp") -> bool:
7373

7474
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
7575
@staticmethod
76-
def get_effects(op: ir.Operation, effects):
77-
transform.only_reads_handle(op.op_operands, effects)
78-
transform.produces_handle(op.results, effects)
79-
transform.modifies_payload(effects)
76+
def get_effects(op: ir.Operation):
77+
return (
78+
transform.only_reads_handle(op.op_operands)
79+
+ transform.produces_handle(op.results)
80+
+ transform.modifies_payload()
81+
)
8082

8183

8284
def assign_tile_sizes(

‎lighthouse/dialects/transform/transform_ext/ops/clear_tile_and_fuse_annotations.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ def allow_repeated_handle_operands(
6666

6767
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
6868
@staticmethod
69-
def get_effects(op: ir.Operation, effects):
70-
transform.only_reads_handle(op.op_operands, effects)
71-
transform.produces_handle(op.results, effects)
72-
transform.modifies_payload(effects)
69+
def get_effects(op: ir.Operation):
70+
return (
71+
transform.only_reads_handle(op.op_operands)
72+
+ transform.produces_handle(op.results)
73+
+ transform.modifies_payload()
74+
)
7375

7476

7577
def clear_tile_and_fuse_annotations(

‎lighthouse/dialects/transform/transform_ext/ops/convert_func_results_to_args.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ def allow_repeated_handle_operands(_op: "ConvertFuncResultsToArgsOp") -> bool:
128128

129129
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
130130
@staticmethod
131-
def get_effects(op: "ConvertFuncResultsToArgsOp", effects):
132-
transform.consumes_handle(op.op_operands, effects)
133-
transform.produces_handle(op.results, effects)
134-
transform.modifies_payload(effects)
131+
def get_effects(op: "ConvertFuncResultsToArgsOp"):
132+
return (
133+
transform.consumes_handle(op.op_operands)
134+
+ transform.produces_handle(op.results)
135+
+ transform.modifies_payload()
136+
)
135137

136138

137139
def convert_func_results_to_args(

‎lighthouse/dialects/transform/transform_ext/ops/extract_handle.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ def allow_repeated_handle_operands(_op: "ExtractHandleOp") -> bool:
5555

5656
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
5757
@staticmethod
58-
def get_effects(op: ir.Operation, effects):
59-
transform.only_reads_handle(op.op_operands, effects)
60-
transform.produces_handle(op.results, effects)
61-
transform.only_reads_payload(effects)
58+
def get_effects(op: ir.Operation):
59+
return (
60+
transform.only_reads_handle(op.op_operands)
61+
+ transform.produces_handle(op.results)
62+
+ transform.only_reads_payload()
63+
)
6264

6365

6466
def extract_handle(

‎lighthouse/dialects/transform/transform_ext/ops/fold_singleton_extract_slice.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,12 @@ def allow_repeated_handle_operands(_op: "FoldSingletonExtractSliceOp") -> bool:
184184

185185
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
186186
@staticmethod
187-
def get_effects(op: ir.Operation, effects):
188-
transform.only_reads_handle(op.op_operands, effects)
189-
transform.produces_handle(op.results, effects)
190-
transform.modifies_payload(effects)
187+
def get_effects(op: ir.Operation):
188+
return (
189+
transform.only_reads_handle(op.op_operands)
190+
+ transform.produces_handle(op.results)
191+
+ transform.modifies_payload()
192+
)
191193

192194

193195
def fold_singleton_extract_slice(

‎lighthouse/dialects/transform/transform_ext/ops/get_fusion_roots.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ def allow_repeated_handle_operands(_op: "GetFusionRootsOp") -> bool:
149149

150150
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
151151
@staticmethod
152-
def get_effects(op: ir.Operation, effects):
153-
transform.only_reads_handle(op.op_operands, effects)
154-
transform.produces_handle(op.results, effects)
155-
transform.only_reads_payload(effects)
152+
def get_effects(op: ir.Operation):
153+
return (
154+
transform.only_reads_handle(op.op_operands)
155+
+ transform.produces_handle(op.results)
156+
+ transform.only_reads_payload()
157+
)
156158

157159

158160
def get_fusion_roots(

‎lighthouse/dialects/transform/transform_ext/ops/get_leading_unit_tile_sizes.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ def allow_repeated_handle_operands(_op: "GetLeadingUnitTileSizesOp") -> bool:
5757

5858
class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
5959
@staticmethod
60-
def get_effects(op: ir.Operation, effects):
61-
transform.only_reads_handle(op.op_operands, effects)
62-
transform.produces_handle(op.results, effects)
63-
transform.only_reads_payload(effects)
60+
def get_effects(op: ir.Operation):
61+
return (
62+
transform.only_reads_handle(op.op_operands)
63+
+ transform.produces_handle(op.results)
64+
+ transform.only_reads_payload()
65+
)
6466

6567

6668
def get_leading_unit_tile_sizes(

0 commit comments

Comments
 (0)