Skip to content

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.

Merge remote-tracking branch 'upstream/main' into feat/fp8_compute_raw

Alexander Eichhorn2c0011413db7
diff --cc invokeai/backend/model_manager/load/model_loaders/krea2.py
index c50e1301cf,dbc4b109c6..f47cfdbc77
--- a/invokeai/backend/model_manager/load/model_loaders/krea2.py
+++ b/invokeai/backend/model_manager/load/model_loaders/krea2.py
@@@ -749,47 -604,12 +749,47 @@@ class Qwen3VLEncoderCheckpointLoader(Mo
model.load_state_dict(sd, assign=True, strict=False)
_reject_incomplete_load(model, what="Qwen3-VL encoder checkpoint")
+ if fp8_layers:
+ # Keep the weights quantized and let CustomLinear run their matmuls on the fp8 tensor
+ # cores. Same resident VRAM as the layerwise-casting path below, but without paying a
+ # fp8->bf16 round trip on every forward.
+ attached = attach_fp8_scales(model, fp8_layers)
+ warn_on_unattached_scales(self._logger, f"Qwen3-VL encoder '{config.name}'", attached, fp8_layers)
+ # The checkpoint quantizes only the language-model linears; the visual tower ships bf16
+ # and would make the encoder ~0.4GB larger resident than the plain fp8_storage path. On a
+ # GPU already holding a ~12GB transformer that is enough to push the transformer into
+ # partial loading, so cast the rest to fp8 storage. Two exclusions:
+ #
+ # - anything carrying a `weight_scale`: those keep their scale and go through
+ # _scaled_mm; the cast hooks would upcast them without it, i.e. a wrong weight.
+ # - `nn.Embedding`: the token embedding table is this model's *input* representation
+ # and it is large (389M params). Measured on qwen3vl_4b_fp8_scaled, quantizing it
+ # doubles the encoder's error against bf16 (relative L2 0.0079 -> 0.0163) to save
+ # 371MiB. That is a bad trade for a model whose entire job is text fidelity. (The
+ # old fp8_storage path did cast it — this is strictly more accurate than before.)
+ self._apply_fp8_to_nn_module(
+ model,
+ storage_dtype=torch.float8_e4m3fn,
+ compute_dtype=model_dtype,
+ skip=lambda _name, module: getattr(module, "weight_scale", None) is not None
+ or isinstance(module, torch.nn.Embedding),
+ )
+ self._logger.info(
+ f"FP8 compute enabled for Qwen3-VL encoder '{config.name}': kept {attached} layer(s) "
+ f"quantized (storage=float8_e4m3fn, matmul=torch._scaled_mm, compute={model_dtype}); "
+ "remaining layers cast to fp8 storage."
+ )
+ # The layerwise-casting path below exists to *produce* fp8 weights from full-precision
+ # ones. These already are fp8, and its hooks would cast them to the compute dtype without
+ # applying weight_scale — a silently wrong weight. It must not run here.
+ return model
+
# Keep an fp8 encoder running in fp8 (storage=float8_e4m3fn, per-layer upcast to the compute
- # dtype during forward) on CUDA. `_should_use_fp8` deliberately excludes text encoders (and the
- # config has no fp8_storage toggle), so apply the hook-based casting directly here. This roughly
- # halves the encoder's resident VRAM (~8.9GB bf16 -> ~4.4GB), which avoids partial-load thrashing
- # when it shares the GPU with a large transformer.
- if source_is_fp8 and self._torch_device.type == "cuda":
+ # dtype during forward) on devices that support fp8 storage. `_should_use_fp8` deliberately
+ # excludes text encoders (and the config has no fp8_storage toggle), so apply the hook-based
+ # casting directly here. This roughly halves the encoder's resident VRAM (~8.9GB bf16 ->
+ # ~4.4GB), which avoids partial-load thrashing when it shares the GPU with a large transformer.
+ if source_is_fp8 and _device_supports_fp8_storage(self._torch_device, self._logger):
# `model.dtype` now reports the float8 storage dtype; `_apply_fp8_to_nn_module` records
# the real compute dtype so callers can recover it via `get_model_compute_dtype`.
self._apply_fp8_to_nn_module(model, storage_dtype=torch.float8_e4m3fn, compute_dtype=model_dtype)