Skip to content
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

Migrating to One-Shot Bufferization #1027

Open
wants to merge 192 commits into
base: main
Choose a base branch
from
Open

Conversation

tzunghanjuang
Copy link
Collaborator

@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.

struct CallbackOpInterface
: public bufferization::OpWithUnstructuredControlFlowBufferizableOpInterfaceExternalModel<
CallbackOpInterface, CallbackOp> {
static bool supportsUnstructuredControlFlow() { return true; }
Copy link
Contributor

Choose a reason for hiding this comment

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

We never use unstructured control flow in callback op. I've set this to false and it works. Can you expand on why this is needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was following the FuncOpInterface. I will set to as false.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you show also why exactly you needed to implement this interface? I am curious. Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to make the interface similar to FuncOp's. https://github.com/llvm/llvm-project/blob/f59b0c76030aff268b78d475e219708d06b982b5/mlir/lib/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.cpp#L325-L351

According to this LLVM commit llvm/llvm-project@6ecebb4,
we might only need it if cf.if appears in func.func. I remember last time I tested it and function like ops did not work without this interface.
I will try using the original interface BufferizableOpInterface::ExternalModel again to see what is going on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems we do not need this OpWithUnstructuredControlFlowBufferizableOpInterfaceExternalModel for CallbackOp. I will remove it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have tried removing OpWithUnstructuredControlFlowBufferizableOpInterfaceExternalModel from ForwardOp and ResverseOP but it would fail tests.

Sample error:

error: 'gradient.forward' op type of entry block argument #0('memref<f64, strided<[], offset: ?>>') must match the type of the corresponding argument in function signature('memref<f64>')

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this is the same error you had in a different place about the layout right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this data layout issue should be removed by identity-layout-map flag. Not sure why this error comes back even though the flag is already set.

@erick-xanadu erick-xanadu changed the title Migrating to One-Shot Bufferization (for new llvm versions with Jax >=0.4.29) Migrating to One-Shot Bufferization Oct 9, 2024
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.

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.

3 participants