llvm / llvm/llvm-project

[MLIR] Inconsistent Results when combining `-linalg-fold-unit-extent-dims` and `-affine-loop-fusion`

Open
#175,111 2 comments 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**test commit**: [5cfd02](https://github.com/llvm/llvm-project/commit/5cfd02f44a43a2e2a085a633b022a62f64ba2b93)

## Description:
When both passes are disabled, the result is correct.
When only one of the two passes is enabled, the result is also correct.
However, when both `-linalg-fold-unit-extent-dims` and `-affine-loop-fusion` are enabled together, the result becomes incorrect and unstable, occasionally producing NaN.

## test case
```
module {
func.func private @printMemrefI8(tensor<*xi8>) attributes {llvm.emit_c_interface}
func.func private @printMemrefF32(tensor<*xf32>)
func.func @main() {
%0 = "tosa.const"() <{values = dense<-2.000000e+01> : tensor<2x5x4xf32>}> : () -> tensor<2x5x4xf32>
%1 = "tosa.const"() <{values = dense<-3.000000e+01> : tensor<2x4x2xf32>}> : () -> tensor<2x4x2xf32>
%3 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
%4 = "tosa.const"() <{values = dense<0.000000e+00> : tensor<1xf32>}> : () -> tensor<1xf32>
%5 = tosa.matmul %0, %1, %3, %4 : (tensor<2x5x4xf32>, tensor<2x4x2xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<2x5x2xf32>
%6 = tosa.log %5 : (tensor<2x5x2xf32>) -> tensor<2x5x2xf32>
%8 = tosa.reduce_sum %6 {axis = 1 : i32} : (tensor<2x5x2xf32>) -> tensor<2x1x2xf32>
%cast_0 = tensor.cast %8 : tensor<2x1x2xf32> to tensor<*xf32>
call @printMemrefF32(%cast_0) : (tensor<*xf32>) -> ()
return
}
}
```

## Command:
### 1. Without `-linalg-fold-unit-extent-dims ` and `-affine-loop-fusion`
#### cmd:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | mlir-opt -linalg-fuse-elementwise-ops \
-one-shot-bufferize="bufferize-function-boundaries=1" -expand-strided-metadata -convert-linalg-to-affine-loops -lower-affine\
-convert-scf-to-cf -convert-to-llvm | mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so
```

#### output:
```
[[[38.9161, 38.9161]],
[[38.9161, 38.9161]]]
```
### 2. With `-linalg-fold-unit-extent-dims ` and `-affine-loop-fusion`
#### cmd:
```
mlir-opt test.mlir -pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | mlir-opt -linalg-fuse-elementwise-ops \
-one-shot-bufferize="bufferize-function-boundaries=1" -linalg-fold-unit-extent-dims -expand-strided-metadata \
-convert-linalg-to-affine-loops -affine-loop-fusion -lower-affine -convert-scf-to-cf -convert-to-llvm | mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project-latest/build/lib/libmlir_runner_utils.so

```
#### output:
The results of repeated executions are inconsistent.
```
[[[106.263, 106.263]],
[[106.263, 106.263]]]
```
```
[[[43.7036, 49.233]],
[[51.711, 53.3525]]]
```
```
[[[-nan, -nan]],
[[-nan, -nan]]]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.