Repository navigation
Studio: load INT8 instead of NVFP4 on pre-Blackwell GPUs - #17229
Conversation
Codex Review Summary: ✅ CompletedThis 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. |
…ad the device before any header probe [skip ci]
…ROCm detector [skip ci]
…not initialised [skip ci]
…ly by the same model, keep unknown picks [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b01df131bf
ℹ️ 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".
| request, nvfp4_route = await _route_nvfp4_request( | ||
| request, "image", user_initiated = user_initiated | ||
| ) |
There was a problem hiding this comment.
Guard active training before probing CUDA for the reroute
On a heterogeneous NVIDIA host with CUDA not yet initialized in the Studio process, an NVFP4 load now probes CUDA before the training interlock runs. _smi_visible_capabilities() deliberately returns no result for heterogeneous cards, so _probe_device_profile() falls through to torch.cuda.get_device_capability(), which initializes CUDA. The image training guard still runs later in load_diffusion_model_gated, and the video route has the same ordering. Consequently, even a load that is ultimately rejected with 409 can create a competing CUDA context and consume VRAM beside an active training subprocess, potentially causing that run to OOM. Run the training guard before this probe, or keep the early routing probe context-free when training is active.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in the latest push. While training owns the GPU the route never initialises CUDA. nvidia-smi still answers for identical cards, and when only torch could read the cards (a mixed box with CUDA not yet initialised) the request is left as is and nothing is cached. The training check is asked only at that point. Tests: test_training_never_initialises_cuda_for_the_route, test_the_route_reads_the_cards_without_cuda_while_training.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
…ell GPUs (unslothai#17343) ## Summary On a pre-Blackwell GPU, Studio could swap a fine-tuned NVFP4 checkpoint for the official base model's INT8 build. It did this whenever the file or repo name spelled a known base. For example, `flux-2-klein-4b-my-finetune-nvfp4.safetensors`, or any file in `my-team/FLUX.2-klein-4B-personal-finetune`, loaded `black-forest-labs/FLUX.2-klein-4B`. The user got the stock model's images with no error, and only a precision fallback note. With this change, a checkpoint is replaced only when it is a known official NVFP4 checkpoint. Every other pick keeps its own weights. ## Root cause unslothai#17229 added `core/inference/diffusion_nvfp4_route.py`. When a pick was not a registered hosted repo, the router found the base with `_named_base`, which checks whether a known base's name appears in the filename or repo id. With an explicit `base_repo`, it used that base directly. Neither proves the checkpoint holds that base's weights, but the router still replaced `model_path` with the base pipeline and dropped the selected file. ## Fix A checkpoint is replaced only when it matches an explicit registry of official NVFP4 checkpoints: - the hosted `unsloth/*-NVFP4` repos and files, read from the family tables (`prequant_repos`, `prequant_variant_repos`, `prequant_filenames`); - a short list of vendor-published files, each mapped to its base: `black-forest-labs/FLUX.2-klein-{4b,9b}-nvfp4`, `FLUX.2-klein-base-{4b,9b}-nvfp4`, `FLUX.1-dev-NVFP4`, `FLUX.1-Kontext-dev-NVFP4`, `FLUX.2-dev-NVFP4`, and `Lightricks/LTX-2.5` `diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors`. Matching rules: - Hub pick: the repo id must be in the registry. If a file is named, it must be that repo's registered file. - Local file: the file name must be a registered official file name (case and punctuation ignored). - If the file sits in a Hub cache snapshot (`models--owner--name`), that repo must be the registered one. - If the Unsloth header records a base (`base_model_id`), it must be the mapped base. - Anything else is kept as picked and logged. That is the behaviour from before unslothai#17229: a weight-only load, or the existing refusal. The name-matching helpers (`_named_base`, `_family_int8_base`, `_video_int8_base`, `_name_tokens`) are removed. Explicit NVFP4 precision picks, text-encoder picks and prequant-path handling are unchanged. The kill switch `UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0` is also unchanged. The registry also keeps the earlier variant guarantees: an LTX-2.5 dev file, LTX-2.3 files and unregistered FLUX.1 variants are still kept. One behaviour change for a valid path: a local `qwen-image-2.1-turbo-nvfp4.safetensors` was previously replaced by `Qwen/Qwen-Image-2.1-Turbo`. No official NVFP4 file has that name, so it is now kept. The test table now expects that. ## Flip test `tests/test_diffusion_nvfp4_route.py::test_a_custom_nvfp4_checkpoint_keeps_its_own_weights` has six cases on a simulated sm_89 device: - a local fine-tune with a custom name, without and with a header naming another base; - the official file name with a header naming another base; - the official file name cached from another repo; - a third-party repo with the official file name; - a third-party `-nvfp4` repo with no filename. All six fail on main (each is replaced by `black-forest-labs/FLUX.2-klein-4B`) and pass with the fix. `test_an_official_nvfp4_checkpoint_is_still_replaced` covers these cases, which still reroute on both main and the fix: - the BFL klein 4B / 9B repos; - `unsloth/Z-Image-Turbo-NVFP4`; - a Hub cache snapshot of the BFL repo; - a local hosted file whose header records the right base. `test_every_trusted_nvfp4_file_resolves_to_a_family` checks that every vendor row maps to a base that a family loads. ## Tests run - `tests/test_diffusion_nvfp4_route.py` and `tests/test_video_routes.py` pass with the fix. This includes the existing 308-case routing matrix: the LTX-2.5 packed file, hosted repo and kill-switch arms are unchanged. - The 83 test files that reference NVFP4 (`-o addopts= -m "not server and not native_mxc"`, CPU) were run on main and with the fix. They have the same 16 failures on both sides: moss_image, export_reap, video_ltx25 and others, all pre-existing and unrelated. The only difference was the intended `qwen-image-2.1-turbo` expectation change. ## Not proven - No GPU run. The decision is a pure routing function, and it is covered on CPU with simulated device profiles. - The vendor file list matches the Hub listings as of today. A new official NVFP4 release needs a row in the registry before it reroutes. Until then it is kept, as it was before unslothai#17229. - A fine-tune saved under the exact official file name, outside a Hub cache and with no recorded base in its header, is still treated as official. Telling it apart would mean hashing multi-GB files.
Problem
NVFP4 only has a native path on NVIDIA Blackwell (compute capability major >= 10: sm100, sm103, sm110, sm120, sm121). On every older NVIDIA card (T4 sm75, A100 sm80, 3090 sm86, L4 / 4090 sm89, H100 sm90) an explicit NVFP4 pick can only run weight-only: the 4-bit codes are widened to bf16 per layer and the GEMM runs in bf16. Today that means one of two things. An explicit
transformer_quant="nvfp4"is refused with a 409 at preflight. An NVFP4 single file (such as the LTX-2.5 packed path from #17067) loads weight-only. Both are worse than the INT8 builds we already ship, which use INT8 tensor cores and are more accurate:Fix
One helper,
core/inference/diffusion_nvfp4_route.py, makes the decision for every entry point:loadanddownload-planroutes. It runs after the disabled-NVFP4 refusal and the account checks, and before any validation, preflight or plan reads the request, so a pre-Blackwell pick never plans or downloads an NVFP4 file. The route's decision, including "leave it as is", is handed tobegin_load, so the plan and the load always agree;DiffusionBackend.begin_loadandVideoBackend.begin_loadfor direct callers that skip the routes.The device is read only for an NVFP4 pick, once per process. When CUDA is not initialised and every visible card has the same capability, the reading comes from nvidia-smi, so a plan previewed during training takes no CUDA context. Otherwise it comes from
torch.cuda.get_device_capabilityper ordinal. ROCm is detected with the backend's ownrocm_bf16.is_rocm_torch, because ROCm reports gfx versions such as 11.x and 12.x through the same call. A failed read is not cached and leaves the request as it is. Tests replace the reading, so the whole matrix runs on CPU.INT8 is the family's normal INT8 resolution: the hosted INT8 ConvRot checkpoint where one is registered, else on-the-fly int8 with the family's rotation policy. A family whose profile denies int8 gets Auto. Off NVIDIA the pick takes whatever Auto picks there, because explicit INT8 on RDNA is slower than bf16.
What the caller sees: one INFO line (
diffusion.nvfp4_route: transformer_quant='nvfp4'->'int8' on nvidia sm_89: NVFP4 needs a Blackwell GPU; using INT8), and the load'sresolved.transformer_quant(andresolved.text_encoder_quant) carriesrequested: "nvfp4",value: "int8",status: "fell_back"and a reason startingNVFP4 needs a Blackwell GPU; using INT8. A replaced checkpoint is also named inreplaced(nvfp4:<file>). The frontend already rendersfell_backwith the reason as a tooltip, so no UI change is needed. No hard error is raised for any of these.Rules:
When a checkpoint is replaced. An NVFP4 single file, or a hosted NVFP4 repo, is replaced only by the same model's pipeline:
base_repo, else the base the names spell. The longest base any family table lists wins, in its canonical spelling, so FLUX.2 klein 9B, Qwen-Image-2.1-Turbo and FLUX.1 dev map to their own repos.ltx-2family file, since 2.0 to 2.3 share a 2.0 pipeline. Other families only when the names spell the pipeline.family_overridetells the families apart.When a checkpoint is kept. It is kept exactly as today, and logged, when any of these holds:
A recorded safetensors header scheme wins over the name, and a pickle is never opened to ask.
Mixed boxes. A load pinned to a card or a
gpu_idsselection reroutes when any card it places work on is pre-Blackwell. With no selection the backend can land on any card, so it reroutes only when every visible card is pre-Blackwell.NVFP4 prequant path. An NVFP4
transformer_prequant_pathis dropped on a rerouted load rather than handed to the INT8 path. An explicit non-NVFP4 precision is kept.Master switch off. With
UNSLOTH_NVFP4_DIFFUSIONoff (the default build), an explicit NVFP4 scheme keeps its existing 400 on every GPU, and the master switch still decides whether NVFP4 exists at all. NVFP4 single files that load today with the switch off (the LTX-2.5 packed path) are rerouted.Auto. Auto already never picks NVFP4 below sm100: the family
auto_preferrow has floor (10, 0), and_AUTO_LADDERhas no NVFP4 rung. A test now checks this for every image and video family, with every gate record and backend probe forced to pass.Kill switch.
UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0returns every request untouched (the same object, no annotation), which restores today's behaviour exactly.Known trade-off and limits
UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0keeps the file.fell_backannotation.Tests
CPU,
tests/test_diffusion_nvfp4_route.py(308 tests):torch.version.hipunset;begin_loadreroutes a direct caller before validation. An unchanged route decision is handed to the backend too.*-NVFP4repo and stages no NVFP4 file, while the same pick under the kill switch asksunsloth/Wan2.2-T2V-A14B-NVFP4.requested: nvfp4,fell_back, the reason andreplaced, and is unchanged when no route is active.Three
tests/test_video_routes.pytests used an explicit nvfp4 on the CPU test host as the example of a refused scheme. On a CPU host that pick now goes to Auto, so they now pin a Blackwell device, where NVFP4 still reaches those refusals.Related suites (52 files: NVFP4, precision, prequant, families, image and video backends and routes, auto policy, load progress, media auto-switch): 3992 passed on head (on the last full run, six timing-sensitive tests failed while the host was loaded and passed when rerun alone, on head and on the merge base). Across the earlier head-vs-merge-base runs (
4a45dbd03e), the failing test IDs were the same on both sides. Those weretest_diffusion_load_progress.py::test_quantized_tensor_is_counted_only_after_success_and_hooks_restore[False], which is flaky under xdist and passes alone, and atest_diffusion_load_cancel_platform_matrix.pycollection error from a missing_sparse_filehelper.GPU evidence (Colab L4 sm89 and A100 40GB sm80)
Every arm ran in a fresh process with
UNSLOTH_NVFP4_DIFFUSION=1. Base is the merge base4a45dbd03e. All runs used an explicit NVFP4 pick unless the arm says "int8".'nvfp4' is not usable for family 'z-image' on this GPU'nvfp4' is not usable for family 'qwen-image-2.1'replaced: nvfp4:...diffusion.nvfp4_route:line, and base, kill-switch and int8 arms logged none.These runs used head
599d6ed542. The head arms were re-run onb01df131b, after the reworked checkpoint-replacement rules, device read and decision order, and matched exactly:replaced, with the INFO linediffusion.nvfp4_route: diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors -> Lightricks/LTX-2.5-Diffusers (INT8) on nvidia sm_80.The commits after that only add the training-active guard on the device read and trim comments.
Not proven on hardware:
begin_load.