Skip to content

feat(cuda-device): add scalar bf16_to_f32 - #1185

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
lucifer1004:pr/device-scalar-bf16-to-f32
Aug 28, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
lucifer1004:pr/device-scalar-bf16-to-f32

Conversation

@lucifer1004

Copy link
Copy Markdown
Contributor

cuda_device::convert has f32_to_bf16 but not the scalar inverse, so reading a raw bf16 means open-coding the high-half widen at each call site:

f32::from_bits(u32::from(bits) << 16)

Added

Function Purpose
bf16_to_f32(u16) -> f32 inverse of f32_to_bf16 for values already exact in bf16

This is the same bit-shift already used by the packed bf16 unpackers (cvt_f32x2_bf16x2 and friends). The low 16 bits of the f32 become zero.

Why no generator / no GPU example

This is not a new PTX intrinsic. There is nothing for the generator to admit, and no new GPU example — the same as existing f32_to_bf16, which is also a host-side bit reinterpret.

Tests

  • 1.0 — 0x3F80 widens to 1.0
  • Sign of zero — 0x8000 widens to -0.0 (bit-identical)
  • Round-trip — bf16_to_f32(f32_to_bf16(1.0)) == 1.0

Host unit tests only; fmt --check clean.

The scalar inverse of f32_to_bf16 was missing, so call sites open-coded
the high-half widen. This is the same bit-shift already used by the
packed bf16 unpackers; host unit tests cover 1.0, -0.0, and round-trip
through f32_to_bf16.

Signed-off-by: Zihua Wu <[email protected]>
@nihalpasham

nihalpasham commented Aug 28, 2026 •

Copy link
Copy Markdown
Collaborator

Sound and mergeable as-is. Verified:

  • the widen is exact for all 65536 bf16 patterns (bf16 is literally the top half of an f32)
  • same layer as nvcc's __bfloat162float and the half crate's bf16::to_f32
  • the tests already catch the plausible wrong versions (numeric cast, missing shift, sign mangling)

Two small things I added at landing:

  • an exhaustive round-trip test:

    for bits in 0..=u16::MAX {
        assert_eq!(f32_to_bf16(bf16_to_f32(bits)), bits);
    }
  • pointing the three hand-rolled copies (gemm_sol, gemm_sol_final, tcgen05_matmul) at the new helper

CI is approved on the newest head. Labels added.

@nihalpasham nihalpasham added enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings labels Aug 28, 2026
bf16_to_f32 places the 16 bits in the f32 high half and f32_to_bf16
truncates them back out, so every u16 pattern (NaNs included) must
survive the trip bit-identically. Assert that exhaustively, following
the every_non_nan_f16_pattern_round_trips convention.

Signed-off-by: nihalpasham <[email protected]>
…hecks

gemm_sol, gemm_sol_final, and tcgen05_matmul each carried a private
copy of the high-half widen for their host-side result checks. The
scalar helper now lives in cuda_device::convert, so drop the local
copies and import the library one.

Signed-off-by: nihalpasham <[email protected]>

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Validation complete on this head (crate tests, per-example gates, compile-only smoketest); CI green. Merging.

@nihalpasham
nihalpasham merged commit 5f5ea18 into NVIDIA:main Aug 28, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants