Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: load NVFP4 diffusion checkpoints from .safetensors only (#16739)

Daniel Hanfbf5da017ce7
diff --git a/scripts/build_prequant_checkpoint.py b/scripts/build_prequant_checkpoint.py
index f022ab4455c..85ba740b0a7 100644
--- a/scripts/build_prequant_checkpoint.py
+++ b/scripts/build_prequant_checkpoint.py
@@ -74,7 +74,14 @@ def upload_destination(
dispatches on the extension alone, so a safetensors build published as ``.pt`` is read as a
pickle and a pickle published as ``.safetensors`` is read from a header it does not have. Either
way the upload succeeds and the artifact is unopenable, after the hours the quantization took.
+
+ nvfp4 publishes safetensors only: Studio never resolves or opens an nvfp4 pickle.
"""
+ if (scheme or "").strip().lower() == "nvfp4" and not safetensors:
+ raise ValueError(
+ "nvfp4 checkpoints are published as .safetensors only (Studio never reads an nvfp4 "
+ "pickle); write --out as a .safetensors file"
+ )
if override:
wanted = ".safetensors" if safetensors else ".pt"
if not override.lower().endswith(wanted):
@@ -216,6 +223,15 @@ def main(argv = None) -> int:
"otherwise",
)
args = p.parse_args(argv)
+ if (args.scheme or "").strip().lower() == "nvfp4" and not str(args.out).lower().endswith(
+ ".safetensors"
+ ):
+ print(
+ "error: --scheme nvfp4 writes .safetensors only (Studio never reads an NVFP4 pickle); "
+ "give --out a .safetensors path",
+ flush = True,
+ )
+ return 2
sys.path.insert(0, str(BACKEND))
import torch
diff --git a/scripts/convert_prequant_to_safetensors.py b/scripts/convert_prequant_to_safetensors.py
index ffdaa290a7b..332998bda78 100644
--- a/scripts/convert_prequant_to_safetensors.py
+++ b/scripts/convert_prequant_to_safetensors.py
@@ -279,7 +279,11 @@ def convert(
facts: list = []
restore = _record_v1_facts(facts)
try:
- ckpt = dp._load_prequant_checkpoint(src, map_location = "cpu", mmap = True)
+ if str(src).lower().endswith(".safetensors"):
+ ckpt = dp._load_prequant_checkpoint(src, map_location = "cpu", mmap = True)
+ else:
+ # Bypasses Studio's nvfp4 pickle refusal: the only way a local nvfp4 .pt becomes .safetensors.
+ ckpt = dp._torch_load_prequant(src, map_location = "cpu", mmap = True)
finally:
restore()
fmt = ckpt.get("format")
diff --git a/studio/backend/core/inference/diffusion_families.py b/studio/backend/core/inference/diffusion_families.py
index 2103d79f9ba..c8cef9e0d93 100644
--- a/studio/backend/core/inference/diffusion_families.py
+++ b/studio/backend/core/inference/diffusion_families.py
@@ -988,9 +988,7 @@ _FAMILIES: tuple[DiffusionFamily, ...] = (
# 2.1's artifacts are baked from 2.1's denoiser: for nvfp4 Turbo quantizes its own weights.
prequant_excluded_bases = ("qwen/qwen-image-2.1-turbo",),
named_variant_bases = ("Qwen/Qwen-Image-2.1-Turbo",),
- # The artifacts are safetensors, not the historical torch.save pickle, so the family has to
- # NAME them: every derived fallback ends in .pt, and without these rows the loader would ask
- # the Hub for a file that is not there and silently fall back to the dense bf16 download.
+ # Named explicitly: a derived safetensors name is a guess planning won't bet the dense shards on.
prequant_filenames = (
("fp8", "Qwen-Image-2.1-FP8.safetensors"),
("int8", "Qwen-Image-2.1-INT8.safetensors"),
diff --git a/studio/backend/core/inference/diffusion_prequant.py b/studio/backend/core/inference/diffusion_prequant.py
index 73631af1eba..fddd6aaf859 100644
--- a/studio/backend/core/inference/diffusion_prequant.py
+++ b/studio/backend/core/inference/diffusion_prequant.py
@@ -81,12 +81,10 @@ _PREQUANT_SAFE_GLOBALS: tuple[tuple[str, str], ...] = (
("torchao.quantization.granularity", "PerRow"),
("torchao.quantization.granularity", "PerTensor"),
("torchao.float8.inference", "Float8MMConfig"),
- # mxfp8 / nvfp4: local bakes only; torchao registers them only on prototype import.
+ # mxfp8: local bakes only; torchao registers it only on prototype import. nvfp4 is never unpickled.
("torchao.prototype.mx_formats.mx_tensor", "MXTensor"),
("torchao.prototype.mx_formats.mx_tensor", "QuantizeTensorToMXKwargs"),
("torchao.prototype.mx_formats.config", "ScaleCalculationMode"),
- ("torchao.prototype.mx_formats.nvfp4_tensor", "NVFP4Tensor"),
- ("torchao.prototype.mx_formats.nvfp4_tensor", "QuantizeTensorToNVFP4Kwargs"),
# torch.save stamps TorchVersion; without it every torchao checkpoint is refused.
("torch.torch_version", "TorchVersion"),
)
@@ -146,15 +144,28 @@ _SCHEME_REQUIRED_GLOBALS: dict = {
"torchao.quantization.quantize_.common.kernel_preference.KernelPreference",
}
),
- "nvfp4": frozenset(
- {
- "torchao.prototype.mx_formats.nvfp4_tensor.NVFP4Tensor",
- "torchao.prototype.mx_formats.nvfp4_tensor.QuantizeTensorToNVFP4Kwargs",
- }
- )
- | _FP8_REQUIRED_GLOBALS,
}
+# nvfp4 checkpoints are read from .safetensors only; a .pt / .pth one is never resolved, cached or unpickled.
+NVFP4_SAFETENSORS_ONLY_MESSAGE = (
+ "NVFP4 diffusion checkpoints are loaded from .safetensors only; {what} is a pickle "
+ "(.pt / .pth) and is refused. Use the .safetensors NVFP4 checkpoint instead."
+)
+
+
+def _is_nvfp4_scheme(scheme: Any) -> bool:
+ return str(scheme or "").strip().lower() == "nvfp4"
+
+
+def nvfp4_pickle_name(scheme: Any, name: Optional[str]) -> bool:
+ """True when ``name`` is a pickle container (.pt / .pth) for the nvfp4 scheme, which is never used."""
+ return _is_nvfp4_scheme(scheme) and str(name or "").strip().lower().endswith(_PICKLE_SUFFIXES)
+
+
+def without_nvfp4_pickles(scheme: Any, names: Sequence[str]) -> list:
+ """``names`` minus the pickle containers when ``scheme`` is nvfp4; every other scheme is unchanged."""
+ return [n for n in names if n and not nvfp4_pickle_name(scheme, n)]
+
def _tuple_safe_globals_supported() -> bool:
"""Checked by version: below torch 2.6, tuple pairs are accepted then break every weights_only load."""
@@ -213,6 +224,9 @@ def restricted_prequant_load_supported(
if is_safetensors_checkpoint(filename):
return safetensors_scheme_supported(scheme)
+ if _is_nvfp4_scheme(scheme):
+ # Safetensors only: a pickle is never readable; the scheme-only question is the safetensors one.
+ return filename is None and safetensors_scheme_supported(scheme)
if not _register_prequant_safe_globals():
return False
key = (scheme or "").strip().lower()
@@ -253,14 +267,24 @@ def _torch_load_prequant(path: str, **kwargs: Any) -> Any:
return rebuild_stray_standins(ckpt)
-def _load_prequant_checkpoint(path: str, **kwargs: Any) -> Any:
- """Dispatches on file extension, not content, so a repo hosting both containers is never misparsed."""
+def _load_prequant_checkpoint(
+ path: str,
+ *,
+ scheme: Optional[str] = None,
+ **kwargs: Any,
+) -> Any:
+ """Dispatches on extension, not content; nvfp4 (asked for or recorded) is refused unless safetensors."""
from .prequant_safetensors import is_safetensors_checkpoint, load_prequant_safetensors
if is_safetensors_checkpoint(path):
ckpt = load_prequant_safetensors(path, mmap = bool(kwargs.get("mmap")))
else:
+ if _is_nvfp4_scheme(scheme):
+ raise ValueError(NVFP4_SAFETENSORS_ONLY_MESSAGE.format(what = repr(str(path))))
ckpt = _torch_load_prequant(path, **kwargs)
+ meta = ckpt.get("metadata") if isinstance(ckpt, dict) else None
+ if isinstance(meta, dict) and _is_nvfp4_scheme(meta.get("scheme")):
+ raise ValueError(NVFP4_SAFETENSORS_ONLY_MESSAGE.format(what = repr(str(path))))
_install_fp8_unit_scale_for(ckpt)
return ckpt
@@ -323,11 +347,12 @@ def _read_prequant_for(
logger: Any = None,
*,
host_copy_follows: bool = False,
+ scheme: Optional[str] = None,
) -> Any:
"""``_load_prequant_checkpoint`` mapped when enabled; anything the mapping cannot open is re-read in full."""
if prequant_mmap_enabled(destination, host_copy_follows = host_copy_follows):
try:
- return _load_prequant_checkpoint(path, map_location = "cpu", mmap = True)
+ return _load_prequant_checkpoint(path, scheme = scheme, map_location = "cpu", mmap = True)
except Exception as exc: # noqa: BLE001 - retried unmapped; the real error resurfaces there
if logger is not None:
logger.info(
@@ -335,7 +360,7 @@ def _read_prequant_for(
type(exc).__name__,
str(exc).splitlines()[0][:200] if str(exc) else "",
)
- return _load_prequant_checkpoint(path, map_location = "cpu")
+ return _load_prequant_checkpoint(path, scheme = scheme, map_location = "cpu")
_PREQUANT_TOGGLE_TOKENS = {"1", "true", "yes", "on", "0", "false", "no", "off"}
@@ -714,12 +739,24 @@ def prefer_cached_pickle_twins(
SAFETENSORS_TWINS_ENV = "UNSLOTH_PREQUANT_SAFETENSORS_TWINS"
-def with_declared_safetensors_twins(names: Sequence[str], declared: Sequence[str]) -> list:
- """Put ``<stem>.safetensors`` right before each declared root ``<stem>.pt`` / ``.pth``; the .pt stays as 404 fallback."""
+def with_declared_safetensors_twins(
+ names: Sequence[str],
+ declared: Sequence[str],
+ *,
+ scheme: Optional[str] = None,
+) -> list:
+ """Put ``<stem>.safetensors`` before each declared root .pt / .pth (kept as 404 fallback); nvfp4 ignores the env."""
import os
out = [n for n in names if n]
- if (os.environ.get(SAFETENSORS_TWINS_ENV) or "").strip().lower() in ("0", "false", "no", "off"):
+ if not _is_nvfp4_scheme(scheme) and (
+ os.environ.get(SAFETENSORS_TWINS_ENV) or ""
+ ).strip().lower() in (
+ "0",
+ "false",
+ "no",
+ "off",
+ ):
return out
wanted = {n for n in declared if n}
ordered: list = []
@@ -888,7 +925,9 @@ def derived_prequant_filenames(
*,
component: Optional[str] = None,
) -> tuple[str, ...]:
- """Safetensors name first: needs no pickle allowlist and validates from its header; .pt stays behind."""
+ """Safetensors name first, .pt behind; nvfp4 derives the safetensors name only."""
+ if _is_nvfp4_scheme(scheme):
+ return (prequant_repo_filename(repo_id, scheme, ".safetensors", component = component),)
names = (
prequant_repo_filename(repo_id, scheme, ".safetensors", component = component),
prequant_repo_filename(repo_id, scheme, ".pt", component = component),
@@ -927,6 +966,9 @@ def resolve_prequant_source(
return None
override = (path_override or "").strip()
if override:
+ if nvfp4_pickle_name(scheme, override):
+ # A local nvfp4 override is read from safetensors only; a pickle is never a source.
+ return None
return PrequantSource(kind = "path", location = override, filename = None)
preferred = None
agnostic = None
@@ -946,13 +988,17 @@ def resolve_prequant_source(
task_specific = preferred is not None and preferred != agnostic
if task_specific:
# Only the same artifact's safetensors twin goes ahead of it: same partition, same weights.
- names = with_declared_safetensors_twins((preferred,), (preferred,))
+ names = without_nvfp4_pickles(
+ scheme, with_declared_safetensors_twins((preferred,), (preferred,), scheme = scheme)
+ )
+ if not names:
+ return None
return PrequantSource(
kind = "repo",
location = repo_id,
filename = names[0],
fallback_filenames = tuple(names[1:]),
- declared_filenames = (preferred,),
+ declared_filenames = (names[0],) if _is_nvfp4_scheme(scheme) else (preferred,),
)
# Declared name first, then derived chain (safetensors ahead); dedup in order. Declared names are default-repo files; variant repos derive their own.
variant_repo = _is_variant_prequant_repo(fam, repo_id)
@@ -960,6 +1006,9 @@ def resolve_prequant_source(
declared = (prequant_repo_filename(repo_id, scheme, ".safetensors"),)
else:
declared = (preferred,) if preferred else ()
+ if _is_nvfp4_scheme(scheme):
+ # Every hosted nvfp4 repo stores exactly ``<Model>-NVFP4.safetensors``: a real name, not a guess.
+ declared = tuple(without_nvfp4_pickles(scheme, declared)) or derived[:1]
# rotated artifact (hosted) first, only in its own repo; the plain chain stays behind it (offline, older caches)
from .diffusion_transformer_quant import (
convrot_prequant_filename,
@@ -989,9 +1038,12 @@ def resolve_prequant_source(
for name in declared + derived:
if name and name not in names:
names.append(name)
- names = with_declared_safetensors_twins(names, declared)
+ names = with_declared_safetensors_twins(names, declared, scheme = scheme)
if comfy_prequant_enabled() and _comfy_prequant_family(fam):
names = with_comfy_twins(names, lead = scheme in COMFY_TWIN_LEADS)
+ names = without_nvfp4_pickles(scheme, names)
+ if not names:
+ return None
return PrequantSource(
kind = "repo",
location = repo_id,
@@ -1010,7 +1062,7 @@ def resolve_prequant_source(
)
except Exception: # noqa: BLE001 - a cache problem is a miss, never a failed load
cached = None
- if cached:
+ if cached and not nvfp4_pickle_name(scheme, cached):
return PrequantSource(kind = "path", location = cached, filename = None)
return None
@@ -1046,7 +1098,7 @@ _LOCAL_PREQUANT_SCHEME: dict[tuple[str, int, int], Optional[str]] = {}
def local_prequant_scheme(path: str) -> Optional[str]:
- """Reads the recorded scheme under the allowlisted weights_only load, so auto never accepts an override baked for another scheme."""
+ """Recorded scheme via the allowlisted load; a pickle recording nvfp4 reads as unknown."""
import os
try:
@@ -1069,6 +1121,8 @@ def local_prequant_scheme(path: str) -> Optional[str]:
if isinstance(obj, dict) and obj.get("format") in PREQUANT_FORMATS:
recorded = (obj.get("metadata") or {}).get("scheme")
scheme = str(recorded) if recorded else None
+ if nvfp4_pickle_name(scheme, real):
+ scheme = None
except Exception: # noqa: BLE001 -- a checkpoint we cannot parse is "unknown", never a match
scheme = None
if scheme is None and is_safetensors_checkpoint(real):
@@ -1112,6 +1166,8 @@ def hosted_nvfp4_repo_ids() -> frozenset:
# Root only: a diffusers pipeline keeps its weights in component folders.
_PREQUANT_PROBE_SUFFIXES = (".safetensors", ".pt", ".pth")
+# nvfp4 is recorded only in safetensors headers; a pickle is never opened to ask.
+_NVFP4_PROBE_SUFFIXES = (".safetensors",)
_PREQUANT_PROBE_LIMIT = 16
@@ -1160,7 +1216,7 @@ def _artifacts_declare_nvfp4(folder: str) -> bool:
return False
probed = 0
for name in names:
- if not name.lower().endswith(_PREQUANT_PROBE_SUFFIXES):
+ if not name.lower().endswith(_NVFP4_PROBE_SUFFIXES):
continue
path = os.path.join(folder, name)
if not os.path.isfile(path):
@@ -1185,7 +1241,12 @@ def declares_nvfp4_checkpoint(model_path: Optional[str]) -> bool:
if os.path.isfile(local):
if not local.lower().endswith(_PREQUANT_PROBE_SUFFIXES):
return False
+ if local.lower().endswith(_PICKLE_SUFFIXES):
+ # Never unpickled to ask: the name decides (an nvfp4 pickle is refused at load anyway).
+ stem = os.path.splitext(os.path.basename(local))[0].lower()
+ return stem.endswith(("-nvfp4", "_nvfp4"))
from .diffusion_nvfp4_flag import is_nvfp4
+
return is_nvfp4(local_prequant_scheme(local))
if os.path.isdir(local):
if _artifacts_declare_nvfp4(local):
@@ -2306,7 +2367,11 @@ def load_prequantized_transformer(
# Pickles load only under the ALLOWLIST, never free-running: a mutated file must fail,
# not run.
ckpt = _read_prequant_for(
- path, placement_device or device, logger, host_copy_follows = host_copy_follows
+ path,
+ placement_device or device,
+ logger,
+ host_copy_follows = host_copy_follows,
+ scheme = scheme,
)
if not _validate_checkpoint(
ckpt,
@@ -2555,15 +2620,25 @@ def _resolve_checkpoint_path(
if source.kind == "path":
import os
+ if nvfp4_pickle_name(scheme, source.location):
+ raise ValueError(
+ NVFP4_SAFETENSORS_ONLY_MESSAGE.format(what = "the local pre-quant override")
+ )
# Expand ~ (the allowlist gate already did), else os.path.isfile sees a literal "~".
expanded = os.path.expanduser(source.location)
return expanded if os.path.isfile(expanded) else None
if source.kind == "repo":
EntryNotFoundError, LocalEntryNotFoundError = _entry_not_found_errors()
- names = list(candidate_filenames_of(source))
+ # nvfp4 never falls back to a pickle name, whatever the source carries.
+ names = without_nvfp4_pickles(scheme, candidate_filenames_of(source))
all_names = list(names)
if scheme is not None:
readable = [n for n in names if restricted_prequant_load_supported(scheme, n)]
+ if not readable and _is_nvfp4_scheme(scheme) and names:
+ raise ValueError(
+ f"the hosted nvfp4 checkpoint ({source.location}) cannot be read here: "
+ f"{_unreadable_why(scheme)}"
+ )
# Empty filter means it should never have been offered; returning None would hide the refusal.
names = readable or names
if not names:
@@ -3414,6 +3489,12 @@ def _unreadable_why(scheme: str) -> str:
ao = getattr(torchao, "__version__", "unknown")
except Exception: # noqa: BLE001
return "torch or torchao is unavailable"
+ if _is_nvfp4_scheme(scheme):
+ return (
+ f"torchao {ao} cannot rebuild an NVFP4 safetensors checkpoint (needs torchao >= 0.17 and the "
+ "safetensors package); "
+ "NVFP4 is never read from a .pt pickle"
+ )
if not _tuple_safe_globals_supported():
return f"torch {torch.__version__} is older than 2.6"
try:
diff --git a/studio/backend/core/inference/nvfp4_gate_record.json b/studio/backend/core/inference/nvfp4_gate_record.json
index 5d99570394a..4f8455e6abb 100644
--- a/studio/backend/core/inference/nvfp4_gate_record.json
+++ b/studio/backend/core/inference/nvfp4_gate_record.json
@@ -7,9 +7,9 @@
"base_repo": "Tongyi-MAI/Z-Image-Turbo",
"policy_id": "zimg_rg76_v1",
"policy_version": 1,
- "checkpoint_sha256": "2453bc13d7abe5b4a49721f4a9d38a9aa16dee20085a9267f8a35867c7d9cc33",
+ "checkpoint_sha256": "0762fa96e1048c8c7732dae0a168f5df952808f76ffa5621a6dc2d27007e453c",
"repo_id": "unsloth/Z-Image-Turbo-NVFP4",
- "filename": "Z-Image-Turbo-NVFP4.pt",
+ "filename": "Z-Image-Turbo-NVFP4.safetensors",
"gptq": true,
"all_pass": true,
"num_pairs": 24,
@@ -87,9 +87,9 @@
"base_repo": "Qwen/Qwen-Image-2.1",
"policy_id": "qwen21_r020_v1",
"policy_version": 1,
- "checkpoint_sha256": "37af1338c21a0c20dd0518f1908f6bc5828740c1cd12c50718552a31d753c24d",
+ "checkpoint_sha256": "6656d1a0c442692f51e1b0ff81e23a0c6f3f693911e13a44035ab100b80f54c0",
"repo_id": "unsloth/Qwen-Image-2.1-NVFP4",
- "filename": "Qwen-Image-2.1-NVFP4.pt",
+ "filename": "Qwen-Image-2.1-NVFP4.safetensors",
"gptq": true,
"all_pass": true,
"num_pairs": 28,
@@ -167,9 +167,9 @@
"base_repo": "black-forest-labs/FLUX.1-schnell",
"policy_id": "flux_r420_v1",
"policy_version": 1,
- "checkpoint_sha256": "d4e4c5e353374e022685f2e48dc7b38d054e36f30809dd8eb77a22a0a5d67006",
+ "checkpoint_sha256": "b8d92016e8a6b6274c6fb5244edf47fc9b646adc0535aa19f477a5396e72e7bc",
"repo_id": "unsloth/FLUX.1-schnell-NVFP4",
- "filename": "FLUX.1-schnell-NVFP4.pt",
+ "filename": "FLUX.1-schnell-NVFP4.safetensors",
"gptq": true,
"all_pass": true,
"num_pairs": 24,
@@ -247,9 +247,9 @@
"base_repo": "Qwen/Qwen-Image-2512",
"policy_id": "qwen2512_m120_attn8_v1",
"policy_version": 1,
- "checkpoint_sha256": "665e8f9a12d31366cfd4ac10a93c751a32219ea2386107b55a43b0cde5955607",
+ "checkpoint_sha256": "0d3720a478323ec5592a56d7e27af75bac7d3f115beeffceb3488d1ca9755ec3",
"repo_id": "unsloth/Qwen-Image-2512-NVFP4",
- "filename": "Qwen-Image-2512-NVFP4.pt",
+ "filename": "Qwen-Image-2512-NVFP4.safetensors",
"gptq": true,
"all_pass": true,
"num_pairs": 28,
diff --git a/studio/backend/core/inference/video_families.py b/studio/backend/core/inference/video_families.py
index 623655a93e3..38992b969e6 100644
--- a/studio/backend/core/inference/video_families.py
+++ b/studio/backend/core/inference/video_families.py
@@ -757,13 +757,13 @@ _FAMILIES: tuple[VideoFamily, ...] = (
pipeline_class = "WanPipeline",
transformer_class = "WanTransformer3DModel",
base_repo = "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
- # fp8 / int8 share one repo, files Wan2.2-TI2V-5B-<SCHEME>.pt. Sizes from Hub metadata (2026-08-04).
+ # fp8 / int8: Wan2.2-TI2V-5B-<SCHEME>.pt; nvfp4: .safetensors only. Sizes from Hub (2026-08-04).
prequant_repos = (
("nvfp4", "unsloth/Wan2.2-TI2V-5B-NVFP4"),
("fp8", "unsloth/Wan2.2-TI2V-5B-FP8"),
("int8", "unsloth/Wan2.2-TI2V-5B-FP8"),
),
- prequant_filenames = (("nvfp4", "Wan2.2-TI2V-5B-NVFP4.pt"),),
+ prequant_filenames = (("nvfp4", "Wan2.2-TI2V-5B-NVFP4.safetensors"),),
prequant_resident_gb_by_scheme = (("nvfp4", 2.9), ("fp8", 5.1), ("int8", 5.0)),
# "wan2.2-5b"/"wan-ti2v" are the picker/GGUF short ids; "wan2.2-ti2v" catches the repo stem
aliases = ("wan2.2-5b", "wan-ti2v", "wan2.2-ti2v", "wan-ti2v-5b"),
@@ -822,8 +822,8 @@ _FAMILIES: tuple[VideoFamily, ...] = (
("int8", "unsloth/Wan2.2-T2V-A14B-FP8"),
),
prequant_filenames = (
- ("nvfp4", "Wan2.2-T2V-A14B-NVFP4.pt"),
- ("nvfp4", "transformer_2", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"),
+ ("nvfp4", "Wan2.2-T2V-A14B-NVFP4.safetensors"),
+ ("nvfp4", "transformer_2", "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors"),
("fp8", "transformer_2", "Wan2.2-T2V-A14B-FP8-2.pt"),
("int8", "transformer_2", "Wan2.2-T2V-A14B-INT8-2.pt"),
),
@@ -938,7 +938,7 @@ _FAMILIES: tuple[VideoFamily, ...] = (
base_repo = "hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-480p_t2v",
unsupported_checkpoints = ((_HV15_I2V_RE, _HV15_I2V_REFUSAL),),
prequant_repos = (("nvfp4", "unsloth/HunyuanVideo-1.5-NVFP4"),),
- prequant_filenames = (("nvfp4", "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.pt"),),
+ prequant_filenames = (("nvfp4", "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.safetensors"),),
prequant_resident_gb_by_scheme = (("nvfp4", 4.8),),
# No bare "hunyuanvideo" alias: it would also claim the incompatible 1.0 repos.
aliases = ("hunyuanvideo-1-5", "hunyuanvideo1.5", "hunyuanvideo1-5", "hv15"),
@@ -983,7 +983,7 @@ _FAMILIES: tuple[VideoFamily, ...] = (
unsupported_checkpoints = ((_HV15_I2V_RE, _HV15_I2V_REFUSAL),),
# RTN, not GPTQ: the 720p transformer is separately trained; 480p corrections do not apply.
prequant_repos = (("nvfp4", "unsloth/HunyuanVideo-1.5-NVFP4"),),
- prequant_filenames = (("nvfp4", "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.pt"),),
+ prequant_filenames = (("nvfp4", "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.safetensors"),),
prequant_resident_gb_by_scheme = (("nvfp4", 4.8),),
aliases = ("hunyuanvideo-1.5-diffusers-720p_t2v", "hv15-720p"),
has_audio = False,
diff --git a/studio/backend/hub/tests/test_companion_assets.py b/studio/backend/hub/tests/test_companion_assets.py
index 3bc8309fc76..5625b5598f7 100644
--- a/studio/backend/hub/tests/test_companion_assets.py
+++ b/studio/backend/hub/tests/test_companion_assets.py
@@ -1156,7 +1156,7 @@ HOSTED_CASES = {
),
"video-nvfp4": (
"unsloth/HunyuanVideo-1.5-NVFP4",
- [("HunyuanVideo-1.5-NVFP4.pt", 4_800_000)],
+ [("HunyuanVideo-1.5-NVFP4.safetensors", 4_800_000)],
"hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-480p_t2v",
[("model_index.json", 400), *_VAE],
("hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-720p_t2v", [*_VAE]),
diff --git a/studio/backend/tests/test_build_prequant_checkpoint.py b/studio/backend/tests/test_build_prequant_checkpoint.py
index 324e2da04aa..fef6cc05be3 100644
--- a/studio/backend/tests/test_build_prequant_checkpoint.py
+++ b/studio/backend/tests/test_build_prequant_checkpoint.py
@@ -247,11 +247,24 @@ def _build_nvfp4(
monkeypatch.setattr(tq, "quantize_", _fake_quantize)
monkeypatch.setattr(dtq, "_make_quant_config", lambda *a, **k: object())
+ # nvfp4 writes safetensors only; the stand-in writer records what the build would store.
+ import core.inference.prequant_safetensors as pst
+
+ written = {}
+ monkeypatch.setattr(pst, "safetensors_prequant_supported", lambda: True)
+ monkeypatch.setattr(pst, "scheme_is_flattenable", lambda *a, **k: None)
+
+ def _save(path, **kw):
+ written.update(kw, path = path)
+ open(path, "wb").close()
+
+ monkeypatch.setattr(pst, "save_prequant_safetensors", _save)
build = _script()
- out = tmp_path / "out.pt"
+ out = tmp_path / "out.safetensors"
argv = ["--base", "b", "--family", fam.name, "--scheme", "nvfp4", "--out", str(out)]
assert build.main(argv + list(extra_argv)) == 0
- return quantized, torch.load(out, weights_only = False)["metadata"]
+ assert written["path"] == str(out)
+ return quantized, written["metadata"]
def test_the_build_skips_ragged_linears_and_records_the_alignment_floor(monkeypatch, tmp_path):
@@ -296,16 +309,25 @@ def test_the_second_expert_builds_from_its_own_subfolder_and_loads_as_transforme
build = _script()
assert (
- build.upload_destination(wan, "nvfp4", rotated = False, component = "transformer_2")
- == "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"
+ build.upload_destination(
+ wan, "nvfp4", rotated = False, component = "transformer_2", safetensors = True
+ )
+ == "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors"
)
- assert build.upload_destination(wan, "nvfp4", rotated = False) == "transformer_nvfp4.pt"
+ assert (
+ build.upload_destination(wan, "nvfp4", rotated = False, safetensors = True)
+ == "Wan2.2-T2V-A14B-NVFP4.safetensors"
+ )
+ for kwargs in ({}, {"component": "transformer_2"}, {"override": "x-NVFP4.pt"}):
+ with pytest.raises(ValueError, match = "safetensors only"):
+ build.upload_destination(wan, "nvfp4", rotated = False, **kwargs)
with pytest.raises(ValueError, match = "transformer_2"):
build.upload_destination(
detect_family("Tongyi-MAI/Z-Image-Turbo"),
"nvfp4",
rotated = False,
component = "transformer_2",
+ safetensors = True,
)
@@ -313,3 +335,30 @@ def test_a_default_build_still_reads_the_transformer_subfolder(monkeypatch, tmp_
loaded = []
_, meta = _build_nvfp4(monkeypatch, tmp_path, loaded = loaded)
assert loaded == ["transformer"] and meta["component"] == "transformer"
+
+
[email protected]("out_name", ["out.pt", "out.pth", "out"])
+def test_an_nvfp4_build_refuses_a_pickle_output(tmp_path, capsys, out_name):
+ """Refused before any model or torch import: nvfp4 is published as .safetensors only."""
+ build = _script()
+ argv = [
+ "--base",
+ "b",
+ "--family",
+ "z-image",
+ "--scheme",
+ "nvfp4",
+ "--out",
+ str(tmp_path / out_name),
+ ]
+ assert build.main(argv) == 2
+ assert "safetensors only" in capsys.readouterr().out
+ assert not (tmp_path / out_name).exists()
+
+
+def test_int8_and_fp8_keep_their_pickle_upload_names():
+ """Only nvfp4 changes: an int8 / fp8 .pt build keeps the legacy name it always used."""
+ build = _script()
+ zimage = detect_family("Tongyi-MAI/Z-Image-Turbo")
+ assert build.upload_destination(zimage, "int8", rotated = False) == "transformer_int8.pt"
+ assert build.upload_destination(zimage, "fp8", rotated = False) == "transformer_fp8.pt"
diff --git a/studio/backend/tests/test_diffusion_nvfp4_gate.py b/studio/backend/tests/test_diffusion_nvfp4_gate.py
index a52e82aac29..a6aa44f13c6 100644
--- a/studio/backend/tests/test_diffusion_nvfp4_gate.py
+++ b/studio/backend/tests/test_diffusion_nvfp4_gate.py
@@ -35,7 +35,7 @@ def _record(**overrides):
"policy_version": 1,
"checkpoint_sha256": "a" * 64,
"repo_id": "unsloth/Z-Image-Turbo-NVFP4",
- "filename": "Z-Image-Turbo-NVFP4.pt",
+ "filename": "Z-Image-Turbo-NVFP4.safetensors",
"gptq": False,
"all_pass": True,
"num_pairs": 28,
diff --git a/studio/backend/tests/test_diffusion_prequant.py b/studio/backend/tests/test_diffusion_prequant.py
index b522ded1e29..bbb82478194 100644
--- a/studio/backend/tests/test_diffusion_prequant.py
+++ b/studio/backend/tests/test_diffusion_prequant.py
@@ -641,7 +641,16 @@ def _load(
monkeypatch.setenv(pq.ALLOW_LOCAL_PREQUANT_PATH_ENV, str(tmp_path))
else:
monkeypatch.delenv(pq.ALLOW_LOCAL_PREQUANT_PATH_ENV, raising = False)
- path = tmp_path / "ckpt.pt"
+ path = tmp_path / ("ckpt.safetensors" if scheme == "nvfp4" else "ckpt.pt")
+ if scheme == "nvfp4":
+ # nvfp4 is read from safetensors only; the stand-in container returns the fake checkpoint.
+ import core.inference.prequant_safetensors as pst
+ def _st_load(_path, **_kwargs):
+ if load_raises:
+ raise RuntimeError("load failed")
+ return ckpt
+
+ monkeypatch.setattr(pst, "load_prequant_safetensors", _st_load)
if exists:
path.write_bytes(b"x")
source = PrequantSource(kind = "path", location = str(path), filename = None)
@@ -935,7 +944,6 @@ def test_the_allowlist_names_every_constructor_the_hosted_checkpoints_use(
required = {
# every TQ_SCHEME the builder can bake, not just the hosted ones
"torchao.prototype.mx_formats.mx_tensor.MXTensor",
- "torchao.prototype.mx_formats.nvfp4_tensor.NVFP4Tensor",
"torchao.dtypes.affine_quantized_tensor.AffineQuantizedTensor",
"torchao.dtypes.uintx.plain_layout.PlainAQTTensorImpl",
"torchao.dtypes.utils.PlainLayout",
@@ -951,6 +959,10 @@ def test_the_allowlist_names_every_constructor_the_hosted_checkpoints_use(
"torch.torch_version.TorchVersion",
}
assert required <= listed, sorted(required - listed)
+ # nvfp4 is never unpickled by Studio: its checkpoints load from safetensors only
+ assert not any(
+ "nvfp4" in f"{module}.{name}".lower() for module, name in pq._PREQUANT_SAFE_GLOBALS
+ )
# nothing outside torch/torchao, so no general-purpose callable is allowed
assert all(
module.split(".")[0] in ("torch", "torchao") for module, _ in pq._PREQUANT_SAFE_GLOBALS
@@ -1173,7 +1185,8 @@ def test_support_is_answered_per_scheme(monkeypatch):
assert pq.restricted_prequant_load_supported("fp8") is True
assert pq.restricted_prequant_load_supported("int8") is False
- assert pq.restricted_prequant_load_supported("nvfp4") is False
+ # nvfp4 never reads a pickle, whatever the allowlist resolves
+ assert pq.restricted_prequant_load_supported("nvfp4", "x-NVFP4.pt") is False
assert pq.restricted_prequant_load_supported() is True
assert pq.restricted_prequant_load_supported("something-else") is True
fam = _fam(prequant_repos = (("int8", "org/hosted-int8"), ("fp8", "org/hosted-fp8")))
@@ -2259,10 +2272,10 @@ def test_pinning_the_fp8_kernel_leaves_the_4_bit_weights_alone(monkeypatch):
assert not hasattr(sd["layers.0.attention.to_q.weight"], "kernel_preference")
-def test_an_nvfp4_install_must_be_able_to_open_the_fp8_weights_too():
- required = pq._SCHEME_REQUIRED_GLOBALS["nvfp4"]
- assert pq._SCHEME_REQUIRED_GLOBALS["fp8"] <= required
- assert "torchao.prototype.mx_formats.nvfp4_tensor.NVFP4Tensor" in required
+def test_nvfp4_has_no_pickle_requirements_because_it_is_never_unpickled():
+ assert "nvfp4" not in pq._SCHEME_REQUIRED_GLOBALS
+ # fp8 / int8 keep their pickle requirements unchanged
+ assert {"int8", "fp8", "mxfp8"} <= set(pq._SCHEME_REQUIRED_GLOBALS)
def test_the_checkpoint_is_released_before_the_device_copy(monkeypatch, tmp_path):
@@ -2512,7 +2525,7 @@ def _nvfp4_repo_source():
return PrequantSource(
kind = "repo",
location = "unsloth/Z-Image-Turbo-NVFP4",
- filename = "Z-Image-Turbo-NVFP4.pt",
+ filename = "Z-Image-Turbo-NVFP4.safetensors",
)
@@ -2643,7 +2656,7 @@ def test_a_readable_prequant_is_not_marked_unreachable(monkeypatch):
def _listing(repo_id):
return types.SimpleNamespace(
- siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.pt", size = 4096)]
+ siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.safetensors", size = 4096)]
)
backend, _tokens = _prefetch_backend(monkeypatch, cached = False, model_info = _listing)
@@ -2659,7 +2672,7 @@ def test_local_only_treats_an_uncached_prequant_as_unreachable(monkeypatch):
def _listing(repo_id):
return types.SimpleNamespace(
- siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.pt", size = 4096)]
+ siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.safetensors", size = 4096)]
)
backend, tokens = _prefetch_backend(monkeypatch, cached = False, model_info = _listing)
@@ -2679,7 +2692,7 @@ def test_a_readable_prequant_still_drops_the_dense_shards(monkeypatch):
def _listing(repo_id):
return types.SimpleNamespace(
- siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.pt", size = 4096)]
+ siblings = [types.SimpleNamespace(rfilename = "Z-Image-Turbo-NVFP4.safetensors", size = 4096)]
)
backend, _tokens = _prefetch_backend(monkeypatch, cached = False, model_info = _listing)
diff --git a/studio/backend/tests/test_nvfp4_diffusion_flag.py b/studio/backend/tests/test_nvfp4_diffusion_flag.py
index 18ea63a8736..be76273006f 100644
--- a/studio/backend/tests/test_nvfp4_diffusion_flag.py
+++ b/studio/backend/tests/test_nvfp4_diffusion_flag.py
@@ -355,8 +355,8 @@ _WAN_SIBLINGS = {
_WAN_SIBLINGS["unsloth/Wan2.2-T2V-A14B-FP8"] = _WAN_SIBLINGS[_WAN_T2V]
_WAN_SIBLINGS |= {
"unsloth/Wan2.2-T2V-A14B-NVFP4": [
- _sib("Wan2.2-T2V-A14B-NVFP4.pt", 8_000_000_000),
- _sib("Wan2.2-T2V-A14B-transformer_2-NVFP4.pt", 8_100_000_000),
+ _sib("Wan2.2-T2V-A14B-NVFP4.safetensors", 8_000_000_000),
+ _sib("Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors", 8_100_000_000),
],
}
@@ -416,7 +416,7 @@ def test_the_image_hub_entry_never_asks_about_an_nvfp4_repo(monkeypatch):
"huggingface_hub.HfApi", lambda *a, **k: types.SimpleNamespace(model_info = asked.append)
)
source = types.SimpleNamespace(
- kind = "repo", location = "unsloth/Z-Image-Turbo-NVFP4", filename = "x-NVFP4.pt"
+ kind = "repo", location = "unsloth/Z-Image-Turbo-NVFP4", filename = "x-NVFP4.safetensors"
)
assert DiffusionBackend._prequant_source_hub_entry(source, None, scheme = "nvfp4") is None
assert DiffusionBackend._prequant_source_hub_entry(source, None) is None
@@ -469,7 +469,10 @@ def test_enabled_the_video_plan_stages_the_hosted_nvfp4_denoisers(monkeypatch):
_enable(monkeypatch)
plan, staged, api = _wan_plan(monkeypatch)
assert "unsloth/Wan2.2-T2V-A14B-NVFP4" in api.asked
- assert {"Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"} <= staged
+ assert {
+ "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ } <= staged
assert "transformer/diffusion_pytorch_model.safetensors" not in staged
diff --git a/studio/backend/tests/test_prequant_native.py b/studio/backend/tests/test_prequant_native.py
index 21ee61ffc1a..034a1030a5b 100644
--- a/studio/backend/tests/test_prequant_native.py
+++ b/studio/backend/tests/test_prequant_native.py
@@ -730,7 +730,10 @@ def test_the_converter_writes_a_bit_identical_nvfp4_artifact(tmp_path, act_scale
conv = _converter()
rec = conv.convert(str(src), str(tmp_path / "out"), repo = None, revision = None, sha = None)
assert rec["verified_bit_identical"] and rec["scheme"] == "nvfp4" and rec["quantized"] == 1
- a = pq._load_prequant_checkpoint(str(src), map_location = "cpu")["state_dict"]
+ # Studio itself refuses the nvfp4 pickle; only the converter's offline read opens it.
+ with pytest.raises(ValueError, match = "safetensors only"):
+ pq._load_prequant_checkpoint(str(src), map_location = "cpu")
+ a = pq._torch_load_prequant(str(src), map_location = "cpu")["state_dict"]
b = pq._load_prequant_checkpoint(rec["dst"], map_location = "cpu", mmap = True)["state_dict"]
got, want = b["blk.proj.weight"], a["blk.proj.weight"]
assert type(got) is nv.NVFP4Tensor
diff --git a/studio/backend/tests/test_prequant_nvfp4_safetensors_only.py b/studio/backend/tests/test_prequant_nvfp4_safetensors_only.py
new file mode 100644
index 00000000000..5e0384f2c39
--- /dev/null
+++ b/studio/backend/tests/test_prequant_nvfp4_safetensors_only.py
@@ -0,0 +1,266 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""NVFP4 diffusion checkpoints load from .safetensors only: no .pt / .pth is resolved, preferred from the
+cache, accepted as a local override or unpickled. int8 / fp8 keep their pickle containers and fallbacks."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+import core.inference.diffusion_prequant as pq
+from core.inference.diffusion_families import detect_family
+from core.inference.video_families import detect_video_family
+
+# The files the hosted unsloth/*-NVFP4 diffusion repos hold (Hub listing), keyed by (base, task).
+HOSTED = {
+ ("image", "Tongyi-MAI/Z-Image-Turbo", None): "Z-Image-Turbo-NVFP4.safetensors",
+ ("image", "Qwen/Qwen-Image-2.1", None): "Qwen-Image-2.1-NVFP4.safetensors",
+ ("image", "black-forest-labs/FLUX.1-schnell", None): "FLUX.1-schnell-NVFP4.safetensors",
+ ("image", "Qwen/Qwen-Image-2512", None): "Qwen-Image-2512-NVFP4.safetensors",
+ ("video", "Wan-AI/Wan2.2-TI2V-5B-Diffusers", None): "Wan2.2-TI2V-5B-NVFP4.safetensors",
+ ("video", "Wan-AI/Wan2.2-T2V-A14B-Diffusers", None): "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ (
+ "video",
+ "Wan-AI/Wan2.2-T2V-A14B-Diffusers",
+ "transformer_2",
+ ): "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ (
+ "video",
+ "hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-480p_t2v",
+ None,
+ ): "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.safetensors",
+ (
+ "video",
+ "hunyuanvideo-community/HunyuanVideo-1.5-Diffusers-720p_t2v",
+ None,
+ ): "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.safetensors",
+}
+PICKLES = (".pt", ".pth")
+
+
[email protected](autouse = True)
+def _nvfp4_on(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_NVFP4_DIFFUSION", "1")
+ for name in (pq.SAFETENSORS_TWINS_ENV, pq.PREFER_SAFETENSORS_ENV):
+ monkeypatch.delenv(name, raising = False)
+
+
+def _family(kind, base):
+ fam = detect_family(base) if kind == "image" else detect_video_family(base)
+ assert fam is not None, base
+ return fam
+
+
[email protected]("twins", [None, "0"], ids = ["twins_on", "twins_off"])
[email protected]("key", sorted(HOSTED, key = str), ids = lambda k: f"{k[1]}-{k[2]}")
+def test_every_hosted_nvfp4_chain_leads_with_the_hosted_safetensors_and_has_no_pickle(
+ key, twins, monkeypatch
+):
+ if twins is not None:
+ monkeypatch.setenv(pq.SAFETENSORS_TWINS_ENV, twins)
+ kind, base, task = key
+ src = pq.resolve_prequant_source(_family(kind, base), "nvfp4", base_repo = base, task = task)
+ assert src is not None and src.kind == "repo"
+ chain = pq.candidate_filenames_of(src)
+ assert chain[0] == HOSTED[key]
+ assert not [n for n in chain if n.lower().endswith(PICKLES)], chain
+ # declared, so planning bets on it without needing the file already cached
+ assert HOSTED[key] in src.declared_filenames
+
+
+def test_the_hosted_repo_set_is_covered():
+ repos = {
+ pq.resolve_prequant_source(_family(k, b), "nvfp4", base_repo = b, task = t).location.lower()
+ for k, b, t in HOSTED
+ }
+ assert repos == set(pq.hosted_nvfp4_repo_ids())
+
+
+def test_nvfp4_derives_only_the_safetensors_name():
+ assert pq.derived_prequant_filenames("unsloth/Model-NVFP4", "nvfp4") == (
+ "Model-NVFP4.safetensors",
+ )
+ assert pq.derived_prequant_filenames(
+ "unsloth/Model-NVFP4", "nvfp4", component = "transformer_2"
+ ) == ("Model-transformer_2-NVFP4.safetensors",)
+
+
+def test_a_family_still_declaring_an_nvfp4_pickle_resolves_only_its_twin():
+ """A stale or third-party table row naming a .pt: the twin is requested, never the pickle."""
+ fam = _family("video", "Wan-AI/Wan2.2-T2V-A14B-Diffusers")
+ stale = type(fam).__new__(type(fam))
+ stale.__dict__.update(fam.__dict__)
+ object.__setattr__(
+ stale,
+ "prequant_filenames",
+ (
+ ("nvfp4", "Wan2.2-T2V-A14B-NVFP4.pt"),
+ ("nvfp4", "transformer_2", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"),
+ ),
+ )
+ for task, want in (
+ (None, "Wan2.2-T2V-A14B-NVFP4.safetensors"),
+ ("transformer_2", "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors"),
+ ):
+ src = pq.resolve_prequant_source(stale, "nvfp4", task = task)
+ chain = pq.candidate_filenames_of(src)
+ assert chain[0] == want
+ assert not [n for n in chain if n.lower().endswith(PICKLES)], chain
+ assert src.declared_filenames == (want,)
+
+
+ "base,scheme,pickle",
+ [
+ ("Tongyi-MAI/Z-Image-Turbo", "int8", "Z-Image-Turbo-INT8.pt"),
+ ("Tongyi-MAI/Z-Image-Turbo", "fp8", "Z-Image-Turbo-FP8.pt"),
+ ],
+)
+def test_int8_and_fp8_chains_keep_their_pickle_fallbacks(base, scheme, pickle):
+ src = pq.resolve_prequant_source(_family("image", base), scheme, base_repo = base)
+ chain = pq.candidate_filenames_of(src)
+ assert pickle in chain
+ assert f"transformer_{scheme}.pt" in chain
+
+
+def test_wan_int8_second_expert_keeps_its_declared_pickle_behind_its_twin():
+ fam = _family("video", "Wan-AI/Wan2.2-T2V-A14B-Diffusers")
+ src = pq.resolve_prequant_source(fam, "int8", task = "transformer_2")
+ assert pq.candidate_filenames_of(src) == (
+ "Wan2.2-T2V-A14B-INT8-2.safetensors",
+ "Wan2.2-T2V-A14B-INT8-2.pt",
+ )
+
+
+def test_only_nvfp4_pickles_are_unreadable():
+ for name in ("x-NVFP4.pt", "x-NVFP4.pth", "transformer_nvfp4.pt"):
+ assert pq.restricted_prequant_load_supported("nvfp4", name) is False
+ assert pq.nvfp4_pickle_name("nvfp4", "x.PT") is True
+ assert pq.nvfp4_pickle_name("int8", "x.pt") is False
+ assert pq.nvfp4_pickle_name("nvfp4", "x.safetensors") is False
+ assert pq.without_nvfp4_pickles("int8", ["a.safetensors", "a.pt"]) == ["a.safetensors", "a.pt"]
+ assert pq.without_nvfp4_pickles("nvfp4", ["a.safetensors", "a.pt", "b.pth"]) == [
+ "a.safetensors"
+ ]
+
+
+def test_the_pickle_allowlist_names_no_nvfp4_class():
+ assert not any("nvfp4" in module.lower() for module, _name in pq._PREQUANT_SAFE_GLOBALS)
+ assert "nvfp4" not in pq._SCHEME_REQUIRED_GLOBALS
+ assert {"int8", "fp8"} <= set(pq._SCHEME_REQUIRED_GLOBALS)
+
+
+def _pickle(path: Path, scheme: str) -> Path:
+ torch = pytest.importorskip("torch")
+ torch.save(
+ {
+ "format": pq.PREQUANT_FORMAT,
+ "metadata": {"scheme": scheme, "component": "transformer"},
+ "state_dict": {"blk.proj.weight": torch.zeros(4, 4)},
+ },
+ str(path),
+ )
+ return path
+
+
+def test_the_loader_refuses_an_nvfp4_pickle(tmp_path):
+ path = _pickle(tmp_path / "Model-NVFP4.pt", "nvfp4")
+ # asked for nvfp4: refused on the extension, before any unpickling
+ with pytest.raises(ValueError, match = "safetensors only"):
+ pq._load_prequant_checkpoint(str(path), scheme = "nvfp4", map_location = "cpu")
+ # not told the scheme: the recorded nvfp4 metadata refuses it
+ with pytest.raises(ValueError, match = "safetensors only"):
+ pq._load_prequant_checkpoint(str(path), map_location = "cpu")
+ with pytest.raises(ValueError, match = "safetensors only"):
+ pq._read_prequant_for(str(path), "cpu", scheme = "nvfp4")
+
+
[email protected]("scheme", ["int8", "fp8"])
+def test_the_loader_still_reads_an_int8_or_fp8_pickle(tmp_path, scheme):
+ path = _pickle(tmp_path / f"Model-{scheme.upper()}.pt", scheme)
+ ckpt = pq._load_prequant_checkpoint(str(path), map_location = "cpu")
+ assert ckpt["metadata"]["scheme"] == scheme
+ assert pq.local_prequant_scheme(str(path)) == scheme
+ assert pq.restricted_prequant_load_supported(scheme, path.name) is True
+
+
[email protected]("scheme", ["int8", "fp8"])
+def test_naming_the_scheme_still_reads_an_int8_or_fp8_pickle(tmp_path, scheme):
+ path = _pickle(tmp_path / f"Model-{scheme.upper()}.pt", scheme)
+ ckpt = pq._load_prequant_checkpoint(str(path), scheme = scheme, map_location = "cpu")
+ assert ckpt["metadata"]["scheme"] == scheme
+
+
[email protected]("scheme", ["int8", "fp8"])
+def test_an_int8_or_fp8_pickle_override_is_still_a_source(tmp_path, monkeypatch, scheme):
+ fam = _family("image", "Tongyi-MAI/Z-Image-Turbo")
+ path = _pickle(tmp_path / f"Model-{scheme.upper()}.pt", scheme)
+ got = pq.resolve_prequant_source(fam, scheme, path_override = str(path))
+ assert got is not None and got.kind == "path"
+ assert pq._resolve_checkpoint_path(got, None, scheme = scheme) == str(path)
+
+
+def test_a_local_nvfp4_pickle_never_reads_as_nvfp4(tmp_path):
+ path = _pickle(tmp_path / "Model-NVFP4.pt", "nvfp4")
+ assert pq.local_prequant_scheme(str(path)) is None
+
+
+def test_a_local_nvfp4_pickle_override_is_refused(tmp_path, monkeypatch):
+ fam = _family("image", "Tongyi-MAI/Z-Image-Turbo")
+ path = _pickle(tmp_path / "Model-NVFP4.pt", "nvfp4")
+ assert pq.resolve_prequant_source(fam, "nvfp4", path_override = str(path)) is None
+ monkeypatch.setenv(pq.ALLOW_LOCAL_PREQUANT_PATH_ENV, str(tmp_path))
+ assert pq.usable_prequant_source(fam, "nvfp4", path_override = str(path)) is None
+ # a hand-built source still never reaches the unpickler
+ src = pq.PrequantSource(kind = "path", location = str(path))
+ with pytest.raises(ValueError, match = "safetensors only"):
+ pq._resolve_checkpoint_path(src, None, scheme = "nvfp4")
+
+
+def test_the_full_loader_reports_the_refused_nvfp4_pickle(tmp_path, monkeypatch):
+ path = _pickle(tmp_path / "Model-NVFP4.pt", "nvfp4")
+ monkeypatch.setenv(pq.ALLOW_LOCAL_PREQUANT_PATH_ENV, str(tmp_path))
+ opened = []
+ monkeypatch.setattr(pq, "_torch_load_prequant", lambda *a, **k: opened.append(a))
+ got = pq.load_prequantized_transformer(
+ object,
+ "Tongyi-MAI/Z-Image-Turbo",
+ pq.PrequantSource(kind = "path", location = str(path)),
+ device = "cpu",
+ dtype = "bfloat16",
+ hf_token = None,
+ scheme = "nvfp4",
+ )
+ assert got is None and opened == []
+ assert "safetensors only" in (pq.last_prequant_failure() or "")
+
+
+def test_nvfp4_detection_never_unpickles(tmp_path, monkeypatch):
+ def _boom(*_a, **_k):
+ raise AssertionError("an nvfp4 probe unpickled a checkpoint")
+
+ _pickle(tmp_path / "Model-NVFP4.pt", "nvfp4")
+ monkeypatch.setattr(pq, "_torch_load_prequant", _boom)
+ folder = tmp_path
+ assert pq._artifacts_declare_nvfp4(str(folder)) is False
+ # a single pickle file is judged by its name, never opened
+ assert pq.declares_nvfp4_checkpoint(str(tmp_path / "Model-NVFP4.pt")) is True
+ other = tmp_path / "Model-INT8.pt"
+ other.write_bytes(b"x")
+ assert pq.declares_nvfp4_checkpoint(str(other)) is False
+
+
+def test_the_gate_records_name_the_hosted_safetensors():
+ record = json.loads(
+ (Path(pq.__file__).parent / "nvfp4_gate_record.json").read_text(encoding = "utf-8")
+ )
+ entries = record.get("records") or record.get("entries") or []
+ assert entries
+ for entry in entries:
+ assert entry["filename"].endswith(".safetensors"), entry["filename"]
+ assert entry["filename"] in HOSTED.values()
+ assert len(entry["checkpoint_sha256"]) == 64
diff --git a/studio/backend/tests/test_prequant_prefer_cached_pickle.py b/studio/backend/tests/test_prequant_prefer_cached_pickle.py
index 21571e96f25..18f62f852cc 100644
--- a/studio/backend/tests/test_prequant_prefer_cached_pickle.py
+++ b/studio/backend/tests/test_prequant_prefer_cached_pickle.py
@@ -768,7 +768,7 @@ def test_a_cached_trailing_fp8_twin_is_used_before_an_uncached_own_file(hub):
assert hub.fetched == ["Model-FP8.safetensors"]
-# nvfp4: the same preference holds for the hosted NVFP4 repos (image families resolve the twin first).
+# nvfp4 is the exception: its hosted repos hold only the .safetensors, and a .pt is never resolved, cached or opened.
NV_REPO = "unsloth/Z-Image-Turbo-NVFP4"
NV_ST, NV_PT = "Z-Image-Turbo-NVFP4.safetensors", "Z-Image-Turbo-NVFP4.pt"
# Captured before any fixture stubs it: the nvfp4 cases ask the install's real readability question.
@@ -781,8 +781,6 @@ def nv_hub(hub, monkeypatch):
monkeypatch.setenv(diffusion_nvfp4_flag.NVFP4_DIFFUSION_ENV, "1")
monkeypatch.setattr(pq, "restricted_prequant_load_supported", _REAL_LOAD_SUPPORTED)
- if not (_REAL_LOAD_SUPPORTED("nvfp4", NV_ST) and _REAL_LOAD_SUPPORTED("nvfp4", NV_PT)):
- pytest.skip("this install cannot read nvfp4 checkpoints")
hub.hosted = {NV_ST, NV_PT}
return hub
@@ -794,7 +792,10 @@ def _nv_source():
fam = detect_family("Tongyi-MAI/Z-Image-Turbo")
source = pq.resolve_prequant_source(fam, "nvfp4")
assert source.location == NV_REPO
- assert pq.candidate_filenames_of(source)[:2] == (NV_ST, NV_PT)
+ chain = pq.candidate_filenames_of(source)
+ # the hosted safetensors leads; no .pt / .pth name anywhere (a ComfyUI-format twin may trail)
+ assert chain[0] == NV_ST and all(n.endswith(".safetensors") for n in chain), chain
+ assert source.declared_filenames == (NV_ST,)
return source
@@ -802,38 +803,82 @@ def _nv_resolve(source, **kw):
return pq._resolve_checkpoint_path(source, None, None, scheme = "nvfp4", **kw)
-def test_nvfp4_cached_pickle_is_kept(nv_hub, caplog):
+def _needs_nvfp4_safetensors():
+ if not _REAL_LOAD_SUPPORTED("nvfp4", NV_ST):
+ pytest.skip("this install cannot read nvfp4 safetensors checkpoints")
+
+
+def test_nvfp4_pickle_is_never_readable():
+ assert _REAL_LOAD_SUPPORTED("nvfp4", NV_PT) is False
+ assert _REAL_LOAD_SUPPORTED("nvfp4", "Model-NVFP4.pth") is False
+ assert _REAL_LOAD_SUPPORTED("nvfp4", "transformer_nvfp4.pt") is False
+
+
+def test_nvfp4_cached_pickle_is_ignored(nv_hub):
+ _needs_nvfp4_safetensors()
nv_hub.cache(NV_PT, NV_REPO)
- caplog.set_level("INFO")
- assert _nv_resolve(_nv_source()) == nv_hub.cached[(NV_REPO, NV_PT)]
- assert NV_ST not in nv_hub.fetched
- assert f"using the cached {NV_PT}" in caplog.text
+ assert _nv_resolve(_nv_source()) == nv_hub.cached[(NV_REPO, NV_ST)]
+ assert nv_hub.fetched == [NV_ST]
-def test_nvfp4_cached_pickle_offline(nv_hub):
+def test_nvfp4_cached_pickle_offline_is_not_used(nv_hub):
+ _needs_nvfp4_safetensors()
nv_hub.cache(NV_PT, NV_REPO)
- assert _nv_resolve(_nv_source(), local_files_only = True) == nv_hub.cached[(NV_REPO, NV_PT)]
- assert nv_hub.downloads == [(NV_PT, True)]
+ with pytest.raises(LocalEntryNotFoundError):
+ _nv_resolve(_nv_source(), local_files_only = True)
+ assert NV_PT not in nv_hub.fetched
+
+
+def test_nvfp4_cached_pickle_is_not_preferred_even_when_offered(nv_hub):
+ """A stale chain naming the .pt (an older caller) still never moves the cached pickle ahead."""
+ nv_hub.cache(NV_PT, NV_REPO)
+ names = [NV_ST, NV_PT]
+ assert pq.prefer_cached_pickle_twins(NV_REPO, names, readable = None)[0] == NV_PT
+ out = pq.prefer_cached_pickle_twins(
+ NV_REPO, names, readable = lambda n: _REAL_LOAD_SUPPORTED("nvfp4", n)
+ )
+ assert out.index(NV_ST) < out.index(NV_PT)
+ assert pq.without_nvfp4_pickles("nvfp4", out) == [NV_ST]
+
+
+def test_nvfp4_stale_pickle_chain_is_filtered_at_download(nv_hub):
+ """Even a source that still lists the .pt (built by older code) never downloads it."""
+ _needs_nvfp4_safetensors()
+ nv_hub.cache(NV_PT, NV_REPO)
+ stale = pq.PrequantSource(
+ kind = "repo", location = NV_REPO, filename = NV_ST, fallback_filenames = (NV_PT,)
+ )
+ assert _nv_resolve(stale) == nv_hub.cached[(NV_REPO, NV_ST)]
+ nv_hub.hosted = {NV_PT}
+ nv_hub.cached.clear()
+ nv_hub.downloads.clear()
+ with pytest.raises(EntryNotFoundError):
+ _nv_resolve(stale)
+ assert nv_hub.fetched == [NV_ST]
def test_nvfp4_new_user_downloads_only_the_safetensors(nv_hub):
+ _needs_nvfp4_safetensors()
assert _nv_resolve(_nv_source()) == nv_hub.cached[(NV_REPO, NV_ST)]
assert nv_hub.fetched == [NV_ST]
-def test_nvfp4_pickle_only_repo_still_loads_the_pickle(nv_hub):
+def test_nvfp4_pickle_only_repo_is_not_loaded(nv_hub):
+ _needs_nvfp4_safetensors()
nv_hub.hosted = {NV_PT}
- assert _nv_resolve(_nv_source()) == nv_hub.cached[(NV_REPO, NV_PT)]
- assert nv_hub.fetched == [NV_ST, NV_PT]
+ with pytest.raises(EntryNotFoundError):
+ _nv_resolve(_nv_source())
+ assert nv_hub.fetched[0] == NV_ST and NV_PT not in nv_hub.fetched
-def test_nvfp4_torchao_without_nvfp4_unflatten_downloads_the_pickle(nv_hub, monkeypatch):
- """torchao 0.16: the twin is hosted but unreadable here, so the hosted .pt is the only download."""
+def test_nvfp4_torchao_without_nvfp4_unflatten_is_refused(nv_hub, monkeypatch):
+ """torchao 0.16 cannot rebuild the NVFP4 safetensors: a clear refusal, never the hosted .pt."""
utils = pytest.importorskip("torchao.prototype.safetensors.safetensors_utils")
monkeypatch.setattr(
utils,
"ALLOWED_TENSORS_SUBCLASSES",
[n for n in utils.ALLOWED_TENSORS_SUBCLASSES if n not in ("NVFP4Tensor", "MXTensor")],
)
- assert _nv_resolve(_nv_source()) == nv_hub.cached[(NV_REPO, NV_PT)]
- assert nv_hub.fetched == [NV_PT]
+ with pytest.raises(ValueError, match = "torchao >= 0.17"):
+ _nv_resolve(_nv_source())
+ assert nv_hub.fetched == []
diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.py
index 40dfdf334d6..0f58ab0f91c 100644
--- a/studio/backend/tests/test_video_backend.py
+++ b/studio/backend/tests/test_video_backend.py
@@ -10788,8 +10788,8 @@ def test_the_download_plan_stages_both_experts_artifacts(monkeypatch):
_PlanSibling("vae/diffusion_pytorch_model.safetensors", 500_000_000),
],
"unsloth/Wan2.2-T2V-A14B-NVFP4": [
- _PlanSibling("Wan2.2-T2V-A14B-NVFP4.pt", 8_000_000_000),
- _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.pt", 8_100_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-NVFP4.safetensors", 8_000_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors", 8_100_000_000),
],
},
)
@@ -10797,13 +10797,13 @@ def test_the_download_plan_stages_both_experts_artifacts(monkeypatch):
"transformer": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-NVFP4.safetensors",
fallback_filename = None,
),
"transformer_2": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
fallback_filename = None,
),
}
@@ -10818,8 +10818,8 @@ def test_the_download_plan_stages_both_experts_artifacts(monkeypatch):
)
staged = {f for e in plan["entries"] for f in e["files"]}
- assert "Wan2.2-T2V-A14B-NVFP4.pt" in staged
- assert "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt" in staged
+ assert "Wan2.2-T2V-A14B-NVFP4.safetensors" in staged
+ assert "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors" in staged
assert not any(
f.endswith("diffusion_pytorch_model.safetensors") and f.startswith("transformer")
for f in staged
@@ -10849,8 +10849,8 @@ def test_an_explicit_scheme_under_speed_off_stages_the_hosted_experts(monkeypatc
_PlanSibling("vae/diffusion_pytorch_model.safetensors", 500_000_000),
],
"unsloth/Wan2.2-T2V-A14B-NVFP4": [
- _PlanSibling("Wan2.2-T2V-A14B-NVFP4.pt", 8_000_000_000),
- _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.pt", 8_100_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-NVFP4.safetensors", 8_000_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors", 8_100_000_000),
],
},
)
@@ -10858,13 +10858,13 @@ def test_an_explicit_scheme_under_speed_off_stages_the_hosted_experts(monkeypatc
"transformer": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-NVFP4.safetensors",
fallback_filename = None,
),
"transformer_2": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
fallback_filename = None,
),
}
@@ -10884,7 +10884,10 @@ def test_an_explicit_scheme_under_speed_off_stages_the_hosted_experts(monkeypatc
staged = {f for e in plan["entries"] for f in e["files"]}
assert "transformer/diffusion_pytorch_model.safetensors" not in staged
assert "transformer_2/diffusion_pytorch_model.safetensors" not in staged
- assert {"Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"} <= staged
+ assert {
+ "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ } <= staged
def test_the_video_status_response_carries_the_nvfp4_backend_label():
@@ -10947,8 +10950,8 @@ def _a14b_plan(monkeypatch):
_PlanSibling("vae/diffusion_pytorch_model.safetensors", 500_000_000),
],
"unsloth/Wan2.2-T2V-A14B-NVFP4": [
- _PlanSibling("Wan2.2-T2V-A14B-NVFP4.pt", 8_000_000_000),
- _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.pt", 8_100_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-NVFP4.safetensors", 8_000_000_000),
+ _PlanSibling("Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors", 8_100_000_000),
],
},
)
@@ -10956,13 +10959,13 @@ def _a14b_plan(monkeypatch):
"transformer": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-NVFP4.safetensors",
fallback_filename = None,
),
"transformer_2": types.SimpleNamespace(
kind = "repo",
location = "unsloth/Wan2.2-T2V-A14B-NVFP4",
- filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt",
+ filename = "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
fallback_filename = None,
),
}
@@ -11002,8 +11005,8 @@ def test_a_card_the_artifact_fits_on_still_stages_the_artifacts(monkeypatch):
)
staged = {f for e in plan["entries"] for f in e["files"]}
- assert "Wan2.2-T2V-A14B-NVFP4.pt" in staged
- assert "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt" in staged
+ assert "Wan2.2-T2V-A14B-NVFP4.safetensors" in staged
+ assert "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors" in staged
assert not any(
f.endswith("diffusion_pytorch_model.safetensors") and f.startswith("transformer")
for f in staged
@@ -11922,7 +11925,9 @@ def test_an_nvfp4_video_checkpoint_the_hub_refuses_installs_no_flashinfer(
def test_an_nvfp4_video_repo_missing_a_denoiser_installs_no_flashinfer(fake_runtime, monkeypatch):
# A14B needs BOTH denoisers.
- installs, listed, _ = _video_install_probe(monkeypatch, listing = ["Wan2.2-T2V-A14B-NVFP4.pt"])
+ installs, listed, _ = _video_install_probe(
+ monkeypatch, listing = ["Wan2.2-T2V-A14B-NVFP4.safetensors"]
+ )
_video_load_to_the_install_gate()
assert installs == []
assert listed == ["unsloth/Wan2.2-T2V-A14B-NVFP4"]
@@ -11943,7 +11948,10 @@ def test_an_nvfp4_video_family_with_no_hosted_checkpoint_installs_no_flashinfer(
def test_a_reachable_nvfp4_video_checkpoint_still_installs_flashinfer(fake_runtime, monkeypatch):
installs, listed, _ = _video_install_probe(
monkeypatch,
- listing = ["Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"],
+ listing = [
+ "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ ],
)
_video_load_to_the_install_gate()
assert listed == ["unsloth/Wan2.2-T2V-A14B-NVFP4"]
@@ -11956,7 +11964,10 @@ def test_with_the_nvfp4_switch_off_a_reachable_video_checkpoint_installs_nothing
monkeypatch.delenv("UNSLOTH_NVFP4_DIFFUSION", raising = False)
installs, listed, _ = _video_install_probe(
monkeypatch,
- listing = ["Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"],
+ listing = [
+ "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ ],
)
_video_load_to_the_install_gate(transformer_quant = None, _video_auto_denoiser_planned = "nvfp4")
assert installs == []
@@ -11979,7 +11990,10 @@ def test_a_video_seed_the_plan_declined_installs_no_flashinfer(fake_runtime, mon
installs, listed, _ = _video_install_probe(
monkeypatch,
- listing = ["Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"],
+ listing = [
+ "Wan2.2-T2V-A14B-NVFP4.safetensors",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
+ ],
)
_video_load_to_the_install_gate(_video_auto_denoiser_planned = DENOISER_SEED_DECLINED)
assert installs == []
@@ -12040,7 +12054,7 @@ def _stub_h3_nvfp4_checkpoint(monkeypatch):
return types.SimpleNamespace(
kind = "repo",
location = "unsloth/MiniMax-H3-NVFP4",
- filename = f"MiniMax-H3-{task}-NVFP4.pt",
+ filename = f"MiniMax-H3-{task}-NVFP4.safetensors",
fallback_filenames = (),
)
@@ -12071,7 +12085,7 @@ def test_the_minimax_h3_modular_path_skips_the_install_for_a_refused_checkpoint(
def test_the_minimax_h3_modular_path_installs_for_a_reachable_checkpoint(fake_runtime, monkeypatch):
installs, listed, dispatched = _video_install_probe(
- monkeypatch, listing = ["MiniMax-H3-ref2va-NVFP4.pt"]
+ monkeypatch, listing = ["MiniMax-H3-ref2va-NVFP4.safetensors"]
)
_stub_h3_nvfp4_checkpoint(monkeypatch)
_video_load_to_the_install_gate(
diff --git a/studio/backend/tests/test_video_prequant.py b/studio/backend/tests/test_video_prequant.py
index 34acae886ec..75d9274631c 100644
--- a/studio/backend/tests/test_video_prequant.py
+++ b/studio/backend/tests/test_video_prequant.py
@@ -1037,7 +1037,7 @@ def _moe_fam(**kwargs):
return _fam(
is_moe = True,
prequant_repos = (("nvfp4", "org/test-quantized"),),
- prequant_filenames = (("nvfp4", "transformer_2", "test-transformer_2-NVFP4.pt"),),
+ prequant_filenames = (("nvfp4", "transformer_2", "test-transformer_2-NVFP4.safetensors"),),
**kwargs,
)
@@ -1049,21 +1049,16 @@ def test_the_components_a_family_seeds_are_its_denoisers():
def test_the_second_expert_is_addressed_through_the_task_slot():
- """The second expert is addressed through the task slot; its only fallback is its own .pt container."""
+ """The second expert is addressed through the task slot; nvfp4 resolves no .pt container at all."""
from core.inference.video_denoiser_prequant import denoiser_prequant_sources
sources = denoiser_prequant_sources(_moe_fam(), "nvfp4", "org/test-video")
assert set(sources) == {"transformer", "transformer_2"}
from core.inference.diffusion_prequant import candidate_filenames_of
- assert candidate_filenames_of(sources["transformer"]) == (
- "test-NVFP4.safetensors",
- "test-NVFP4.pt",
- "transformer_nvfp4.pt",
- )
+ assert candidate_filenames_of(sources["transformer"]) == ("test-NVFP4.safetensors",)
assert candidate_filenames_of(sources["transformer_2"]) == (
"test-transformer_2-NVFP4.safetensors",
- "test-transformer_2-NVFP4.pt",
)
diff --git a/studio/backend/tests/test_video_prequant_safetensors_twins.py b/studio/backend/tests/test_video_prequant_safetensors_twins.py
index 62f10e8c77f..c6e6b7a174a 100644
--- a/studio/backend/tests/test_video_prequant_safetensors_twins.py
+++ b/studio/backend/tests/test_video_prequant_safetensors_twins.py
@@ -57,51 +57,6 @@ CHAINS = {
["Wan2.2-T2V-A14B-INT8-2.pt"],
["Wan2.2-T2V-A14B-INT8-2.safetensors", "Wan2.2-T2V-A14B-INT8-2.pt"],
),
- ("wan2.2-ti2v-5b", "nvfp4", None, None): (
- ["Wan2.2-TI2V-5B-NVFP4.pt", "Wan2.2-TI2V-5B-NVFP4.safetensors", "transformer_nvfp4.pt"],
- ["Wan2.2-TI2V-5B-NVFP4.safetensors", "Wan2.2-TI2V-5B-NVFP4.pt", "transformer_nvfp4.pt"],
- ),
- ("wan2.2-t2v-a14b", "nvfp4", None, None): (
- ["Wan2.2-T2V-A14B-NVFP4.pt", "Wan2.2-T2V-A14B-NVFP4.safetensors", "transformer_nvfp4.pt"],
- ["Wan2.2-T2V-A14B-NVFP4.safetensors", "Wan2.2-T2V-A14B-NVFP4.pt", "transformer_nvfp4.pt"],
- ),
- ("wan2.2-t2v-a14b", "nvfp4", None, "transformer_2"): (
- ["Wan2.2-T2V-A14B-transformer_2-NVFP4.pt"],
- [
- "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
- "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt",
- ],
- ),
- ("hunyuanvideo-1.5", "nvfp4", None, None): (
- [
- "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.pt",
- "HunyuanVideo-1.5-NVFP4.safetensors",
- "HunyuanVideo-1.5-NVFP4.pt",
- "transformer_nvfp4.pt",
- ],
- [
- "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.safetensors",
- "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.pt",
- "HunyuanVideo-1.5-NVFP4.safetensors",
- "HunyuanVideo-1.5-NVFP4.pt",
- "transformer_nvfp4.pt",
- ],
- ),
- ("hunyuanvideo-1.5-720p", "nvfp4", None, None): (
- [
- "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.pt",
- "HunyuanVideo-1.5-NVFP4.safetensors",
- "HunyuanVideo-1.5-NVFP4.pt",
- "transformer_nvfp4.pt",
- ],
- [
- "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.safetensors",
- "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.pt",
- "HunyuanVideo-1.5-NVFP4.safetensors",
- "HunyuanVideo-1.5-NVFP4.pt",
- "transformer_nvfp4.pt",
- ],
- ),
# Already twin-first (derived chain or declared safetensors): unchanged.
("minimax-h3", "fp8", None, None): (
["MiniMax-H3-FP8.safetensors", "MiniMax-H3-FP8.pt", "transformer_fp8.pt"],
@@ -136,6 +91,22 @@ CHAINS = {
}
+# nvfp4 is safetensors only: no .pt / .pth name in its chain, with the twins switch on or off.
+NVFP4_CHAINS = {
+ ("wan2.2-ti2v-5b", None): ["Wan2.2-TI2V-5B-NVFP4.safetensors"],
+ ("wan2.2-t2v-a14b", None): ["Wan2.2-T2V-A14B-NVFP4.safetensors"],
+ ("wan2.2-t2v-a14b", "transformer_2"): ["Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors"],
+ ("hunyuanvideo-1.5", None): [
+ "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4.safetensors",
+ "HunyuanVideo-1.5-NVFP4.safetensors",
+ ],
+ ("hunyuanvideo-1.5-720p", None): [
+ "HunyuanVideo-1.5-Diffusers-720p_t2v-NVFP4.safetensors",
+ "HunyuanVideo-1.5-NVFP4.safetensors",
+ ],
+}
+
+
@pytest.fixture(autouse = True)
def _env(monkeypatch):
monkeypatch.setenv("UNSLOTH_NVFP4_DIFFUSION", "1")
@@ -166,6 +137,28 @@ def test_kill_switch_restores_the_old_chain(key, monkeypatch):
assert _chain(key)[1] == CHAINS[key][0]
[email protected]("twins", [None, "0"], ids = ["twins_on", "twins_off"])
[email protected]("key", sorted(NVFP4_CHAINS, key = str), ids = lambda k: "-".join(map(str, k)))
+def test_nvfp4_chain_is_safetensors_only(key, twins, monkeypatch):
+ if twins is not None:
+ monkeypatch.setenv(TWINS_ENV, twins)
+ fam, task = key
+ src, chain = _chain((fam, "nvfp4", None, task))
+ assert chain == NVFP4_CHAINS[key]
+ assert src.declared_filenames == (chain[0],)
+
+
+def test_no_video_family_declares_an_nvfp4_pickle():
+ rows = [
+ (fam.name, entry)
+ for fam in vf._FAMILIES
+ for entry in getattr(fam, "prequant_filenames", ()) or ()
+ if entry[0] == "nvfp4"
+ ]
+ assert len(rows) == 5
+ assert all(entry[-1].endswith(".safetensors") for _name, entry in rows), rows
+
+
def test_every_declared_video_pickle_gets_its_twin_first():
"""Over the whole table, so a new family row cannot skip the rule."""
seen = 0
@@ -182,7 +175,7 @@ def test_every_declared_video_pickle_gets_its_twin_first():
twin = name[: -len(".pt")] + ".safetensors"
assert chain.index(twin) == chain.index(name) - 1, (fam.name, chain)
seen += 1
- assert seen >= 8
+ assert seen >= 4
class FakeHub:
@@ -241,9 +234,15 @@ CELLS = [
("minimax-h3", "int8", None, None, "MiniMax-H3-INT8-ConvRot"),
("minimax-h3", "fp8", None, "ref2va", "MiniMax-H3-Ref2VA-FP8"),
("wan2.2-t2v-a14b", "fp8", None, "transformer_2", "Wan2.2-T2V-A14B-FP8-2"),
+ ("wan2.2-t2v-a14b", "int8", None, "transformer_2", "Wan2.2-T2V-A14B-INT8-2"),
+ ("minimax-h3", "int8", None, "ref2va", "MiniMax-H3-Ref2VA-INT8-ConvRot"),
+]
+# nvfp4 shapes: a secondary expert and an agnostic declared name. Their .pt is never asked for.
+NVFP4_CELLS = [
("wan2.2-t2v-a14b", "nvfp4", None, "transformer_2", "Wan2.2-T2V-A14B-transformer_2-NVFP4"),
("hunyuanvideo-1.5", "nvfp4", None, None, "HunyuanVideo-1.5-Diffusers-480p_t2v-NVFP4"),
]
+NVFP4_CELL_IDS = [f"{c[0]}-{c[3]}" for c in NVFP4_CELLS]
CELL_IDS = [f"{c[0]}-{c[1]}-{c[3]}" for c in CELLS]
@@ -367,7 +366,7 @@ def _video_sources(monkeypatch, fam_name, scheme):
@pytest.mark.parametrize("cached_pt", [False, True], ids = ["new_user", "cached_pt"])
def test_planner_prices_the_file_each_expert_will_open(cached_pt, tmp_path, monkeypatch):
- """Wan A14B: both experts. Disk / memory planning counts the twin for a new user, the .pt for a cached one."""
+ """Wan A14B nvfp4: both experts. Planning and fetching count the .safetensors, even with a stale .pt cached."""
from core.inference.video import VideoBackend
stems = ["Wan2.2-T2V-A14B-NVFP4", "Wan2.2-T2V-A14B-transformer_2-NVFP4"]
@@ -391,7 +390,7 @@ def test_planner_prices_the_file_each_expert_will_open(cached_pt, tmp_path, monk
if cached_pt:
for s in stems:
hub.cache(s + ".pt")
- suffix = ".pt" if cached_pt else ".safetensors"
+ suffix = ".safetensors"
api = _Api(sorted(hub.hosted))
location, files = VideoBackend._denoiser_prequant_hub_files(None, "nvfp4", None, api)
assert location == repo
@@ -410,3 +409,39 @@ def test_planner_prices_the_file_each_expert_will_open(cached_pt, tmp_path, monk
assert fetched == [s + suffix for s in stems]
for src, stem in zip(sources.values(), stems):
assert _resolve(src, "nvfp4") == hub.cached[(repo, stem + suffix)]
+
+
[email protected]("cell", NVFP4_CELLS, ids = NVFP4_CELL_IDS)
[email protected]("offline", [False, True], ids = ["online", "offline"])
+def test_nvfp4_a_cached_pickle_is_never_opened(cell, offline, tmp_path, monkeypatch):
+ src, scheme, stem, hub = _setup(cell, tmp_path, monkeypatch)
+ hub.cache(stem + ".pt")
+ if offline:
+ with pytest.raises(LocalEntryNotFoundError):
+ _resolve(src, scheme, local_files_only = True)
+ else:
+ assert _resolve(src, scheme) == hub.cached[(src.location, stem + ".safetensors")]
+ assert stem + ".pt" not in hub.fetched
+ assert pq.cached_checkpoint_path(src, online = False) != hub.cached[(src.location, stem + ".pt")]
+
+
[email protected]("cell", NVFP4_CELLS, ids = NVFP4_CELL_IDS)
+def test_nvfp4_unreadable_twin_is_refused_not_swapped_for_the_pickle(cell, tmp_path, monkeypatch):
+ src, scheme, stem, hub = _setup(
+ cell,
+ tmp_path,
+ monkeypatch,
+ readable = lambda scheme = None, filename = None: not str(filename).endswith(".safetensors"),
+ )
+ hub.cache(stem + ".pt")
+ with pytest.raises(ValueError, match = "cannot be read here"):
+ _resolve(src, scheme)
+ assert hub.fetched == []
+
+
[email protected]("cell", NVFP4_CELLS, ids = NVFP4_CELL_IDS)
+def test_nvfp4_twin_not_hosted_never_falls_back_to_the_pickle(cell, tmp_path, monkeypatch):
+ src, scheme, stem, hub = _setup(cell, tmp_path, monkeypatch, hosted_twin = False)
+ with pytest.raises(EntryNotFoundError):
+ _resolve(src, scheme)
+ assert stem + ".pt" not in hub.fetched
diff --git a/studio/backend/tests/test_video_wan_hosted_prequant.py b/studio/backend/tests/test_video_wan_hosted_prequant.py
index 61fff198753..682c1aa5ed9 100644
--- a/studio/backend/tests/test_video_wan_hosted_prequant.py
+++ b/studio/backend/tests/test_video_wan_hosted_prequant.py
@@ -75,7 +75,7 @@ def test_nvfp4_rows_are_unchanged():
assert (
"nvfp4",
"transformer_2",
- "Wan2.2-T2V-A14B-transformer_2-NVFP4.pt",
+ "Wan2.2-T2V-A14B-transformer_2-NVFP4.safetensors",
) in a14b.prequant_filenames