Download load_control_branch.py from cornerbench-anon-2027/CornerFactory: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/cornerbench-anon-2027/CornerFactory/resolve/main/load_control_branch.py
- Command line
-
hf download hf://cornerbench-anon-2027/CornerFactory/load_control_branch.py
-
curl -L -o load_control_branch.py https://huggingface.co/cornerbench-anon-2027/CornerFactory/resolve/main/load_control_branch.py
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 | |