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/pid-1.5

Alexander Eichhorn8e37d460d681
diff --cc invokeai/backend/pid/decode.py
index 21bc315530,55d7fa94ca..19ec1649e0
--- a/invokeai/backend/pid/decode.py
+++ b/invokeai/backend/pid/decode.py
@@@ -31,15 -30,8 +31,16 @@@ from torch import Tenso
from invokeai.backend.model_manager.taxonomy import BaseModelType
from invokeai.backend.pid._src.networks.pid_net import PidNet
+from invokeai.backend.pid.state_dict_utils import PID_VERSION_BY_LQ_HIDDEN_DIM, PiDVersion
++from invokeai.backend.quantization.dequantizing_linear import peak_dequant_transient_bytes
+from invokeai.backend.quantization.int8_convrot import (
+ extract_int8_convrot_markers,
- peak_int8_dequant_transient_bytes,
+ reject_unmarked_int8_weights,
+ split_int8_convrot_layers,
+ swap_in_int8_linears,
+)
from invokeai.backend.util.logging import InvokeAILogger
+ from invokeai.backend.util.state_dict_loading import log_unexpected_keys
_PID_ACTIVATION_CHUNK_SIZE = 1024
@@@ -186,26 -161,8 +187,26 @@@ def estimate_pid_decode_working_memory
latent: Tensor,
backbone: BaseModelType,
pid_memory_optimization: bool = False,
+ pid_net: torch.nn.Module | None = None,
) -> int:
- """Estimate the working memory in bytes for a PiD decode of *latent*.
+ """Estimate the working memory in bytes for a PiD decode of *latent* by *pid_net*.
+
+ The activations, plus — for an `int8_tensorwise` decoder — the weight its largest int8 Linear dequantizes per
+ forward, which the model's resident size does not cover. That weight materializes in the input's dtype, float32
+ at most. Returns 0 for unsupported backbones so callers fall back to the cache's default reservation.
+ """
+ activations = _estimate_pid_activation_memory(latent, backbone, pid_memory_optimization)
+ if activations == 0 or pid_net is None:
+ return activations
- return activations + peak_int8_dequant_transient_bytes(pid_net, torch.float32)
++ return activations + peak_dequant_transient_bytes(pid_net, torch.float32)
+
+
+def _estimate_pid_activation_memory(
+ latent: Tensor,
+ backbone: BaseModelType,
+ pid_memory_optimization: bool = False,
+) -> int:
+ """Estimate the activation working memory in bytes for a PiD decode of *latent*.
Each decoded image is ``latent_spatial * sr_scale * latent_spatial_down_factor`` pixels per side. PidNet
runs in float32 (see ``model_loaders/pid_decoder.py``), so the element size is 4 bytes. The per-pixel term
@@@ -331,27 -269,13 +332,24 @@@ def load_pid_decoder(state_dict: dict[A
+ (f" (+ {len(not_strings) - 5} more)" if len(not_strings) > 5 else "")
)
+ # Comfy-Org's `int8_tensorwise` builds keep their marked Linears int8. Each becomes an `Int8ConvrotLinear`
+ # whose `weight`/`weight_scale` buffers the load below fills with the stored tensors, so the contract's keys
+ # still match; the markers are taken out of *state_dict*. The dense tensors reach the float32 parameters
+ # through `load_state_dict`'s own copy.
+ int8_markers = extract_int8_convrot_markers(state_dict)
+ reject_unmarked_int8_weights(state_dict, int8_markers, "PiD")
+ if int8_markers:
+ swap_in_int8_linears(
+ net, state_dict, split_int8_convrot_layers(state_dict, int8_markers, torch.float32, model=net)
+ )
+
- # strict=False so we can report missing and unexpected keys separately; both are fatal. The model
- # cache builds loaders under `skip_torch_weight_init()`, which no-ops every `reset_parameters()`,
- # so a key the checkpoint does not supply is left as uninitialised memory rather than a sane
- # default — a partial checkpoint would decode to garbage / NaNs instead of failing.
+ # strict=False so we can report missing and unexpected keys separately. Missing keys are fatal:
+ # the model cache builds loaders under `skip_torch_weight_init()`, which no-ops every
+ # `reset_parameters()`, so a key the checkpoint does not supply is left as uninitialised memory
+ # rather than a sane default — a partial checkpoint would decode to garbage / NaNs instead of
+ # failing. Unexpected keys are exporter noise and are only logged (see `log_unexpected_keys`).
missing, unexpected = net.load_state_dict(state_dict, strict=False)
- if unexpected:
- raise RuntimeError(
- f"PiD checkpoint has unexpected keys not present in PidNet: {unexpected[:5]}"
- + (f" (+ {len(unexpected) - 5} more)" if len(unexpected) > 5 else "")
- )
+ log_unexpected_keys("PiD checkpoint", unexpected)
if missing:
lq = [k for k in missing if k.startswith("lq_proj.")]
detail = (
diff --cc tests/backend/model_manager/configs/test_pid_decoder_config.py
index f5d6b3f847,e1b69f3930..f8115572f9
--- a/tests/backend/model_manager/configs/test_pid_decoder_config.py
+++ b/tests/backend/model_manager/configs/test_pid_decoder_config.py
@@@ -511,12 -345,11 +528,12 @@@ class TestUnusableCheckpointIsNeverRegi
[
("_partial", "missing 456 of the weights"),
("_missing_backbone_weight", "missing 1 of the weights required by PidNet"),
- ("_truncated_v1_5", "lq_proj hidden dim 1024"),
- ("_intact_v1_5", "lq_proj hidden dim 1024"),
+ ("_truncated_v1_5", "missing 1 of the weights required by PidNet"),
+ ("_unknown_architecture", "lq_proj hidden dim 768"),
+ ("_marker_beside_a_float_weight", "quantizes 1 layer(s) other than as int8"),
("_unsupported_latent_channels", "32 latent channels"),
("_malformed_discriminator", "malformed lq_proj.latent_proj.0.weight"),
- ("_bare_with_a_non_string_key", "2 keys PidNet does not expect"),
+ ("_bare_with_a_non_string_key", "1 keys that are not strings"),
],
)
def test_factory_returns_no_config_even_with_allow_unknown(self, case: str, expected_reason: str) -> None: