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

Implement detensorize-scf pass (for / if / while) #1075

Merged
merged 53 commits into from
Sep 24, 2024
Merged

Conversation

vincentmr
Copy link
Contributor

@vincentmr vincentmr commented Aug 29, 2024

Context:
After applying func.func(linalg-detensorize{aggressive-mode}), operations in the SCF dialect still request scalar tensors for their operands/arguments and yield scalar tensor results as well. This leads to the introduction of several tensor.from_elements and tensor.extract operations around the operation interface.

Description of the Change:
Introduce pass to detensorize scf::for / if / while. The while detensorization is currently limited to cases where the before/after blocks have the same operands.

Benefits:

Possible Drawbacks:

Related GitHub Issues:
[sc-71505]

@vincentmr vincentmr changed the title Implement detensorize-scf-for pass Implement detensorize-scf pass (for / if) Sep 4, 2024
@vincentmr vincentmr marked this pull request as ready for review September 4, 2024 20:06
@vincentmr vincentmr marked this pull request as draft September 4, 2024 20:09
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.

Just a few general comments:

  1. We can separate this into multiple functions
  2. If we can, I think we can separate this into two PatternOpRewriters, one for the IfOp, and one for the ForOp
  3. The walkers will walk in post-order. I think you may be able to get access to the YieldOp for the current op by looking at the terminator of the basic block of the op you are looking at. This could clean the logic behind first_yield.

@vincentmr
Copy link
Contributor Author

Hi Erick, thanks for the feedback.

  1. Agreed & done.
  2. It may not matter but I need to modify some code outside of the context of the op so I'm wondering whether PatternOpRewriters are the best thing here. I'll reach out in PV to discuss the strategy here.
  3. My previous use of replaceUsesWithIf in the retensorize-after-if block could cause conflicts between the FromElementsOp but we're good now. I'll just remove the logic.

Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

Thanks Vincent, this is nice work 💯

I left some info on things that would definitely not be obvious to someone starting to learn MLIR, hopefully you find these helpful :)
(There are still so many functions in the library I don't know about either 😅 )

mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
@vincentmr vincentmr marked this pull request as ready for review September 12, 2024 19:55
Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

Really nice work, it's coming together very well 💯

We talked about a slightly different structure for the while pattern so I'm leaving the for loop / while loop review until after that, but the if pattern is looking really good with some small cleanups :)

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
frontend/test/lit/test_detensorize.py Outdated Show resolved Hide resolved
mlir/test/Catalyst/DetensorizeSCFTest.mlir Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
mlir/lib/Catalyst/Transforms/DetensorizeSCFPass.cpp Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.87%. Comparing base (242244d) to head (5a39f9f).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1075   +/-   ##
=======================================
  Coverage   97.87%   97.87%           
=======================================
  Files          76       76           
  Lines       10847    10847           
  Branches     1282     1282           
=======================================
  Hits        10616    10616           
  Misses        179      179           
  Partials       52       52           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

mlir/test/Catalyst/DetensorizeSCFTest.mlir Outdated Show resolved Hide resolved
mlir/test/Catalyst/DetensorizeSCFTest.mlir Outdated Show resolved Hide resolved
mlir/include/Catalyst/Transforms/Passes.td Show resolved Hide resolved
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.

Awesome

@dime10 dime10 merged commit 4524773 into main Sep 24, 2024
42 checks passed
@dime10 dime10 deleted the detensorize_scf_pass branch September 24, 2024 13:41
rauletorresc pushed a commit that referenced this pull request Oct 9, 2024
**Context:**
After applying `func.func(linalg-detensorize{aggressive-mode})`,
operations in the SCF dialect still request scalar tensors for their
operands/arguments and yield scalar tensor results as well. This leads
to the introduction of several tensor.from_elements and tensor.extract
operations around the operation interface.

**Description of the Change:**
Introduce pass to detensorize `scf::for / if / while`. The while
detensorization is currently limited to
[cases](https://github.com/PennyLaneAI/catalyst/pull/1075/files#diff-f6ee410945b4f019017fb45560a6ae38d9568439e3933ff7aa1673b1094ecffbR217-R225)
where the before/after blocks have the same operands.

**Benefits:**

**Possible Drawbacks:**

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

---------

Co-authored-by: David Ittah <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants