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 Eichhorn5a5ddaf2959c
diff --cc invokeai/backend/model_manager/load/model_loaders/anima.py
index b2dd04a035,97782b07ff..fb149c03b2
--- a/invokeai/backend/model_manager/load/model_loaders/anima.py
+++ b/invokeai/backend/model_manager/load/model_loaders/anima.py
@@@ -152,70 -157,22 +184,43 @@@ class AnimaCheckpointModel(ModelLoader)
# Drop runtime-derived buffers and exporter metadata that aren't model weights.
sd = _filter_non_model_keys(sd)
+ target_device = TorchDevice.choose_torch_device()
+ model_dtype = TorchDevice.choose_anima_inference_dtype(target_device)
+
+ # ComfyUI 'scaled fp8': an fp8 weight plus a `weight_scale`. `_filter_non_model_keys` above
+ # keeps those keys, and `load_state_dict` below rejects the checkpoint outright over them --
+ # 500 unexpected keys on a plain scaled export, 749 on one that also ships `comfy_quant`
+ # markers. Such a checkpoint therefore does not load at all today.
+ #
+ # Anima keeps `q_proj`/`k_proj`/`v_proj` separate and the only key rewrite is a prefix strip,
+ # so a sibling scale travels with its weight and nothing has to be split.
+ keep_fp8 = should_keep_fp8_weights(target_device)
+ header_hints = parse_quantization_metadata(read_safetensors_metadata(model_path, logger))
+ path_map = _strip_anima_prefix_from_layer_paths(list(header_hints))
+ layer_hints = {
+ **extract_comfy_quant_hints(sd),
+ **{path_map.get(name, name): hints for name, hints in header_hints.items()},
+ }
+ fp8_layers = extract_fp8_scaled_layers(sd, layer_hints=layer_hints)
+ if fp8_layers and not keep_fp8:
+ # Without the matmul, keeping them quantized would halve VRAM but dequantize on every
+ # forward. Fold the scale into the weight instead.
+ dequantize_fp8_scaled(sd, fp8_layers, model_dtype)
+ fp8_layers = {}
+
# Create an empty AnimaTransformer with Anima's default architecture parameters
with accelerate.init_empty_weights():
- model = AnimaTransformer(
- max_img_h=240,
- max_img_w=240,
- max_frames=1,
- in_channels=16,
- out_channels=16,
- patch_spatial=2,
- patch_temporal=1,
- concat_padding_mask=True,
- model_channels=2048,
- num_blocks=28,
- num_heads=16,
- mlp_ratio=4.0,
- crossattn_emb_channels=1024,
- pos_emb_cls="rope3d",
- # Anima reuses the Cosmos-Predict2 2B Text2Image DiT, which trains with
- # rope_scale=(t=1.0, h=4.0, w=4.0). The NTK-scaled spatial RoPE base is
- # mandatory; omitting it (theta=10000 on all axes) shifts every step's
- # velocity ~7% off and compounds into degraded images. Matches diffusers
- # CosmosTransformer3DModel rope_scale via *_extrapolation_ratio.
- rope_h_extrapolation_ratio=4.0,
- rope_w_extrapolation_ratio=4.0,
- rope_t_extrapolation_ratio=1.0,
- use_adaln_lora=True,
- adaln_lora_dim=256,
- extra_per_block_abs_pos_emb=False,
- image_model="anima",
- )
+ model = AnimaTransformer(**ANIMA_TRANSFORMER_CONFIG)
- # Determine safe dtype
- target_device = TorchDevice.choose_torch_device()
- model_dtype = TorchDevice.choose_anima_inference_dtype(target_device)
+ skip_patterns = tuple(getattr(model, "_skip_layerwise_casting_patterns", None) or ())
+ # Layers the cast would dequantize anyway are folded here, scale applied, so the cast never
+ # strips a scale that can no longer be put back.
+ fp8_layers = split_fp8_scaled_layers(sd, fp8_layers, model_dtype, model=model, skip_patterns=skip_patterns)
- # Handle memory management
- new_sd_size = sum(ten.nelement() * model_dtype.itemsize for ten in sd.values())
- self._ram_cache.make_room(new_sd_size)
-
- # Convert to target dtype (skip non-float tensors like embedding indices)
- for k in sd.keys():
- if sd[k].is_floating_point():
- sd[k] = sd[k].to(model_dtype)
+ self._ram_cache.make_room(
+ predict_cast_state_dict_size(sd, model_dtype, keep_fp8=keep_fp8, model=model, skip_patterns=skip_patterns)
+ )
+ kept = cast_state_dict(sd, model_dtype, keep_fp8=keep_fp8, model=model, skip_patterns=skip_patterns)
load_result = model.load_state_dict(sd, assign=True, strict=False)
if load_result.unexpected_keys: