-
Notifications
You must be signed in to change notification settings - Fork 25
Extend DotGeneral CSE to recognize operand-swapped equivalence #1726
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
8
commits into
main
Choose a base branch
from
copilot/expand-dotgeneral-cse-pass
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.
+180
−1
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
83ea63f
Initial plan
Copilot e8bfe48
Add specialized CSE pattern for DotGeneralOp with operand swapping
Copilot 4d34104
Update test cases to only include valid CSE scenarios
Copilot 21865da
Refactor CSEDotGeneral to eliminate code duplication
Copilot b46b0fa
Address code review feedback: extract dimension checking helper and r…
Copilot fb4a147
Fix trailing newlines in test file
Copilot f288f1b
Further refactor to eliminate remaining code duplication
Copilot 9dea61e
Refactor CSEDotGeneral to reuse more of the generic CSE pattern struc…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // RUN: enzymexlamlir-opt --enzyme-hlo-opt %s | FileCheck %s | ||
|
|
||
| // Test that dot_general CSE recognizes swapped operands as equivalent | ||
| // when the dimension numbers are also appropriately swapped | ||
|
|
||
| // CHECK-LABEL: func.func @test_dotgeneral_cse_symmetric | ||
| func.func @test_dotgeneral_cse_symmetric(%arg0: tensor<4x4xf32>, %arg1: tensor<4x4xf32>) -> (tensor<4xf32>, tensor<4xf32>) { | ||
| // Both operations have symmetric dimension specs: batching_dims = [0] x [0], contracting_dims = [1] x [1] | ||
| // This computes: for each b in [0,4), result[b] = sum_k arg0[b,k] * arg1[b,k] | ||
| // When operands are swapped with these symmetric specs, the operations are equivalent | ||
| // because multiplication is commutative: arg0[b,k] * arg1[b,k] = arg1[b,k] * arg0[b,k] | ||
| %0 = stablehlo.dot_general %arg0, %arg1, batching_dims = [0] x [0], contracting_dims = [1] x [1] : (tensor<4x4xf32>, tensor<4x4xf32>) -> tensor<4xf32> | ||
| %1 = stablehlo.dot_general %arg1, %arg0, batching_dims = [0] x [0], contracting_dims = [1] x [1] : (tensor<4x4xf32>, tensor<4x4xf32>) -> tensor<4xf32> | ||
| return %0, %1 : tensor<4xf32>, tensor<4xf32> | ||
| } | ||
|
|
||
| // CHECK: %[[V0:.+]] = stablehlo.dot_general %arg0, %arg1 | ||
| // CHECK-NEXT: return %[[V0]], %[[V0]] | ||
|
|
||
| // CHECK-LABEL: func.func @test_dotgeneral_cse_with_batching | ||
| func.func @test_dotgeneral_cse_with_batching(%arg0: tensor<2x3x4xf32>, %arg1: tensor<2x3x4xf32>) -> (tensor<2xf32>, tensor<2xf32>) { | ||
| // With batching_dims = [0] x [0] and contracting_dims = [1, 2] x [1, 2] | ||
| // This computes: for each b in [0,2), result[b] = sum_{i,j} arg0[b,i,j] * arg1[b,i,j] | ||
| // These are equivalent when operands are swapped (Frobenius inner product is commutative) | ||
| %0 = stablehlo.dot_general %arg0, %arg1, batching_dims = [0] x [0], contracting_dims = [1, 2] x [1, 2] : (tensor<2x3x4xf32>, tensor<2x3x4xf32>) -> tensor<2xf32> | ||
| %1 = stablehlo.dot_general %arg1, %arg0, batching_dims = [0] x [0], contracting_dims = [1, 2] x [1, 2] : (tensor<2x3x4xf32>, tensor<2x3x4xf32>) -> tensor<2xf32> | ||
| return %0, %1 : tensor<2xf32>, tensor<2xf32> | ||
| } | ||
|
|
||
| // CHECK: %[[V0:.+]] = stablehlo.dot_general %arg0, %arg1 | ||
| // CHECK-NEXT: return %[[V0]], %[[V0]] | ||
|
|
||
| // CHECK-LABEL: func.func @test_dotgeneral_no_cse_different_shapes | ||
| func.func @test_dotgeneral_no_cse_different_shapes(%arg0: tensor<4x8xf32>, %arg1: tensor<8x4xf32>) -> (tensor<4x4xf32>, tensor<8x8xf32>) { | ||
| // These have different output shapes and should NOT be CSE'd | ||
| // First: A(4x8) × B(8x4) → (4x4) | ||
| // Second: B(8x4) × A(4x8) with same contracting dims → (8x8) | ||
| %0 = stablehlo.dot_general %arg0, %arg1, contracting_dims = [1] x [0] : (tensor<4x8xf32>, tensor<8x4xf32>) -> tensor<4x4xf32> | ||
| %1 = stablehlo.dot_general %arg1, %arg0, contracting_dims = [1] x [0] : (tensor<8x4xf32>, tensor<4x8xf32>) -> tensor<8x8xf32> | ||
| return %0, %1 : tensor<4x4xf32>, tensor<8x8xf32> | ||
| } | ||
|
|
||
| // CHECK: %[[V0:.+]] = stablehlo.dot_general %arg0, %arg1 | ||
| // CHECK-NEXT: %[[V1:.+]] = stablehlo.dot_general %arg1, %arg0 | ||
| // CHECK-NEXT: return %[[V0]], %[[V1]] | ||
|
|
||
| // CHECK-LABEL: func.func @test_dotgeneral_no_cse_different_computation | ||
| func.func @test_dotgeneral_no_cse_different_computation(%arg0: tensor<4x8xf32>, %arg1: tensor<8x4xf32>) -> (tensor<4x4xf32>, tensor<4x4xf32>) { | ||
| // These have the same output shape but compute different things | ||
| // First: A(4x8) × B(8x4) → C(4x4) where C[i,j] = sum_k A[i,k] * B[k,j] | ||
| // Second: B(8x4) × A(4x8) with contracting_dims=[0]x[1] → D(4x4) where D[i,j] = sum_k B[k,i] * A[j,k] | ||
| // These are NOT equivalent (D is not the same as C) | ||
| %0 = stablehlo.dot_general %arg0, %arg1, contracting_dims = [1] x [0] : (tensor<4x8xf32>, tensor<8x4xf32>) -> tensor<4x4xf32> | ||
| %1 = stablehlo.dot_general %arg1, %arg0, contracting_dims = [0] x [1] : (tensor<8x4xf32>, tensor<4x8xf32>) -> tensor<4x4xf32> | ||
| return %0, %1 : tensor<4x4xf32>, tensor<4x4xf32> | ||
| } | ||
|
|
||
| // CHECK: %[[V0:.+]] = stablehlo.dot_general %arg0, %arg1 | ||
| // CHECK-NEXT: %[[V1:.+]] = stablehlo.dot_general %arg1, %arg0 | ||
| // CHECK-NEXT: return %[[V0]], %[[V1]] | ||
|
|
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 can we reuse more of the CSE struct
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.
Refactored to follow the structure of the generic CSE pattern more closely. The implementation now directly mirrors the CSE template's iteration and checking logic, only adding the DotGeneral-specific swapped equivalence check. Commit: 9dea61e