Skip to content

gpt-oss: decode packed bnb 4-bit fused experts through the module forward - #1908

Merged
danielhanchen merged 1 commit into
mainfrom
dh/s2-w17-gptoss-decode-z
Oct 20, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/s2-w17-gptoss-decode-z

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

A gpt-oss loaded from a BF16 checkpoint with load_in_4bit = True keeps the stock fused expert class with bitsandbytes 4-bit (packed 2D Params4bit) gate_up_proj / down_proj (unslothai/unsloth#14400). At the first one-token decode step moe_forward_inference_bf16 passed those packed tensors to torch.bmm and generation failed with batch2 must be a 3D tensor.

_has_packed_bnb4bit_experts detects packed 4-bit expert weights, and moe_forward_inference_bf16 then uses the module forward (which dequantizes them), the same fallback it already takes for active expert LoRA. Dense experts keep the bmm kernel.

Tests

Not covered

  • Decode speed of the fallback (module forward per token) is not timed.

…ward

moe_forward_inference_bf16 called torch.bmm on the fused expert weights, which
are packed 2D Params4bit once a BF16 gpt-oss is loaded with load_in_4bit, so
generate failed with 'batch2 must be a 3D tensor'. Use the module forward,
which dequantizes them, as is already done for active expert LoRA.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T20:56:47.507311Z 43c9692 Manual request
🔒 Security Review ✅ Completed 2026-10-20T20:55:31.117286Z 43c9692 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 43c9692c7f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen danielhanchen self-assigned this Oct 20, 2026
@danielhanchen
danielhanchen merged commit e1d0b6d into main Oct 20, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 21, 2026
…xperts and router (unslothai#14400)

## Summary
Loading a gpt-oss checkpoint that is not pre-quantized (a merged
fine-tune, a local save, `unsloth/gpt-oss-20b-BF16`) with `load_in_4bit
= True` silently left every MoE expert and the router randomly
initialised. `_load_in_4bit_` in UNSLOTH_MODEL_NAME makes unsloth_zoo
install the split-layout bnb classes (`experts.gate_up_projs.<i>`,
`router.linear`), which only match pre-quantized checkpoints; a fused
checkpoint has none of those keys, so they stayed at init and were not
quantized either. The only sign was UNEXPECTED / MISSING lines in the
load report.

- Only set `_load_in_4bit_` for gpt-oss when the checkpoint has a
quantization config (loader.py and `sync_unsloth_model_name_bnb_flags`).
Fused checkpoints keep the stock classes and are quantized by the
regular fused MoE bnb path.
- Raise a clear error if the split classes were already installed for a
fused base (adapter-only repo on a BF16 base).
- The `gpt-oss-20b` / `gpt-oss-120b` name check that skips the bnb
config now only applies when the config has a quantization config
(MXFP4), so a BF16 gpt-oss-20b checkpoint is quantized.

- For a bitsandbytes config the expert layout is read from the
checkpoint's tensor names (index json or safetensors headers; headers
only on the Hub, no weight download): split `gate_up_projs.<i>` names
keep the split classes, fused names (for example
`unsloth/gpt-oss-20b-bnb-4bit`, dense BF16 experts) load through the
stock classes. Unreadable names fall back to the previous rule. A 4-bit
save of fused experts without quant state (packed bytes only) raises a
clear error instead of loading random experts.
- Non pre-quantized checkpoints in 4-bit float16 are refused (the fused
experts would overflow); bfloat16 is unaffected.
- One-token decode on packed fused experts needs
unslothai/unsloth-zoo#1908 (merge it first).

Pre-quantized split bnb, MXFP4 and 16-bit loads are unchanged.

## Tests
`tests/test_gpt_oss_bf16_load_in_4bit.py` (tiny gpt-oss saved as BF16
with distinct expert / router weights): main 7 failed, this PR 7 passed
(torch 2.10, 2.13; 2.14 without bitsandbytes skips the GPU test).

| load | experts | expert 0 vs checkpoint (max abs) | router vs
checkpoint | logits rel err |
|---|---|---|---|---|
| main, 4-bit | split bnb class, plain bf16, random init | 0.161
(checkpoint max 0.156) | 0.115 | 0.365 vs bf16 |
| this PR, 4-bit | stock class, Params4bit | 0.0186 (NF4 error) | 0 |
0.0078 vs transformers with the same dequantized weights |
| 16-bit (both) | stock | 0 | 0 | 0.0072 vs bf16 |

LoRA forward + backward gives non-zero expert grads. Existing gpt-oss
loader tests unchanged (42 passed, 2 skipped on both).

## Not covered
Real 20B weights, transformers 4.x, pre-quantized and MXFP4 checkpoints
end to end (unchanged by construction; unit tests cover their config
dicts).

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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