Skip to content

spark-model: reserve lazy FP8->BF16 copies in the KV budget (#71) - #83

Merged
AzeezIsh merged 1 commit into
mainfrom
fix/cublas-bf16-copy-budget
Sep 26, 2026
Merged

AzeezIsh merged 1 commit into
mainfrom
fix/cublas-bf16-copy-budget

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Fixes the OOM hazard logged on #71 (found while validating #77).

Problem

With ATLAS_CUBLAS_GEMM=1, ATLAS_CUTLASS_GEMM=1 or ATLAS_FP8_ROWWISE=1, dequant_fp8_bf16_cached materialises a BF16 [N,K] copy of each FP8 projection on the first prefill that routes it through cuBLASLt or CUTLASS BF16. The KV pool is sized before that, so the copies sit outside the memory budget. On a unified-memory GB10, nvidia/Qwen3.8-27B-NVFP4 at util 0.88 planned 54.2 GB pre-KV + 49.9 GB KV. The first prefill's copies then took the box to 2.1 GB MemAvailable, and the watchdog killed the serve (job 262).

Fix

factory/build.rs KV sizing now subtracts ops::lazy_bf16_reserve(&store) next to the inference reserve.

  • The reserve is numel * 2 over the store's resident FP8 matrices, which is the superset the dequant can reach.
  • Routed experts are excluded: they run the MoE kernels and never reach the dequant, and counting them would book an FP8-MoE pack's whole expert set.
  • Deferred tensors (e.g. the qwen4_exp PLE n-gram table) are not resident and do not count.
  • With a flag on, the boot log states the reserve, and the KV cache: line gains a + X GB lazy BF16 term. A config that cannot fit now refuses at boot with the flag named in the error, instead of hanging the box on the first request.
  • With every flag off, the reserve is 0 and every log line and number is unchanged.

Validation

  • fmt, clippy -p spark-model --tests; unit tests cover the byte count, including routed-expert exclusion, and the flag predicate.
  • reiner job 274, arm C71: 27B, ATLAS_CUBLAS_GEMM=1, util 0.88, with 2.6k and 10k prefills. Pass = the reserve is logged, the prefills run, and MemAvailable stays clear of the watchdog.
  • Merges with the GB10 merge train (gated by ST-995 + the 2.5 h agentic perf leg on the combined tree).

With ATLAS_CUBLAS_GEMM, ATLAS_CUTLASS_GEMM or ATLAS_FP8_ROWWISE set,
dequant_fp8_bf16_cached materialises a BF16 [N,K] copy of each FP8
projection on the first prefill. That happens after the KV pool is sized, so
the copies sit outside the memory budget. On a unified-memory GB10 at util
0.88 the 27B dropped to 2.1 GB MemAvailable (reiner job 262).

The KV sizing now reserves the upper bound up front: numel * 2 over the
store's resident FP8 matrices, excluding routed experts, which run the MoE
kernels and never reach the dequant. The KV pool shrinks by that amount, and
a config that cannot fit refuses at boot instead of hanging the box on the
first request. With the flags off the reserve is 0 and every log line and
number is unchanged.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Validated on GB10: reiner job 274, arm C71.

Setup: merge-train tree 53f4a3d3a, all-targets build. The serve was nvidia/Qwen3.8-27B-NVFP4 with ATLAS_CUBLAS_GEMM=1 at util 0.88, the same config that took the box to 2.1 GB MemAvailable and got killed in job 262.

  • Boot log:
    • lazy BF16 copies (ATLAS_CUBLAS_GEMM / ATLAS_CUTLASS_GEMM / ATLAS_FP8_ROWWISE): reserving 15.6 GB outside the KV pool for dequantised FP8 weights
    • KV cache: … 54.8 GB pre-KV + 1.2 GB reserve + 15.6 GB lazy BF16 → 33.7 GB for KV → 552320 max KV tokens
  • Result:
    • MemAvailable stayed between 17.37 and 17.56 GB across the boot warm-up and both prefill rounds (7 × 2.6k at a 2.42 s median, 7 × 10.2k at a 9.46 s median).
    • 0 watchdog kills.
    • The copies had already materialised during the warm-up request, so the flat trace is memory holding after them, not copies failing to appear.
  • Unit tests: lazy_bf16_* pass on GB10.

The reserve is the documented superset (every resident FP8 matrix except routed experts), so it is conservative. Even so, the 27B keeps 552k KV tokens at util 0.88.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Merged as part of the GB10 merge train (main 1ab38536b). The train was #78, #67, #68, #82, #83, then #89 and #90.

Gates on the combined tree:

  • ST-995 (job 280): PASS, 84.72 / 84.68 against bars of 82.72 / 81.65.
  • Agentic perf leg (job 298): VALID. 1007 of 1007 turns, 0 failed, 0 missing, 89 min.
  • Support matrix (job 346): green. Nemotron-3.5-Lightning NVFP4, Qwen3.6-35B-A3B NVFP4 and Qwen3.6-35B-A3B FP8 each boot with 0 unresolved lookups on main66 + fixes and on the train tree. Greedy probes are identical between the two trees (mean |Δlogprob| 0.0000), and 10k TTFT is equal or slightly faster.

Tree check: main has the same tree as the job-346 build (f8a04db1f): git diff is empty. main also differs from the tree the gates ran on (53f4a3d3a) only by the marker [expected_absent] declarations (f261446bd), which are TOML and tests only.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant