Repository navigation
Train gpt-oss MXFP4 LoRA through unsloth-zoo's packed experts when load_in_16bit is not set - #11929
Conversation
…t is not set load_in_4bit = False without load_in_16bit kept the native Mxfp4GptOssExperts, whose triton_kernels matmul_ogs forward has no backward. With triton_kernels importable (vLLM registers its copy) Unsloth's class raises "Backwards pass using MXFP4 is still under construction". Without it, the transformers class returns an output detached from the graph, so every LoRA below a MoE layer trains on the residual gradient only (gpt-oss-20b: layer 0 LoRA gradient cosine to the bf16 reference about 0, total norm 7.5 vs 13.0). When unsloth_zoo keeps the experts packed (keep_mxfp4_experts_packed), ask for Mxfp4Config(dequantize = True) on that load too. The experts stay MXFP4 in memory and are decoded one layer at a time inside a differentiable forward. Without that zoo path, for full finetuning, and with UNSLOTH_MXFP4_KEEP_PACKED=0 the native load is unchanged.
unsloth_zoo cannot keep offloaded experts packed and only learns about the offload once transformers resolves the map, after the quantization config is built. With an explicit map that sends layers to cpu or disk, dequantize = True therefore turned every expert into a bf16 copy at load (gpt-oss-20b: 41.8 GB of GPU memory instead of 13.7 GB) where the native load did not dequantize before. Check the map in the helper and leave such loads as they were.
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 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 security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
…checkpoint to CPU auto / sequential / balanced resolve after the packed-experts decision, and transformers spills to CPU or disk once the accelerators are full; unsloth_zoo then dequantizes every expert to 16 bit. Size the safetensors checkpoint (Hub file metadata or the local folder) against the accelerators' free memory, capped by max_memory, and keep the native load when it would not fit. Unknown sizes keep the packed path.
…fload, and check packed support first The Hub lookup now takes the caller's revision. A known checkpoint size with no measured accelerator capacity (every card excluded or capped at 0 by max_memory) keeps the native load. The unsloth_zoo packed-path check runs before any sizing, so an older unsloth_zoo never triggers the Hub lookup or the accelerator probe.
…/disk maps, probe only allowed cards, size the cache offline Count top-level safetensors of the selected variant only: gpt-oss-120b's original/ folder doubled its size to 121.5 GiB and wrongly turned the packed path off on an 80 GB card. A device_map naming cpu or disk keeps the native load. With max_memory, only the cards it allows are probed. When the Hub is unreachable (local_files_only / HF_HUB_OFFLINE), the cached snapshot for the revision and cache_dir is sized instead.
…, skip cards that fail the probe, accept torch.device maps and the QuantizationMethod enum
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
unsloth/unsloth/models/vision.py
Lines 1877 to 1878 in eb5e8da
When the caller passes quantization_config=Mxfp4Config() (whose default is dequantize=False), the packed-path check can construct a replacement with dequantize=True, but this condition discards it because user_quantization_config is non-null and leaves the original object in kwargs. The subsequent from_pretrained therefore takes the native MXFP4 expert path, so LoRA below the MoE layers again receives the incomplete/detached gradient this change is intended to fix; update or replace the caller-supplied MXFP4 config when the packed-path gate succeeds.
ℹ️ 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".
| backend = torch.cuda | ||
| if not torch.cuda.is_available() and getattr(torch, "xpu", None) is not None: | ||
| if torch.xpu.is_available(): | ||
| backend = torch.xpu |
There was a problem hiding this comment.
Probe NPU memory before selecting packed experts
Fresh evidence after the XPU fix is that this repository also supports DEVICE_TYPE="npu", and Accelerate resolves automatic maps using torch.npu.device_count() and torch.npu.mem_get_info(). On such a host CUDA and XPU are unavailable, so this code leaves backend as torch.cuda, sets probed=False, and returns True without any capacity check; an oversized checkpoint can then spill to CPU after dequantize=True was selected and expand into 16-bit weights. Select the active DEVICE_TYPE_TORCH backend rather than special-casing only XPU.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this. On NPU the Transformers MXFP4 quantizer already sets dequantize=True itself (validate_environment only keeps MXFP4 on cuda, xpu or cpu), so the native load there is a bf16 copy either way and this gate cannot make it larger.
…ps the native load
…download snapshot_download(local_files_only=True) checks the cached tree listing, which names files a load never fetches (metal/, original/), so it raised and the size stayed unknown: an offline load too large for the GPU still took the packed path and zoo then dequantized every expert to 16 bit.
…s offload, before keeping MXFP4 experts packed device_map auto / balanced caps every card but the last at size / n, so summing raw free memory said a 60 GiB checkpoint fits on 80 + 4 GiB cards while accelerate would offload experts (which zoo then dequantizes to 16 bit). An explicit map that puts only lm_head, embeddings, norms, attention or the router on CPU no longer turns packed experts off; anything that could hold experts still does.
… matching zoo's guard
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
…/api/health) (#12275) * Wait for the fake nvidia-smi child to start instead of sleeping in the gpu_query tests Four tests started a thread, slept a fixed 0.15 to 0.5 s, then changed the fake's state, assuming the older child had already read it. On a loaded runner the child can start later and read the state meant for the next reading (seen as test_a_child_started_before_an_invalidation_is_not_joined reading 64 MiB instead of 180000). The fake logs each call after reading its state, so wait for that line. * Re-measure a cold /api/health call once before failing the launcher-deadline test A shared CI runner stalled one cold call to 2.06s against the desktop probe's 2.0s timeout (#11929's Python 3.13 shard); the same call takes 1.02-1.07s idle and on a contended core. A fresh process measures again and the lower reading is asserted, so a cold-path regression (checked with a 1.2s first-call delay) still fails while a one-off stall does not. The 2.0s bound itself is unchanged. * Trim comments
Depends on unslothai/unsloth-zoo#1358 (packed MXFP4 experts). unslothai/unsloth-zoo#1386 is also needed for correct sliding-window masks on transformers 5. Without #1358 this change is a no-op.
Problem
A LoRA load of native gpt-oss (
load_in_4bit = False, noload_in_16bit) keepsMxfp4GptOssExperts, whose triton_kernelsmatmul_ogsforward has no backward:NotImplementedError: Backwards pass using MXFP4 is still under construction!.Change
When unsloth-zoo reports that the experts stay packed (
keep_mxfp4_experts_packed()), the loader also asks forMxfp4Config(dequantize = True)on a LoRA load. The experts stay MXFP4 in memory and are decoded one layer at a time inside a differentiable forward.The native load is unchanged:
UNSLOTH_MXFP4_KEEP_PACKED=0Before / after (B200, transformers 5.17)
Backwards pass using MXFP4 is still under construction!Cost
On the same load used only for inference (20b, 3 prompts, 4 loads per arm, shared GPU so treat as noisy):
The prefill cost comes from unsloth-zoo #1358 decoding each layer's experts in every forward.
UNSLOTH_MXFP4_KEEP_PACKED=0restores the native load for inference-only use.Scope
Of 448 load configurations (7 models x 4 load modes x full finetuning x
UNSLOTH_MXFP4_KEEP_PACKEDx device map xFastLanguageModel/FastModel), 24 change, all native MXFP4 to packed:load_in_4bit = Falseorload_in_8bit = Trueload_in_4bit = True, since bitsandbytes is turned off for MXFP4 checkpointsWith an unsloth-zoo that lacks #1358, all 448 are identical to main.
Tests
tests/test_mxfp4_lora_packed_experts_dequantize.pyhas 9 CPU tests. All fail on main and pass here. Reverting the loader change fails the dequantize test, and removing the offload check fails the offload test. The related loader, gpt-oss and MXFP4 test files show no other change in failures on transformers 5.17, 5.4 and 4.57.6.