Skip to content

GPU: f32_to_half keeps NaN and rounds normals to nearest even (#843) - #1732

Open
moe-miau wants to merge 2 commits into
Marvisak:mainfrom
moe-miau:fix/gpu-f32-to-half-nan-rne
Open

moe-miau wants to merge 2 commits into
Marvisak:mainfrom
moe-miau:fix/gpu-f32-to-half-nan-rne

Conversation

@moe-miau

Copy link
Copy Markdown

Fixes #843.

This is AndyLatimer's fix from the issue (branch fix/gpu-f32-to-half-nan-and-rne, 63111de), cherry-picked unchanged onto main with the original author kept on the commit. It was posted as a fork branch while PRs were limited to collaborators. The only PR that carried it (#1069) was closed without being merged, so nothing open fixes #843 now.

f32_to_half (crates/victory-gpu/src/primitive.rs, used for F16 attributes in the rectangle-list expansion) had two IEEE-754 errors:

  • NaN became infinity. An f32 NaN has exponent field 0xff, so it fell into exp >= 31 and returned ±0x7c00. It now returns a quiet half NaN that keeps the sign and the upper payload bits (0x7e00 | man >> 13), so a signalling NaN with a low payload stays a NaN.
  • Normals rounded ties away from zero. (man + 0x1000) >> 13 rounded 1.0 + 2^-11 up to odd 0x3c01. It now rounds to nearest even (man + 0x0fff + lsb), matching the subnormal branch just above it. The carry still stops at 0x7c00 (65520 rounds to infinity).

The added test covers quiet and signalling NaNs of both signs, infinities, ties on both sides, the overflow boundary, and a round trip of every half bit pattern.

Checked on Windows 11 x64, Rust 1.98.1 MSVC, main ffe310e + this commit:

  • cargo clippy --workspace --all-targets --locked -- -D warnings and the --all-features variant: clean
  • cargo test --workspace --locked: pass (victory-gpu: 51 passed, including the new test)
  • cargo xtask policy: ok

Independent checks are posted on #843: the new test fails on main, and an exhaustive comparison over all 2^32 f32 inputs against Rust's as f16 found 0 mismatches on non-NaN inputs.

AI-assisted (Claude Code): I reviewed the diff and reran the checks. The fix itself is AndyLatimer's (commit author Manuel Pérez).

🤖 Generated with Claude Code

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: this crate is Windows-only (D3D12/windows_sys/Win32) — nothing compiles or runs on macOS; source review of the diff.

f32_to_half fixes: NaN preserved as quiet f16 NaN with sign and upper payload bits (man >> 13, bit 9 set so a signalling NaN with payload below bit 13 never becomes infinity), overflow → infinity, and round-to-nearest-even implemented manually via lsb/sticky trick. The unit test covers quiet/signalling NaNs both signs plus normals. This is host-side bit manipulation — pure logic, but victory-gpu doesn't compile on macOS; the test runs on Windows only.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

NVIDIA run: RTX 3070 8 GB, driver 616.56, Windows 11, debug, at the PR head 5c781cf. cargo test -p victory-gpu -- f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even: passes.

AI-assisted (Claude Code). Synthetic tests only.

PastiiJay added a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
Code tree of fxceless/victory-fork 4e17478 (all rendering work by its author and
the VIctory contributors) with, on top:
- AMD fixes: skip of 39f0e9aeb156a61c, 32-lane programs per half wave and NaN/Inf
  filter, configurable texture upload queue, VIctory Marvisak#1599
- upstream VIctory Marvisak#334 (buffer-state tracking) and Marvisak#1271 (globally coherent glc UAVs)
- fork PRs Marvisak#95, Marvisak#71, Marvisak#32, Marvisak#86; VIctory Marvisak#1696, Marvisak#1338, Marvisak#1291, Marvisak#1732
- DirectStorage page lists read at the title's submit (analysis by JappertjeV, klbf-lab#3)
- offline pipeline pre-build that saves as it goes and skips driver-crashing pipelines
- Series S plane composition, VICTORY_FFX_SIMPLE, scripted keys in release builds
- docs/klbf/skip-amd.txt: AMD skip list used for our runs

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. The f32_to_half fix in crates/victory-gpu/src/primitive.rs is unchanged from the head judged before: NaN becomes a quiet half NaN that keeps its sign, and normals round to nearest even with (man + 0x0fff + lsb) >> 13. The new commit be58082 only adds a test. reconstructed_fp16_corners_preserve_nan_and_infinity runs the F16 rectangle expansion (expand_rects) and checks that a NaN input stays NaN, that infinities are kept, and that infinity minus infinity in the fourth-corner arithmetic gives NaN. It keeps its original author.

On your reply: noted, and it merges cleanly with current main.

CI: passing (check).


Generated by Claude Code

@moe-miau
moe-miau force-pushed the fix/gpu-f32-to-half-nan-rne branch from 5c781cf to 1b97ccd Compare November 25, 2026 02:38
@moe-miau

Copy link
Copy Markdown
Author

Rebased onto main 940d32f (head 1b97ccd). The only conflict was primitive.rs's tests: main added new tests at the end of the module and so does this PR; both are kept, and the f32_to_half change applied unchanged.

Checked on Windows 11, RTX 4070 SUPER, Rust 1.98.1 MSVC: both clippy configurations are clean, cargo xtask policy passes (182 files), and f32_to_half_preserves_nan_and_rounds_normals_to_nearest_even passes. cargo test --workspace has 5 failures, the same 5 that fail on unmodified 940d32f on this NVIDIA card (dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views from the 1,000,000-descriptor heap cap, plus gathers_at_a_level_match_the_hardware and the two stride_tests hardware checks); none involves this change.

AI-assisted (Claude Code).

@StExpeditus

Copy link
Copy Markdown

Test report: RTX 5060 Ti (Blackwell), Windows 11 Pro 26200, driver 621.24. Branch at 1b97ccd. cargo test --workspace --locked --no-fail-fast -> 697 passed, 5 failed, 10 ignored. The failures are the same 5 pre-existing NVIDIA 1M-view-heap-cap failures (also on main; see my reports on #2030 and #1940). Nothing introduced by this PR fails on this machine.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I reran the FP16 rectangle fallback from #2264 on 1b97ccd. The same synthetic Windows/WARP point draw read [+inf, +inf, -inf, -inf] on 940d32f and [NaN, +inf, -inf, NaN] on this head. This covers both an input NaN and a NaN generated by the fourth-corner arithmetic.

The test-only 541917b sits directly on 1b97ccd. It verifies the generated FP16 vertex through expand_rects alongside the converter tests already here. All 10 primitive tests, strict GPU Clippy with diagnostics and the staged asset policy pass.

If the extra regression is useful, it can be cherry-picked as-is. If it is folded into another commit, please retain Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

Carry the rectangle regression from Marvisak#2264. Test both a NaN input and an infinity subtraction in the rectangle fallback while retaining the converter fix from Marvisak#1732.
@moe-miau

Copy link
Copy Markdown
Author

@andrewpozdnakov7-jpg Thanks: cherry-picked as-is (541917b → be58082, your authorship kept). cargo test -p victory-gpu --lib primitive: 10 passed.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Code review and RTX 5090 + WARP verification at head be58082eb266 merged cleanly onto main (63bcca43c6a0):

  • IEEE 754 binary32 -> binary16 audit (crates/victory-gpu/src/primitive.rs:158-192):
    • NaN: raw_exp == 0xff && man != 0 returns sign | 0x7e00 | (man >> 13) as u16, preserving sign + top 10 payload bits and forcing the quiet bit (0x0200) so low-payload sNaNs (e.g. 0x7f80_0001) quiet properly instead of collapsing to ±Inf.
    • Overflow / ±Inf: exp >= 31 returns sign | 0x7c00; max finite boundary 0x477f_efff -> 0x7bff, tie-to-even 65520.0 (0x477f_f000) rounds to 0x7c00.
    • Subnormals / underflow: exp < -10 (< 2^-25) returns sign (±0); -10 <= exp <= 0 rounds to nearest-even with carry naturally promoting max subnormal to min normal 0x0400.
    • Normals: lsb = (man >> 13) & 1; sign | (half + ((man + 0x0fff + lsb) >> 13)) as u16 handles RNE and mantissa-to-exponent carry cleanly. Confirmed primitive.rs:128 (expand_rects) is the sole call site.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy (default + --features diagnostics): 0 warnings
    • primitive:: draw::tests: 31/31 passed on both RTX 5090 and WARP (including exhaustive 65,536 u16 round-trip check).

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GPU: f32_to_half turns NaN into infinity and ties normals away from zero (fix on a fork)

8 participants