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
Merge branch 'feat/fp8_quantized_guard' into feat/fp8_compute_raw
Alexander Eichhorndecabd515fab
diff --cc invokeai/backend/model_manager/load/load_default.pyindex bd39b23cd4,409ec81db6..0505b04dff--- a/invokeai/backend/model_manager/load/load_default.py+++ b/invokeai/backend/model_manager/load/load_default.py@@@ -464,7 -490,7 +507,8 @@@ class ModelLoader(ModelLoaderBase)model: torch.nn.Module,storage_dtype: torch.dtype,compute_dtype: torch.dtype,+ extra_skip_patterns: tuple[str, ...] = (),+ skip: Optional[Callable[[str, torch.nn.Module], bool]] = None,) -> None:"""Apply FP8 layerwise casting to a plain nn.Module.@@@ -474,12 -500,15 +518,21 @@@Without the skip list, precision-sensitive tiny learned scalars (e.g. FLUX RMSNorm.scale)get crushed to FP8 and quality degrades noticeably.+ `extra_skip_patterns` carries the model's own declared exclusions (diffusers'+ `_skip_layerwise_casting_patterns`), which are model-specific and cannot be inferred from+ layer types or generic name patterns.++ `skip` excludes further modules by (dotted name, module). Its one caller uses it to leave+ scaled-fp8 layers alone: those already hold fp8 weights plus a `weight_scale`, and the cast+ hooks installed here would upcast them *without* applying that scale — a silently wrong+ weight. Casting only the remainder lets a partly-quantized checkpoint (fp8 language model,+ bf16 visual tower) end up fully fp8-resident.++ Modules holding already-quantized weights are skipped regardless of their class. This is a+ backstop behind the format check in `_should_use_fp8`, which cannot see quantization that+ is not reflected in the model's format (e.g. a `diffusers`-format checkpoint whose weights+ were quantized by an external tool).+Records the compute dtype on the model. After the cast, `model.dtype` reports the float8storage dtype, which must never be used to create or cast tensors — torch has no arithmetickernels for it (see `get_model_compute_dtype`). The marker is set here rather than at the@@@ -490,10 -520,8 +544,10 @@@for module_name, module in model.named_modules():if not isinstance(module, _FP8_SUPPORTED_PYTORCH_LAYERS):continue- if any(re.search(pattern, module_name) for pattern in _FP8_DEFAULT_SKIP_PATTERNS):+ if any(re.search(pattern, module_name) for pattern in skip_patterns):continue+ if skip is not None and skip(module_name, module):+ continueparams = list(module.parameters(recurse=False))if not params:continuediff --cc invokeai/backend/model_manager/load/model_loaders/z_image.pyindex 47e9015c4a,0ca5dfb010..868a1f2c69--- a/invokeai/backend/model_manager/load/model_loaders/z_image.py+++ b/invokeai/backend/model_manager/load/model_loaders/z_image.py@@@ -587,22 -483,10 +587,29 @@@ class ZImageCheckpointModel(ModelLoadermodel.load_state_dict(sd, assign=True)- # Every param is uniform `model_dtype` at this point (the loop above casts the whole state- # dict, including ComfyUI fp8 checkpoints, whose scale metadata was filtered out above), so- # the layerwise cast has a single unambiguous compute dtype to restore to.+ if fp8_layers:+ attached = attach_fp8_scales(model, fp8_layers)+ self._logger.info(f"Z-Image: kept {attached} layer(s) in fp8 (scaled fp8 checkpoint, fp8_compute enabled)")+ warn_on_unattached_scales(self._logger, "Z-Image", attached, fp8_layers)+ marked = sum(1 for layer in fp8_layers.values() if layer.full_precision_matmul)+ if marked and full_precision_hints_respected():+ self._logger.info(+ f"Z-Image: {marked} of {len(fp8_layers)} layer(s) are marked full_precision_matrix_mult "+ "and will dequantize per forward. Set fp8_compute_full_precision_hints=false to run "+ "them on the fp8 tensor cores instead."+ )+ elif kept:+ self._logger.info(+ f"Z-Image: kept {kept} raw fp8 weight(s) quantized (no weight_scale in the checkpoint); "+ "they will run on the fp8 tensor cores with unit scaling."+ )++++ # FP8 *storage* on top. When nothing was kept quantized above, every param is uniform++ # `model_dtype` here, so the layerwise cast has one unambiguous compute dtype to restore to.++ # When weights *were* kept fp8, `_apply_fp8_layerwise_casting` bails out on its own (and++ # says so in the log): its hooks would restore the compute dtype before every forward and++ # silently disable the fp8 matmul, for no VRAM saving.+ model = self._apply_fp8_layerwise_casting(model, config, SubModelType.Transformer)return model