Skip to content

Commit 739f7ff

Browse files
authored
[xegpu] Remove obsolete fused_attention_schedule (#312)
- Adds basic attention layer tile size inference to the new parameter-free xegpu pipeline (wg_tiling_schedule, annotate_layout_schedule, etc.). - Adds options to override tile sizes with user-defined values. In `fused_attention.py` example CLI parameters are propagated to the pipeline. - Removes now obsolete `fused_attention_schedule`. - `extract_handle` transform op supports parameter handles as well. Assisted-by: Claude
1 parent 4c48432 commit 739f7ff

14 files changed

Lines changed: 719 additions & 579 deletions

‎examples/xegpu/fused_attention.py‎

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: %PYTHON %s --dump-kernel=xegpu-wg | FileCheck %s
2+
# RUN: %PYTHON %s --dump-kernel=xegpu-wg --wg-rows 64 --sg-rows 32 --reduction-tile 32 --q-load-tile 16 16 --prefetch-tile 32 16 --nb-prefetch 2 | FileCheck %s
23
# CHECK: module attributes {gpu.container_module} {
34

45
"""
@@ -20,8 +21,56 @@
2021
from lighthouse.ingress.mlir_gen.gpu_attention_payload import (
2122
generate_gpu_attention_payload,
2223
)
23-
from lighthouse.schedule.xegpu import fused_attention_schedule, xegpu_to_binary
2424
from lighthouse.schedule.parameters import ScheduleParameters
25+
from lighthouse.schedule import xegpu
26+
27+
28+
def get_xegpu_pipeline(parameters: dict, stop_at_stage: str) -> list:
29+
wg_tile = parameters.get("wg_tile", None)
30+
sg_tile = parameters.get("sg_tile", None)
31+
reduction_tile = parameters.get("reduction_tile", None)
32+
nb_prefetch = parameters.get("nb_prefetch", None)
33+
prefetch_tile = parameters.get("prefetch_tile", None)
34+
q_load_tile = parameters.get("q_load_tile", None)
35+
v_load_tile = parameters.get("v_load_tile", None)
36+
schedules = []
37+
if stop_at_stage == "initial":
38+
return schedules
39+
schedules.append(xegpu.cleanup_schedule())
40+
if stop_at_stage == "cleanup":
41+
return schedules
42+
schedules.append(
43+
xegpu.wg_tiling_schedule(
44+
wg_tile=wg_tile,
45+
reduction_tile=reduction_tile,
46+
)
47+
)
48+
if stop_at_stage == "tiled":
49+
return schedules
50+
schedules.append(xegpu.vectorize_schedule())
51+
if stop_at_stage == "vectorized":
52+
return schedules
53+
schedules.append(xegpu.bufferize_schedule())
54+
if stop_at_stage == "bufferized":
55+
return schedules
56+
schedules.append(xegpu.outline_gpu_func_schedule(sg_tile=sg_tile))
57+
if stop_at_stage == "gpu-outlining":
58+
return schedules
59+
schedules.append(xegpu.vector_to_xegpu_schedule())
60+
if stop_at_stage == "xegpu-initial":
61+
return schedules
62+
schedules.append(
63+
xegpu.annotate_layouts_schedule(
64+
sg_tile=sg_tile,
65+
nb_prefetch=nb_prefetch,
66+
prefetch_tile=prefetch_tile,
67+
q_load_tile=q_load_tile,
68+
v_load_tile=v_load_tile,
69+
)
70+
)
71+
if stop_at_stage == "xegpu-wg":
72+
return schedules
73+
return schedules
2574

2675

2776
def fused_attention_complexity(
@@ -187,18 +236,14 @@ def schedule_modules(
187236
"""Generate transform schedule for fused attention."""
188237
schedules = []
189238
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))
190-
191-
schedules.append(
192-
fused_attention_schedule(
193-
stop_at_stage=stop_at_stage,
194-
params=parameters,
195-
)
239+
schedules.extend(
240+
get_xegpu_pipeline(parameters=parameters[0], stop_at_stage=stop_at_stage)
196241
)
197242

198243
if stop_at_stage and stop_at_stage != "final":
199244
return schedules
200245

201-
schedules.append(xegpu_to_binary())
246+
schedules.append(xegpu.xegpu_to_binary())
202247

203248
return schedules
204249

@@ -247,12 +292,6 @@ def parse_cli():
247292
default=16,
248293
help="Number of Q*K^T*V rows computed by each subgroup",
249294
)
250-
parser.add_argument(
251-
"--subgroup-size",
252-
type=int,
253-
default=16,
254-
help="Subgroup size",
255-
)
256295
parser.add_argument(
257296
"--reduction-tile",
258297
type=int,
@@ -308,8 +347,8 @@ def parse_cli():
308347
type=str,
309348
choices=[
310349
"initial",
350+
"cleanup",
311351
"tiled",
312-
"reduction-tiled",
313352
"vectorized",
314353
"bufferized",
315354
"gpu-outlining",
@@ -341,13 +380,8 @@ def parse_cli():
341380

342381
layer_params = {
343382
"layer_kind": "attention",
344-
"batch_size": args.batch_size,
345-
"n_head": args.n_head,
346-
"n_ctx": args.n_ctx,
347-
"d_head": args.d_head,
348383
"wg_tile": [1, args.wg_rows],
349-
"sg_rows": args.sg_rows,
350-
"subgroup_size": args.subgroup_size,
384+
"sg_tile": [0, args.sg_rows],
351385
"reduction_tile": [0, 0, args.reduction_tile],
352386
"q_load_tile": args.q_load_tile,
353387
"v_load_tile": args.v_load_tile,

‎examples/xegpu/kernel_bench.py‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RUN: %PYTHON %s -l 2 -b 9 --dump-kernel=xegpu-wg | FileCheck %s
2-
# RUN: %PYTHON %s -l 1 -b 97 --input-shapes 32x32x512x64 32x32x512x64 32x32x512x64 --dump-kernel xegpu-wg | FileCheck %s
2+
# RUN: %PYTHON %s -l 2 -b 9 --dump-kernel=xegpu-wg --use-gpu-pipeline | FileCheck %s
3+
# RUN: %PYTHON %s -l 1 -b 97 --input-shapes 32x32x512x64 32x32x512x64 32x32x512x64 --dump-kernel xegpu-wg --use-gpu-pipeline | FileCheck %s
34
# REQUIRES: torch
45
# REQUIRES: kernel_bench
56

@@ -70,7 +71,6 @@
7071
elemwise_schedule,
7172
xegpu_to_binary,
7273
reduction_schedule,
73-
fused_attention_schedule,
7474
)
7575
from lighthouse.pipeline.helper import PipelineInterrupt
7676
from lighthouse.ingress.torch import gpu_backend, TargetDialect
@@ -284,13 +284,8 @@ def infer_parameters(
284284
# MHA case
285285
layer_params = {
286286
"layer_kind": "attention",
287-
"batch_size": batch_size,
288-
"n_head": n_head,
289-
"n_ctx": n_ctx,
290-
"d_head": d_head,
291287
"wg_tile": [1, 1, 128],
292-
"sg_rows": 16,
293-
"subgroup_size": 16,
288+
"sg_tile": [0, 0, 16],
294289
"reduction_tile": [0, 0, 0, 64],
295290
"q_load_tile": [16, 16],
296291
"v_load_tile": [32, 32],
@@ -301,13 +296,8 @@ def infer_parameters(
301296
# GQA case
302297
layer_params = {
303298
"layer_kind": "attention",
304-
"batch_size": batch_size,
305-
"n_head": n_head,
306-
"n_ctx": n_ctx,
307-
"d_head": d_head,
308299
"wg_tile": [1, 1, 1, 128],
309-
"sg_rows": 16,
310-
"subgroup_size": 16,
300+
"sg_tile": [0, 0, 16],
311301
"reduction_tile": [0, 0, 0, 0, 64],
312302
"q_load_tile": [16, 16],
313303
"v_load_tile": [32, 32],
@@ -618,10 +608,7 @@ def lower_to_llvm(
618608
stop_at_stage=stop_at_stage,
619609
)
620610
elif schedule_kind == "attention":
621-
schedule = fused_attention_schedule(
622-
params=schedule_params,
623-
stop_at_stage=stop_at_stage,
624-
)
611+
raise NotImplementedError("Attention kernels require --use-gpu-pipeline.")
625612
else:
626613
raise ValueError(f"Unsupported schedule kind: {schedule_kind}")
627614
pipeline = [schedule]

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def flatten(handles):
9898
layout = r // s
9999
if w > 0 and s > 0:
100100
layout = w // s
101+
if layout < 1:
102+
raise ValueError(
103+
f"Invalid sg_layout value: {layout}. wg={w}, sg={s}, red={r}."
104+
)
101105
sg_layout.append(layout)
102106
# sg_data tracks the tiled dim: subgroup tile, else reduction.
103107
sg_data.append(s if s > 0 else (r if r > 0 else 1))

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1+
from typing import TypeVar
2+
13
from mlir import ir
24
from mlir.dialects import ext, transform
5+
from mlir.dialects._ods_common import get_op_result_or_value
36
from mlir.dialects.transform import DiagnosedSilenceableFailure
47

58
from lighthouse.dialects.transform.transform_ext import TransformExtensionDialect
69

710

11+
HandleType = TypeVar("HandleType", bound=transform.AnyOpType | transform.AnyParamType)
12+
13+
814
class ExtractHandleOp(TransformExtensionDialect.Operation, name="extract_handle"):
915
"""
1016
Returns the handle at the specified index in `target`.
1117
1218
Args:
13-
target: Handle(s) to target op(s)
19+
target: Handle associated with operations or parameters.
1420
index: Index of the handle to extract. Supports Python-style indexing.
1521
silenceable: If False (default), an out-of-range index raises a
1622
definite failure; if True, a silenceable failure.
1723
Returns:
18-
The handle at the specified index in `target`.
24+
A handle of the same type as `target`, associated with its indexed element.
1925
"""
2026

21-
target: ext.Operand[transform.AnyOpType]
27+
target: ext.Operand[HandleType]
2228
index: ext.Operand[transform.AnyParamType]
2329
silenceable: ir.IntegerAttr = ext.attribute(
2430
default_factory=lambda: ir.IntegerAttr.get(ir.IntegerType.get_signless(1), 0)
2531
)
26-
ops: ext.Result[transform.AnyOpType[()]] = ext.infer_result()
32+
ops: ext.Result[HandleType] = ext.result(default_factory=transform.AnyOpType.get)
2733

2834
@classmethod
2935
def attach_interface_impls(cls, ctx=None):
@@ -38,14 +44,19 @@ def apply(
3844
results: transform.TransformResults,
3945
state: transform.TransformState,
4046
) -> DiagnosedSilenceableFailure:
41-
target_ops = state.get_payload_ops(op.target)
47+
is_param = isinstance(op.target.type, transform.AnyParamType)
48+
targets = (
49+
state.get_params(op.target)
50+
if is_param
51+
else state.get_payload_ops(op.target)
52+
)
4253
index_attr = state.get_params(op.index)
4354
if len(index_attr) == 1 and isinstance(index_attr[0], ir.IntegerAttr):
4455
index = index_attr[0].value
4556
else:
4657
return DiagnosedSilenceableFailure.SilenceableFailure
4758

48-
n = len(target_ops)
59+
n = len(targets)
4960
if index >= n or index < -n:
5061
msg = (
5162
f"extract_handle: invalid index {index} for target of length {n}. "
@@ -56,8 +67,11 @@ def apply(
5667
return DiagnosedSilenceableFailure.SilenceableFailure
5768
op.location.emit_error(msg)
5869
return DiagnosedSilenceableFailure.DefiniteFailure
59-
handle = target_ops[index]
60-
results.set_ops(op.ops, [handle])
70+
handle = targets[index]
71+
if is_param:
72+
results.set_params(op.ops, [handle])
73+
else:
74+
results.set_ops(op.ops, [handle])
6175
return DiagnosedSilenceableFailure.Success
6276

6377
@staticmethod
@@ -75,27 +89,29 @@ def get_effects(op: ir.Operation):
7589

7690

7791
def extract_handle(
78-
target: ir.Value[transform.AnyOpType],
92+
target: ir.Value[HandleType],
7993
index: int | ir.Value[transform.AnyParamType],
8094
silenceable: bool = False,
81-
) -> ir.Value:
95+
) -> ir.Value[HandleType]:
8296
"""
8397
snake_case wrapper to create a ExtractHandleOp.
8498
8599
Args:
86-
target: Handle(s) to target op(s)
100+
target: Handle associated with operations or parameters.
87101
index: Index of the handle to extract. Supports Python-style indexing.
88102
silenceable: If False (default), an out-of-range index raises a
89103
definite failure; if True, a silenceable failure.
90104
Returns:
91-
The handle at the specified index in `target`.
105+
A handle of the same type as `target`, associated with its indexed element.
92106
"""
107+
target = get_op_result_or_value(target)
93108
if isinstance(index, int):
94109
param_attr = ir.IntegerAttr.get(ir.IntegerType.get_signless(64), index)
95110
index = transform.ParamConstantOp(transform.AnyParamType.get(), param_attr)
96111

97112
return ExtractHandleOp(
98113
target=target,
114+
ops=target.type,
99115
index=index,
100116
silenceable=ir.IntegerAttr.get(
101117
ir.IntegerType.get_signless(1), int(silenceable)

0 commit comments

Comments
 (0)