-
Notifications
You must be signed in to change notification settings - Fork 19
[xegpu] Generalize fused attention schedule for KernelBench #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+234
−16
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37d99f2
transform_ext: add filter_contraction_ops op
tkarna b9e832a
trace producers: fix handle deduplication
tkarna 630675b
fused attention schedule: generalize to support kb attention benchmark
tkarna 82d9f65
harder scaled const op matching
tkarna 070d218
add tests for filter_contraction_ops
tkarna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
lighthouse/dialects/transform/transform_ext/ops/filter_contraction_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| from mlir import ir | ||
| from mlir.dialects import transform, linalg | ||
|
|
||
| from lighthouse.dialects.transform.transform_ext.utils.make_filter_handles_op import ( | ||
| make_filter_handles_op, | ||
| ) | ||
| from lighthouse.utils.mlir import opview, indexing_maps, linalg_inputs, dim_position | ||
|
|
||
|
|
||
| def _map_dims(m: ir.AffineMap) -> set[int]: | ||
| """Set of iteration-dim positions referenced by an affine map's results.""" | ||
| dims = set() | ||
| for r in m.results: | ||
| pos = dim_position(r) | ||
| if pos is not None: | ||
| dims.add(pos) | ||
| return dims | ||
|
|
||
|
|
||
| def _is_structural_contraction(ov: ir.OpView) -> bool: | ||
| """Detect a matmul-like op from its indexing maps alone (body-agnostic). | ||
|
|
||
| A contraction has a reduction dimension (one that is absent from the | ||
| output) that is shared by at least two inputs. This ignores the body, so it | ||
| also matches contractions with extra elementwise operands (e.g. a scaled or | ||
| dequantized input) that the strict multiply-accumulate matcher rejects. | ||
| """ | ||
| maps = indexing_maps(ov) | ||
| inputs = linalg_inputs(ov) | ||
| if not maps or not inputs: | ||
| return False | ||
| num_inputs = len(inputs) | ||
| input_maps = maps[:num_inputs] | ||
| output_maps = maps[num_inputs:] | ||
|
|
||
| out_dims: set[int] = set() | ||
| for m in output_maps: | ||
| out_dims |= _map_dims(m) | ||
|
|
||
| # Reduction dims are the iteration dims not present in any output. | ||
| reduction_dims = set(range(maps[0].n_dims)) - out_dims | ||
| for k in reduction_dims: | ||
| if sum(1 for m in input_maps if k in _map_dims(m)) >= 2: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def is_contraction_op(op: ir.Operation | ir.OpView) -> bool: | ||
| """Check whether the op is a linalg contraction (matmul-like) op. | ||
|
|
||
| Recognizes both named contractions (e.g. linalg.batch_matmul) and their | ||
| generic form, independent of rank. A contraction contracts a reduction | ||
| dimension shared by two inputs, which distinguishes it from the surrounding | ||
| elementwise or plain (single-input) reduction ops. | ||
| """ | ||
| ov = opview(op) | ||
| if "linalg" not in ov.operation.name: | ||
| return False | ||
| return linalg.isa_contraction_op(ov) or _is_structural_contraction(ov) | ||
|
|
||
|
|
||
| FilterContractionOpsOp = make_filter_handles_op( | ||
| "filter_contraction_ops", is_contraction_op | ||
| ) | ||
|
tkarna marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def filter_contraction_ops(target: ir.Value[transform.AnyOpType]) -> ir.Value: | ||
| """ | ||
| snake_case wrapper to create a FilterContractionOpsOp. | ||
|
|
||
| Args: | ||
| target: Handle to target op(s). | ||
| Returns: | ||
| Handle to the contraction-op subset of `target`. | ||
| """ | ||
| return FilterContractionOpsOp(target=target).ops | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # RUN: %PYTHON %s | FileCheck %s | ||
|
|
||
| from mlir import ir | ||
| from mlir.dialects import transform | ||
| from mlir.dialects.transform import structured | ||
|
|
||
| import lighthouse.dialects as lh_dialects | ||
| from lighthouse import transform as lh_transform | ||
| from lighthouse.dialects.transform import transform_ext | ||
| from lighthouse.schedule.builders import schedule_boilerplate | ||
|
|
||
|
|
||
| def apply_filter(payload: str, name: str): | ||
| with ir.Context(), ir.Location.unknown(): | ||
| lh_dialects.register_and_load() | ||
| module = ir.Module.parse(payload) | ||
| with schedule_boilerplate() as (sched, named_seq): | ||
| candidates = lh_transform.match_op( | ||
| named_seq.bodyTarget, structured.MatchInterfaceEnum.LinalgOp | ||
| ) | ||
| filtered = transform_ext.filter_contraction_ops(candidates) | ||
| transform.print_(target=filtered, name=name) | ||
| transform.yield_() | ||
| sched.body.operations[0].apply(module.operation) | ||
|
|
||
|
|
||
| # Named matmul and batch_matmul mixed with elementwise ops. | ||
| NAMED = """ | ||
| #id = affine_map<(d0, d1) -> (d0, d1)> | ||
| module { | ||
| func.func @main( | ||
| %a: tensor<8x8xf32>, | ||
| %m0: tensor<4x8xf32>, | ||
| %m1: tensor<8x4xf32>, | ||
| %x: tensor<2x4x8xf32>, | ||
| %y: tensor<2x8x4xf32>) | ||
| -> (tensor<8x8xf32>, tensor<4x4xf32>, tensor<2x4x4xf32>) { | ||
| %e0 = tensor.empty() : tensor<8x8xf32> | ||
| %add = linalg.add ins(%a, %a : tensor<8x8xf32>, tensor<8x8xf32>) | ||
| outs(%e0 : tensor<8x8xf32>) -> tensor<8x8xf32> | ||
|
|
||
| %e1 = tensor.empty() : tensor<8x8xf32> | ||
| %gen = linalg.generic {indexing_maps = [#id, #id], iterator_types = ["parallel", "parallel"]} | ||
| ins(%a : tensor<8x8xf32>) | ||
| outs(%e1 : tensor<8x8xf32>) { | ||
| ^bb0(%i: f32, %o: f32): | ||
| linalg.yield %i : f32 | ||
| } -> tensor<8x8xf32> | ||
|
|
||
| %e2 = tensor.empty() : tensor<4x4xf32> | ||
| %mm = linalg.matmul ins(%m0, %m1 : tensor<4x8xf32>, tensor<8x4xf32>) | ||
| outs(%e2 : tensor<4x4xf32>) -> tensor<4x4xf32> | ||
|
|
||
| %e3 = tensor.empty() : tensor<2x4x4xf32> | ||
| %bm = linalg.batch_matmul ins(%x, %y : tensor<2x4x8xf32>, tensor<2x8x4xf32>) | ||
| outs(%e3 : tensor<2x4x4xf32>) -> tensor<2x4x4xf32> | ||
| return %gen, %mm, %bm : tensor<8x8xf32>, tensor<4x4xf32>, tensor<2x4x4xf32> | ||
| } | ||
| } | ||
| """ | ||
|
|
||
| # CHECK-LABEL: IR printer: NAMED | ||
| # CHECK: linalg.matmul | ||
| # CHECK: linalg.batch_matmul | ||
| # CHECK-NOT: linalg.add | ||
| # CHECK-NOT: linalg.generic | ||
| apply_filter(NAMED, name="NAMED") | ||
|
|
||
|
|
||
| # A linalg.generic with elementwise (extf) ops fused into the matmul body, | ||
| # preceded by a linalg.fill that must not be matched. | ||
| FUSED = """ | ||
| #map = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d4)> | ||
| #map1 = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d4, d3)> | ||
| #map2 = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3)> | ||
| module { | ||
| func.func @main( | ||
| %a: tensor<1x1x128x1024xbf16>, | ||
| %b: tensor<1x1x1024x512xbf16>) -> tensor<1x1x128x512xf32> { | ||
| %cst = arith.constant 0.0 : f32 | ||
| %e = tensor.empty() : tensor<1x1x128x512xf32> | ||
| %fill = linalg.fill ins(%cst : f32) outs(%e : tensor<1x1x128x512xf32>) -> tensor<1x1x128x512xf32> | ||
| %mm = linalg.generic {indexing_maps = [#map, #map1, #map2], | ||
| iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction"]} | ||
| ins(%a, %b : tensor<1x1x128x1024xbf16>, tensor<1x1x1024x512xbf16>) | ||
| outs(%fill : tensor<1x1x128x512xf32>) { | ||
| ^bb0(%in: bf16, %in_18: bf16, %out: f32): | ||
| %ea = arith.extf %in : bf16 to f32 | ||
| %eb = arith.extf %in_18 : bf16 to f32 | ||
| %mul = arith.mulf %ea, %eb : f32 | ||
| %acc = arith.addf %out, %mul : f32 | ||
| linalg.yield %acc : f32 | ||
| } -> tensor<1x1x128x512xf32> | ||
| return %mm : tensor<1x1x128x512xf32> | ||
| } | ||
| } | ||
| """ | ||
|
|
||
| # CHECK-LABEL: IR printer: FUSED | ||
| # CHECK: linalg.generic | ||
| # CHECK-NOT: linalg.fill | ||
| apply_filter(FUSED, name="FUSED") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.