-
Notifications
You must be signed in to change notification settings - Fork 7
[transform] Basic example of applying a schedule to a payload #12
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
+91
−20
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2de74a3
[transform] Simple Python and cmd interface for applying schedules
rolfmorel 3a51e04
Improvements and address feedback
rolfmorel 19920ba
Save
rolfmorel 971e7ef
Merge remote-tracking branch 'origin/main' into lighthouse-invoke
rolfmorel 08ee4a1
Address feedback
rolfmorel e94f333
Address Andrzej's feedback
rolfmorel d037047
Merge remote-tracking branch 'origin/main' into lighthouse-invoke
rolfmorel 6f29ea9
KISS. With an emphasis on S.
rolfmorel 40f97cc
Merge remote-tracking branch 'origin/main' into lighthouse-invoke
rolfmorel abc7403
Remove line - thanks Adam!
rolfmorel fe03285
Final cleanup
rolfmorel 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
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
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
82 changes: 82 additions & 0 deletions
82
python/examples/schedule/transform_a_payload_according_to_a_schedule.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,82 @@ | ||
| # Simply demonstrates applying a schedule to a payload. | ||
| # To do so generates a basic payload and a basic schedule, purely as an example. | ||
|
|
||
| from mlir.ir import Context, Location, InsertionPoint, Operation, Module | ||
| from mlir.ir import RankedTensorType, F32Type, UnitAttr | ||
| from mlir.dialects import arith, func, linalg, tensor, transform | ||
| from mlir.dialects.transform import structured | ||
|
|
||
|
|
||
| def example_payload() -> Module: | ||
| """IR for: | ||
| Zero = ... | ||
| X = matmul(..., C=Zero) | ||
| Y = matmul(..., C=Zero) | ||
| Res = add(X, Y) | ||
|
|
||
| Can be re-written to: | ||
| X = matmul(..., C=Zero) | ||
| Res = matmul(..., C=X) | ||
| """ | ||
|
|
||
| print("NOTE: example payload module:") | ||
| payload = Module.create() | ||
| with InsertionPoint(payload.body): | ||
| matrixType = RankedTensorType.get([16, 16], F32Type.get()) | ||
|
|
||
| @func.func(matrixType, matrixType, matrixType) | ||
| def fold_add_on_two_matmuls(matrixA, matrixB, weights): | ||
| empty = tensor.empty(matrixType.shape, matrixType.element_type) | ||
| c0 = arith.constant(F32Type.get(), 0.0) | ||
| zero_init = linalg.fill(c0, outs=[empty]) | ||
| A_x_weights = linalg.matmul(matrixA, weights, outs=[zero_init]) | ||
| empty2 = tensor.empty(matrixType.shape, matrixType.element_type) | ||
| zero_init2 = linalg.fill(c0, outs=[empty2]) | ||
| B_x_weights = linalg.matmul(matrixB, weights, outs=[zero_init2]) | ||
| added = linalg.add(A_x_weights, B_x_weights, outs=[empty]) | ||
| return added | ||
|
|
||
| print(payload) | ||
| return payload | ||
|
|
||
|
|
||
| def example_schedule() -> Module: | ||
| """Basic schedule wrapping a single rewrite pattern.""" | ||
|
|
||
| print("NOTE: example schedule module:") | ||
| schedule_module = Module.create() | ||
| schedule_module.operation.attributes["transform.with_named_sequence"] = ( | ||
| UnitAttr.get() | ||
| ) | ||
| with InsertionPoint(schedule_module.body): | ||
| named_seq = transform.named_sequence( | ||
| "__transform_main", | ||
| input_types=[transform.any_op_t()], | ||
| result_types=[], | ||
| arg_attrs=[{"transform.readonly": UnitAttr.get()}], | ||
| ) | ||
|
|
||
| with InsertionPoint(named_seq.body): | ||
| func = structured.MatchOp.match_op_names( | ||
| named_seq.bodyTarget, ["func.func"] | ||
| ) # TODO: fix syntax upstream | ||
| with InsertionPoint(transform.apply_patterns(func).patterns): | ||
| Operation.create( | ||
| "transform.apply_patterns.linalg.fold_add_into_dest" | ||
| ) # TODO: expose dedicated builder upstream | ||
| transform.yield_([]) | ||
|
|
||
| print(schedule_module) | ||
| return schedule_module | ||
|
|
||
|
|
||
| with Context(), Location.unknown(): | ||
| payload = example_payload() | ||
| schedule_module = example_schedule() | ||
| # Actual schedule is defined by the contained transform.named_sequence: | ||
| schedule: transform.NamedSequenceOp = schedule_module.body.operations[0] | ||
|
|
||
| schedule.apply(payload) # The actual transformation happens here. | ||
|
|
||
| print("NOTE: result of applying schedule to payload:") | ||
| print(payload) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets avoid generic names like
@example_payloadand use something descriptive instead. For example, what name would we use for the next example?@example_payload_1? That doesn't scale 😅How about
generate_payload_two_matmuls_and_add? We could skipgenerate_payloadif the filename was ...generate_payload.pyor something similar ;-) Yes, I do think that having separate files would help.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to preface, I see your point about setting a good example (pun intended) around naming.
Not sure if it's needed in this particular case. At least from the the perspective how I approach it here.
If we go with a more granular approach of multiple files with small examples working together (like you propose in another comment), then it might need different design approach, indeed.
I'd argue that specificity adds more information and implies that sth about the exact form/shape/implementation is important in a presented item. This addition can add to or distract from the core message.
I see this file as a self-contained example that focuses primarily on mechanism behind taking two MLIR modules: payload IR and a schedule, and executing them.
As such, I doubt there's need for scaling. Each standalone example script could have
@example_payloadas long as that specific payload doesn't matter for the overall idea we're communicating.This particular IR could be an empty function and little would change (% lit checks and perhaps some user confusion due to "uselessness" of a schedule doing effectively nothing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @adam-smnk ! That captures my perspective on what is happening here very well!