CornerFactory / load_control_branch.py
cornerbench-anon-2027's picture
Add anonymous 720p control weights
815630d
Raw History Blame Contribute Delete
1.24 kB
"""Load one released controller into an already-initialized compatible module."""
from pathlib import Path
import torch
from safetensors.torch import load_file
def load_control_branch(control_branch, checkpoint: str | Path):
state = load_file(str(checkpoint), device="cpu")
prefix = "control_branch."
if not state or not all(key.startswith(prefix) for key in state):
raise ValueError("Expected a CornerFactory control-branch checkpoint.")
state = {key[len(prefix):]: value for key, value in state.items()}
expected = control_branch.state_dict()
missing = sorted(set(expected) - set(state))
unexpected = sorted(set(state) - set(expected))
mismatched = sorted(key for key in set(expected) & set(state)
if expected[key].shape != state[key].shape)
if missing or unexpected or mismatched:
raise ValueError(f"Incompatible controller: missing={missing}, "
f"unexpected={unexpected}, shape_mismatch={mismatched}")
if not all(torch.isfinite(value).all().item() for value in state.values()):
raise ValueError("The checkpoint contains a non-finite tensor.")
control_branch.load_state_dict(state, strict=True)
return control_branch