File size: 2,396 Bytes
d2c5d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39e5699
 
 
 
 
 
 
 
d2c5d46
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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