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(Unary(...)) optimizations #213

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

Missing Transpose(Unary(...)) optimizations #213

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

Comments

@avik-pal
Copy link
Collaborator

avik-pal commented Jan 3, 2025

module {
  func.func @main(%arg0: tensor<1x3x2x2xf32>) -> (tensor<1x3x2x2xf32>, tensor<1x3x2x2xf32>) {
    %cst = stablehlo.constant dense<0.317560464> : tensor<1x3x2x2xf32>
    %cst_0 = stablehlo.constant dense<1.23323452> : tensor<1x3x2x2xf32>
    %0 = stablehlo.multiply %cst_0, %arg0 : tensor<1x3x2x2xf32>
    %1 = stablehlo.add %cst, %0 : tensor<1x3x2x2xf32>
    %2 = stablehlo.transpose %1, dims = [3, 2, 1, 0] : (tensor<1x3x2x2xf32>) -> tensor<2x2x3x1xf32>
    %3 = stablehlo.cosine %2 : tensor<2x2x3x1xf32>
    %4 = stablehlo.sine %2 : tensor<2x2x3x1xf32>
    %5 = stablehlo.transpose %4, dims = [3, 2, 1, 0] : (tensor<2x2x3x1xf32>) -> tensor<1x3x2x2xf32>
    %6 = stablehlo.transpose %3, dims = [3, 2, 1, 0] : (tensor<2x2x3x1xf32>) -> tensor<1x3x2x2xf32>
    return %5, %6 : tensor<1x3x2x2xf32>, tensor<1x3x2x2xf32>
  }
}
@avik-pal avik-pal added the good first issue Good for newcomers label Jan 3, 2025
@avik-pal
Copy link
Collaborator Author

avik-pal commented Jan 3, 2025

This is probably a bit more involved, we need to check if uses of %2 are in unary operations where eliminating the transpose is a valid op.

    %2 = stablehlo.transpose %1, dims = [3, 2, 1, 0] : (tensor<1x3x2x2xf32>) -> tensor<2x2x3x1xf32>
    %3 = stablehlo.cosine %2 : tensor<2x2x3x1xf32>
    %4 = stablehlo.sine %2 : tensor<2x2x3x1xf32>

@avik-pal
Copy link
Collaborator Author

avik-pal commented Jan 3, 2025

We need 2 transforms here:

  • Transpose -> Unary Op -> Transpose can be simplified to Unary Op
  • Transpose -> Unary Op can be simplified to Unary Op -> Transpose. Only apply this if the transpose is being used with other unary ops

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

1 participant