-
Notifications
You must be signed in to change notification settings - Fork 158
Fix incorrect CPU float16 implementation #2395
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
Open
eliasachermann
wants to merge
8
commits into
spcl:main
Choose a base branch
from
eliasachermann:fix/cpu-half-conversion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−8
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
334cbf7
correct CPU half (float16) conversion, add default constructor
eliasachermann 463d6b0
Remove success message from test script
ThrudPrimrose a3ce049
Add inf and nan tests
eliasachermann e33d5b0
Update Copyright
eliasachermann 46d46da
Merge branch 'main' into fix/cpu-half-conversion
ThrudPrimrose 0c202be
Merge branch 'spcl:main' into fix/cpu-half-conversion
eliasachermann 8f9b212
Merge branch 'main' into fix/cpu-half-conversion
ThrudPrimrose 6b755ac
Merge branch 'main' into fix/cpu-half-conversion
ThrudPrimrose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Copyright 2019-2021 ETH Zurich and the DaCe authors. All rights reserved. | ||
|
|
||
| import dace | ||
| import numpy as np | ||
|
|
||
| N = dace.symbol("N") | ||
|
|
||
|
|
||
| def _roundtrip_program(): | ||
| """A program that stores float32 input as float16 and reads it back as float32. | ||
|
|
||
| This forces both conversion directions of the host ``dace::half``: | ||
| ``half(float)`` on the store and ``operator float()`` on the load. | ||
| """ | ||
|
|
||
| @dace.program | ||
| def f32_to_f16_to_f32(inp: dace.float32[N], out: dace.float32[N]): | ||
| tmp = np.ndarray([N], dace.float16) | ||
| for i in dace.map[0:N]: | ||
| with dace.tasklet: | ||
| a << inp[i] | ||
| t >> tmp[i] | ||
| t = a # float32 -> float16 | ||
| for i in dace.map[0:N]: | ||
| with dace.tasklet: | ||
| t << tmp[i] | ||
| o >> out[i] | ||
| o = t # float16 -> float32 | ||
|
|
||
| return f32_to_f16_to_f32 | ||
|
|
||
|
|
||
| def test_float16_cpu_roundtrip_matches_numpy(): | ||
| """Host float32<->float16 conversion must agree with IEEE-754 (NumPy float16). | ||
|
|
||
| Covers zero, normals, exact-rounding cases, and small/subnormal magnitudes | ||
| that the previous bit-hack conversion corrupted. | ||
| """ | ||
| prog = _roundtrip_program() | ||
| inp = np.array( | ||
| [ | ||
| 0.0, | ||
| -0.0, | ||
| 1.0, | ||
| -1.0, | ||
| 2.0, | ||
| 3.0, | ||
| 0.5, | ||
| -0.5, | ||
| 1.25, | ||
| -2.5, | ||
| 100.0, | ||
| 1024.0, | ||
| 65504.0, | ||
| 1e-3, | ||
| 6e-5, | ||
| 6e-8, | ||
| -6e-8, | ||
| ], | ||
| dtype=np.float32, | ||
| ) | ||
| out = np.full(inp.shape, np.nan, dtype=np.float32) | ||
| prog(inp=inp, out=out, N=inp.size) | ||
|
|
||
| # NumPy's float16 is IEEE-754 round-to-nearest-even -- the reference. | ||
| expected = inp.astype(np.float16).astype(np.float32) | ||
| np.testing.assert_array_equal(out, expected) | ||
|
|
||
|
|
||
| def test_float16_cpu_random_roundtrip_matches_numpy(): | ||
| """Randomized sweep of the host conversion against NumPy float16.""" | ||
| rng = np.random.default_rng(0) | ||
| inp = (rng.standard_normal(4096) * 10.0).astype(np.float32) | ||
| out = np.full(inp.shape, np.nan, dtype=np.float32) | ||
|
|
||
| prog = _roundtrip_program() | ||
| prog(inp=inp, out=out, N=inp.size) | ||
|
|
||
| expected = inp.astype(np.float16).astype(np.float32) | ||
| np.testing.assert_array_equal(out, expected) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_float16_cpu_roundtrip_matches_numpy() | ||
| test_float16_cpu_random_roundtrip_matches_numpy() | ||
| print("All tests passed.") | ||
|
ThrudPrimrose marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a subnormal number to your tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added inf and nan tests