Issue 130886
Summary tosa::ReduceSum Op goes wrong with type Float16.
Labels new issue
Assignees
Reporter CBalaa
    My mlir code output wrongly because of tosa::ReduceOp, I try to lower tosa::ReduceOp to linalg.Reduce and scf.for, and they all go wrong! 

Platform: X86 Ubuntu 22.04

I lower tosa::ReduceOp to scf::for op, I think it is logically correct. But my code expected to output [5120] on x86 platform, but got [2048], I wonder why and how to fix it:

```mlir
module attributes {test.weight = dense<1.000000e+00> : tensor<5120xf16>} {
  memref.global "private" constant @__constant_5120xf16 : memref<5120xf16> = dense<1.000000e+00> {alignment = 64 : i64}
  func.func private @printMemrefF16(memref<*xf16>)
 func.func private @main() {
    %c1 = arith.constant 1 : index
    %c5120 = arith.constant 5120 : index
    %c0 = arith.constant 0 : index
    %cst = arith.constant 0.000000e+00 : f16
    %0 = memref.get_global @__constant_5120xf16 : memref<5120xf16>
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<f16>
    memref.store %cst, %alloc[] : memref<f16>
    scf.for %arg0 = %c0 to %c5120 step %c1 {
      %1 = memref.load %0[%arg0] : memref<5120xf16>
      %2 = memref.load %alloc[] : memref<f16>
      %3 = arith.addf %1, %2 : f16
      memref.store %3, %alloc[] : memref<f16>
    }

    %expand_shape = memref.expand_shape %alloc [] output_shape [1] : memref<f16> into memref<1xf16>
    %cast = memref.cast %expand_shape : memref<1xf16> to memref<*xf16>
    call @printMemrefF16(%cast) : (memref<*xf16>) -> ()
    return
 }
}
```

makefile: (variables like MLIR_OPT should change to your executable.)
```makefile
#!/bin/bash
MLIR_OPT := ../../thirdparty/llvm/build/bin/mlir-opt
MLIR_TRANSLATE := ../../thirdparty/llvm/build/bin/mlir-translate
MLIR_CPU_RUNNER := ../../thirdparty/llvm/build/bin/mlir-cpu-runner
LLC := ../../thirdparty/llvm/build/bin/llc
OPT_FLAG := -O0

MLIR_LIB := ../../thirdparty/llvm/build/lib
AIPILER_PY_PATH := ../../python

MLIR_RUNNER_UTILS := ../../thirdparty/llvm/build/lib/libmlir_runner_utils.so
MLIR_C_RUNNER_UTILS := ../../thirdparty/llvm/build/lib/libmlir_c_runner_utils.so
MTRIPLE := x86_64-unknown-linux-gnu

test: test.mlir
	${MLIR_OPT}  $^ \
		-pass-pipeline="builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith), \
		eliminate-empty-tensors, empty-tensor-to-alloc-tensor, one-shot-bufferize{bufferize-function-boundaries}, \
		convert-linalg-to-loops, convert-vector-to-scf, expand-strided-metadata, convert-vector-to-llvm, \
		arith-expand, lower-affine, convert-arith-to-llvm, convert-math-to-llvm, convert-math-to-libm, \
		finalize-memref-to-llvm, convert-scf-to-cf)" --mlir-print-ir-after-failure | \
	${MLIR_OPT}	-llvm-request-c-wrappers | \
	${MLIR_OPT}	-pass-pipeline="builtin.module(convert-arith-to-llvm, convert-math-to-llvm, convert-math-to-libm, convert-func-to-llvm, reconcile-unrealized-casts)" | \
	${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
		-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to