Skip to content

Unsupported rank-reducing subtensor op lowering to flow ops #5476

Closed

Description

@hanhanW

Talked to @MaheshRavishankar , rank-reducing subtensor op can't be lowered to Flow::TensorSliceOp. Instead, it should be Flow::TensorReshapeOp.

https://github.com/google/iree/blob/59bf64f80f78262ae93a698074097f4e15eb1a8f/iree/compiler/Dialect/Flow/Transforms/ConvertToFlowTensorOps.cpp#L77-L82

I have a local patch to work with my issue in ASR, but I don't know if it is a proper fix. Since @MaheshRavishankar is working on related things and it's probably related to #5392. I'll leave it to him.

This is my workaround snippet, which is modified from above snippet.

    Value source = subTensorOp.source();
    if (subTensorOp.getSourceType().getRank() !=
        subTensorOp.getType().getRank()) {
      SmallVector<Value, 4> sourceDynSizes, resultDynSizes;
      auto sourceType = subTensorOp.getSourceType();
      for (auto i : llvm::seq<unsigned>(0, sourceType.getNumDynamicDims())) {
        sourceDynSizes.push_back(rewriter.create<ConstantIndexOp>(
            loc, sourceType.getDynamicDimIndex(i)));
      }

      auto resultType = subTensorOp.getType();
      for (auto i : llvm::seq<unsigned>(0, resultType.getNumDynamicDims())) {
        resultDynSizes.push_back(rewriter.create<ConstantIndexOp>(
            loc, resultType.getDynamicDimIndex(i)));
      }

      rewriter.replaceOpWithNewOp<TensorReshapeOp>(
          subTensorOp, resultType, source, sourceDynSizes, resultDynSizes);
    } else {
      SmallVector<Value, 4> sourceSizesVals = sizesVals;
      sourceSizesVals[0] = rewriter.createOrFold<memref::DimOp>(loc, source, 0);
      rewriter.replaceOpWithNewOp<TensorSliceOp>(
          subTensorOp, subTensorOp.getType(), subTensorOp.source(),
          sourceSizesVals, offsetVals, sizesVals, sizesVals);
    }

To repro:

func @dynamic_slice(%arg: tensor<2x513xi16>, %start1: tensor<i64>, %start2: tensor<i64>) -> tensor<513xi16> {
  %0 = "mhlo.dynamic-slice"(%arg, %start1, %start2) {
    slice_sizes = dense<[1, 513]> : tensor<2xi64>
  } : (tensor<2x513xi16>, tensor<i64>, tensor<i64>) -> tensor<513xi16>
  return %0 : tensor<513xi16>
}

$ iree-translate --iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=vulkan-spirv dynamic_slice.mlir

Activity

  1. added
    compiler/dialects
    codegen
    on Apr 16, 2021
  2. MaheshRavishankar commented on Apr 16, 2021

    @MaheshRavishankar
    Collaborator

    I am fine with adding the reshape for now that will unblock you. I can clean it up when I have all other things plumbed through. But I am not sure I follow the logic here. Can you add some comments here.

  3. hanhanW commented on Apr 19, 2021

    @hanhanW
    ContributorAuthor

    After thinking a bit more, I think I was wrong.

    This is something comping from:

    func @dynamic_slice(%arg0: tensor<2x513xi16>, %arg1: tensor<i32>, %arg2: tensor<i32>) -> tensor<513xi16> {
      %c1_i32 = constant 1 : i32
      %c0_i32 = constant 0 : i32
      %0 = tensor.extract %arg1[] : tensor<i32>
      %1 = cmpi slt, %0, %c1_i32 : i32
      %2 = select %1, %0, %c1_i32 : i32
      %3 = cmpi sgt, %2, %c0_i32 : i32
      %4 = select %3, %2, %c0_i32 : i32
      %5 = index_cast %4 : i32 to index
      %6 = tensor.extract %arg2[] : tensor<i32>
      %7 = cmpi slt, %6, %c0_i32 : i32
      %8 = select %7, %6, %c0_i32 : i32
      %9 = cmpi sgt, %8, %c0_i32 : i32
      %10 = select %9, %8, %c0_i32 : i32
      %11 = index_cast %10 : i32 to index
      %12 = subtensor %arg0[%5, %11] [1, 513] [1, 1] : tensor<2x513xi16> to tensor<513xi16>
      return %12 : tensor<513xi16>
    }
    

    The prototype was wrong. I directly lowered the subtensor op to flow.tensor.reshape. It is not a reshape op. The rank-reducing subtensor op basically is subtensor (with same rank) + reshape. With this way, I can address the issue I hit later.

    I think rank-reducing feature is part of subtensor op and it is the proper way to connect it to flow ops. I will send out the PR soon.

  4. MaheshRavishankar commented on Apr 19, 2021

    @MaheshRavishankar
    Collaborator

    Ok. Thanks. I am currently looking into a revamp of bufferization, so for now this makes sense. Let me know if you want to chat about this some more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegencompiler/dialects

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions