[PR #2955] [Engine] f32 -> bf16 conversions in PLE and prefill keep a NaN a NaN (as bf16_bits.hpp does) #4359

Open
opened 2026-11-07 15:35:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2955
Author: @sergqwer
Created: 10/17/2026
Status: 🔄 Open

Base: main ← Head: pr/bf16-nan


📝 Commits (1)

  • c52d60b engine: f32 -> bf16 conversions in PLE and prefill keep a NaN a NaN, as bf16_bits.hpp does

📊 Changes

3 files changed (+9 additions, -1 deletions)

View changed files

📝 src/kernels/cuda/ple.cu (+2 -0)
📝 src/prefill/kernels.cu (+2 -0)
📝 src/prefill/prefill.cpp (+5 -1)

📄 Description

Problem and result

The engine has private f32 -> bf16 helpers that round to nearest even. Unlike bf16_bits.hpp (bf16_from_f32) and dequant_bf16.cu, they have no NaN test. The rounding add carries a NaN out of the mantissa, so a NaN is masked instead of reaching the NaN guard: for example 0x7FFFFFFF (the NaN pattern in #879) becomes 0x8000, i.e. -0.

  • Before: 131,072 of the 16,777,214 f32 NaN patterns come out of bf16_bits (PLE) and bf (prefill) as -0, +0 or an infinity.
  • After: every NaN comes out as a quiet NaN of the same sign. Every other input converts to exactly the same bits as before, so normal output does not change (checked over all 2^32 inputs, and end to end below).

None needed: a focused correctness fix. It was #2955 before the 0.2.0-beta tracker reset, unchanged except for the rebase. The fix is sebastianrcnt's (sebastianrcnt/Strata ef1a7ef5), co-author of the commit.

Approach

Each helper tests the magnitude bits first and returns the top half with the quiet bit set ((i >> 16) | 64), sign kept, as bf16_bits.hpp does:

  • src/kernels/cuda/ple.cu bf16_bits (to_bf16_kernel);
  • src/prefill/kernels.cu bf: the BF16 activation image of every prefill norm, plus bf_lo and dequant.

The two host dumps in src/prefill/prefill.cpp get the same test: STRATA_PREFILL_DUMP_R_ALL (round to nearest even) and the STRATA_DBG_FEAT route record (round half up; that rounding is kept, only the NaN test is new).

Left alone: host references in parity tests and benches (finite data), fused_gr.cu hc_q8_bf16_kernel (weights x scales, documented as finite) and sycl/. On main the set of private 0x7fff rounding sites is the same as on 0.1.45, so the rebase found no new helper to cover.

Validation

Revision: this branch, one commit c52d60b3 on main e88ee39d (v0.2.0-beta + 12 commits), against a build of main e88ee39d itself. Machine: RTX 5090 (sm_120), Ryzen 9 9950X3D, 128 GB, Windows 11; MSVC 2022 17.14, CUDA 13.3, Ninja, release configure -DSTRATA_ENABLE_CUDA=ON -DSTRATA_PORTABLE=ON -DCMAKE_CUDA_ARCHITECTURES="75-real;86-real;89-real;120-real" -DSTRATA_GGML_DIR=<llama.cpp at the pinned 3cf03257>, target strata.

Passed:

  • All 2^32 f32 bit patterns. A generated CUDA file holds the four helpers verbatim from main (old) and from this branch (new); built with nvcc -O2 -std=c++17 -gencode arch=compute_120,code=sm_120 -I <this branch>/include. Host (all four helpers) and device (the two device helpers, RTX 5090):

    check host device
    non-NaN inputs that convert differently, old vs new 0 0
    NaN inputs that do not give a quiet NaN of the same sign (new) 0 0
    new round-to-nearest-even helpers != bf16_from_f32 0 0
    NaN inputs the old bf16_bits / bf masked (of 16,777,214) 131,072 / 131,072 131,072 / 131,072
  • Engine identity on the NVFP4 GPTQ pack (Maximilian228/Qwen3.8-Flash-Next-NVFP4-GPTQ-Strata, upstream setup's --family nvfp4 --model NVFP4 file set). strata --serve with setup's model files plus --expert-cache 3000 --prefill auto --spec 6 --spec-min-p 0.7 --max-context 262144 --kv int8 --gpu 0 --pcie-frac 0 --adapt-every 1000000, env STRATA_PREFILL_CPU_SHARE=0 STRATA_PCIE_BALANCE=0 STRATA_IQ_MT_MIN=1 STRATA_DUMP_FIRST_LOGITS=<file>; a 2,100-token and a 32,037-token prompt, 32 greedy tokens each (both stop at the end of turn). First-token logits and tokens are byte-identical to main's:

    prompt sha256 of the first-token logits (main twice, this branch) greedy tokens
    2K 1e9c612c2e6c7a32... identical (4)
    32K f5b2872c7ac30b17... identical (20)

    (main e88ee39d gives the same hashes as v0.1.45 did.)

Failed: none observed.

Not run:

  • HIP: ple.cu and kernels.cu are compiled for HIP too; no ROCm toolchain here. The change is plain integer code.
  • Metal: compiles prefill.cpp (the two host dumps); no Mac here.
  • SYCL: not touched (sycl/ has its own copies, left as they are).
  • A real NaN reaching these helpers in a run: the proof above is over the helpers' code, not a reproduced NaN.

Limits

Only these four helpers. The host references in tests and benches and hc_q8_bf16_kernel keep their plain rounding, because their inputs are finite.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2955 **Author:** [@sergqwer](https://github.com/sergqwer) **Created:** 10/17/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/bf16-nan` --- ### 📝 Commits (1) - [`c52d60b`](https://github.com/Niko1221/Strata/commit/c52d60b31094c82ecbc73faaacbb517e692c19b9) engine: f32 -> bf16 conversions in PLE and prefill keep a NaN a NaN, as bf16_bits.hpp does ### 📊 Changes **3 files changed** (+9 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/kernels/cuda/ple.cu` (+2 -0) 📝 `src/prefill/kernels.cu` (+2 -0) 📝 `src/prefill/prefill.cpp` (+5 -1) </details> ### 📄 Description ## Problem and result The engine has private f32 -> bf16 helpers that round to nearest even. Unlike `bf16_bits.hpp` (`bf16_from_f32`) and `dequant_bf16.cu`, they have no NaN test. The rounding add carries a NaN out of the mantissa, so a NaN is masked instead of reaching the NaN guard: for example 0x7FFFFFFF (the NaN pattern in #879) becomes 0x8000, i.e. -0. - Before: 131,072 of the 16,777,214 f32 NaN patterns come out of `bf16_bits` (PLE) and `bf` (prefill) as -0, +0 or an infinity. - After: every NaN comes out as a quiet NaN of the same sign. Every other input converts to exactly the same bits as before, so normal output does not change (checked over all 2^32 inputs, and end to end below). ## Related discussion None needed: a focused correctness fix. It was #2955 before the 0.2.0-beta tracker reset, unchanged except for the rebase. The fix is sebastianrcnt's (sebastianrcnt/Strata ef1a7ef5), co-author of the commit. ## Approach Each helper tests the magnitude bits first and returns the top half with the quiet bit set (`(i >> 16) | 64`), sign kept, as `bf16_bits.hpp` does: - `src/kernels/cuda/ple.cu` `bf16_bits` (`to_bf16_kernel`); - `src/prefill/kernels.cu` `bf`: the BF16 activation image of every prefill norm, plus `bf_lo` and dequant. The two host dumps in `src/prefill/prefill.cpp` get the same test: `STRATA_PREFILL_DUMP_R_ALL` (round to nearest even) and the `STRATA_DBG_FEAT` route record (round half up; that rounding is kept, only the NaN test is new). Left alone: host references in parity tests and benches (finite data), `fused_gr.cu` `hc_q8_bf16_kernel` (weights x scales, documented as finite) and `sycl/`. On `main` the set of private `0x7fff` rounding sites is the same as on 0.1.45, so the rebase found no new helper to cover. ## Validation Revision: this branch, one commit c52d60b3 on `main` **e88ee39d** (v0.2.0-beta + 12 commits), against a build of `main` e88ee39d itself. Machine: RTX 5090 (sm_120), Ryzen 9 9950X3D, 128 GB, Windows 11; MSVC 2022 17.14, CUDA 13.3, Ninja, release configure `-DSTRATA_ENABLE_CUDA=ON -DSTRATA_PORTABLE=ON -DCMAKE_CUDA_ARCHITECTURES="75-real;86-real;89-real;120-real" -DSTRATA_GGML_DIR=<llama.cpp at the pinned 3cf03257>`, target `strata`. **Passed:** - **All 2^32 f32 bit patterns.** A generated CUDA file holds the four helpers verbatim from `main` (old) and from this branch (new); built with `nvcc -O2 -std=c++17 -gencode arch=compute_120,code=sm_120 -I <this branch>/include`. Host (all four helpers) and device (the two device helpers, RTX 5090): | check | host | device | | --- | --- | --- | | non-NaN inputs that convert differently, old vs new | 0 | 0 | | NaN inputs that do not give a quiet NaN of the same sign (new) | 0 | 0 | | new round-to-nearest-even helpers != `bf16_from_f32` | 0 | 0 | | NaN inputs the old `bf16_bits` / `bf` masked (of 16,777,214) | 131,072 / 131,072 | 131,072 / 131,072 | - **Engine identity** on the NVFP4 GPTQ pack (`Maximilian228/Qwen3.8-Flash-Next-NVFP4-GPTQ-Strata`, upstream setup's `--family nvfp4 --model NVFP4` file set). `strata --serve` with setup's model files plus `--expert-cache 3000 --prefill auto --spec 6 --spec-min-p 0.7 --max-context 262144 --kv int8 --gpu 0 --pcie-frac 0 --adapt-every 1000000`, env `STRATA_PREFILL_CPU_SHARE=0 STRATA_PCIE_BALANCE=0 STRATA_IQ_MT_MIN=1 STRATA_DUMP_FIRST_LOGITS=<file>`; a 2,100-token and a 32,037-token prompt, 32 greedy tokens each (both stop at the end of turn). First-token logits and tokens are byte-identical to `main`'s: | prompt | sha256 of the first-token logits (`main` twice, this branch) | greedy tokens | | --- | --- | --- | | 2K | `1e9c612c2e6c7a32...` | identical (4) | | 32K | `f5b2872c7ac30b17...` | identical (20) | (`main` e88ee39d gives the same hashes as v0.1.45 did.) **Failed:** none observed. **Not run:** - HIP: `ple.cu` and `kernels.cu` are compiled for HIP too; no ROCm toolchain here. The change is plain integer code. - Metal: compiles `prefill.cpp` (the two host dumps); no Mac here. - SYCL: not touched (`sycl/` has its own copies, left as they are). - A real NaN reaching these helpers in a run: the proof above is over the helpers' code, not a reproduced NaN. ## Limits Only these four helpers. The host references in tests and benches and `hc_q8_bf16_kernel` keep their plain rounding, because their inputs are finite. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4359
No description provided.