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 1 year ago • 3 comments

My git version is ea44647.

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<9.5> : tensor<10x20xf32>} : () -> tensor<10x20xf32>
    %0 = tosa.reduce_prod %arg0 {axis = 1 : i32} : (tensor<10x20xf32>) -> tensor<10x1xf32>
    %1 = tosa.floor %0 : (tensor<10x1xf32>) -> tensor<10x1xf32>
    %rtn1 = tensor.cast %1 : tensor<10x1xf32> 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))' | \
/path/llvm-project/build/bin/mlir-opt -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:

[[3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19], 
 [3.58486e+19]]

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))' | \
/path/llvm-project/build/bin/mlir-opt -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:

[[9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18], 
 [9.22337e+18]]

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 13 '24 08:12 Lambor24

Pattern 'convertFloorOp' expand "math.floorf" to "float(int(input)) + increase".

Values exceeding the representable range of int64 will experience precision loss when converting from floating-point to integer type.

Can we delete this pattern? @bviyer

cxy-1993 avatar Dec 20 '24 06:12 cxy-1993

As far as I can tell, I don't think we are using this extensively. Is it possible to disable it just for values that do not fall in the appropriate range?

bviyer avatar Dec 21 '24 00:12 bviyer

As far as I can tell, I don't think we are using this extensively. Is it possible to disable it just for values that do not fall in the appropriate range?

Yes, this requires an analysis similar to IntRangeAnalysis, but we currently don't have a float version. Furthermore, invoking data flow analysis within a pattern is also not suitable due to the performance cost. Perhaps we could add a data flow solver to the parameters of this pattern?

Regardless, I suggest closing this pattern for now and reopening it when we have a clearer idea of how to proceed. Please help review this PR:https://github.com/llvm/llvm-project/pull/120865

cxy-1993 avatar Dec 22 '24 01:12 cxy-1993