Skip to content

Bug in f32->f16 truncation on CPU: 0.49990 is truncated to 0.25. #20163

Closed

Description

@bjacob

(Messed up original Issue description with edits, but see the next comment for the minimal testcase)

Activity

  1. added
    bug 🐞
    codegen/llvm
    on Mar 5, 2025
  2. added a commit that references this issue on Mar 11, 2025
  3. bjacob commented on Mar 12, 2025

    @bjacob
    CollaboratorAuthor

    Minimized to a LLVM x86 codegen bug for truncation of f32 to f16.

    Minimal MLIR testcase:

    func.func @test_trunc_f32_f16() -> () {
      %input_x = util.unfoldable_constant dense<[0.49990]> : tensor<1xf32>
      %result_empty = tensor.empty() : tensor<1xf16>
      %result = linalg.generic {indexing_maps = [
          affine_map<(d0) -> (d0)>,
          affine_map<(d0) -> (d0)>
        ], iterator_types = ["parallel"]}
        ins(%input_x : tensor<1xf32>) outs(%result_empty : tensor<1xf16>) {
      ^bb0(%x: f32, %out: f16):
        %result = arith.truncf %x : f32 to f16
        linalg.yield %result : f16
      } -> tensor<1xf16>
      check.expect_almost_eq_const(%result,   dense<[0.5]> : tensor<1xf16>, atol 0.01) :  tensor<1xf16>
      return
    }
    ➜  iree-build tools/iree-compile --iree-hal-target-device=llvm-cpu --iree-llvmcpu-target-cpu=generic ~/a.mlir -o ~/b.vmfb && tools/iree-check-module --device=local-task --module=$HOME/b.vmfb
    [==========] Running 1 test from 1 test suite.
    [----------] Global test environment set-up.
    [----------] 1 test from module
    [ RUN      ] module.test_trunc_f32_f16
    iree/runtime/src/iree/modules/check/module.cc:568: Failure
    Failed
    Expected near equality of these values. Contents does not match to tolerance parameters atol=0.01, rtol=0. The first failure occurs at index 0 as the lhs value 0.25 differs from the rhs value 0.5.
      lhs:
        1xf16=0.25
      rhs:
        1xf16=0.5
    
    [  FAILED  ] module.test_trunc_f32_f16, where GetParam() = 0 (24 ms)
    
  4. bjacob commented on Mar 12, 2025

    @bjacob
    CollaboratorAuthor

    The bug only manifests itself when compiling to a CPU target that lacks a native instruction for f32 to f16 truncation (--iree-llvmcpu-target-cpu=generic). When we enable x86 F16C instructions (--iree-llvmcpu-target-cpu=host on any contemporary CPU) the issue goes away.

  5. changed the title [-]`math.powf` `f16` correctness bug: `powf(2.0, -1.0) = 0.25` instead of `0.5`.[/-] [+]LLVM bug in f32->f16 truncation on CPU[/+] on Mar 12, 2025
  6. bjacob commented on Mar 13, 2025

    @bjacob
    CollaboratorAuthor

    Actually the truncation from float to half here is implemented in our own libdevice. From the generated .ll file:

    define internal signext i16 @iree_f2h_ieee(float noundef %0) local_unnamed_addr #3 {
    

    So it looks like a bug in iree_f2h_ieee.

  7. changed the title [-]LLVM bug in f32->f16 truncation on CPU[/-] [+]Bug in f32->f16 truncation on CPU[/+] on Mar 13, 2025
  8. changed the title [-]Bug in f32->f16 truncation on CPU[/-] [+]Bug in f32->f16 truncation on CPU: 0.49990 is truncated to 0.25.[/+] on Mar 13, 2025
  9. added a commit that references this issue on Mar 14, 2025
    e33829c
  10. added a commit that references this issue on Mar 17, 2025
    0cc6a6f
  11. added a commit that references this issue on Mar 24, 2025
    1ad60c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞codegen/llvm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions