Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 3 additions & 55 deletions examples/xegpu/fused_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,6 @@
from lighthouse.schedule import xegpu


def get_xegpu_pipeline(parameters: dict, stop_at_stage: str) -> list:
wg_tile = parameters.get("wg_tile", None)
sg_tile = parameters.get("sg_tile", None)
reduction_tile = parameters.get("reduction_tile", None)
nb_prefetch = parameters.get("nb_prefetch", None)
prefetch_tile = parameters.get("prefetch_tile", None)
q_load_tile = parameters.get("q_load_tile", None)
v_load_tile = parameters.get("v_load_tile", None)
schedules = []
if stop_at_stage == "initial":
return schedules
schedules.append(xegpu.cleanup_schedule())
if stop_at_stage == "cleanup":
return schedules
schedules.append(
xegpu.wg_tiling_schedule(
wg_tile=wg_tile,
reduction_tile=reduction_tile,
)
)
if stop_at_stage == "tiled":
return schedules
schedules.append(xegpu.vectorize_schedule())
if stop_at_stage == "vectorized":
return schedules
schedules.append(xegpu.bufferize_schedule())
if stop_at_stage == "bufferized":
return schedules
schedules.append(xegpu.outline_gpu_func_schedule(sg_tile=sg_tile))
if stop_at_stage == "gpu-outlining":
return schedules
schedules.append(xegpu.vector_to_xegpu_schedule())
if stop_at_stage == "xegpu-initial":
return schedules
schedules.append(
xegpu.annotate_layouts_schedule(
sg_tile=sg_tile,
nb_prefetch=nb_prefetch,
prefetch_tile=prefetch_tile,
q_load_tile=q_load_tile,
v_load_tile=v_load_tile,
)
)
if stop_at_stage == "xegpu-wg":
return schedules
return schedules


def fused_attention_complexity(
batch_size: int, n_head: int, n_ctx: int, d_head: int, nbytes: int
):
Expand Down Expand Up @@ -237,14 +189,10 @@ def schedule_modules(
schedules = []
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))
schedules.extend(
get_xegpu_pipeline(parameters=parameters[0], stop_at_stage=stop_at_stage)
xegpu.get_xegpu_pipeline(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this also use the reduction schedule? is then worth updating the PR desc (it does not mention about fused attention?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, previously fused_attention defined its own get_xegpu_pipeline function. In this PR it's updated to use the common one defined in xegpu_pipeline.py. There no change in the attention lowering.

parameters=parameters[0], stop_at_stage=stop_at_stage
)
)

if stop_at_stage and stop_at_stage != "final":
return schedules

schedules.append(xegpu.xegpu_to_binary())

return schedules

def shared_libs(self) -> list[str]:
Expand Down
44 changes: 6 additions & 38 deletions examples/xegpu/kernel_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@
from lighthouse.schedule.xegpu import (
mlp_schedule,
elemwise_schedule,
xegpu_to_binary,
reduction_schedule,
)
from lighthouse.pipeline.helper import PipelineInterrupt
from lighthouse.ingress.torch import gpu_backend, TargetDialect
Expand Down Expand Up @@ -545,34 +543,6 @@ 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_schedule())
if stop_at_stage == "cleanup":
return schedules
schedules.append(xegpu.wg_tiling_schedule())
if stop_at_stage == "tiled":
return schedules
schedules.append(xegpu.vectorize_schedule())
if stop_at_stage == "vectorized":
return schedules
schedules.append(xegpu.bufferize_schedule())
if stop_at_stage == "bufferized":
return schedules
schedules.append(xegpu.outline_gpu_func_schedule())
if stop_at_stage == "gpu-outlining":
return schedules
schedules.append(xegpu.vector_to_xegpu_schedule())
if stop_at_stage == "xegpu-initial":
return schedules
schedules.append(xegpu.annotate_layouts_schedule())
if stop_at_stage == "xegpu-wg":
return schedules
return schedules


def lower_to_llvm(
mod: ir.Module,
schedule_kind: str,
Expand All @@ -586,7 +556,9 @@ def lower_to_llvm(
"""Lower payload module to LLVM using the specified schedule and parameters."""
Runner.make_function_callable(mod, payload_func_name)
if use_gpu_pipeline:
pipeline = get_xegpu_pipeline(stop_at_stage)
pipeline = xegpu.get_xegpu_pipeline(
parameters=schedule_params[0], stop_at_stage=stop_at_stage
)
else:
if schedule_kind == "mlp":
schedule = mlp_schedule(
Expand All @@ -602,24 +574,20 @@ def lower_to_llvm(
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,
)
raise NotImplementedError("Reduction kernels require --use-gpu-pipeline.")
elif schedule_kind == "attention":
raise NotImplementedError("Attention kernels require --use-gpu-pipeline.")
else:
raise ValueError(f"Unsupported schedule kind: {schedule_kind}")
pipeline = [schedule]
if not stop_at_stage or stop_at_stage == "final":
pipeline.append(xegpu.xegpu_to_binary())

# define lowering pipeline
schedules = []
if benchmark:
schedules.append(Runner.get_bench_wrapper_schedule(payload_func_name))
schedules.extend(pipeline)
if not stop_at_stage or stop_at_stage == "final":
schedules.append(xegpu_to_binary())
if not schedules:
return mod

Expand Down
27 changes: 6 additions & 21 deletions examples/xegpu/layer_norm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: %PYTHON %s --dump-kernel=xegpu-wg | FileCheck %s
# RUN: %PYTHON %s --wg-rows 32 --sg-rows 16 --reduction-step-size 32 --dump-kernel=xegpu-wg | FileCheck %s
# CHECK: module attributes {gpu.container_module} {

"""
Expand All @@ -21,7 +22,7 @@
generate_gpu_layer_norm_payload,
)
from lighthouse.schedule.parameters import ScheduleParameters
from lighthouse.schedule.xegpu import reduction_schedule, xegpu_to_binary
from lighthouse.schedule import xegpu


def layer_norm_complexity(M: int, N: int, nbytes: int):
Expand Down Expand Up @@ -142,20 +143,13 @@ def schedule_modules(
"""Generate transform schedule for layer_norm."""
schedules = []
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))

schedules.append(
reduction_schedule(
payload_func_name=self.payload_function_name,
schedules.extend(
xegpu.get_xegpu_pipeline(
parameters=parameters[0],
stop_at_stage=stop_at_stage,
params=parameters,
disable_multi_reduction_to_contract=True,
)
)

if stop_at_stage and stop_at_stage != "final":
return schedules

schedules.append(xegpu_to_binary())

return schedules

def shared_libs(self) -> list[str]:
Expand Down Expand Up @@ -186,12 +180,6 @@ def parse_cli():
default=8,
help="Number of rows per subgroup.",
)
parser.add_argument(
"--subgroup-size",
type=int,
default=16,
help="Subgroup size.",
)
parser.add_argument(
"--reduction-step-size",
type=int,
Expand Down Expand Up @@ -260,11 +248,9 @@ def parse_cli():
[
{
"layer_kind": "reduction",
"sizes": args.sizes,
"wg_tile": [args.wg_rows, 0],
"sg_tile": [args.sg_rows, 0],
"reduction_tile": [0, args.reduction_step_size],
"subgroup_size": args.subgroup_size,
}
]
)
Expand Down Expand Up @@ -340,7 +326,6 @@ def list2str(a):
f"dt={dtype} "
f"wg-rows={args.wg_rows} "
f"sg-rows={args.sg_rows} "
f"subgroup-size={args.subgroup_size} "
f"time(us): {elapsed:.2f} "
f"GFLOPS: {gflops:.2f} "
)
2 changes: 1 addition & 1 deletion examples/xegpu/nanoGPT_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
`_tile_one_matmul` / `_tile_one_layernorm` / `_tile_one_fused_attention_region`
helpers.

We can't reuse the repo's per-op schedules (reduction_schedule, mlp_schedule)
We can't reuse the repo's per-op schedules (mlp_schedule)
directly, because each assumes the module contains only its op.
The nanoGPT module is mixed (matmul + layernorm + softmax + elementwise), so we
build one combined schedule that handles all op classes. The strategy:
Expand Down
26 changes: 5 additions & 21 deletions examples/xegpu/softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from lighthouse.ingress.mlir_gen import get_mlir_elem_type
from lighthouse.ingress.mlir_gen.gpu_softmax_payload import generate_gpu_softmax_payload
from lighthouse.schedule.parameters import ScheduleParameters
from lighthouse.schedule.xegpu import reduction_schedule, xegpu_to_binary
from lighthouse.schedule import xegpu


def softmax_complexity(M: int, N: int, nbytes: int):
Expand Down Expand Up @@ -138,20 +138,13 @@ def schedule_modules(
"""Generate transform schedule for softmax."""
schedules = []
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))

schedules.append(
reduction_schedule(
payload_func_name=self.payload_function_name,
schedules.extend(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity, how do we fully automate things like layout decisions number of prefetches etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. I'm hoping we can implement heuristics for setting valid prefetch tile size configs (e.g., if we know the shape of the tile to be fetched and have (constraints for) thread layout, it's doable). This already exists for gemm kernels. But to get best performance I think we'll have run autotuner (also already exists for gemms).

xegpu.get_xegpu_pipeline(
parameters=parameters[0],
stop_at_stage=stop_at_stage,
params=parameters,
disable_multi_reduction_to_contract=True,
)
)

if stop_at_stage and stop_at_stage != "final":
return schedules

schedules.append(xegpu_to_binary())

return schedules

def shared_libs(self) -> list[str]:
Expand Down Expand Up @@ -182,12 +175,6 @@ def parse_cli():
default=8,
help="Number of rows per subgroup.",
)
parser.add_argument(
"--subgroup-size",
type=int,
default=16,
help="Subgroup size.",
)
parser.add_argument(
"--reduction-step-size",
type=int,
Expand Down Expand Up @@ -250,11 +237,9 @@ def parse_cli():
[
{
"layer_kind": "reduction",
"sizes": args.sizes,
"wg_tile": [args.wg_rows, 0],
"sg_tile": [args.sg_rows, 0],
"reduction_tile": [0, args.reduction_step_size],
"subgroup_size": args.subgroup_size,
}
]
)
Expand Down Expand Up @@ -329,7 +314,6 @@ def list2str(a):
f"dt={dtype} "
f"wg-rows={args.wg_rows} "
f"sg-rows={args.sg_rows} "
f"subgroup-size={args.subgroup_size} "
f"time(us): {elapsed:.2f} "
f"GFLOPS: {gflops:.2f} "
)
Loading
Loading