[water] Add FX importer handlers for arithmetic, unary, and attention-specific ops#1054
Merged
martin-luecke merged 4 commits intomainfrom Mar 6, 2026
Merged
Conversation
c60315d to
2bcc7ac
Compare
ftynse
approved these changes
Mar 6, 2026
lit_tests/kernel/wave/mlir_to_fx.py
Outdated
Comment on lines
+515
to
+547
| def _assert_pre_decompose_roundtrip(kernel, subs: dict, label: str) -> None: | ||
| """Compile through graph passes (stopping before decompose_reduce_ops), | ||
| emit MLIR, roundtrip, and assert equivalence. | ||
|
|
||
| Runs the full pipeline up to but not including `decompose_reduce_ops` | ||
| (which introduces `wave.shuffle` ops not yet supported by the | ||
| importer). | ||
| """ | ||
| options = WaveCompileOptions(subs=subs, compile_to_mlir=True) | ||
| with IndexingContext() as idxc: | ||
| idxc.set_subs(options.subs) | ||
| kernel.initialize_wave_constraints() | ||
| kernel.initialize_symbolic_constraints() | ||
| kernel.initialize_workgroup_constraints() | ||
| trace = kernel._trace(location_capture_config=options.location_capture_config) | ||
| graph_passes = build_graph_passes(kernel, trace, options) | ||
| for p in graph_passes: | ||
| if p.__name__ == decompose_reduce_ops.__name__: | ||
| break | ||
| p() | ||
|
|
||
| mlir_text, diagnostics, _ = emitter.emit_wave_dialect( | ||
| trace, kernel.constraints, options | ||
| ) | ||
| errors = error_diagnostics(diagnostics) | ||
| assert errors == [], f"[{label}] unexpected emit errors: {errors}" | ||
|
|
||
| fx_trace, fx_constraints, fx_options, fx_diags = emitter.mlir_to_fx(mlir_text) | ||
| errors = error_diagnostics(fx_diags) | ||
| assert errors == [], f"[{label}] unexpected import errors: {errors}" | ||
|
|
||
| assert_traces_equivalent(trace, fx_trace, subs=options.subs) | ||
| print(f" {label}: OK") |
Contributor
There was a problem hiding this comment.
Can we generalize this with the function for pre-canonical forms and take the name of the before which to stop as an argument?
Contributor
Author
There was a problem hiding this comment.
Generalized this with the _assert_roundtrip above
…-specific ops Signed-off-by: Martin Lücke <martin.luecke@amd.com>
Signed-off-by: Martin Lücke <martin.luecke@amd.com>
6444ba1 to
7a93129
Compare
Signed-off-by: Martin Lücke <martin.luecke@amd.com>
Signed-off-by: Martin Lücke <martin.luecke@amd.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This adds MLIR-to-FX import handlers for the ops needed by attention kernels: binary arithmetic (add, sub, mul, max, min, select), unary (exp2, reciprocal), cast, permute, self_index, apply_expr, extract, and reshape.
The apply_expr handler reconstructs the original sympy expression lambda from the MLIR WaveExprListAttr and combinator attribute, folding the affine map results back through the appropriate sympy constructor.
As the reconstructed lambda is a new Python object, the existing trace equivalence checker cannot compare it by identity. A
_check_callable_equivalenthelper is added that evaluates both lambdas with fresh symbolic inputs and verifies the resulting sympy expressions are equivalent via simplify(a - b) == 0. This allows roundtrip tests to confirm that ApplyExpr nodes carry semantically identical expressions despite being distinct function objects.