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

Missing Transpose of Binary Op optimization #210

Open
avik-pal opened this issue Jan 2, 2025 · 3 comments
Open

Missing Transpose of Binary Op optimization #210

avik-pal opened this issue Jan 2, 2025 · 3 comments
Labels
good first issue Good for newcomers

Comments

@avik-pal
Copy link
Collaborator

avik-pal commented Jan 2, 2025

module {
  func.func @main(%arg0: tensor<16x4xf32>) -> tensor<16x4xf32> {
    %0 = stablehlo.transpose %arg0, dims = [1, 0] : (tensor<16x4xf32>) -> tensor<4x16xf32>
    %1 = stablehlo.subtract %0, %0 : tensor<4x16xf32>
    %2 = stablehlo.transpose %1, dims = [1, 0] : (tensor<4x16xf32>) -> tensor<16x4xf32>
    return %2 : tensor<16x4xf32>
  }
}

missed it in my last PR. Probably not working since %0 has 2 uses, which can be specialized here

@avik-pal avik-pal added the good first issue Good for newcomers label Jan 2, 2025
@wsmoses
Copy link
Member

wsmoses commented Jan 2, 2025

Perhaps the better check would be “only used in this op” rather than a specific use count

@avik-pal
Copy link
Collaborator Author

avik-pal commented Jan 3, 2025

How do I run the transpose binary_op optimization before transpose -> reshape optimization? Else we get this which could have been just sqrt and divide

module {
  func.func @main(%arg0: tensor<4x1x1x1xf32>) -> tensor<4x1x1x1xf32> {
    %cst = stablehlo.constant dense<1.000000e+00> : tensor<1x1x1x4xf32>
    %0 = stablehlo.sqrt %arg0 : tensor<4x1x1x1xf32>
    %1 = stablehlo.reshape %0 : (tensor<4x1x1x1xf32>) -> tensor<1x1x1x4xf32>
    %2 = stablehlo.divide %cst, %1 : tensor<1x1x1x4xf32>
    %3 = stablehlo.reshape %2 : (tensor<1x1x1x4xf32>) -> tensor<4x1x1x1xf32>
    return %3 : tensor<4x1x1x1xf32>
  }
}

@wsmoses
Copy link
Member

wsmoses commented Jan 3, 2025

Longer term there's some ongoing work to use e-graphs to automatically search for what the optimal ordering of rewrite rules is [and this is a useful example] (cc @aryavohra @smjleo @ftynse @albertcohen @jbachurski )

However here probably the solution is to generalize the rsqrt optimization to handle intermediate reshape/transposes (which is done similarly elsewhere)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants