Skip to content

Commit ffd2f1e

Browse files
committed
Merge branch 'main' into softmax_reduction_tiling
2 parents f1857aa + 25200ff commit ffd2f1e

10 files changed

Lines changed: 148 additions & 160 deletions

File tree

‎examples/xegpu/enumerate_matmul_schedules.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
# Check that at least one constraint was reified into the schedule.
4949
# CHECK: constrain_params
5050
schedules = wload.schedule_modules(parameters=parameters)
51-
assert len(schedules) == 2, "Expected two schedule modules to be returned"
51+
assert len(schedules) == 3, "Expected three schedule modules to be returned"
5252

5353
print(schedules[1])

‎examples/xegpu/matmul.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
MemoryManager,
2727
GPUMemoryManager,
2828
)
29-
from lighthouse.schedule.xegpu.mlp_schedule import get_schedule_module
29+
from lighthouse.schedule.xegpu import mlp_schedule, xegpu_to_binary
3030
from lighthouse.utils.numpy import mlir_to_numpy_dtype
3131
from lighthouse.ingress.mlir_gen import generate_gpu_matmul_payload, get_mlir_elem_type
3232

@@ -159,13 +159,22 @@ def schedule_modules(
159159
self, stop_at_stage: Optional[str] = None, parameters: Optional[dict] = None
160160
) -> list[ir.Module]:
161161
assert parameters is not None, "Schedule parameters must be provided"
162-
return [
163-
Runner.get_bench_wrapper_schedule(self.payload_function_name),
164-
get_schedule_module(
162+
schedules = []
163+
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))
164+
165+
schedules.append(
166+
mlp_schedule(
165167
stop_at_stage=stop_at_stage,
166168
params=[parameters],
167-
),
168-
]
169+
)
170+
)
171+
172+
if stop_at_stage and stop_at_stage != "final":
173+
return schedules
174+
175+
schedules.append(xegpu_to_binary())
176+
177+
return schedules
169178

170179
def shared_libs(self) -> list[str]:
171180
return ["libmlir_levelzero_runtime.so"]

‎examples/xegpu/mlp.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
GPUMemoryManager,
3232
)
3333
from lighthouse.utils.numpy import mlir_to_numpy_dtype
34-
from lighthouse.schedule.xegpu.mlp_schedule import get_schedule_module
34+
from lighthouse.schedule.xegpu import mlp_schedule, xegpu_to_binary
3535
from lighthouse.ingress.mlir_gen import (
3636
generate_gpu_mlp_payload,
3737
get_mlir_elem_type,
@@ -224,13 +224,23 @@ def payload_module(self) -> ir.Module:
224224
def schedule_modules(
225225
self, stop_at_stage: Optional[str] = None, parameters: Optional[dict] = None
226226
) -> list[ir.Module]:
227-
return [
228-
Runner.get_bench_wrapper_schedule(self.payload_function_name),
229-
get_schedule_module(
227+
assert parameters is not None, "Schedule parameters must be provided"
228+
schedules = []
229+
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))
230+
231+
schedules.append(
232+
mlp_schedule(
230233
stop_at_stage=stop_at_stage,
231234
params=parameters,
232-
),
233-
]
235+
)
236+
)
237+
238+
if stop_at_stage and stop_at_stage != "final":
239+
return schedules
240+
241+
schedules.append(xegpu_to_binary())
242+
243+
return schedules
234244

235245
def shared_libs(self) -> list[str]:
236246
return ["libmlir_levelzero_runtime.so"]

‎examples/xegpu/softmax.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from lighthouse.utils.numpy import mlir_to_numpy_dtype
2020
from lighthouse.ingress.mlir_gen import get_mlir_elem_type
2121
from lighthouse.ingress.mlir_gen.gpu_softmax_payload import generate_gpu_softmax_payload
22-
from lighthouse.schedule.xegpu.softmax_schedule import get_softmax_schedule_module
22+
from lighthouse.schedule.xegpu import softmax_schedule, xegpu_to_binary
2323

2424

2525
def softmax_complexity(M: int, N: int, nbytes: int):
@@ -134,13 +134,22 @@ def schedule_modules(
134134
self, stop_at_stage: Optional[str] = None, parameters: Optional[dict] = None
135135
) -> list[ir.Module]:
136136
"""Generate transform schedule for softmax."""
137-
return [
138-
Runner.get_bench_wrapper_schedule(self.payload_function_name),
139-
get_softmax_schedule_module(
137+
schedules = []
138+
schedules.append(Runner.get_bench_wrapper_schedule(self.payload_function_name))
139+
140+
schedules.append(
141+
softmax_schedule(
140142
stop_at_stage=stop_at_stage,
141143
parameters=parameters,
142-
),
143-
]
144+
)
145+
)
146+
147+
if stop_at_stage and stop_at_stage != "final":
148+
return schedules
149+
150+
schedules.append(xegpu_to_binary())
151+
152+
return schedules
144153

145154
def shared_libs(self) -> list[str]:
146155
return ["libmlir_levelzero_runtime.so"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .xegpu_to_binary import xegpu_to_binary
2+
from .mlp_schedule import mlp_schedule
3+
from .softmax_schedule import softmax_schedule
4+
5+
__all__ = [
6+
"mlp_schedule",
7+
"softmax_schedule",
8+
"xegpu_to_binary",
9+
]

‎lighthouse/schedule/xegpu/helper.py‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎lighthouse/schedule/xegpu/mlp_schedule.py‎

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
match_and_split,
1717
PipelineInterrupt,
1818
)
19-
from lighthouse.schedule.xegpu.helper import bundle_xegpu_to_binary
2019

20+
from lighthouse.schedule import schedule_boilerplate
2121
from lighthouse.dialects import smt_ext
2222
from lighthouse.dialects.transform import smt_ext as td_smt_ext
2323
from lighthouse.dialects.transform.tune_ext import knob, KnobValue
@@ -104,66 +104,39 @@ def params_with_constraints_imposed(
104104
}
105105

106106

107-
def get_schedule_module(
107+
def mlp_schedule(
108108
params: list[dict[str, int | None]],
109109
stop_at_stage: str = "",
110110
) -> ir.Module:
111-
"""Generate transform schedule module."""
111+
"""Generate transform schedule module for MLP payload."""
112112
assert params is not None and len(params) > 0, "params must be provided."
113-
mod = ir.Module.create()
114-
mod.operation.attributes["transform.with_named_sequence"] = ir.UnitAttr.get()
115-
with ir.InsertionPoint(mod.body):
116-
named_sequence = transform.named_sequence(
117-
"__transform_main",
118-
[transform.AnyOpType.get()], # input types
119-
[], # output types
120-
arg_attrs=[{"transform.readonly": ir.UnitAttr.get()}],
113+
with schedule_boilerplate() as (schedule, named_seq):
114+
# match the payload module
115+
anytype = transform.AnyOpType.get()
116+
func = match(named_seq.bodyTarget, ops={"func.func"})
117+
payload_mod = transform.get_parent_op(
118+
anytype,
119+
func,
120+
op_name="builtin.module",
121+
deduplicate=True,
121122
)
122-
with ir.InsertionPoint(named_sequence.body):
123-
# match the payload module
124-
anytype = transform.AnyOpType.get()
125-
func = match(named_sequence.bodyTarget, ops={"func.func"})
126-
payload_mod = transform.get_parent_op(
127-
anytype,
128-
func,
129-
op_name="builtin.module",
130-
deduplicate=True,
123+
for i, layer_params in enumerate(params):
124+
layer_params |= params_with_constraints_imposed(
125+
layer_params, knob_name_prefix=f"layer_{i}_"
131126
)
132-
for i, layer_params in enumerate(params):
133-
layer_params |= params_with_constraints_imposed(
134-
layer_params, knob_name_prefix=f"layer_{i}_"
135-
)
136127

137-
xegpu_mlp_transform_schedule(
128+
try:
129+
bundle_xegpu_mlp_schedule(
138130
payload_mod,
139131
params=params,
140132
stop_at_stage=stop_at_stage,
141133
)
134+
except PipelineInterrupt:
135+
pass
136+
finally:
137+
transform.yield_()
142138

143-
return mod
144-
145-
146-
def xegpu_mlp_transform_schedule(
147-
mod: ir.Value[transform.AnyOpType],
148-
params: list[dict[str, int | KnobValue]],
149-
stop_at_stage: str = "",
150-
):
151-
"""Transform schedule for MLP-like payload."""
152-
try:
153-
mod = bundle_xegpu_mlp_schedule(
154-
mod,
155-
params=params,
156-
stop_at_stage=stop_at_stage,
157-
)
158-
159-
mod = bundle_xegpu_to_binary(
160-
mod,
161-
stop_at_stage=stop_at_stage,
162-
)
163-
except PipelineInterrupt:
164-
pass
165-
finally:
166-
transform.yield_()
139+
return schedule
167140

168141

169142
def bundle_xegpu_mlp_schedule(
@@ -183,51 +156,49 @@ def bundle_xegpu_mlp_schedule(
183156

184157
# tile each layer separately
185158
for matmul_op, layer_params in zip(matmul_ops, params):
159+
# tunable parameters: wg and k tiling
160+
wg_tile = [layer_params["wg_m"], layer_params["wg_n"]]
161+
k_tile = layer_params["k_tile"]
162+
186163
# find the last tileable consumer of the matmul
187164
consumers = transform_ext.get_tileable_consumers(matmul_op)
188165
leaf_consumer_op = transform_ext.extract_handle(consumers, -1)
189166

190-
# tunable parameters: wg level tiling
191-
wg_tile = [layer_params["wg_m"], layer_params["wg_n"]]
192-
k_tile = layer_params["k_tile"]
193-
194-
_, wg_loop = structured.FuseOp(
195-
leaf_consumer_op, tile_sizes=wg_tile, use_forall=True
196-
).results
197-
transform.apply_cse(mod)
198-
canonicalize(mod)
167+
# wg tiling
168+
_, [wg_loop], _ = lh_transform.tile(
169+
leaf_consumer_op,
170+
tile_sizes=wg_tile,
171+
fuse_producers=True,
172+
use_forall=True,
173+
apply_cleanup=False,
174+
)
199175

200176
# k loop tiling
201177
wg_matmul = match(wg_loop, ops={"linalg.matmul"})
202-
wgk_matmul, k_loop = structured.TileUsingForOp(
203-
wg_matmul, sizes=[0, 0, k_tile]
204-
).results
178+
_, [k_loop], _ = lh_transform.tile(wg_matmul, tile_sizes=[0, 0, k_tile])
205179

206180
func = transform.get_parent_op(
207181
anytype,
208182
k_loop,
209183
op_name="func.func",
210184
deduplicate=True,
211185
)
212-
transform.apply_cse(func)
213-
canonicalize(func)
186+
lh_transform.cleanup(func)
214187

215188
if stop_at_stage == "tiled":
216189
raise PipelineInterrupt()
217190

218191
# vectorize
219-
# FIXME use structured.structured_vectorize_children_and_apply_patterns
220-
func = structured.VectorizeChildrenAndApplyPatternsOp(
192+
func = structured.structured_vectorize_children_and_apply_patterns(
193+
transform.any_op_t(),
221194
func,
222195
fold_type_extensions_into_contract=True,
223-
).result
196+
)
224197

225198
# hoist loop invariant vector read/store ops
226199
k_loop = match(func, ops={"scf.for"})
227-
loop.HoistLoopInvariantSubsetsOp(k_loop)
228-
229-
transform.apply_cse(func)
230-
canonicalize(func)
200+
lh_transform.loop_hoisting(k_loop)
201+
lh_transform.cleanup(func)
231202

232203
if stop_at_stage == "vectorized":
233204
raise PipelineInterrupt()

0 commit comments

Comments
 (0)