Spaces:
Running on Zero
Running on Zero
Download diffcve/utils.py from hugging-apps/diffcve-2607: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/spaces/hugging-apps/diffcve-2607/resolve/main/diffcve/utils.py
- Command line
-
hf download hf://spaces/hugging-apps/diffcve-2607/diffcve/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/hugging-apps/diffcve-2607/resolve/main/diffcve/utils.py
2.4 kB
| import importlib | |
| from inspect import isfunction | |
| from pathlib import Path | |
| import torch | |
| PROJECT_ROOT = Path(__file__).resolve().parents[1] | |
| def resolve_path(path): | |
| path = Path(path).expanduser() | |
| return path if path.is_absolute() else PROJECT_ROOT / path | |
| def exists(value): | |
| return value is not None | |
| def default(value, fallback): | |
| return value if exists(value) else fallback() if isfunction(fallback) else fallback | |
| def instantiate_from_config(config): | |
| module, name = config['target'].rsplit('.', 1) | |
| return getattr(importlib.import_module(module), name)(**config.get('params', {})) | |
| def load_checkpoint(model, path): | |
| path = resolve_path(path) | |
| checkpoint = torch.load(path, map_location='cpu') | |
| state = checkpoint.get('state_dict', checkpoint) | |
| expected = model.state_dict() | |
| selected = {} | |
| prefixes = { | |
| 'model.': 'denoiser.', | |
| 'priorcond_stage_model.': 'prior_condition_encoder.', | |
| 'structcond_stage_model.': 'structure_encoder.', | |
| 'vaep.': 'vae.', | |
| } | |
| for key, value in state.items(): | |
| target = key | |
| for prefix, replacement in prefixes.items(): | |
| if target.startswith(prefix): | |
| target = replacement + target[len(prefix):] | |
| break | |
| target = target.replace('.diffusion_model.', '.network.') | |
| if target not in expected: | |
| # peft >= 0.7 moved LoRA-wrapped base weights behind a `base_layer` | |
| # submodule; the released checkpoints were saved with peft 0.6.2, | |
| # where they sit directly on the wrapped module. Retry with the | |
| # `base_layer` segment inserted before the final parameter name. | |
| head, dot, param = target.rpartition('.') | |
| if dot and f'{head}.base_layer.{param}' in expected: | |
| target = f'{head}.base_layer.{param}' | |
| if target in expected: | |
| if target in selected: | |
| raise RuntimeError(f'{path}: duplicate inference tensor: {target}') | |
| selected[target] = value | |
| missing = set(expected) - selected.keys() | |
| if missing: | |
| raise RuntimeError(f'{path}: missing inference tensors: {sorted(missing)}') | |
| model.load_state_dict(selected, strict=True) | |
| print(f'Loaded {len(selected)} inference tensors from {path.name}; ' | |
| f'skipped {len(state) - len(selected)} unused checkpoint tensors.') | |
| return model | |