Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6531c3b
lowering_common: add gpu-outlining pipeline stage + updates
tkarna Sep 21, 2026
0e1b97f
transform: add transform.alternatives op wrapper
tkarna Oct 1, 2026
2c4906e
smt_ext: fix constrain_params interface registration
tkarna Oct 5, 2026
6e365ff
transform_ext: add matmul_analysis and xegpu_param_selector helpers
tkarna Sep 24, 2026
03ae8df
transform_ext: add infer_xegpu_gemm_params transform op
tkarna Sep 24, 2026
79c781a
transform_ext: add get_param_dict_entry transform op
tkarna Sep 24, 2026
b88dc2f
transform_ext: extract_handle has a silenceable bool arg
tkarna Oct 1, 2026
512a519
transform_ext: add emit_definite_failure op
tkarna Oct 1, 2026
f1c0601
transform_ext: add compute_num_threads op
tkarna Oct 1, 2026
8714c6d
transform_ext: add compute_sg_layout op
tkarna Oct 1, 2026
3ea2b12
transform_ext: add infer_xegpu_reduction_params
tkarna Oct 1, 2026
e09c9a5
matmul analysis: add method to infer wg and k tile sizes from IR
tkarna Sep 25, 2026
57dcacc
matmul_analysis: add tests
tkarna Sep 25, 2026
9f1e7a4
XeGPUParameterSelector: support fixed wg, sg and k tile sizes
tkarna Sep 25, 2026
6461582
infer_xegpu_gemm_params: add force wg/sg/k tile arguments
tkarna Sep 25, 2026
0913e2a
replace_with_fused_attention: tile_size can be a transform param
tkarna Oct 1, 2026
5d55b73
lowering_common: vectorize: hoist after cleanup
tkarna Oct 1, 2026
2b53fda
transform_ext: add infer_xegpu_attention_params op
tkarna Oct 1, 2026
42b3f59
xegpu: add outline_gpu_func schedule
tkarna Sep 21, 2026
0f4ec1a
xegpu: add vectorize schedule
tkarna Sep 24, 2026
8e2a325
xegpu: add bufferize schedule
tkarna Sep 24, 2026
5a64e87
xegpu: add vector_to_xegpu schedule
tkarna Sep 24, 2026
60c48cf
xegpu: add annotate_layouts schedule
tkarna Sep 24, 2026
aa04f81
xegpu: add cleanup schedule
tkarna Oct 1, 2026
37cf38a
xegpu: add wg_tiling schedule
tkarna Oct 1, 2026
eaa40f3
fix llama3 schedule
tkarna Oct 2, 2026
5628489
inspect_payload: stop tracing producers at matmul op
tkarna Oct 5, 2026
f634cea
kernel_bench: use new pipeline via use-gpu-pipeline CLI arg
tkarna Oct 1, 2026
2c64eb0
address comments
tkarna Oct 6, 2026
c0327ac
add _schedule suffix to all schedules
tkarna Oct 6, 2026
0caceac
matmul_analysis: add guardrails and tests
tkarna Oct 6, 2026
efe4f9b
update matmul_analysis and tests
tkarna Oct 7, 2026
4d68ae9
address comments
tkarna Oct 7, 2026
6603d55
address more comments
tkarna Oct 7, 2026
017c07b
matmul_analysis: more fixes and tests
tkarna Oct 7, 2026
7acf84d
add transform op tests + update docstrings
tkarna Oct 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 71 additions & 26 deletions examples/xegpu/kernel_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
from lighthouse.pipeline.helper import PipelineInterrupt
from lighthouse.ingress.torch import gpu_backend, TargetDialect
from lighthouse.ingress.torch.compile import TorchMemoryManager
from lighthouse.schedule import xegpu
from tune_matmul_costmodel import optimize_payload
from csv_logger import CSVLogger

Expand Down Expand Up @@ -418,6 +419,7 @@ def infer_params_and_lower(
params_cache_json: str | None = "kb_params.json",
dump_parameters: bool = True,
enable_tuning: bool = True,
use_gpu_pipeline: bool = False,
verbose: int = 0,
) -> ir.Module:
"""
Expand Down Expand Up @@ -509,6 +511,7 @@ def infer_params_and_lower(
stop_at_stage=stop_at_stage,
benchmark=benchmark,
payload_func_name=payload_func_name,
use_gpu_pipeline=use_gpu_pipeline,
)

if stop_at_stage:
Expand All @@ -521,6 +524,34 @@ def infer_params_and_lower(
return lowered_mod


def get_xegpu_pipeline(stop_at_stage):
schedules = []
if stop_at_stage == "initial":
return schedules
schedules.append(xegpu.cleanup())
if stop_at_stage == "cleanup":
return schedules
schedules.append(xegpu.wg_tiling())
if stop_at_stage == "tiled":
return schedules
schedules.append(xegpu.vectorize())
if stop_at_stage == "vectorized":
return schedules
schedules.append(xegpu.bufferize())
if stop_at_stage == "bufferized":
return schedules
schedules.append(xegpu.outline_gpu_func())
if stop_at_stage == "gpu-outlining":
return schedules
schedules.append(xegpu.vector_to_xegpu())
if stop_at_stage == "xegpu-initial":
return schedules
schedules.append(xegpu.annotate_layouts())
if stop_at_stage == "xegpu-wg":
return schedules
return schedules


def lower_to_llvm(
mod: ir.Module,
schedule_kind: str,
Expand All @@ -529,41 +560,46 @@ def lower_to_llvm(
stop_at_stage: str | None,
benchmark: bool,
payload_func_name: str,
use_gpu_pipeline: bool = False,
) -> ir.Module:
"""Lower payload module to LLVM using the specified schedule and parameters."""
Runner.make_function_callable(mod, payload_func_name)
if schedule_kind == "mlp":
schedule = mlp_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
device=device,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "elemwise":
schedule = elemwise_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "reduction":
schedule = reduction_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "attention":
schedule = fused_attention_schedule(
params=schedule_params,
stop_at_stage=stop_at_stage,
)
if use_gpu_pipeline:
pipeline = get_xegpu_pipeline(stop_at_stage)
Comment thread
tkarna marked this conversation as resolved.
Resolved
Comment thread
tkarna marked this conversation as resolved.
Resolved
else:
raise ValueError(f"Unsupported schedule kind: {schedule_kind}")
if schedule_kind == "mlp":
schedule = mlp_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
device=device,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "elemwise":
schedule = elemwise_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "reduction":
schedule = reduction_schedule(
params=schedule_params,
payload_func_name=payload_func_name,
stop_at_stage=stop_at_stage,
)
elif schedule_kind == "attention":
schedule = fused_attention_schedule(
params=schedule_params,
stop_at_stage=stop_at_stage,
)
else:
raise ValueError(f"Unsupported schedule kind: {schedule_kind}")
pipeline = [schedule]

# define lowering pipeline
schedules = []
if benchmark:
schedules.append(Runner.get_bench_wrapper_schedule(payload_func_name))
schedules.append(schedule)
schedules.extend(pipeline)
if not stop_at_stage or stop_at_stage == "final":
schedules.append(xegpu_to_binary())
driver = TransformDriver(schedules=schedules)
Expand All @@ -586,6 +622,7 @@ def lower_and_execute_benchmark(
stop_at_stage: str | None = None,
verbose: int = 0,
dump_parameters: bool = True,
use_gpu_pipeline: bool = False,
) -> dict:
"""
High-level function to lower and execute a KernelBench benchmark.
Expand Down Expand Up @@ -668,6 +705,7 @@ def lower_and_execute_benchmark(
params_cache_json=f"kb_params_level{level}-{id}.json",
dump_parameters=dump_parameters,
enable_tuning=execute,
use_gpu_pipeline=use_gpu_pipeline,
verbose=verbose,
)
backend = gpu_backend(
Expand Down Expand Up @@ -891,6 +929,7 @@ def parser_cli_args():
choices=[
"imported",
"initial",
"cleanup",
"tiled",
"vectorized",
"bufferized",
Expand All @@ -900,6 +939,11 @@ def parser_cli_args():
"final",
],
)
parser.add_argument(
"--use-gpu-pipeline",
action="store_true",
help="Use the parameter-free GPU pipeline for execution.",
)
parser.add_argument(
"--nruns",
type=int,
Expand Down Expand Up @@ -984,6 +1028,7 @@ def parser_cli_args():
verbose=args.verbose,
stop_at_stage=stop_at_stage,
dump_parameters=args.dump_parameters,
use_gpu_pipeline=args.use_gpu_pipeline,
)
if stop_at_stage:
continue
Expand Down
2 changes: 1 addition & 1 deletion examples/xegpu/llama3_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _bundle(
raise PipelineInterrupt()

# Shared with the per-op xegpu schedules: forall -> scf.parallel -> gpu.launch.
func = convert_to_gpu_launch(mod, "payload")
func = convert_to_gpu_launch(mod, payload_func_name="payload")

# launch threads per kernel, in IR (build) order = `kinds`.
launches = match_and_split(mod, ops={"gpu.launch"}, nhandles=nkernels)
Expand Down
16 changes: 8 additions & 8 deletions lighthouse/dialects/transform/smt_ext/ops/constrain_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def body(self):

@classmethod
def attach_interface_impls(cls, ctx=None):
if not hasattr(cls, "_interfaces_attached"):
cls.ConstrainParamsTransformOpInterfaceModel.attach(
cls.OPERATION_NAME, context=ctx
)
cls.ConstrainParamsMemoryEffectsOpInterfaceModel.attach(
cls.OPERATION_NAME, context=ctx
)
setattr(cls, "_interfaces_attached", True)
# Interfaces are context-bound, so (re)attach for every context; a
# process-global guard would leave later contexts without the interface.
cls.ConstrainParamsTransformOpInterfaceModel.attach(
cls.OPERATION_NAME, context=ctx
)
cls.ConstrainParamsMemoryEffectsOpInterfaceModel.attach(
cls.OPERATION_NAME, context=ctx
)

class ConstrainParamsTransformOpInterfaceModel(transform.TransformOpInterface):
"""TransformOpInterface impl for evaluating the SMT constraints and producing new params."""
Expand Down
15 changes: 14 additions & 1 deletion lighthouse/dialects/transform/transform_ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
from .ops.wrap_in_benching_func import wrap_in_benching_func
from .ops.assign_tile_sizes import assign_tile_sizes
from .ops.get_named_attribute import get_named_attribute
from .ops.get_param_dict_entry import get_param_dict_entry
from .ops.get_tile_sizes import get_tile_sizes
from .ops.param_cmp_eq import param_cmp_eq
from .ops.replace import replace
from .ops.convert_func_results_to_args import convert_func_results_to_args
from .ops.emit_definite_failure import emit_definite_failure
from .ops.extract_handle import extract_handle
from .ops.get_tileable_consumers import get_tileable_consumers
from .ops.get_tiling_sizes import get_tiling_sizes
from .ops.infer_xegpu_gemm_params import infer_xegpu_gemm_params
from .ops.infer_xegpu_attention_params import infer_xegpu_attention_params
from .ops.infer_xegpu_reduction_params import infer_xegpu_reduction_params
from .ops.trace_producers import trace_producers
from .ops.reverse_handles import reverse_handles
from .ops.update_address_space import update_address_space
Expand All @@ -24,6 +29,8 @@
from .ops.get_leading_unit_tile_sizes import get_leading_unit_tile_sizes
from .ops.move_offsets_to_subview import move_offsets_to_subview
from .ops.clear_tile_and_fuse_annotations import clear_tile_and_fuse_annotations
from .ops.compute_num_threads import compute_num_threads
from .ops.compute_sg_layout import compute_sg_layout
from .ops.get_fusion_roots import get_fusion_roots
from .ops.propagate_tile_sizes import propagate_tile_sizes
from .ops.sfc_remap_forall import sfc_remap_forall
Expand All @@ -36,7 +43,10 @@
"TransformExtensionDialect",
"assign_tile_sizes",
"clear_tile_and_fuse_annotations",
"compute_num_threads",
"compute_sg_layout",
"convert_func_results_to_args",
"emit_definite_failure",
"extract_handle",
"filter_by_name",
"filter_contraction_ops",
Expand All @@ -47,10 +57,13 @@
"get_fusion_roots",
"get_leading_unit_tile_sizes",
"get_named_attribute",
"get_named_attribute",
"get_param_dict_entry",
"get_tile_sizes",
"get_tileable_consumers",
"get_tiling_sizes",
"infer_xegpu_attention_params",
"infer_xegpu_gemm_params",
"infer_xegpu_reduction_params",
"move_offsets_to_subview",
"param_cmp_eq",
"propagate_tile_sizes",
Expand Down
118 changes: 118 additions & 0 deletions lighthouse/dialects/transform/transform_ext/ops/compute_num_threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from collections.abc import Sequence

from mlir import ir
from mlir.dialects import ext, transform
from mlir.dialects.transform import DiagnosedSilenceableFailure

from lighthouse.dialects.transform.transform_ext import TransformExtensionDialect


class ComputeNumThreadsOp(
TransformExtensionDialect.Operation, name="compute_num_threads"
):
"""
Compute the workgroup thread count from wg and sg tile params.

Returns `base * prod_i(wg_i // sg_i)` over the tile dimensions, treating an
Comment thread
tkarna marked this conversation as resolved.
Resolved
untiled (0) dim as 1 to avoid division by zero.

The wg and sg tiles are each passed as one or more param operands via the
single `tiles` operand list; `num_wg_handles` marks how many leading
operands make up the wg tile (the rest make up the sg tile). Each operand
may itself carry several i64 attrs, so the tiles may be supplied either as
one param-per-dim handle or as a single handle holding all dims. All params
are read at apply time, so the tile rank may vary with the payload.

Args:
tiles: wg tile param operand(s) followed by sg tile param operand(s).
num_wg_handles: Number of leading `tiles` operands forming the wg tile.
base: Multiplier applied to the subgroup count (e.g. the subgroup size).
Return:
Param holding the thread count as a single i64.
"""

tiles: Sequence[ext.Operand[transform.AnyParamType]]
num_wg_handles: ir.IntegerAttr
base: ir.IntegerAttr = ext.attribute(
default_factory=lambda: ir.IntegerAttr.get(ir.IntegerType.get_signless(64), 1)
)
num_threads: ext.Result[transform.AnyParamType[()]] = ext.infer_result()

@classmethod
def attach_interface_impls(cls, ctx=None):
cls.TransformOpInterfaceModel.attach(cls.OPERATION_NAME, context=ctx)
cls.MemoryEffectsOpInterfaceModel.attach(cls.OPERATION_NAME, context=ctx)

class TransformOpInterfaceModel(transform.TransformOpInterface):
@staticmethod
def apply(
op: "ComputeNumThreadsOp",
_rewriter: transform.TransformRewriter,
results: transform.TransformResults,
state: transform.TransformState,
) -> DiagnosedSilenceableFailure:
operands = list(op.tiles)
num_wg = op.num_wg_handles.value
wg = [
ir.IntegerAttr(a).value
for handle in operands[:num_wg]
for a in state.get_params(handle)
]
sg = [
ir.IntegerAttr(a).value
for handle in operands[num_wg:]
for a in state.get_params(handle)
]
if len(wg) != len(sg):
return DiagnosedSilenceableFailure.SilenceableFailure

count = op.base.value
for w, s in zip(wg, sg):
# Treat an untiled (0) dim as 1 to avoid division by zero.
count *= (w or 1) // (s or 1)
Comment thread
tkarna marked this conversation as resolved.
Resolved

i64 = ir.IntegerType.get_signless(64)
results.set_params(op.num_threads, [ir.IntegerAttr.get(i64, count)])
return DiagnosedSilenceableFailure.Success

@staticmethod
def allow_repeated_handle_operands(_op: "ComputeNumThreadsOp") -> bool:
return False

class MemoryEffectsOpInterfaceModel(ir.MemoryEffectsOpInterface):
@staticmethod
def get_effects(op: ir.Operation):
return (
transform.only_reads_handle(op.op_operands)
+ transform.produces_handle(op.results)
+ transform.only_reads_payload()
)


def compute_num_threads(
wg_tile: ir.Value[transform.AnyParamType] | Sequence[ir.Value],
sg_tile: ir.Value[transform.AnyParamType] | Sequence[ir.Value],
base: int | ir.IntegerAttr = 1,
) -> ir.Value[transform.AnyParamType]:
"""
snake_case wrapper to create a ComputeNumThreadsOp.

Args:
wg_tile: Param(s) holding the workgroup tile sizes. Either a single
handle with one i64 per dim, or a list of per-dim handles.
sg_tile: Param(s) holding the subgroup tile sizes, in the same layout as
`wg_tile`.
base: Multiplier applied to the subgroup count (e.g. the subgroup size).
Return:
Param holding the thread count as a single i64.
"""
i64 = ir.IntegerType.get_signless(64)
wg_handles = list(wg_tile) if isinstance(wg_tile, (list, tuple)) else [wg_tile]
sg_handles = list(sg_tile) if isinstance(sg_tile, (list, tuple)) else [sg_tile]
if not isinstance(base, ir.IntegerAttr):
base = ir.IntegerAttr.get(i64, base)
return ComputeNumThreadsOp(
tiles=wg_handles + sg_handles,
num_wg_handles=ir.IntegerAttr.get(i64, len(wg_handles)),
base=base,
).num_threads
Loading
Loading