Skip to content

Commit 2521ccb

Browse files
authored
[xegpu] Generalize reduction schedule to work with nD inputs (#278)
Updates `reduction_schedule` so that KernelBench `level1/36_RMSNorm_.py` case can be executed. - Generalized `reduction_schedule` parameters: `wg_tile`, `sg_tile`, `reduction_tile` parameters are n-D lists. - Adds optional `wg_subtile` parameter to add a persistent for loop to reduce WG tile size. - Reductions are tiled with `tile_using_for`; avoids iter dim permutation issue caused by `tile_reduction_using_for`. - Applies `math-expand-ops` pass after vectorization; e.g. pow(x, 2) -> x*x - Adds `sink_extract_slice_into_loop` transform_ext op to avoid spurious `vector.transfer_read/write` patterns. - The common `vectorize` function takes `disable_multi_reduction_to_contract_patterns` argument; Setting it avoids converting `vector.multi_reduction` ops to `vector.contract` which xegpu lowering does not support correctly at the moment. Assisted-by: Claude
1 parent 6968443 commit 2521ccb

8 files changed

Lines changed: 826 additions & 85 deletions

File tree

‎examples/xegpu/kernel_bench.py‎

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ def infer_parameters(
181181
schedule_params = ScheduleParameters([layer_params])
182182
schedule_kind = "elemwise"
183183
elif len(elemwise) > 0 and len(reduction) > 0 and len(batch_matmuls) == 0:
184+
# Elementwise + reduction kernel
185+
iter_space = reduction[0]["iterators"]
186+
n_reduction_dims = sum(s == "reduction" for s in iter_space)
184187
# elemwise + reduction kernel, e.g. softmax or layer norm
185188
shape = elemwise[-1]["shape"]
186189
res_elemtype = elemwise[-1]["elemtype"]
@@ -189,25 +192,50 @@ def infer_parameters(
189192
res_bytes = elemtype_bytes[res_elemtype]
190193
read_bytes = int(np.prod(shape)) * res_bytes
191194
write_bytes = int(np.prod(shape)) * res_bytes
192-
193-
layer_params = {
194-
"layer_kind": "reduction",
195-
"wg_rows": 64,
196-
"sg_rows": 8,
197-
"subgroup_size": 16,
198-
"reduction_step_size": 32,
199-
}
195+
if len(shape) == 2:
196+
# 2d softmax like kernel
197+
layer_params = {
198+
"layer_kind": "reduction",
199+
"wg_tile": [64, 0],
200+
"sg_tile": [8, 0],
201+
"subgroup_size": 16,
202+
"reduction_tile": [0, 32],
203+
}
204+
205+
elif len(shape) == 4 and iter_space[1] == "reduction" and n_reduction_dims == 1:
206+
# 4d rms norm like kernel
207+
layer_params = {
208+
"layer_kind": "reduction",
209+
"sizes": shape,
210+
"wg_tile": [1, 0, 128, 128],
211+
"wg_subtile": [0, 0, 64, 128],
212+
"sg_tile": [0, 0, 8, 32],
213+
"reduction_tile": [0, 8, 0, 0],
214+
"subgroup_size": 16,
215+
}
216+
else:
217+
raise ValueError(
218+
f"Unsupported kernel shape {shape} with iter_space {iter_space}"
219+
)
200220

201221
# Ensure shape is divisible by tile sizes.
202222
# Padding or remainder handling is not implemented yet.
203-
if shape[0] % layer_params["wg_rows"] != 0:
204-
raise ValueError(
205-
f"Shape {shape} dimension 0 not divisible by wg_rows={layer_params['wg_rows']}"
206-
)
207-
if shape[1] % layer_params["reduction_step_size"] != 0:
208-
raise ValueError(
209-
f"Shape {shape} dimension 1 not divisible by reduction_step_size={layer_params['reduction_step_size']}"
210-
)
223+
wg_sizes = (
224+
layer_params["wg_subtile"]
225+
if "wg_subtile" in layer_params
226+
else layer_params["wg_tile"]
227+
)
228+
reduction_tile = layer_params["reduction_tile"]
229+
for i, (size, wg, red) in enumerate(zip(shape, wg_sizes, reduction_tile)):
230+
if wg > 0 and size % wg != 0:
231+
raise ValueError(
232+
f"Shape {shape} dimension {i} not divisible by wg_tile={wg_sizes}"
233+
)
234+
if red > 0 and size % red != 0:
235+
raise ValueError(
236+
f"Shape {shape} dimension {i} not divisible by reduction_tile={reduction_tile}"
237+
)
238+
211239
schedule_params = ScheduleParameters([layer_params])
212240
schedule_kind = "reduction"
213241
elif len(elemwise) > 0 and len(reduction) > 0 and len(batch_matmuls) > 0:
@@ -609,7 +637,7 @@ def lower_and_execute_benchmark(
609637
params_cache_json=f"kb_params_level{level}-{id}.json",
610638
dump_parameters=dump_parameters,
611639
enable_tuning=execute,
612-
verbose=2,
640+
verbose=verbose,
613641
)
614642
backend = gpu_backend(
615643
fn_compile,
@@ -647,13 +675,20 @@ def lower_and_execute_benchmark(
647675
layer.get("kind") == "batch_matmul"
648676
for layer in kernel_metadata.get("layers", [])
649677
)
678+
is_reduction = any(
679+
layer.get("kind") == "reduction"
680+
for layer in kernel_metadata.get("layers", [])
681+
)
650682
atol = abs(result_ref).max() * 1e-3
651683
rtol = 1e-3
652684
if result_ref.dtype == torch.bfloat16:
653685
rtol = 2e-2
654686
if is_attention:
655-
# Attention fuses 2 matmuls and a softmax, needs a looser atol.
687+
# Attention fuses 2 matmuls and a softmax.
656688
atol = 2e-2
689+
elif is_reduction:
690+
# RMS norm currently uses bf16 accumulator.
691+
atol = 6e-2
657692
elif is_mlp:
658693
atol = 7e-3
659694
success = torch.allclose(result, result_ref, rtol=rtol, atol=atol)

‎examples/xegpu/layer_norm.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ def parse_cli():
261261
{
262262
"layer_kind": "reduction",
263263
"sizes": args.sizes,
264-
"wg_rows": args.wg_rows,
265-
"sg_rows": args.sg_rows,
264+
"wg_tile": [args.wg_rows, 0],
265+
"sg_tile": [args.sg_rows, 0],
266+
"reduction_tile": [0, args.reduction_step_size],
266267
"subgroup_size": args.subgroup_size,
267-
"reduction_step_size": args.reduction_step_size,
268268
}
269269
]
270270
)

‎examples/xegpu/softmax.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ def parse_cli():
251251
{
252252
"layer_kind": "reduction",
253253
"sizes": args.sizes,
254-
"wg_rows": args.wg_rows,
255-
"sg_rows": args.sg_rows,
254+
"wg_tile": [args.wg_rows, 0],
255+
"sg_tile": [args.sg_rows, 0],
256+
"reduction_tile": [0, args.reduction_step_size],
256257
"subgroup_size": args.subgroup_size,
257-
"reduction_step_size": args.reduction_step_size,
258258
}
259259
]
260260
)

‎lighthouse/dialects/transform/transform_ext/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .ops.get_fusion_roots import get_fusion_roots
2727
from .ops.propagate_tile_sizes import propagate_tile_sizes
2828
from .ops.sfc_remap_forall import sfc_remap_forall
29+
from .ops.sink_extract_slice_into_loop import sink_extract_slice_into_loop
2930

3031
__all__ = [
3132
"TransformExtensionDialect",
@@ -53,6 +54,7 @@
5354
"replace_with_fused_attention",
5455
"reverse_handles",
5556
"sfc_remap_forall",
57+
"sink_extract_slice_into_loop",
5658
"trace_producers",
5759
"update_address_space",
5860
"wrap_in_benching_func",

0 commit comments

Comments
 (0)