llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR] Inconsistent output when executing MLIR program with and without `-test-expand-math`

Open Lambor24 opened this issue 2 years ago • 2 comments

My git version is 35619c7.

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without the -test-expand-math.

Steps to Reproduce:

1. MLIR Program (test.mlir):

test.mlir:

module {
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() -> () {
    %arg0 = "tosa.const"() {value = dense<2.88> : tensor<51x5x89xf32>} : () -> tensor<51x5x89xf32>
    %0 = tosa.ceil %arg0 : (tensor<51x5x89xf32>) -> tensor<51x5x89xf32>
    %1 = tosa.reduce_sum %0 {axis = 2 : i32} : (tensor<51x5x89xf32>) -> tensor<51x5x1xf32>
    %2 = tosa.exp %1 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %3 = tosa.exp %2 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %4 = tosa.reciprocal %3 : (tensor<51x5x1xf32>) -> tensor<51x5x1xf32>
    %5 = tosa.reshape %4 {new_shape = array<i64: 5, 51>} : (tensor<51x5x1xf32>) -> tensor<5x51xf32>
    %6 = tosa.reduce_min %5 {axis = 0 : i32} : (tensor<5x51xf32>) -> tensor<1x51xf32>
    %7 = tosa.pow %6, %6 : (tensor<1x51xf32>, tensor<1x51xf32>) -> tensor<1x51xf32>
    %rtn1 = tensor.cast %7 : tensor<1x51xf32> to tensor<*xf32>
    call @printMemrefF32(%rtn1) : (tensor<*xf32>) -> ()
    return
  }
}

2. Command to Run Without -test-expand-math:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so

3. Output Without -test-expand-math:

[[1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1]]

4. Command to Run With -test-expand-math:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -test-expand-math -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so

5. Output With -test-expand-math:

[[-nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan]]

I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused this result.

Lambor24 avatar Dec 06 '24 09:12 Lambor24

without expansion, math.powf gets lowered to lbm llvm.intr.pow. Operands are obviously pow(0,0), which should be 1.

The rewritten version using math dialect expansion patterns results in something like exp( (rhs/2) * log(lhs*lhs) ). NaN is observed since the exp argument evaluates to 0 * -inf which is -nan.

I think it's a bug, the powf expansion pattern needs to handle lhs=0 correctly (this is based on the idea that the goal of the rewrites is to model the default lowering behavior, which is to libm powf).

christopherbate avatar Dec 06 '24 18:12 christopherbate

@llvm/issue-subscribers-bug

Author: None (Lambor24)

My git version is [35619c7](https://github.com/llvm/llvm-project/commit/35619c791d1f5128d16c7a8e099e8856e12ab39c).

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without the -test-expand-math.

Steps to Reproduce:

1. MLIR Program (test.mlir):

test.mlir:

module {
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func @<!-- -->main() -&gt; () {
    %arg0 = "tosa.const"() {value = dense&lt;2.88&gt; : tensor&lt;51x5x89xf32&gt;} : () -&gt; tensor&lt;51x5x89xf32&gt;
    %0 = tosa.ceil %arg0 : (tensor&lt;51x5x89xf32&gt;) -&gt; tensor&lt;51x5x89xf32&gt;
    %1 = tosa.reduce_sum %0 {axis = 2 : i32} : (tensor&lt;51x5x89xf32&gt;) -&gt; tensor&lt;51x5x1xf32&gt;
    %2 = tosa.exp %1 : (tensor&lt;51x5x1xf32&gt;) -&gt; tensor&lt;51x5x1xf32&gt;
    %3 = tosa.exp %2 : (tensor&lt;51x5x1xf32&gt;) -&gt; tensor&lt;51x5x1xf32&gt;
    %4 = tosa.reciprocal %3 : (tensor&lt;51x5x1xf32&gt;) -&gt; tensor&lt;51x5x1xf32&gt;
    %5 = tosa.reshape %4 {new_shape = array&lt;i64: 5, 51&gt;} : (tensor&lt;51x5x1xf32&gt;) -&gt; tensor&lt;5x51xf32&gt;
    %6 = tosa.reduce_min %5 {axis = 0 : i32} : (tensor&lt;5x51xf32&gt;) -&gt; tensor&lt;1x51xf32&gt;
    %7 = tosa.pow %6, %6 : (tensor&lt;1x51xf32&gt;, tensor&lt;1x51xf32&gt;) -&gt; tensor&lt;1x51xf32&gt;
    %rtn1 = tensor.cast %7 : tensor&lt;1x51xf32&gt; to tensor&lt;*xf32&gt;
    call @<!-- -->printMemrefF32(%rtn1) : (tensor&lt;*xf32&gt;) -&gt; ()
    return
  }
}

2. Command to Run Without -test-expand-math:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so

3. Output Without -test-expand-math:

[[1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1]]

4. Command to Run With -test-expand-math:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named,tosa-to-linalg))' | \
/path/llvm-project/build/bin/mlir-opt -tosa-to-tensor -tosa-to-arith -one-shot-bufferize="bufferize-function-boundaries" -convert-linalg-to-loops -test-expand-math -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -finalize-memref-to-llvm -convert-func-to-llvm -reconcile-unrealized-casts | \
/path/llvm-project/build/bin/mlir-cpu-runner -e main -entry-point-result=void \
-shared-libs=/path/llvm-project/build/lib/libmlir_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_c_runner_utils.so \
-shared-libs=/path/llvm-project/build/lib/libmlir_async_runtime.so

5. Output With -test-expand-math:

[[-nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan,   -nan]]

I'm not sure if there is any bug in my program or if the wrong usage of the above passes caused this result.

llvmbot avatar Dec 06 '24 18:12 llvmbot