Skip to content

Migrating to One-Shot Bufferization #1027

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

Closed
wants to merge 192 commits into from
Closed

Conversation

tzunghanjuang
Copy link
Contributor

@tzunghanjuang tzunghanjuang commented Aug 15, 2024

Context:
MLIR removed old bufferization (Conversion-Based bufferization) passes and introduced new One-Shot-Bufferization pass. [Link]
Note that Ownership-based Buffer Deallocation will be handled in a different PR.

  • Only update llvm and mhlo. Jax sticks with 0.4.28.

Description of the Change:
Newly Added Bufferization Interface:
Comparing implemented interface methods with BufferizationOpInterface.td.
Operation List:
https://github.com/PennyLaneAI/catalyst/blob/8d0324db672d507d651b46fd65e9058455a9bb03/mlir/lib/Bufferization.md

Major Updates:

  • Create one-shot-bufferize interface for catalyst-bufferize, gradient-bufferize, and quantum-bufferize.
  • The operations to be bufferized should be registered with declarePromisedInterfaces.
  • Bufferize FunctionOPInterface like ForwardOP and ReverseOp need to access OneShotModuleBufferize.
  • Gradient.ReturnOP is ReturnLike now to support bufferization.
  • Set function-boundary-type-conversion to identity-layout-map to avoid invalid type conversion between Memref<T> and Memref<T, Stride<...>>
  • Use allow-return-allocs-from-loops flag to avoid scp.for and scp.while to avoid arg and yield types mismatch
  • Async tests need copy-before-write flag

Required LLVM and MHLO Patches:

Related GitHub Issues:
[sc-71487]
[Discourse discussion about migration]
[Discord discussion about FunctionOpInterface and FuncOp]
[Discord discussion about memory layout]
[LLVM Upstream]

[LLVM PR1]
[LLVM PR2]

llvm/llvm-project@7f04a8a

youtube link with discussion about one-shot bufferizer
pdf with info

@tzunghanjuang tzunghanjuang force-pushed the bufferization-update branch 2 times, most recently from 2c43ec4 to dce6cc2 Compare August 15, 2024 20:09
@erick-xanadu
Copy link
Contributor

This is awesome! Keep up the good work and let me know if you need some help :)

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/changelog.md on your branch with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link
Contributor

@erick-xanadu erick-xanadu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 @dime10 , I'll leave you some time to ask questions if you have any. If not let me know and I can merge.

@erick-xanadu
Copy link
Contributor

Adding do-not merge label to wait after the release.

paul0403 added a commit that referenced this pull request May 1, 2025
**Context:**
This work is based on #1027 

As part of the mlir update, the bufferization of the custom catalyst
dialects need to migrate to the new one-shot bufferization interface, as
opposed to the old pattern-rewrite style bufferization passes. See more
context in #1027.

As an example, here is how the new bufferization interface is used for
mlir's core `arith` dialect:

https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.cpp

https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.h

https://github.com/llvm/llvm-project/blob/7ee0097b486b31be8b9a1750b2cd47580efd9587/mlir/lib/Dialect/Arith/IR/ArithDialect.cpp#L54


**Description of the Change:**
On the current mlir commit we track, both the old and new bufferization
styles exist. The old pattern rewrite style is deprecated.

To ease the workflow organization, we migrate one dialect at a time.
This PR migrates the `Quantum` dialect's bufferization to the new
one-shot interface.

Note that the new one-shot interface is supposed to be called only once
in the pipeline. However, because we haven't migrated all the dialects
yet, we simply swap out the old `--quantum--bufferize` pass in-place,
with the new one-shot bufferization pass running on the quantum dialect
only.


**Benefits:**
Align with mlir practices; one step closer to updating mlir.


[sc-71487]

---------

Co-authored-by: Tzung-Han Juang <[email protected]>
Co-authored-by: paul0403 <[email protected]>
Co-authored-by: Paul <[email protected]>
Co-authored-by: David Ittah <[email protected]>
paul0403 added a commit that referenced this pull request May 12, 2025
**Context:**
This work is based on #1027 .

As part of the mlir update, the bufferization of the custom catalyst
dialects need to migrate to the new one-shot bufferization interface, as
opposed to the old pattern-rewrite style bufferization passes.
See more context in #1027.

The `Quantum` dialect was migrated in #1686 .

**Description of the Change:**
MIgrate `Catalyst` dialect to one-shot bufferization.

**Benefits:**
Align with mlir practices; one step closer to updating mlir.

[sc-71487]

---------

Co-authored-by: Tzung-Han Juang <[email protected]>
paul0403 added a commit that referenced this pull request May 23, 2025
**Context:**
This work is based on #1027 .

As part of the mlir update, the bufferization of the custom catalyst
dialects need to migrate to the new one-shot bufferization interface, as
opposed to the old pattern-rewrite style bufferization passes.
See more context in #1027.

The `Quantum` dialect was migrated in #1686 .
The `Catalyst` dialect was migrated in #1708 .

Note that #1139 refactors the gradient dialect's bufferization into
preprocess, bufferization, and postprocess.
Only the middle bufferization stage is supposed to be replaced by
one-shot bufferization.

**Description of the Change:**
Migrate `Gradient` dialect to one-shot bufferization.

**Benefits:**
Align with mlir practices; one step closer to updating mlir.

[sc-71487]

---------

Co-authored-by: Tzung-Han Juang <[email protected]>
paul0403 added a commit that referenced this pull request May 26, 2025
…1751)

**Context:**
This work is based on #1027.

Now that we have migrated all the individual dialects, we should migrate
the entire bufferization pipeline.

The `Quantum` dialect was migrated in
#1686 .
The `Catalyst` dialect was migrated in
#1708 .
The `Gradient` dialect was migrated in
#1740 .

See more context in #1027. 

Upstream changes in llvm were required for this bufferization update. As
a result, the llvm version and mlir-hlo version were updated to
```
mhlo=25b008569f413d76cfa8f481f3a84e82b89c47f4
llvm=5f74671c85877e03622e8d308aee15ed73ccee7c
```
These are the versions tracked by jax 0.4.32. 
These are the earliest jax-tagged versions with complete upstream
bufferization changes.

**Related GitHub Issues:**
[sc-71487]

---------

Co-authored-by: Tzung-Han Juang <[email protected]>
@paul0403
Copy link
Member

Closing in favor of #1686 , #1708 , #1740 and #1751 .

Thanks TzungHan for the good work! We have successfully migrated to one-shot-bufferization :)

@paul0403 paul0403 closed this May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author:build-wheels Run the wheel building workflows on this Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants