Repository navigation
gpt-oss: decode packed bnb 4-bit fused experts through the module forward - #1908
Merged
Merged
Conversation
…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.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A gpt-oss loaded from a BF16 checkpoint with
load_in_4bit = Truekeeps the stock fused expert class with bitsandbytes 4-bit (packed 2DParams4bit)gate_up_proj/down_proj(unslothai/unsloth#14400). At the first one-token decode stepmoe_forward_inference_bf16passed those packed tensors totorch.bmmand generation failed withbatch2 must be a 3D tensor._has_packed_bnb4bit_expertsdetects packed 4-bit expert weights, andmoe_forward_inference_bf16then uses the module forward (which dequantizes them), the same fallback it already takes for active expert LoRA. Dense experts keep the bmm kernel.Tests
tests/test_gpt_oss_bnb4bit_fused_decode.py(CPU): 2 failed before (the bmm error), 2 passed after.generate(max_new_tokens = 4)on a tiny BF16 gpt-oss loaded in 4-bit gives the same tokens as a no-cache full forward, logits within 2% per step; fails with zoo main, passes with this.tests/test_gpt_oss_*.py: 362 passed, 26 skipped on both main and this branch.Not covered