-
Notifications
You must be signed in to change notification settings - Fork 26
Add ReverseConstProp pattern to fold reverse of constant tensors #1655
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-reverse-of-constant
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3295456
Add ReverseConstProp pattern to fold reverse of constant tensors
Copilot 2e05e38
Handle splat constants in ReverseConstProp by replacing with operand
Copilot ac4e80a
Add ReverseConstProp pattern to transform dialect
Copilot c0bb87e
Change splat constant test to use huge tensor size above max threshold
Copilot f417b75
Add reverse_const_prop pattern to primitives.py
Copilot e977cfb
Add addReverseConstProp function and populatePatterns implementation …
Copilot 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // RUN: enzymexlamlir-opt --pass-pipeline="builtin.module(enzyme-hlo-opt)" %s | FileCheck %s | ||
|
|
||
| // Test 1: Reverse a non-splat constant along one dimension | ||
| module { | ||
| func.func @main() -> tensor<4xf64> { | ||
| %cst = stablehlo.constant dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf64> | ||
| %0 = stablehlo.reverse %cst, dims = [0] : tensor<4xf64> | ||
| return %0 : tensor<4xf64> | ||
| } | ||
| } | ||
|
|
||
| // CHECK: func.func @main() -> tensor<4xf64> { | ||
| // CHECK-NEXT{LITERAL}: %cst = stablehlo.constant dense<[4.000000e+00, 3.000000e+00, 2.000000e+00, 1.000000e+00]> : tensor<4xf64> | ||
| // CHECK-NEXT: return %cst : tensor<4xf64> | ||
| // CHECK-NEXT: } | ||
|
|
||
| // Test 2: Reverse a 2D constant along both dimensions | ||
| module { | ||
| func.func @main() -> tensor<2x3xf64> { | ||
| %cst = stablehlo.constant dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]> : tensor<2x3xf64> | ||
| %0 = stablehlo.reverse %cst, dims = [0, 1] : tensor<2x3xf64> | ||
| return %0 : tensor<2x3xf64> | ||
| } | ||
| } | ||
|
|
||
| // CHECK: func.func @main() -> tensor<2x3xf64> { | ||
| // CHECK-NEXT{LITERAL}: %cst = stablehlo.constant dense<[[6.000000e+00, 5.000000e+00, 4.000000e+00], [3.000000e+00, 2.000000e+00, 1.000000e+00]]> : tensor<2x3xf64> | ||
| // CHECK-NEXT: return %cst : tensor<2x3xf64> | ||
| // CHECK-NEXT: } | ||
|
|
||
| // Test 3: Reverse a 2D constant along just the first dimension | ||
| module { | ||
| func.func @main() -> tensor<2x3xf64> { | ||
| %cst = stablehlo.constant dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]> : tensor<2x3xf64> | ||
| %0 = stablehlo.reverse %cst, dims = [0] : tensor<2x3xf64> | ||
| return %0 : tensor<2x3xf64> | ||
| } | ||
| } | ||
|
|
||
| // CHECK: func.func @main() -> tensor<2x3xf64> { | ||
| // CHECK-NEXT{LITERAL}: %cst = stablehlo.constant dense<[[4.000000e+00, 5.000000e+00, 6.000000e+00], [1.000000e+00, 2.000000e+00, 3.000000e+00]]> : tensor<2x3xf64> | ||
| // CHECK-NEXT: return %cst : tensor<2x3xf64> | ||
| // CHECK-NEXT: } | ||
|
|
||
| // Test 4: Reverse a 2D constant along just the second dimension | ||
| module { | ||
| func.func @main() -> tensor<2x3xf64> { | ||
| %cst = stablehlo.constant dense<[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]> : tensor<2x3xf64> | ||
| %0 = stablehlo.reverse %cst, dims = [1] : tensor<2x3xf64> | ||
| return %0 : tensor<2x3xf64> | ||
| } | ||
| } | ||
|
|
||
| // CHECK: func.func @main() -> tensor<2x3xf64> { | ||
| // CHECK-NEXT{LITERAL}: %cst = stablehlo.constant dense<[[3.000000e+00, 2.000000e+00, 1.000000e+00], [6.000000e+00, 5.000000e+00, 4.000000e+00]]> : tensor<2x3xf64> | ||
| // CHECK-NEXT: return %cst : tensor<2x3xf64> | ||
| // CHECK-NEXT: } | ||
|
|
||
| // Test 5: Reverse a splat constant - should replace with operand | ||
| // Use a huge tensor shape that is clearly above any max threshold | ||
| module { | ||
| func.func @main() -> tensor<1000000xf64> { | ||
| %cst = stablehlo.constant dense<5.0> : tensor<1000000xf64> | ||
| %0 = stablehlo.reverse %cst, dims = [0] : tensor<1000000xf64> | ||
| return %0 : tensor<1000000xf64> | ||
| } | ||
| } | ||
|
|
||
| // CHECK: func.func @main() -> tensor<1000000xf64> { | ||
| // CHECK-NEXT: %cst = stablehlo.constant dense<5.000000e+00> : tensor<1000000xf64> | ||
| // CHECK-NEXT: return %cst : tensor<1000000xf64> | ||
| // CHECK-NEXT: } |
Oops, something went wrong.
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.
@copilot you also need to add this to the list of patterns in the transform dialect, look at earlier PRs like #1653 for examples (see src/enzyme_ad/jax/TransformOps/TransformOps.td)
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.
Done in commit 992788b. Added
ApplyReverseConstPropPatternsto TransformOps.td following the same pattern as other parameterized constant propagation patterns.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.
@copilot add this pattern in primitives.py along with the other const prop patterns
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.
Done in commit 577ea72. Added
reverse_const_prop({max_constant_threshold})to the constant propagation patterns section in primitives.py.