Skip to content

[DispatchCreation] Improve BubbleExpandThroughExtract #21015

Closed

Description

@IanWood1

BubbleExpandThroughExtract converts extract_slice -> expand_shape to expand_shape -> extract_slice. This allows cloning the extract_slice into it's consumer dispatch. This is useful in the case where the extract_slice is not contiguous and results in a slow memcpy.

However, this pattern is limited. From the pattern's comment:

/// Bubbles a `tensor.expand_shape` op through a `tensor.extract_slice` op. This
/// pattern only gets applied when the `extract_slice` doesn't modify dimensions
/// that are expanded by the `expand_shape` and when the `extract_slice` is
/// completely static.

Some examples:

/// In the case below it should be possible to "bubble"
util.func public @possible(%arg0 : tensor<4096xf16>) -> (tensor<32x64xf16>) {
  %extracted_slice = tensor.extract_slice %arg0[0] [2048] [1] : tensor<4096xf16> to tensor<2048xf16>
  %expanded_239 = tensor.expand_shape %extracted_slice[[0, 1]] output_shape [32, 64] : tensor<2048xf16> into tensor<32x64xf16>
  util.return %expanded_239 : tensor<32x64xf16>
}

/// Can't bubble in this case
util.func public @impossible(%arg0 : tensor<2049xf16>) -> (tensor<32x64xf16>) {
  %extracted_slice= tensor.extract_slice %arg0[0] [2048] [1] : tensor<2049xf16> to tensor<2048xf16>
  %expanded_239 = tensor.expand_shape %extracted_slice[[0, 1]] output_shape [32, 64] : tensor<2048xf16> into tensor<32x64xf16>
  util.return %expanded_239 : tensor<32x64xf16>
}

I think the main constraint is that the product of all but the last output dimension must divide the input (pre-extracted) shape. I.e. 32 divides 4096 making the first one possible but 32 does not divide 2049 making it impossible.

Activity

  1. IanWood1 commented on Jun 16, 2025

    @IanWood1
    MemberAuthor

    @MaheshRavishankar here's an example of what we discussed:

    // Before propagation
    util.func public @main(%arg0 : tensor<?xf16>, %val : index) -> (tensor<32x?xf16>) {
      %extracted_slice = tensor.extract_slice %arg0[0] [%val] [1] : tensor<?xf16> to tensor<?xf16>
      %cst32 = arith.constant 32 : index
      %div = arith.divsi %val, %cst32 : index
      %expanded_239 = tensor.expand_shape %extracted_slice[[0, 1]] output_shape [32, %div] : tensor<?xf16> into tensor<32x?xf16>
      util.return %expanded_239 : tensor<32x?xf16>
    }
    
    
    // After propagation
    util.func public @main(%arg0 : tensor<?xf16>, %val : index) -> (tensor<32x?xf16>) {
      %cst = arith.constant 0 : index
      %cst32 = arith.constant 32 : index
      %dim = tensor.dim %arg0, %cst : tensor<?xf16>
      // 32 needs to divide %dim
      %div = arith.divsi %dim, %cst32 : index
      %expanded_239 = tensor.expand_shape %arg0[[0, 1]] output_shape [32, %div] : tensor<?xf16> into tensor<32x?xf16>
      %extracted_slice = tensor.extract_slice %expanded_239[0, 0] [32, %val] [1, 1] : tensor<32x?xf16> to tensor<32x?xf16>
      util.return %expanded_239 : tensor<32x?xf16>
    }
    

    The dynamic dim of %arg0 need to be known to be divisible by 32 to be able to propagate.

  2. IanWood1 commented on Jun 18, 2025

    @IanWood1
    MemberAuthor

    I was a bit wrong above, here is the actual case that need to be bubbled:

      %extracted_slice_446 = tensor.extract_slice %expanded_444
        [0, 31, 0, 0, 0, 0] 
        [%9, 1, 1, 8, 32, 128] 
        [1, 1, 1, 1, 1, 1] 
        : tensor<?x32x2x8x32x128xf8E4M3FNUZ> to tensor<?x8x32x128xf8E4M3FNUZ>
      %expanded_447 = tensor.expand_shape %extracted_slice_446 
        [[0], [1], [2], [3, 4]] 
        output_shape [%9, 8, 32, 2, 64] 
        : tensor<?x8x32x128xf8E4M3FNUZ> into tensor<?x8x32x2x64xf8E4M3FNUZ>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions