Skip to content

Lowering mhlo.reverse to linalg_ext.reverse. - #7163

Merged
GMNGeoffrey merged 2 commits into
iree-org:mainfrom
hanhanW:mhlo-rev-to-linalg-ext
Sep 28, 2021
Merged

GMNGeoffrey merged 2 commits into
iree-org:mainfrom
hanhanW:mhlo-rev-to-linalg-ext

Conversation

@hanhanW

@hanhanW hanhanW commented Sep 23, 2021

Copy link
Copy Markdown
Contributor

It's a step toward #5045

@hanhanW

hanhanW commented Sep 23, 2021

Copy link
Copy Markdown
Contributor Author

Depends on #7159

@hanhanW

hanhanW commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

Got the smallest repro here:

func @foo() -> tensor<1x2x2x3xf32> {
  %cst = constant dense<[[[[-0.322270721, -0.454585314, 0.0548270941],
                           [0.0948997139, -0.129087329, 0.47688508]],
                          [[-0.0104956627, 0.771698594, 0.304780841],
                           [-0.580368757, 0.325046659, 0.251613259]]]]> : tensor<1x2x2x3xf32>
  %0 = "mhlo.reverse"(%cst) {
    dimensions = dense<[0, 1]> : tensor<2xi64>
  } : (tensor<1x2x2x3xf32>) -> tensor<1x2x2x3xf32>
  return %0 : tensor<1x2x2x3xf32>
}

It crashes in LinalgBufferize. IR before the pass: https://gist.githubusercontent.com/hanhanW/f01bc4d1c245dc0aea2ad3c1580c245d/raw

Looking...

@hanhanW

hanhanW commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

Looks like an issue starting from init_tensor.

IR after DispatchLinalgOnTensors: https://gist.githubusercontent.com/hanhanW/eb687de16a0f320306100ced7deaa3b4/raw

It will generate something like:

%2 = linalg.init_tensor [1, 2, 2, 3] : tensor<1x2x2x3xf32>
// ...
%16 = tensor.extract_slice %2[0, %arg2, %arg3, %arg4] [1, %5, %8, %11] [1, 1, 1, 1] : tensor<1x2x2x3xf32> to tensor<1x?x?x?xf32>
%17 = tensor.cast %16 : tensor<1x?x?x?xf32> to tensor<?x?x?x?xf32>
%18 = linalg_ext.reverse ... outs(%17 ...

After canonicalization, it turns into:

%15 = linalg.init_tensor [1, %4, %7, %10] : tensor<1x?x?x?xf32>
%16 = tensor.cast %15 : tensor<1x?x?x?xf32> to tensor<?x?x?x?xf32>

In this case, the mapping table can't map the source of cast op to an input buffer, which failed in the check.

https://github.com/google/iree/blob/c8f10a4ac2c637258979b8ed74c2178c4e95ae62/iree/compiler/Codegen/Common/LinalgBufferizePass.cpp#L995-L1003

@hanhanW

hanhanW commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

Tested with add operation with 4D tensor, the main difference is that tensor.cast ops are inserted in this case.

In the beginning, the IR is:

%24 = tensor.extract_slice %cst[0, %arg1, %arg3, %arg5] [%8, %15, %19, %23] [1, 1, 1, 1] : tensor<1x2x2x3xf32> to tensor<?x?x?x?xf32>

Then it triggers InsertSliceOpConstantArgumentFolder pattern, which produces:

%12 = tensor.extract_slice <<UNKNOWN SSA VALUE>>[0, %arg1, %arg3, %arg5] [%c1, %3, %7, %11] [1, 1, 1, 1] : tensor<1x2x2x3xf32> to tensor<?x?x?x?xf32>

Then OpWithOffsetSizesAndStridesConstantArgumentFolder<mlir::tensor::ExtractSliceOp, SliceReturnTypeCanonicalizer, SliceCanonicalizer> is triggered, which produces:

%12 = tensor.extract_slice <<UNKNOWN SSA VALUE>>[0, %arg1, %arg3, %arg5] [1, %3, %7, %11] [1, 1, 1, 1] : tensor<1x2x2x3xf32> to tensor<1x?x?x?xf32>
%13 = tensor.cast %12 : tensor<1x?x?x?xf32> to tensor<?x?x?x?xf32>

The original shape is tensor<?x?x?x?xf32>, and it could get inferred by canonicalizer. To keep the type consistently, we will insert tensor.cast in this case.

@hanhanW

hanhanW commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

There are couple workarounds. The most obvious one to me is adding a canonicalization pattern to pass the init_tensor op directly to linalg/linalg_ext ops if it is linalg.init_tensor -> tensor.cast -> linalg.* pattern.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

There are couple workarounds. The most obvious one to me is adding a canonicalization pattern to pass the init_tensor op directly to linalg/linalg_ext ops if it is linalg.init_tensor -> tensor.cast -> linalg.* pattern.

Yeah having this pattern makes sense to me. Let me know if you want me to look into this further.

@hanhanW

hanhanW commented Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

Linalg dialect already has the patterns, I copied it from upstream and added it to LinalgExt dialect in #7179

@hanhanW
hanhanW force-pushed the mhlo-rev-to-linalg-ext branch from b298b31 to 9e9c360 Compare September 27, 2021 18:09
@hanhanW
hanhanW changed the base branch from hanhan-rev2 to main September 27, 2021 18:10
@hanhanW
hanhanW enabled auto-merge (squash) September 27, 2021 19:02
@GMNGeoffrey
GMNGeoffrey merged commit 2cc9a12 into iree-org:main Sep 28, 2021
@hanhanW
hanhanW deleted the mhlo-rev-to-linalg-ext branch September 28, 2021 17:19
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.

4 participants