File size: 5,796 Bytes
c456c14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from __future__ import annotations

from typing import Callable, Mapping, Any

import torch
import tree
from allenact.utils.system import get_logger
from torch import nn

from .file_utils import f_join


def freeze_module(module: nn.Module | torch.Tensor) -> nn.Module:
    if torch.is_tensor(module):
        module.requires_grad = False
        return module
    for param in module.parameters():
        param.requires_grad = False
    module.eval()
    return module


def unfreeze_module(module: nn.Module | torch.Tensor) -> nn.Module:
    if torch.is_tensor(module):
        module.requires_grad = True
        return module
    for param in module.parameters():
        param.requires_grad = True
    module.train()
    return module


def freeze_bn(module: nn.Module):
    for mod in module.modules():
        if "BatchNorm" in type(mod).__name__:
            mod.momentum = 0.0
            mod.eval()
    return module


def replace_submodules(
    root_module: nn.Module,
    predicate: Callable[[nn.Module], bool],
    func: Callable[[nn.Module], nn.Module],
) -> nn.Module:
    """
    predicate: Return true if the module is to be replaced.
    func: Return new module to use.
    """
    if predicate(root_module):
        return func(root_module)

    bn_list = [
        k.split(".")
        for k, m in root_module.named_modules(remove_duplicate=True)
        if predicate(m)
    ]
    for *parent, k in bn_list:
        parent_module = root_module
        if len(parent) > 0:
            parent_module = root_module.get_submodule(".".join(parent))
        if isinstance(parent_module, nn.Sequential):
            src_module = parent_module[int(k)]
        else:
            src_module = getattr(parent_module, k)
        tgt_module = func(src_module)
        if isinstance(parent_module, nn.Sequential):
            parent_module[int(k)] = tgt_module
        else:
            setattr(parent_module, k, tgt_module)
    # verify that all BN are replaced
    bn_list = [
        k.split(".")
        for k, m in root_module.named_modules(remove_duplicate=True)
        if predicate(m)
    ]
    assert len(bn_list) == 0
    return root_module


def bn_to_gn(
    module: nn.Module,
    group_ratio: int = 16,
    device: str | int | torch.device | None = None,
):
    return replace_submodules(
        root_module=module,
        predicate=lambda x: isinstance(x, (nn.BatchNorm2d, nn.BatchNorm1d)),
        func=lambda x: nn.GroupNorm(
            num_groups=x.num_features // group_ratio,
            num_channels=x.num_features,
            device=device,
        ),
    )


def torch_load(*fpath: str, map_location="cpu") -> dict:
    fpath = str(f_join(*fpath))
    return torch.load(fpath, map_location=map_location)


def tree_value_at_path(obj, paths: Tuple):
    try:
        for p in paths:
            obj = obj[p]
        return obj
    except Exception as e:
        raise ValueError(f"{e}\n\n-- Incorrect nested path {paths} for object: {obj}.")


def implements_method(object, method: str):
    """
    Returns:
        True if object implements a method
    """
    return hasattr(object, method) and callable(getattr(object, method))


def load_state_dict(
    objects,
    states,
    strip_prefix: str | None = None,
    strict: bool = True,
    filter_prefix: list[str] | str | None = None,
    verbose: bool = True,
):
    """
    Args:
        strict: objects and states must match exactly
        strip_prefix: only match the keys that have the prefix, and strip it
    """

    def _load(paths, obj):
        if not implements_method(obj, "load_state_dict"):
            raise ValueError(
                f"Object {type(obj)} does not support load_state_dict() method"
            )
        try:
            state = tree_value_at_path(states, paths)
        except ValueError:  # paths do not exist in `states` structure
            if strict:
                raise
            else:
                return
        if strip_prefix:
            assert isinstance(strip_prefix, str)
            state = {
                k[len(strip_prefix) :]: v
                for k, v in state.items()
                if k.startswith(strip_prefix)
            }
        if filter_prefix:
            state = {
                k: v
                for k, v in state.items()
                if all(
                    not k.startswith(p)
                    for p in (
                        (filter_prefix,)
                        if isinstance(filter_prefix, str)
                        else filter_prefix
                    )
                )
            }
        if isinstance(obj, nn.Module):
            return obj.load_state_dict(state, strict=strict)
        else:
            return obj.load_state_dict(state)

    keys = tree.map_structure_with_path(_load, objects)
    if not strict and verbose:
        if keys.missing_keys:
            get_logger().debug(
                f'Missing key(s) in state_dict: {", ".join(keys.missing_keys)}'
            )
        if keys.unexpected_keys:
            get_logger().debug(
                f'Unexpected key(s) in state dict: {", ".join(keys.unexpected_keys)}'
            )
    return keys


def load_pl_state_dict(
    model: nn.Module,
    states: Mapping[str:Any] | str,
    strip_prefix: str | None = None,
    filter_prefix: list[str] | str | None = None,
    strict: bool = True,
    verbose: bool = True,
    **kwargs,
):
    if isinstance(states, str):
        states = torch_load(states, **kwargs)
    if isinstance(strip_prefix, str) and strip_prefix[-1] != ".":
        strip_prefix += "."
    return load_state_dict(
        model,
        states.get("state_dict", states),
        strip_prefix=strip_prefix,
        strict=strict,
        filter_prefix=filter_prefix,
        verbose=verbose,
    )