Skip to content

[mlir] Inconsistent output when executing MLIR program with --linalg-fuse-elementwise-ops linalg-fold-unit-extent-dims --affine-loop-fusion #119683

@Emilyaxe

Description

@Emilyaxe

git version: b2b267e

system: Ubuntu 18.04.6 LTS

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without --linalg-fuse-elementwise-ops linalg-fold-unit-extent-dims --affine-loop-fusion .
The output also varies when running the process with optimizations multiple times.
The output becomes correct when any of these three options is removed, so I'm unsure which optimization contains the bug.

Steps to Reproduce:

1. MLIR Program (tosa.mlir):

tosa.mlir:

module {
  func.func private @printMemrefI32(tensor<*xi32>)
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %1 = "tosa.const"() <{values = dense<-7982> : tensor<1x22x10xi32>}> : () -> tensor<1x22x10xi32>
    %73 = tosa.argmax %1 {axis = 1 : i32} : (tensor<1x22x10xi32>) -> tensor<1x10xi32>
    %88 = tosa.abs %73 : (tensor<1x10xi32>) -> tensor<1x10xi32>
    %89 = tosa.clamp %88 {max_val = 16 : i32,  min_val = 0 : i32} : (tensor<1x10xi32>) -> tensor<1x10xi32>
    %90 = tosa.arithmetic_right_shift %73, %89 {round = true} : (tensor<1x10xi32>, tensor<1x10xi32>) -> tensor<1x10xi32>
    %cast = tensor.cast %90 : tensor<1x10xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
    return
  }
}

2. Command to Run without optimizations :

/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir \
-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt  \
-tosa-to-arith --linalg-fuse-elementwise-ops --linalg-fold-unit-extent-dims="use-rank-reducing-slices"  \
  -one-shot-bufferize="bufferize-function-boundaries"  -finalize-memref-to-llvm   -convert-linalg-to-affine-loops \
 --expand-strided-metadata   -convert-vector-to-llvm  -convert-arith-to-llvm  -lower-affine \
 -convert-math-to-llvm   -finalize-memref-to-llvm  -convert-scf-to-cf  -convert-arith-to-llvm -convert-cf-to-llvm -convert-func-to-llvm  \
-reconcile-unrealized-casts | timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner  \
-e main -entry-point-result=void --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so \
 --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so

3. Output without optimizations ::

[[0,   0,   0,   0,   0,   0,   0,   0,   0,   0]]

4. Command to Run with optimizations :

/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt a.mlir \
-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))" | /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt  \
-tosa-to-arith --linalg-fuse-elementwise-ops --linalg-fold-unit-extent-dims="use-rank-reducing-slices"  \
  -one-shot-bufferize="bufferize-function-boundaries"  -finalize-memref-to-llvm   -convert-linalg-to-affine-loops \
--affine-loop-fusion  --expand-strided-metadata   -convert-vector-to-llvm  -convert-arith-to-llvm  -lower-affine \
 -convert-math-to-llvm   -finalize-memref-to-llvm  -convert-scf-to-cf  -convert-arith-to-llvm -convert-cf-to-llvm -convert-func-to-llvm  \
-reconcile-unrealized-casts | timeout 10 /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-cpu-runner  \
-e main -entry-point-result=void --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_c_runner_utils.so \
 --shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_runner_utils.so \
--shared-libs=/data/szy/MLIR/llvm-release/llvm-project/build/lib/libmlir_async_runtime.so

5. Output with optimizations :

[[-17909,   -17909,   -17909,   -17909,   -17909,   -17909,   -17909,   -17909,   -17909,   -17909]]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions