Repository navigation
Add unsloth.precision: one registry for low-precision recipes, refusals and high-precision layers - #16387
Merged
Conversation
…tural HP layers, engagement One table entry per format (fp16, bf16, FP8 tensorwise / rowwise / rowwise_with_gw_hp / blockwise, MXFP8, MXFP6, NVFP4, grouped MoE FP8 / MXFP8, QAT schemes, frozen fp8 bases) with per-GEMM fwd / dgrad / wgrad dtypes and scaling, high-precision layer policy, GradScaler kind and a per-GPU gate table filled from the L4 / RTX PRO 6000 probe. float8_training reads its named recipes and measured-faster table from the registry (unchanged values), refuses MXFP6 / MXFP8 / NVFP4 / blockwise with their own reasons before the model is loaded (fp16 for formats whose quantizer rejects it, blockwise off sm90), skips MoE routers found by structure as well as by name, and records engagement. QAT records how many layers it fake-quantizes. UNSLOTH_STRICT_ENGAGE=1 turns a zero-layer conversion into an error.
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. |
… as the expert count Qwen3-Next has num_experts == moe_intermediate_size == 512, so every expert and shared-expert gate_proj matched the config-width fallback and was kept out of float8 conversion (size_filter = False) and listed as a router. Skip modules inside an expert / shared expert, and accept gate_proj only beside an experts container.
…ratch paths The sm89 / sm120 / sm100 gate evidence printed outputs/precision_probe and a plans/ file in user-facing refusals; neither exists in the repo.
…ts list An MoE with more experts per block than blocks (Qwen2-MoE: 60 experts, 24 layers) returned model.layers.0.mlp.experts, so first_n / last_n / last_frac kept experts in high precision instead of the first / last decoder blocks. Ignore experts lists and lists nested inside another block list.
…) calls A plain TorchDispatchMode makes Dynamo refuse fullgraph frames (non-infra torch dispatch mode present). The counter now sets ignore_compile_internals, so compiled callables run and their low-precision GEMMs are still counted at runtime. [skip ci]
… vision or speech encoder
[skip ci]
[skip ci]
…ecision ops decoder_layers accepts a one-block decoder, finds the language decoder from the input embedding's nearest stack (Idefics3 / SmolVLM on transformers 4.57 returned the vision encoder), and gives an encoder-only model (BERT) its single stack. Lists of bare Linears are not block stacks. find_routers treats stacked 3-D expert weights as an experts container whatever their name and reads a router's width through a gating module that wraps one Linear (GraniteMoE / GraniteMoeHybrid on 4.57 found no router). find_output_heads keeps task heads outside the base model (score, classifier) when the model has no output embedding. count_lowp_gemms counts opaque custom ops that run low-precision GEMMs inside (unsloth::nvfp4_qat_linear, unsloth fp8 block matmuls, unsloth_zoo::mxfp4_ogs_moe, fbgemm f8f8bf16 family) once at their boundary, matched by qualified name. New parametrized test over 43 tiny transformers configs on the meta device.
[skip ci]
…MV, mxfp8 QAT admits bf16 only
[skip ci]
…ecision GEMM counts once
[skip ci]
…uter's inner Linear in high precision
…ainers that own stacked experts SwitchTransformers keeps its experts in a ModuleDict, so its routers were missed and a wrapped router's inner Linear could be converted to fp8. JetMoE's attention MoE container (self_attention.experts) owns stacked expert weights and its own router, but every module under a path containing 'expert' was skipped. Expert subtrees are now excluded by structure: members of an experts collection and shared-expert MLPs. Adds SwitchTransformers and JetMoE family rows and a ModuleDict float8 filter case. [skip ci]
…gerly (torch < 2.9 has no ignore_compile_internals) [skip ci]
danielhanchen
added a commit
that referenced
this pull request
Nov 1, 2026
…16428) ## Summary `Source lint`'s "No new risky loader call sites" step fails on every PR based on current main. #16413 shows it, a frontend-only PR: ``` 1 risky loader call site(s) not in the baseline: unsloth/models/float8_training.py:29 [dynamic-import] spec_from_file_location ``` #16387 (`0bfae01738`, "Add unsloth.precision") added `_load_precision()` to `unsloth/models/float8_training.py`. It imports `unsloth.precision`. When the file is loaded on its own, as the gloo test workers do by path without the `unsloth` package, it falls back to `spec_from_file_location` on `unsloth/precision/__init__.py` in the sibling directory. The path is built from `__file__` and a constant, not from any input, so it is safe. It just was not recorded. ## Change `python scripts/lint_risky_loaders.py --update`, as the step asks, then the new entry's `REVIEW ME` replaced with why it is safe. The diff adds that one entry and changes nothing else in the baseline. ## Testing - `python scripts/lint_risky_loaders.py`: exit 1 on main with the finding above; here, `ok: 116 risky loader call site(s), all recorded`. - `tests/security/test_lint_risky_loaders.py`: 46 passed.
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.
Why
Low-precision training choices are spread across
float8_training.py, the QAT schemes in_utils.pyand the FP8 / NVFP4 / MXFP4 kernels, each with its own name checks. Requests for formats Unsloth cannot train in yet (MXFP6, MXFP8 or NVFP4 under float16, blockwise FP8 off Hopper) fall through to a generic "not supported" or, worse, start loading first. The float8 layer filter also matches routers by exact name only, so a router calledwgis converted to fp8.What
A new
unsloth.precisionpackage that every low-precision path reads from:UNSLOTH_STRICT_ENGAGE=1turns "converted zero layers" into an error, and a counter records low-precision matmul calls.float8_training.pynow reads its recipe names and measured-faster table from the registry with unchanged values (a test pins them to the previous literals). The loader passesdtype, so a float16 MXFP8 request is refused before loading. QAT records how many layers it fake-quantizes.One intended behaviour change: the float8 filter also keeps shape-detected routers in high precision. On dense and MoE Qwen3 the converted set is identical to before.
Measured support (torch 2.13, torchao 0.18, op level, fp32 reference)
Tests
tests/test_precision_registry.py: 26 CPU tests (refusals raise before load, registry values pinned to the previous literals, structural router / head / embedding selection, strict engagement).wgis converted to fp8, and strict mode with zero converted layers returns[]. After: specific refusals, the router stays in high precision, strict mode raises.Not covered