Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(models): keep FP8 Storage off for scaled FP8 checkpoints
Alexander Eichhorn00e63202a4c9
diff --git a/docs/src/content/docs/configuration/fp8-storage.mdx b/docs/src/content/docs/configuration/fp8-storage.mdxindex 6523e6c241..145c795c0b 100644--- a/docs/src/content/docs/configuration/fp8-storage.mdx+++ b/docs/src/content/docs/configuration/fp8-storage.mdx@@ -11,7 +11,7 @@ FP8 Storage cuts a model's VRAM footprint roughly in half by keeping weights onIt pairs well with [Low-VRAM mode](/configuration/low-vram-mode/): low-VRAM mode streams layers between RAM and VRAM, while FP8 Storage shrinks the layers themselves.:::caution[For full precision models only]-FP8 Storage only applies to **full precision** checkpoints (FP16 / BF16 / FP32). It is **silently a no-op** for already-quantized formats — **GGUF**, **NF4**, **int8**, and [**SDNQ**](/configuration/sdnq-quantization/) checkpoints carry their own storage precision and the loader returns a different module type that the FP8 layer cast does not touch. If your model is already quantized, the toggle has no effect; use the full-precision variant of the model if you want to enable FP8 Storage. Checkpoints that store FP8 weights are the exception: InvokeAI loads them at BF16 unless FP8 Storage is on, so they get it automatically when installed (see [When installing](#when-installing)).+FP8 Storage only applies to **full precision** checkpoints (FP16 / BF16 / FP32). It is **silently a no-op** for already-quantized formats — **GGUF**, **NF4**, **int8**, and [**SDNQ**](/configuration/sdnq-quantization/) checkpoints carry their own storage precision and the loader returns a different module type that the FP8 layer cast does not touch. If your model is already quantized, the toggle has no effect; use the full-precision variant of the model if you want to enable FP8 Storage. Checkpoints that store plain FP8 weights are the exception: InvokeAI loads them at BF16 unless FP8 Storage is on, so they get it automatically when installed. Checkpoints that store *scaled* FP8 are left alone — see [When installing](#when-installing) for both cases.:::## Requirements@@ -56,7 +56,8 @@ The setting takes effect on the next load. If the model is already in the cache,### When installing-- **Checkpoints that already store FP8 weights get FP8 Storage automatically.** InvokeAI reads the weight types from the file while identifying the model, not its name, so a renamed `fp8_scaled` file is recognised and a full-precision file with "fp8" in its name is not. Only the denoiser counts: an all-in-one checkpoint with an FP8 text encoder beside a full-precision UNet or transformer is left alone. Without this, an FP8 checkpoint would be loaded at BF16 and take twice the VRAM.+- **Checkpoints that store plain FP8 weights get FP8 Storage automatically.** InvokeAI reads the weight types from the file while identifying the model, not its name, so a full-precision file with "fp8" in its name is not affected. Only the denoiser counts: an all-in-one checkpoint with an FP8 text encoder beside a full-precision UNet or transformer is left alone. Without this, an FP8 checkpoint would be loaded at BF16 and take twice the VRAM.+- **"Scaled" FP8 checkpoints are deliberately left alone.** Their weights mean nothing without the per-tensor scale stored next to them, and FP8 Storage has no scale to apply: the loader folds the scale into the weight, and the cast then rounds that to *unscaled* FP8. On FLUX.2 Klein 4B that rounds 3.4% of the weights to zero — the scales are around 1e-3, so the loss is underflow rather than clipping — and it saves nothing, because the file was already one byte per weight. You can still switch it on yourself; [FP8 Compute](#fp8-compute) is the setting that keeps these files exactly as they are.- **For any other model, tick FP8 storage next to the install source** (a file path, URL or HuggingFace repo, and in folder scan results). It applies to the models you install from there.Either way the setting stays editable under **Default Settings** afterwards. Re-identifying a model resets its default settings, including this one, to what identification chooses.@@ -122,7 +123,7 @@ If you see unexpected quality regressions, disable FP8 Storage on the affected mEverything above describes FP8 *Storage*, which changes how weights are **stored** while the math still runs in BF16. `fp8_compute` is a separate, global setting in `invokeai.yaml` that also does the **math** in FP8, on GPUs that have hardware for it. That makes generation faster, not just smaller.-It is for models that were **already saved in FP8** by whoever published them — you'll often see these labelled "fp8" or "fp8_scaled" in the filename. Without it InvokeAI unpacks them to BF16 while loading, and FP8 Storage — which they get automatically when installed — keeps them FP8 in VRAM; with `fp8_compute` on, they stay as they are and run directly on the GPU's FP8 hardware, and FP8 Storage has nothing left to do for those layers.+It is for models that were **already saved in FP8** by whoever published them — you'll often see these labelled "fp8" or "fp8_scaled" in the filename. Without it InvokeAI unpacks them to BF16 while loading, and FP8 Storage keeps them FP8 in VRAM (plain FP8 files get it automatically when installed, scaled ones do not — see [When installing](#when-installing)); with `fp8_compute` on, they stay as they are and run directly on the GPU's FP8 hardware, and FP8 Storage has nothing left to do for those layers.Not every part of a model stays in FP8 — the small, precision-sensitive pieces are always unpacked. Those are a tiny share of the weights, so you still get nearly the full VRAM saving.diff --git a/invokeai/backend/model_manager/configs/factory.py b/invokeai/backend/model_manager/configs/factory.pyindex a09aac8ac6..dd4b17f252 100644--- a/invokeai/backend/model_manager/configs/factory.py+++ b/invokeai/backend/model_manager/configs/factory.py@@ -187,6 +187,7 @@ from invokeai.backend.model_manager.taxonomy import (ModelType,variant_type_adapter,)+from invokeai.backend.quantization.fp8_scaled import is_scale_metadata_keylogger = logging.getLogger(__name__)@@ -202,11 +203,18 @@ SettingsT = TypeVar("SettingsT", bound=BaseModel)def _denoiser_stores_float8_weights(mod: ModelOnDisk) -> bool:- """Whether the model's denoiser weights are stored in float8, judged by the safetensors headers' dtypes and never- by a file name.+ """Whether the denoiser's weights are stored in a float8 the storage cast can reproduce, judged by the safetensors+ headers and never by a file name.Only headers are read, a few KB each. A header that cannot be read is no evidence of float8 weights: identificationdid not need that file, and picking a default setting is no reason to fail an install.++ A *scaled* fp8 checkpoint is deliberately excluded. Its weights mean nothing without the per-tensor scale stored+ beside them, and FP8 Storage has no scale to apply: the loader folds the scale into the weight and hands the cast+ plain bf16, which then rounds to *unscaled* fp8. Measured on `flux-2-klein-4b-fp8`, that flushes 3.4% of the+ weights to exactly zero -- by underflow, the scales being ~1e-3, not by saturation -- for 2-4% relative L2 error+ per layer, and it buys nothing: the file already was one byte per weight. Keeping such a file in its own scaled+ form is the lossless way to spend that byte, but that is a loader change rather than a default setting."""if mod.path.is_file():candidates = [mod.path]@@ -217,6 +225,7 @@ def _denoiser_stores_float8_weights(mod: ModelOnDisk) -> bool:if path.parent == mod.path or path.relative_to(mod.path).parts[0] in _DENOISER_SUBFOLDERS]+ stores_float8 = Falsefor path in candidates:if path.suffix != ".safetensors":continue@@ -225,14 +234,15 @@ def _denoiser_stores_float8_weights(mod: ModelOnDisk) -> bool:except (OSError, ValueError) as e:logger.debug(f"Could not read the safetensors header of {path} to look for float8 weights: {e}")continue- if any(- not key.startswith(_BUNDLED_COMPONENT_KEY_PREFIXES)- and isinstance(info, dict)- and info.get("dtype") in _FP8_SAFETENSORS_DTYPES- for key, info in header.items()- ):- return True- return False+ denoiser = {key: info for key, info in header.items() if not key.startswith(_BUNDLED_COMPONENT_KEY_PREFIXES)}+ if any(is_scale_metadata_key(key) for key in denoiser):+ return False+ # No early return on a float8 hit: a sharded denoiser can keep its scales in a shard of its own, and finding+ # them there still means the whole checkpoint is scaled.+ stores_float8 = stores_float8 or any(+ isinstance(info, dict) and info.get("dtype") in _FP8_SAFETENSORS_DTYPES for info in denoiser.values()+ )+ return stores_float8def _identified_default_settings(diff --git a/tests/backend/model_manager/configs/test_identified_default_settings.py b/tests/backend/model_manager/configs/test_identified_default_settings.pyindex ae9e44e50b..be791ae2d6 100644--- a/tests/backend/model_manager/configs/test_identified_default_settings.py+++ b/tests/backend/model_manager/configs/test_identified_default_settings.py@@ -1,8 +1,8 @@"""The default settings a model is identified with.-FP8 Storage is switched on for a checkpoint whose denoiser weights are stored in float8, read from the weights' dtypes-and never from the file name: a Comfy "fp8_scaled" file that nobody renamed and a full-precision file that somebody did-must both come out right. Settings passed along with an install land on top of what identification chose.+FP8 Storage is switched on for a checkpoint whose denoiser weights are stored in *unscaled* float8, read from the+weights' dtypes and the scale keys beside them, never from the file name: a full-precision file somebody named+"fp8_scaled" and a genuinely scaled fp8 checkpoint must both come out right. Settings passed along with an install land on top of what identification chose.Single files are tiny Qwen-Image checkpoints, the smallest main model identification recognises from real keys. Foldersare the installer's SDXL diffusers fixture, whose placeholder weight files have no readable header at all.@@ -47,6 +47,27 @@ def test_float8_denoiser_weights_turn_fp8_storage_on_whatever_the_file_is_calledassert config.default_settings.fp8_storage is True[email protected]("scale_key", ["img_in.weight_scale", "img_in.scale_weight"])+def test_a_scaled_fp8_checkpoint_is_left_alone(tmp_path: Path, scale_key: str) -> None:+ """Comfy's fp8_scaled weights mean nothing without the per-tensor scale stored beside them, and the storage cast+ has none to apply: the loader folds the scale into the weight and the cast rounds that to *unscaled* fp8. Measured+ on `flux-2-klein-4b-fp8`, that flushes 3.4% of the weights to exactly zero -- underflow, the scales being ~1e-3 --+ for 2-4% relative L2 error per layer, and it saves nothing: the file already was one byte per weight.++ Both spellings of the scale key are checked because matching only `.weight_scale` is the miss that keeps recurring+ (see `iter_weight_scale_pairs`).+ """+ path = _qwen_image_checkpoint(+ tmp_path / "qwen_image.safetensors",+ torch.float8_e4m3fn,+ **{scale_key: torch.ones(1)},+ )++ config = _identify(path)++ assert config.default_settings is None or config.default_settings.fp8_storage is None++def test_a_full_precision_file_named_fp8_is_left_alone(tmp_path: Path) -> None:config = _identify(_qwen_image_checkpoint(tmp_path / "qwen_image_fp8_scaled.safetensors", torch.bfloat16))