Skip to content

1d dynamic-slice does not work #5876

Closed

Description

@hanhanW

Different from other cases, the subtensor would be replaced with flow.tensor.slice in this case. In this case, they won't get dispatched and tensor.extract would be remained outside dispatches.

func @dynamic_1d_slice() -> tensor<2xi32> attributes { iree.module.export } {
  %input = iree.unfoldable_constant dense<[1, 2, 3, 4]> : tensor<4xi32>
  %start1 = iree.unfoldable_constant dense<1> : tensor<i64>
  %result = "mhlo.dynamic-slice"(%input, %start1) {
    slice_sizes = dense<[2]> : tensor<1xi64>
  } : (tensor<4xi32>, tensor<i64>) -> tensor<2xi32>
  return %result : tensor<2xi32>
}

To repro:

$ iree-translate --iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=dylib-llvm-aot dynamic_slice.mlir

IR before ConvertToFlowTensorsOps:

// *** IR Dump Before ConvertToFlowTensorOps ***
func @dynamic_1d_slice() -> tensor<2xi32> attributes {iree.module.export, iree.reflection = {f = "I1!R8!B5!t6d2", fv = "1"}} {
  %cst = constant dense<[1, 2, 3, 4]> : tensor<4xi32>
  %cst_0 = constant dense<1> : tensor<i32>
  %c0_i32 = constant 0 : i32
  %c2_i32 = constant 2 : i32
  %0 = iree.do_not_optimize(%cst) : tensor<4xi32>
  %1 = iree.do_not_optimize(%cst_0) : tensor<i32>
  %2 = tensor.extract %1[] : tensor<i32>
  %3 = cmpi slt, %2, %c2_i32 : i32
  %4 = select %3, %2, %c2_i32 : i32
  %5 = cmpi sgt, %4, %c0_i32 : i32
  %6 = select %5, %4, %c0_i32 : i32
  %7 = index_cast %6 : i32 to index
  %8 = subtensor %0[%7] [2] [1] : tensor<4xi32> to tensor<2xi32>
  return %8 : tensor<2xi32>
}

IR after ConvertToFlowTensorsOps:

// *** IR Dump Before CSE ***
func @dynamic_1d_slice() -> tensor<2xi32> attributes {iree.module.export, iree.reflection = {f = "I1!R8!B5!t6d2", fv = "1"}} {
  %cst = constant dense<[1, 2, 3, 4]> : tensor<4xi32>
  %cst_0 = constant dense<1> : tensor<i32>
  %c0_i32 = constant 0 : i32
  %c2_i32 = constant 2 : i32
  %c2 = constant 2 : index
  %0 = iree.do_not_optimize(%cst) : tensor<4xi32>
  %1 = iree.do_not_optimize(%cst_0) : tensor<i32>
  %2 = tensor.extract %1[] : tensor<i32>
  %3 = cmpi slt, %2, %c2_i32 : i32
  %4 = select %3, %2, %c2_i32 : i32
  %5 = cmpi sgt, %4, %c0_i32 : i32
  %6 = select %5, %4, %c0_i32 : i32
  %7 = index_cast %6 : i32 to index
  %8 = flow.tensor.slice %0[%7 for %c2] : tensor<4xi32> -> tensor<2xi32>
  return %8 : tensor<2xi32>
}

Activity

  1. added
    codegen
    on May 13, 2021
  2. MaheshRavishankar commented on May 13, 2021

    @MaheshRavishankar
    Collaborator

    Is this on ToT?

  3. MaheshRavishankar commented on May 13, 2021

    @MaheshRavishankar
    Collaborator

    Making the check here https://github.com/google/iree/blob/dd2a8def906e67ec8e8c66cb58372e21ab01ec92/iree/compiler/Dialect/Flow/Transforms/ConvertToFlowTensorOps.cpp#L55

    if ((dim != 0 && (!matchVal(offsets[dim], 0) ||
                      !matchVal(sizes[dim], baseShape[dim])) ||
        (dim == 0 && (offsets[dim] == ShapedType::kDynamicStrideAndOffset))
    

    should fix the issue

  4. hanhanW commented on May 13, 2021

    @hanhanW
    ContributorAuthor

    Yes, it's on ToT. I will give it a shot.

  5. hanhanW commented on May 13, 2021

    @hanhanW
    ContributorAuthor

    I think this works:

         if ((dim != 0 && (!matchVal(offsets[dim], 0) ||
                           !matchVal(sizes[dim], baseShape[dim]))) ||
             (dim == 0 &&
              !matchVal(offsets[dim], ShapedType::kDynamicStrideOrOffset))) {

    side note:
    I probably did it wrong, I will hit other issues like orderedOps.size() == ops.size() && "ordering of inlined operations failed" in ASR model. Will file another issue for it later.

  6. hanhanW commented on May 13, 2021

    @hanhanW
    ContributorAuthor

    I'll leave it to you and work on other things.

  7. MaheshRavishankar commented on May 13, 2021

    @MaheshRavishankar
    Collaborator

    ok, ill take a look

  8. added a commit that references this issue on May 13, 2021
    1c31cde
  9. added a commit that references this issue on May 13, 2021
    18e8991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions