Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLIR] Inconsistent output when executing MLIR program with and without -affine-loop-fusion #132155

Open
Lambor24 opened this issue Mar 20, 2025 · 1 comment
Labels

Comments

@Lambor24
Copy link

My git version is 6003c30.

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without the -affine-loop-fusion.

Steps to Reproduce:

1. MLIR Program (test.mlir):

test.mlir:

module {
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func @main() {
    %0 = "tosa.const"() <{values = dense<2.000000e+00> : tensor<1x3x9x4xf32>}> : () -> tensor<1x3x9x4xf32>
    %1 = "tosa.const"() <{values = dense<3.000000e+00> : tensor<12x2x5x4xf32>}> : () -> tensor<12x2x5x4xf32>
    %2 = "tosa.const"() <{values = dense<4.000000e+00> : tensor<12xf32>}> : () -> tensor<12xf32>
    %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.conv2d %0, %1, %2, %3, %4 {acc_type = f32, dilation = array<i64: 1, 2>, pad = array<i64: 1, 1, 1, 1>, stride = array<i64: 1, 1>} : (tensor<1x3x9x4xf32>, tensor<12x2x5x4xf32>, tensor<12xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x4x3x12xf32>
    %6 = tosa.reduce_sum %5 {axis = 1 : i32} : (tensor<1x4x3x12xf32>) -> tensor<1x1x3x12xf32>
    %cast = tensor.cast %6 : tensor<1x1x3x12xf32> to tensor<*xf32>
    call @printMemrefF32(%cast) : (tensor<*xf32>) -> ()
    return
  }
}

2. Command to Run Without -affine-loop-fusion:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -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-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-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 -affine-loop-fusion:

[[[[592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592], 
   [736,     736,     736,     736,     736,     736,     736,     736,     736,     736,     736,     736], 
   [592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592]]]]

4. Command to Run With -affine-loop-fusion:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -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-affine-loops -affine-loop-fusion -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-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 -affine-loop-fusion:

[[[[16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16], 
   [16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16], 
   [16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16]]]]

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 llvmbot added the mlir label Mar 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 20, 2025

@llvm/issue-subscribers-mlir

Author: None (Lambor24)

My git version is [6003c30](https://github.com/llvm/llvm-project/commit/6003c3055a4630be31cc3d459cdbb88248a007b9).

Description:

I am experiencing an inconsistent result when executing the same MLIR program with and without the -affine-loop-fusion.

Steps to Reproduce:

1. MLIR Program (test.mlir):

test.mlir:

module {
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func @<!-- -->main() {
    %0 = "tosa.const"() &lt;{values = dense&lt;2.000000e+00&gt; : tensor&lt;1x3x9x4xf32&gt;}&gt; : () -&gt; tensor&lt;1x3x9x4xf32&gt;
    %1 = "tosa.const"() &lt;{values = dense&lt;3.000000e+00&gt; : tensor&lt;12x2x5x4xf32&gt;}&gt; : () -&gt; tensor&lt;12x2x5x4xf32&gt;
    %2 = "tosa.const"() &lt;{values = dense&lt;4.000000e+00&gt; : tensor&lt;12xf32&gt;}&gt; : () -&gt; tensor&lt;12xf32&gt;
    %3 = "tosa.const"() &lt;{values = dense&lt;0.000000e+00&gt; : tensor&lt;1xf32&gt;}&gt; : () -&gt; tensor&lt;1xf32&gt;
    %4 = "tosa.const"() &lt;{values = dense&lt;0.000000e+00&gt; : tensor&lt;1xf32&gt;}&gt; : () -&gt; tensor&lt;1xf32&gt;
    %5 = tosa.conv2d %0, %1, %2, %3, %4 {acc_type = f32, dilation = array&lt;i64: 1, 2&gt;, pad = array&lt;i64: 1, 1, 1, 1&gt;, stride = array&lt;i64: 1, 1&gt;} : (tensor&lt;1x3x9x4xf32&gt;, tensor&lt;12x2x5x4xf32&gt;, tensor&lt;12xf32&gt;, tensor&lt;1xf32&gt;, tensor&lt;1xf32&gt;) -&gt; tensor&lt;1x4x3x12xf32&gt;
    %6 = tosa.reduce_sum %5 {axis = 1 : i32} : (tensor&lt;1x4x3x12xf32&gt;) -&gt; tensor&lt;1x1x3x12xf32&gt;
    %cast = tensor.cast %6 : tensor&lt;1x1x3x12xf32&gt; to tensor&lt;*xf32&gt;
    call @<!-- -->printMemrefF32(%cast) : (tensor&lt;*xf32&gt;) -&gt; ()
    return
  }
}

2. Command to Run Without -affine-loop-fusion:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -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-affine-loops -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-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 -affine-loop-fusion:

[[[[592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592], 
   [736,     736,     736,     736,     736,     736,     736,     736,     736,     736,     736,     736], 
   [592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592,     592]]]]

4. Command to Run With -affine-loop-fusion:

/path/llvm-project/build/bin/mlir-opt test.mlir -pass-pipeline='builtin.module(func.func(tosa-to-linalg-named))' | \
/path/llvm-project/build/bin/mlir-opt -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-affine-loops -affine-loop-fusion -lower-affine -convert-scf-to-cf -expand-strided-metadata -convert-cf-to-llvm -convert-arith-to-llvm -convert-math-to-llvm -canonicalize -finalize-memref-to-llvm -convert-func-to-llvm | \
/path/llvm-project/build/bin/mlir-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 -affine-loop-fusion:

[[[[16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16], 
   [16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16], 
   [16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16,     16]]]]

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants