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

[Lang] Subnormal floating point numbers are flushed to zero by pybind #3548

Closed
sjwsl opened this issue Nov 18, 2021 · 0 comments
Closed

[Lang] Subnormal floating point numbers are flushed to zero by pybind #3548

sjwsl opened this issue Nov 18, 2021 · 0 comments
Labels
potential bug Something that looks like a bug but not yet confirmed

Comments

@sjwsl
Copy link
Contributor

sjwsl commented Nov 18, 2021

Subnormal numbers (approximately 1e-45 to 1e-38) are flushed to zero by pybind now, which causes some confusing behaviors. For example

import taichi as ti
ti.init(arch=ti.gpu)


@ti.kernel
def constant_folded_on_py() -> ti.f32:
    return 1e-10 * 1e-30


@ti.kernel
def calculated_on_device() -> ti.f32:
    x = 1e-10
    y = 1e-30
    return x * y


print(1e-10 * 1e-30)
print(constant_folded_on_py())
print(calculated_on_device())

The output (same on the CUDA backend) is

[Taichi] version 0.8.6, llvm 10.0.0, commit cb5670e5, osx, python 3.9.7
[Taichi] Starting on arch=metal
1.0000000000000001e-40
0.0
9.99994610111476e-41

The different results of 1 and 3 are fine because they use 64-bit and 32-bit multiplication respectively. The problem is that 1e-40 (a subnormal number) on the Python side cannot be passed to the C++ side.

Currently, we set FTZ (Flush subnormal numbers To Zero) on the CUDA backend but it doesn't seem to work. The existing FTZ tests do not make sense too IMHO, because 1e-45 used in the tests is actually smaller than the minimal subnormal number so it can't tell whether FTZ works... And the CPU backend seems to correctly flush subnormal numbers to zero because the output is

[Taichi] version 0.8.6, llvm 10.0.0, commit cb5670e5, linux, python 3.9.7
[Taichi] Starting on arch=x64
1.0000000000000001e-40
0.0
0.0

I think we need a discussion about how to handle these small float numbers, the basic purpose is to make the behaviors consistent on

  • different backends
  • Python side and C++ side
@sjwsl sjwsl added the potential bug Something that looks like a bug but not yet confirmed label Nov 18, 2021
@sjwsl sjwsl closed this as completed Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
potential bug Something that looks like a bug but not yet confirmed
Projects
None yet
Development

No branches or pull requests

1 participant