Skip to content

Generalize the tensor.gather to linalg.generic #412

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

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

zhczhong
Copy link
Member

The tensor.gather is not bufferized and here add a pass to transform it to linalg.generic so that it could be further lowered correctly.(Also add tensor::populateDecomposeTensorConcatPatterns to handle tensor.concat)

func.func @gather_single_gather_dim(%arg0: tensor<2x2x2x2xf32>, %arg1: tensor<2x3x1xindex>) -> tensor<2x3x2x2x2xf32> {
tensor<2x3x1xindex>) outs(%0 : tensor<2x3x2x2x2xf32>)
  %1 = tensor.gather %arg0[%arg1] gather_dims([1]) : (tensor<2x2x2x2xf32>, tensor<2x3x1xindex>) -> tensor<2x3x2x2x2xf32>
  return %1 : tensor<2x3x2x2x2xf32>
}

to

  func.func @gather_single_gather_dim(%arg0: tensor<2x2x2x2xf32>, %arg1: tensor<2x3x1xindex>) -> tensor<2x3x2x2x2xf32> {
    %0 = tensor.empty() : tensor<2x3x2x2x2xf32>
    %1 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]} ins(%arg1 : tensor<2x3x1xindex>) outs(%0 : tensor<2x3x2x2x2xf32>) {
    ^bb0(%in: index, %out: f32):
      %2 = linalg.index 2 : index
      %3 = linalg.index 3 : index
      %4 = linalg.index 4 : index
      %extracted = tensor.extract %arg0[%2, %in, %3, %4] : tensor<2x2x2x2xf32>
      linalg.yield %extracted : f32
    } -> tensor<2x3x2x2x2xf32>
    return %1 : tensor<2x3x2x2x2xf32>
  }

Copy link
Contributor

@dchigarev dchigarev left a comment

Choose a reason for hiding this comment

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

Looks good overall, left a few questions regarding the logic

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it work with dynamic shapes?

Copy link
Member Author

Choose a reason for hiding this comment

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

The dynamic support and the corresponding test is added

@kurapov-peter kurapov-peter force-pushed the zhicong/tensor_transform branch from 0e575af to 42a1a9f Compare November 27, 2024 11:55
@kurapov-peter kurapov-peter merged commit 238706b into main Nov 27, 2024
6 checks passed
@kurapov-peter kurapov-peter deleted the zhicong/tensor_transform branch November 27, 2024 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants