feat(bfp-quant): serve attention+GDN projection weights as bfloat4_b (#62) #99

Merged
Grok merged 1 commit from bfp4-overlay into master 2026-09-26 10:34:48 +02:00
Owner

Lands the bf4 weight overlay on master, split out of the WIP deploy branch deploy/g2-prefix-0019 so it is not blocked behind that one.

Why

Master has none of QWEN36_ATTN_GDN_BF4. Serving a grid-snapped "G2" checkpoint on master therefore produces 256 BFLOAT4_B tensors (the four MLP projections) against 128 BFLOAT8_B (attention and GDN) across all 64 layers — G2's re-rounded values at the stock dtype mix. That is the worst of both, and it means no bfp4 serving measurement currently exists. This flag is the precondition for one.

Re-derived over the validated byte accounting, it is worth 3.72 ms/token of weight read (20.24 -> 16.52) and ~1.8 GiB/card.

What it does, precisely

QWEN36_ATTN_GDN_BF4=1 switches 5 weight sites in attention/tp.py (wqkv-fused / wqkv / wk / wv / wo) and 5 in gdn/tp.py (qkvzab / qkvz / ab / out / out.colpar) from bfloat8_b to bfloat4_b. Default off; unset, both files are byte-identical in behaviour to master.

Weights only. Activations, the KV cache and the GDN state are untouched. In particular the ttnn.typecast(q|k|v, bfloat8_b) sites on the QWEN_SDPA_BF8 path keep bfloat8_b explicitly — a blanket dtype substitution would have caught the query cast at attention/tp.py:766 and silently made activations bf4.

It is a weight-format switch, not a quantiser: it says how the loader packs whatever the checkpoint holds. Over a checkpoint not rounded onto the bfp4_b grid it degrades to round-to-nearest bf4 and is measurably worse than production (KL 0.176 vs 0.111). Pair it only with a grid-snapped checkpoint; G2 is the measured pairing (KL 0.085, top-1 0.944, GSM8K 58/60).

Two things worth reviewing

1. gdn/tp.py was rebuilt on master's copy, not carried over. The development branch predates #86, and a direct copy would have silently deleted master's column-parallel prefill out-projection, _silu_mul, the guarded recurrent_gated_delta_rule_decode_packed_ttnn import and the loguru logger — 66 deletions. The hunk here is re-applied onto master's file instead; the diff is +23/-10 and touches nothing else.

2. out_colpar takes the same dtype as the other four. It did not exist when the overlay was written. If it had been left at bfloat8_b, running QWEN36_GDN_OUTPROJ_COLPAR together with bf4 would serve that one projection at bf8 while the banner claimed all-bf4. Flagging it explicitly since it is the one judgement call in the patch rather than a mechanical re-apply.

Scope / inertness

  • weight_mapping.py and bfp4_unpack.py add bfp4-packed-v1 checkpoint loading. They key off bfp4: safetensors metadata and are inert without it. Master's model_config.py already imports the five symbols these provide (is_fp8_checkpoint, is_nvfp4_checkpoint, load_qwen36_state_dict_fp8, load_qwen36_state_dict_nvfp4, remap_qwen36_state_dict) — all present.
  • Nothing mounts the two new files yet. The modules/tenstorrent-serving.nix mount lines and the weightsBf4 / bfp4Packed options stay on the deploy branch; landing them here would put my weight_mapping.py over the in-image copy on the FP8 path too, which is a behaviour change this PR should not smuggle in.
  • model.py and tp_common.py differ between the branches purely from master-side drift (#85, #76, #87, #86). The development branch never touched them and neither does this PR.

Pre-existing hazard, not introduced here

The tensor cache names bins by tensor + dtype, never by checkpoint. Two checkpoints sharing a dtype therefore collide. That is unchanged by this PR but it bites bf4 arms in practice — use a distinct cache_dir per checkpoint.

Licensing

ThinkingCap is PolyForm Small Business 1.0.0. This change ships code only; no weights are published or redistributed, and the use case is internal serving. Recorded here so it is on file.

Refs #62.

🤖 Generated with Claude Code

Lands the bf4 weight overlay on master, split out of the WIP deploy branch `deploy/g2-prefix-0019` so it is not blocked behind that one. ## Why Master has none of `QWEN36_ATTN_GDN_BF4`. Serving a grid-snapped "G2" checkpoint on master therefore produces **256 BFLOAT4_B tensors (the four MLP projections) against 128 BFLOAT8_B (attention and GDN) across all 64 layers** — G2's re-rounded *values* at the stock dtype mix. That is the worst of both, and it means no bfp4 serving measurement currently exists. This flag is the precondition for one. Re-derived over the validated byte accounting, it is worth **3.72 ms/token of weight read (20.24 -> 16.52) and ~1.8 GiB/card**. ## What it does, precisely `QWEN36_ATTN_GDN_BF4=1` switches 5 weight sites in `attention/tp.py` (wqkv-fused / wqkv / wk / wv / wo) and 5 in `gdn/tp.py` (qkvzab / qkvz / ab / out / out.colpar) from `bfloat8_b` to `bfloat4_b`. Default off; unset, both files are byte-identical in behaviour to master. **Weights only.** Activations, the KV cache and the GDN state are untouched. In particular the `ttnn.typecast(q|k|v, bfloat8_b)` sites on the `QWEN_SDPA_BF8` path keep `bfloat8_b` explicitly — a blanket dtype substitution would have caught the query cast at `attention/tp.py:766` and silently made activations bf4. It is a weight-**format** switch, not a quantiser: it says how the loader packs whatever the checkpoint holds. Over a checkpoint *not* rounded onto the bfp4_b grid it degrades to round-to-nearest bf4 and is measurably worse than production (KL 0.176 vs 0.111). Pair it only with a grid-snapped checkpoint; G2 is the measured pairing (KL 0.085, top-1 0.944, GSM8K 58/60). ## Two things worth reviewing **1. `gdn/tp.py` was rebuilt on master's copy, not carried over.** The development branch predates #86, and a direct copy would have silently deleted master's column-parallel prefill out-projection, `_silu_mul`, the guarded `recurrent_gated_delta_rule_decode_packed_ttnn` import and the loguru logger — 66 deletions. The hunk here is re-applied onto master's file instead; the diff is +23/-10 and touches nothing else. **2. `out_colpar` takes the same dtype as the other four.** It did not exist when the overlay was written. If it had been left at `bfloat8_b`, running `QWEN36_GDN_OUTPROJ_COLPAR` together with bf4 would serve that one projection at bf8 while the banner claimed all-bf4. Flagging it explicitly since it is the one judgement call in the patch rather than a mechanical re-apply. ## Scope / inertness - `weight_mapping.py` and `bfp4_unpack.py` add bfp4-packed-v1 checkpoint loading. They key off `bfp4:` safetensors metadata and are inert without it. Master's `model_config.py` already imports the five symbols these provide (`is_fp8_checkpoint`, `is_nvfp4_checkpoint`, `load_qwen36_state_dict_fp8`, `load_qwen36_state_dict_nvfp4`, `remap_qwen36_state_dict`) — all present. - **Nothing mounts the two new files yet.** The `modules/tenstorrent-serving.nix` mount lines and the `weightsBf4` / `bfp4Packed` options stay on the deploy branch; landing them here would put my `weight_mapping.py` over the in-image copy on the FP8 path too, which is a behaviour change this PR should not smuggle in. - `model.py` and `tp_common.py` differ between the branches purely from master-side drift (#85, #76, #87, #86). The development branch never touched them and neither does this PR. ## Pre-existing hazard, not introduced here The tensor cache names bins by tensor + dtype, never by checkpoint. Two checkpoints sharing a dtype therefore collide. That is unchanged by this PR but it bites bf4 arms in practice — use a distinct `cache_dir` per checkpoint. ## Licensing ThinkingCap is PolyForm Small Business 1.0.0. This change ships code only; no weights are published or redistributed, and the use case is internal serving. Recorded here so it is on file. Refs #62. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(bfp-quant): serve attention+GDN projection weights as bfloat4_b (#62)
Some checks failed
tt-stack-ci / Report upstream drift (pull_request) Successful in 8s
tt-stack-ci / Build simulators and check the host module (pull_request) Failing after 20s
tt-stack-ci / Report upstream drift (push) Successful in 6s
tt-stack-ci / Build simulators and check the host module (push) Failing after 15s
50c2a777cb
QWEN36_ATTN_GDN_BF4=1 makes the attention and GDN projection loaders pack
their weights as bfloat4_b instead of bfloat8_b. Without it master serves a
grid-snapped "G2" checkpoint at the stock dtype mix -- 256 BFLOAT4_B tensors
(the four MLP projections) against 128 BFLOAT8_B (attention and GDN) across
all 64 layers -- i.e. G2's re-rounded VALUES at production precision, which
is the worst of both and makes a bfp4 serving measurement impossible to take.

Over the validated byte accounting the flag is worth 3.72 ms/token of weight
read (20.24 -> 16.52) and ~1.8 GiB/card.

WEIGHTS ONLY. Activations, the KV cache and the GDN state are untouched: the
q/k/v typecast sites on the QWEN_SDPA_BF8 path keep bfloat8_b explicitly.

This is a weight-FORMAT switch, not a quantiser -- it says how the loader
packs whatever the checkpoint holds. Over a checkpoint not rounded onto the
bfp4_b grid it is plain round-to-nearest bf4 and measurably worse than
production (KL 0.176 vs 0.111). Pair it only with a grid-snapped checkpoint.

gdn/tp.py is rebuilt on master's copy rather than carried over from the
development branch, so the column-parallel prefill out-projection from #86
survives; its out_colpar weight takes the same dtype as the other four, so
QWEN36_GDN_OUTPROJ_COLPAR cannot silently serve that projection at bf8 while
the banner claims all-bf4.

weight_mapping.py and bfp4_unpack.py add bfp4-packed-v1 checkpoint loading.
They are inert without "bfp4:" safetensors metadata, and nothing mounts them
yet -- the nixos module change lands separately.

Weights are not published or redistributed by this change; ThinkingCap is
PolyForm Small Business 1.0.0 and the use case is internal serving only.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Grok merged commit 50c2a777cb into master 2026-09-26 10:34:48 +02:00
Author
Owner

agent-gateway closeout (merged): cleared agent:* labels.
Hermes kanban card (if any) is left for Hermes to archive/complete.

agent-gateway closeout (merged): cleared `agent:*` labels. Hermes kanban card (if any) is left for Hermes to archive/complete.
Sign in to join this conversation.
No reviewers
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
bitpartner/tt-stack!99
No description provided.