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

transforms: add restrict flag to StencilShapeMinimize pass #3411

Merged
merged 14 commits into from
Nov 12, 2024

Conversation

emmau678
Copy link
Contributor

@emmau678 emmau678 commented Nov 8, 2024

Add a restrict flag to the StencilShapeMinimize pass which restricts the grid size of a stencil computation.

@emmau678 emmau678 self-assigned this Nov 8, 2024
@superlopuh
Copy link
Member

Could you please complete the checklist in the description?

@emmau678 emmau678 requested a review from n-io November 8, 2024 11:13
@emmau678 emmau678 added the transformations Changes or adds a transformatio label Nov 8, 2024
xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
emmau678 and others added 3 commits November 8, 2024 13:30
Co-authored-by: Nicolai Stawinoga <36768051+n-io@users.noreply.github.com>
Co-authored-by: Nicolai Stawinoga <36768051+n-io@users.noreply.github.com>
Copy link

codecov bot commented Nov 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.17%. Comparing base (a731a28) to head (3bc2d9c).
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3411      +/-   ##
==========================================
+ Coverage   90.15%   90.17%   +0.01%     
==========================================
  Files         455      456       +1     
  Lines       57429    57562     +133     
  Branches     5530     5543      +13     
==========================================
+ Hits        51775    51905     +130     
- Misses       4195     4197       +2     
- Partials     1459     1460       +1     

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

@emmau678 emmau678 marked this pull request as ready for review November 8, 2024 14:15
@emmau678 emmau678 requested a review from n-io November 8, 2024 14:16
Copy link
Collaborator

@n-io n-io left a comment

Choose a reason for hiding this comment

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

Great, while there are ways to combine this into one filecheck mlir file (if you grep for --check-prefix), you could also make a new one. Happy to help with either way.

xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
xdsl/transforms/stencil_shape_minimize.py Outdated Show resolved Hide resolved
emmau678 and others added 2 commits November 8, 2024 14:48
Co-authored-by: Nicolai Stawinoga <36768051+n-io@users.noreply.github.com>
@emmau678
Copy link
Contributor Author

emmau678 commented Nov 8, 2024

Great, while there are ways to combine this into one filecheck mlir file (if you grep for --check-prefix), you could also make a new one. Happy to help with either way.

any help on how to get started on this would be greatly appreciated!

@n-io
Copy link
Collaborator

n-io commented Nov 8, 2024

Great, while there are ways to combine this into one filecheck mlir file (if you grep for --check-prefix), you could also make a new one. Happy to help with either way.

any help on how to get started on this would be greatly appreciated!

I just pushed a filecheck to your branch, but it will likely break. The last thing to do is to run shape inference (ShapeInferencePass().apply(ctx, op)) inside the if self.restrict conditional to tie everything together. Wanna do the honours? :)

@@ -91,6 +92,7 @@ def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
]
)
).rewrite_module(op)
ShapeInferencePass().apply(ctx, op)
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend against this

Copy link
Member

Choose a reason for hiding this comment

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

@n-io, what's the motivation for doing this inside the pass and not as part of the pass pipeline?

Copy link
Collaborator

@n-io n-io Nov 8, 2024

Choose a reason for hiding this comment

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

The most immediate purpose is to undo the InvalidateTemps() rewrite pattern. The pass in general is super useful for restricting the stencil grid size to something small enough that for instance it can be run in a simulator. The idea is simple: invalidate temps, modify size on store ops, un-invalidate temps. The result will then be picked up by stencil-shape-minimize, which will handle the rest. The only alternative would be to separate this out from shape-minimize altogether, though running stencil-shape-minimize{restrict=30,30,30} seems to make a lot of sense in my head instead of having it as a fully independent pass, and I do think they have to be run together in order to produce a program that passes validation (which we want after every logical pass).

Copy link
Member

Choose a reason for hiding this comment

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

Ah OK, that makes sense. The main thing that triggered me was calling a pass inside of another pass, for the same reason, as the contract is that a pass leaves the IR in a valid state, if the IR is in a valid state to begin with. In that sense it would make sense to split out the body of the shape inference pass into a helper method, and to call into that from here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've seen this done in a few places, though was never sure why helper methods are preferred, including by post_walk_func?

Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that it's preferable to expose the least restrictive API when possible. For example, there's no need to pass the context when performing shape inference. It's probably also to make things more flexible in general, like you may want to only perform shape inference on a subset of your module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I follow - do I need to make some updates here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please! Could pull out the body of the shape inference pass into a python function that takes any operation, and no MLContext, and call that helper function from here instead of the pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, thank you!

emmau678 and others added 2 commits November 11, 2024 09:07
Co-authored-by: Nicolai Stawinoga <36768051+n-io@users.noreply.github.com>
Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

It would probably be good to get @n-io's tick before merging, but LGTM!

Copy link
Collaborator

@n-io n-io left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge!

xdsl/transforms/stencil_shape_minimize.py Show resolved Hide resolved
@emmau678 emmau678 merged commit e971815 into main Nov 12, 2024
15 checks passed
@emmau678 emmau678 deleted the add_restrict_flag branch November 12, 2024 14:07
EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
…ct#3411)

Add a restrict flag to the StencilShapeMinimize pass which restricts the
grid size of a stencil computation.

---------

Co-authored-by: emmau678 <eu233@Emma-laptop>
Co-authored-by: Nicolai Stawinoga <36768051+n-io@users.noreply.github.com>
Co-authored-by: n-io <n-io@users.noreply.github.com>
Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants