Skip to content

Studio: load INT8 instead of NVFP4 on pre-Blackwell GPUs - #17229

Merged
danielhanchen merged 9 commits into
mainfrom
studio-nvfp4-pre-blackwell-int8
Nov 9, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-nvfp4-pre-blackwell-int8

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

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:

model INT8 ConvRot LPIPS vs bf16 best NVFP4 LPIPS vs bf16 (fake-quant)
FLUX.2 klein 4B 0.0335 0.128
Z-Image 0.077 0.125

Fix

One helper, core/inference/diffusion_nvfp4_route.py, makes the decision for every entry point:

  • the image and video load and download-plan routes. 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 to begin_load, so the plan and the load always agree;
  • DiffusionBackend.begin_load and VideoBackend.begin_load for 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_capability per ordinal. ROCm is detected with the backend's own rocm_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.

host explicit NVFP4 DiT NVFP4 text encoder NVFP4 single file / hosted NVFP4 repo of a known model
NVIDIA sm >= 10 (Blackwell) unchanged unchanged unchanged
NVIDIA sm80 to sm90 INT8 INT8 that model's pipeline with INT8
NVIDIA sm75 (T4), image INT8 (the fp16-card int8 linear from #17167), Auto if that is switched off Auto (no TE int8 below sm80) that model's pipeline with INT8
NVIDIA sm75 (T4), video Auto (video INT8 needs bf16) Auto that model's pipeline with Auto
ROCm / XPU / MPS / CPU Auto Auto that model's pipeline with Auto

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's resolved.transformer_quant (and resolved.text_encoder_quant) carries requested: "nvfp4", value: "int8", status: "fell_back" and a reason starting NVFP4 needs a Blackwell GPU; using INT8. A replaced checkpoint is also named in replaced (nvfp4:<file>). The frontend already renders fell_back with 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:

    • Image: the explicit 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.
    • Video: LTX-2.5 distilled files only, since the pack runs only its distilled DiT. Never an ltx-2 family file, since 2.0 to 2.3 share a 2.0 pipeline. Other families only when the names spell the pipeline.
    • Hosted NVFP4 repos: only when one family owns the repo, or the filename or family_override tells the families apart.
  • When a checkpoint is kept. It is kept exactly as today, and logged, when any of these holds:

    • its family or variant cannot be resolved;
    • the pick names its own text-encoder or VAE files;
    • the load was not picked by hand (media auto-switch tracks the model by the path it asked for);
    • the local path does not exist.

    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_ids selection 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_path is 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_DIFFUSION off (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_prefer row has floor (10, 0), and _AUTO_LADDER has 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=0 returns every request untouched (the same object, no annotation), which restores today's behaviour exactly.

Known trade-off and limits

  • LTX-2.5 on A100-40. The priority matrix measured the explicit NVFP4 packed file at 14.6 s/clip against 16.4 s/clip for INT8, with LPIPS 0.212 against 0.137. The single-clip run below has the two within about 2%. INT8 also holds more VRAM (30.9 GiB peak against 23.3 GiB), in exchange for clearly better quality.
  • Fine-tunes. A fine-tuned NVFP4 file whose name matches a stock variant loads that stock variant's INT8 weights. UNSLOTH_NVFP4_PRE_BLACKWELL_INT8=0 keeps the file.
  • Mixed boxes with no GPU selection. On a box with both Blackwell and pre-Blackwell cards, an explicit NVFP4 with no selection is left as is.
  • Manual downloads. The Hub download manager can still fetch an NVFP4 file the user downloads by hand. Only loads and download plans are rerouted.
  • MLX. On Apple Silicon the MLX routing engine takes no route argument, so a rerouted pick shows the INFO line but not the fell_back annotation.

Tests

CPU, tests/test_diffusion_nvfp4_route.py (308 tests):

  • Routing matrix. Capabilities {7.5, 8.0, 8.6, 8.9, 9.0, 10.0, 12.0} x devices {cuda, rocm, mps, cpu, xpu} x picks {explicit DiT nvfp4, TE nvfp4, hosted NVFP4 source, hosted NVFP4 repo, LTX-2.5 packed NVFP4 file, unknown local NVFP4 file, auto}.
  • Switches. The kill switch arm for every pick, and the NVFP4-switch-off arm.
  • Blackwell. sm100 / sm103 / sm110 / sm120 / sm121 requests come back untouched (the same request object).
  • Device reading.
    • mixed boxes, and selections that span cards;
    • ROCm reporting 12.0, including wheels that leave torch.version.hip unset;
    • nvidia-smi answering without initialising CUDA (identical cards only);
    • an unreadable device that is neither cached nor acted on;
    • an ordinary request never reading the device;
    • no CUDA initialisation while training is active.
  • Variant safety.
    • FLUX.2 klein 9B, Qwen-Image-2.1-Turbo, FLUX.1 dev and LTX-2.5 distilled map to their own pipelines.
    • An LTX-2.5 dev file, an LTX-2.3 file and an unspelled FLUX.1 variant are kept.
    • Shared hosted repos (HunyuanVideo-1.5 480p / 720p) are disambiguated or kept.
    • Component-file picks, hand-free loads and missing paths are kept.
    • A bare folder or file path decides the same as its (folder, file) pair.
  • Entry points. Every load and download-plan route (image and video) reroutes after its refusals and account checks. begin_load reroutes a direct caller before validation. An unchanged route decision is handed to the backend too.
  • No NVFP4 download. The rerouted Wan 2.2 A14B plan asks no *-NVFP4 repo and stages no NVFP4 file, while the same pick under the kill switch asks unsloth/Wan2.2-T2V-A14B-NVFP4.
  • Resolved record. It reports requested: nvfp4, fell_back, the reason and replaced, and is unchanged when no route is active.

Three tests/test_video_routes.py tests 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 were test_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 a test_diffusion_load_cancel_platform_matrix.py collection error from a missing _sparse_file helper.

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 base 4a45dbd03e. All runs used an explicit NVFP4 pick unless the arm says "int8".

card model arm result engaged resolved.transformer_quant DiT layers peak GiB s/step
L4 Z-Image-Turbo 1024, 8 steps base refused (409 at preflight) - 'nvfp4' is not usable for family 'z-image' on this GPU - - -
L4 Z-Image-Turbo head renders int8 fell_back, "NVFP4 needs a Blackwell GPU; using INT8; seeded from the hosted checkpoint ...Z-Image-Turbo-INT8-ConvRot.safetensors" ConvRotLinear 239 14.84 0.746
L4 Z-Image-Turbo head, kill switch refused, same text as base - - - - -
L4 Z-Image-Turbo head, explicit int8 renders int8 applied, same hosted file ConvRotLinear 239 14.84 0.755
L4 Qwen-Image-2.1-Turbo 1024, 8 steps base refused (409 at preflight) - 'nvfp4' is not usable for family 'qwen-image-2.1' - - -
L4 Qwen-Image-2.1-Turbo head renders int8 fell_back, seeded from ...Qwen-Image-2.1-Turbo-INT8-ConvRot.safetensors ConvRotLinear 224 17.29 0.823
L4 Qwen-Image-2.1-Turbo head, kill switch refused, same text as base - - - - -
L4 Qwen-Image-2.1-Turbo head, explicit int8 renders int8 applied ConvRotLinear 224 17.29 0.831
A100-40 Z-Image-Turbo base / head kill switch refused, same text - - - - -
A100-40 Z-Image-Turbo head renders int8 fell_back, hosted INT8 ConvRot ConvRotLinear 239 16.23 0.233
A100-40 Z-Image-Turbo head, explicit int8 renders int8 applied ConvRotLinear 239 16.23 0.226
A100-40 LTX-2.5 NVFP4 distilled file, 768x512x49, 8 steps base renders None (packed W4A16) "not engaged (dense bf16 DiT loaded)" PackedBlockLinear 1176 23.26 1.061
A100-40 LTX-2.5 NVFP4 file head renders int8 fell_back, "NVFP4 needs a Blackwell GPU; using INT8 from Lightricks/LTX-2.5-Diffusers instead of diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors", replaced: nvfp4:... NativeWeightOnlyLinear 480 (W8A8, g256 ConvRot) 30.93 1.086
A100-40 LTX-2.5 NVFP4 file head, kill switch renders, same as base None same as base PackedBlockLinear 1176 23.26 1.050
A100-40 LTX-2.5-Diffusers pipeline head, explicit int8 renders int8 applied NativeWeightOnlyLinear 480 30.93 1.081
  • Each head NVFP4 arm logged exactly one diffusion.nvfp4_route: line, and base, kill-switch and int8 arms logged none.
  • No arm requested an NVFP4 URL from the Hub.
  • Each head NVFP4 arm has the same artifact, layer census and render statistics as the explicit int8 arm, so it is the normal INT8 resolution.
  • The kill switch matches base byte for byte on the refusal text, and on the LTX layer census, peak VRAM and render statistics.
  • s/step is one steady image or clip per arm with no A/A repeats, so treat it as indicative, not a speed claim.

These runs used head 599d6ed542. The head arms were re-run on b01df131b, after the reworked checkpoint-replacement rules, device read and decision order, and matched exactly:

  • L4 Z-Image-Turbo, head NVFP4: int8, the same hosted INT8 ConvRot file, the same render statistics.
  • L4 Z-Image-Turbo, kill switch: the same refusal text as base.
  • A100-40 LTX-2.5 file, head NVFP4: int8 and replaced, with the INFO line diffusion.nvfp4_route: diffusion_models/ltx-2.5-22b-distilled-transformer-nvfp4.safetensors -> Lightricks/LTX-2.5-Diffusers (INT8) on nvidia sm_80.
  • A100-40 LTX-2.5 file, kill switch: identical to base (PackedBlockLinear 1176, 23.26 GiB).

The commits after that only add the training-active guard on the device read and trim comments.

Not proven on hardware:

  • Blackwell (no Blackwell card was available for this run). That path is covered by CPU tests, which show every sm >= 10 request comes back as the same object with no reroute and no annotation.
  • T4 and the text-encoder NVFP4 pick were not run on a GPU.
  • The HTTP routes were exercised on CPU only. The GPU arms used begin_load.

@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T11:14:57.188637Z 3fa45fd Manual request
ℹ️ 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[bot]

This comment was marked as resolved.

…ad the device before any header probe

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…ly by the same model, keep unknown picks

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +48832 to +48834
request, nvfp4_route = await _route_nvfp4_request(
request, "image", user_initiated = user_initiated
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3fa45fdb74

ℹ️ 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 deleted the studio-nvfp4-pre-blackwell-int8 branch November 9, 2026 13:56
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 10, 2026
…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.
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