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 'origin/main' into feat/int8-convrot-shared

Alexander Eichhorneb7019daf0d3
diff --cc invokeai/backend/model_manager/load/model_loaders/z_image.py
index 0164405e7e,ffda6eaf9c..5b8c3ec977
--- a/invokeai/backend/model_manager/load/model_loaders/z_image.py
+++ b/invokeai/backend/model_manager/load/model_loaders/z_image.py
@@@ -503,31 -473,29 +503,41 @@@ class ZImageCheckpointModel(ModelLoader
for k in keys_to_remove:
del sd[k]
- # Handle memory management and dtype conversion
- new_sd_size = sum([ten.nelement() * model_dtype.itemsize for ten in sd.values()])
+ # ComfyUI 'int8_tensorwise' checkpoints. The markers are read *after* the key conversion
+ # above, which carries them (and their scales) through the fused-QKV split onto the module
+ # names the model actually has -- so no re-keying is needed here.
+ int8_markers = extract_int8_convrot_markers(sd)
+ sd = drop_unconsumed_quantization_sidecars(sd)
+ orphans = sorted(k for k, v in sd.items() if v.dtype is torch.int8 and k[: -len(".weight")] not in int8_markers)
+ if orphans:
+ raise ValueError(
+ f"Z-Image checkpoint has {len(orphans)} int8 weight(s) with no `comfy_quant` marker, "
+ f"e.g. {orphans[:3]}. Loading them would produce a model that runs and generates noise."
+ )
+
+ # int8 payloads are one byte, not two: reserving the compute dtype's width for them would
+ # ask the cache to free memory this load never uses.
+ new_sd_size = sum(ten.nelement() * max(ten.element_size(), model_dtype.itemsize) for ten in sd.values())
self._ram_cache.make_room(new_sd_size)
- # Convert to target dtype
- for k in sd.keys():
- sd[k] = sd[k].to(model_dtype)
+ cast_unquantized(sd, model_dtype, int8_markers)
+ swap_in_int8_linears(model, sd, int8_markers)
model.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.
+ # `assign=True` aliases every param to its `sd` tensor, so the dict keeps the whole model
+ # alive a second time. The FP8 cast below allocates the fp8 copy per param while the
+ # `model_dtype` original is still reachable through `sd`, pushing peak RAM to ~1.5x what
+ # `make_room()` reserved above (~17.4GB actual vs ~11.5GB reserved for Z-Image). Dropping
+ # the dict's references lets each original free as soon as its param is cast.
+ sd.clear()
+
+ # Every param is uniform `model_dtype` at this point, so the layerwise cast has a single
+ # unambiguous compute dtype to restore to.
+ #
+ # Caveat, pre-existing and not addressed here: for a ComfyUI *scaled*-fp8 checkpoint the
+ # filter above drops `.scale_weight` / `scaled_fp8` without folding them in, so the raw fp8
+ # codes are cast to `model_dtype` unscaled and the model loads with wrong weights. That is a
+ # separate bug in the key filtering, not something this cast makes safe.
model = self._apply_fp8_layerwise_casting(model, config, SubModelType.Transformer)
return model