convert_model_modality.py 10.5 KB
Newer Older
zcxzcx1's avatar
zcxzcx1 committed
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import math
from typing import List

import torch
import torch.nn as nn
from e3nn.o3 import Irreps, Linear

import sevenn._keys as KEY
from sevenn.model_build import build_E3_equivariant_model

modal_module_dict = {
    KEY.USE_MODAL_NODE_EMBEDDING: 'onehot_to_feature_x',
    KEY.USE_MODAL_SELF_INTER_INTRO: 'self_interaction_1',
    KEY.USE_MODAL_SELF_INTER_OUTRO: 'self_interaction_2',
    KEY.USE_MODAL_OUTPUT_BLOCK: 'reduce_input_to_hidden',
}


def _get_scalar_index(irreps: Irreps):
    scalar_indices = []
    for idx, (_, (l, p)) in enumerate(irreps):  # noqa
        if (
            l == 0 and p == 1
        ):  # get index of parameter for scalar (0e), which is used for modality
            scalar_indices.append(idx)

    return scalar_indices


def _reshape_weight_of_linear(
    irreps_in: Irreps, irreps_out: Irreps, weight: torch.Tensor
) -> List[torch.Tensor]:
    linear = Linear(irreps_in, irreps_out)
    linear.weight = nn.Parameter(weight)
    return list(linear.weight_views())


def _erase_linear_modal_params(
    model_state_dct: dict,
    erase_modal_indices: List[int],
    key: str,
    irreps_in: Irreps,
    irreps_out: Irreps,
):
    orig_input_dim = irreps_in.count('0e')
    new_input_dim = orig_input_dim - len(erase_modal_indices)

    orig_weight = model_state_dct[key + '.linear.weight']
    scalar_idx = _get_scalar_index(irreps_in)
    linear_weight_list = _reshape_weight_of_linear(
        irreps_in, irreps_out, orig_weight
    )

    new_weight_list = []

    for idx, l_p_weight in enumerate(linear_weight_list[:-1]):
        new_weight = torch.reshape(l_p_weight, (1, -1)).squeeze()
        if idx in scalar_idx:
            new_weight = new_weight * math.sqrt(new_input_dim / orig_input_dim)

        new_weight_list.append(new_weight)

    """
    Following works for normalization = `path`, which is not used in SEVENNet
    for l_p_weight in linear_weight_list[:-1]:
        new_weight_list.append(torch.reshape(l_p_weight, (1, -1)).squeeze())
    """

    flattened_weight = torch.cat(new_weight_list)

    return flattened_weight


def _get_modal_weight_as_bias(
    model_state_dct: dict,
    key: str,
    ref_index: int,
    irreps_in: Irreps,
    irreps_out: Irreps,
):
    assert ref_index != -1
    input_dim = irreps_in.count('0e')
    output_dim = irreps_out.count('0e')
    orig_weight = model_state_dct[key + '.linear.weight']
    orig_bias = model_state_dct[key + '.linear.bias']
    if len(orig_bias) == 0:
        orig_bias = torch.zeros(output_dim, dtype=orig_weight.dtype)

    modal_weight = _reshape_weight_of_linear(
        irreps_in, irreps_out, orig_weight
    )[-1]

    new_bias = orig_bias + modal_weight[ref_index] / math.sqrt(input_dim)

    return new_bias


def _append_modal_weight(
    model_state_dct: dict,  # state dict to be targeted
    key: str,  # linear weight modune name
    irreps_in: Irreps,  # irreps_in before modality append
    irreps_out: Irreps,
    append_number: int,
):
    # This works for normalization = `element`, default in SEVENNet.
    # (normalization = `path` is curruently deprecated in SEVENNet.)
    input_dim = irreps_in.count('0e')
    output_dim = irreps_out.count('0e')
    new_input_dim = input_dim + append_number
    orig_weight = model_state_dct[key + '.linear.weight']
    scalar_idx = _get_scalar_index(irreps_in)
    linear_weight_list = _reshape_weight_of_linear(
        irreps_in, irreps_out, orig_weight
    )

    new_weight_list = []

    # TODO: combine following as function with _erase_linear_modal_params

    for idx, l_p_weight in enumerate(linear_weight_list):
        new_weight = torch.reshape(l_p_weight, (1, -1)).squeeze()
        if idx in scalar_idx:
            new_weight = new_weight * math.sqrt(new_input_dim / input_dim)

        new_weight_list.append(new_weight)

    flattened_weight_list = []
    for l_p_weight in new_weight_list:
        flattened_weight_list.append(
            torch.reshape(l_p_weight, (1, -1)).squeeze()
        )
    flattened_weight = torch.cat(flattened_weight_list)

    append_weight = torch.cat([
        flattened_weight,
        torch.zeros(append_number * output_dim, dtype=flattened_weight.dtype),
    ])  # zeros: starting from common model

    return append_weight


def get_single_modal_model_dct(
    model_state_dct: dict,
    config: dict,
    ref_modal: str,
    from_processing_cp: bool = False,
    is_deploy: bool = False,
):
    """
    Convert multimodal model state dictionary to single modal model.
    Modal is selected by `ref_modal`

    `model_state_dct`: model state dictionary from multimodal checkpoint file
    `config`: dictionary containing configuration of the checkpoint model
    `ref_modal`: modal that are going to be converted
    `from_processing_cp`: if True, use modal_map of the checkpoint file
    `is_deploy`: if True, model is build with single-modal shift and scale
    """
    if (
        not from_processing_cp and not config[KEY.USE_MODALITY]
    ):  # model is already single modal
        return model_state_dct

    config[KEY.USE_BIAS_IN_LINEAR] = True
    config['_deploy'] = is_deploy

    model = build_E3_equivariant_model(config)
    del config['_deploy']
    key_add = '_cp' if from_processing_cp else ''
    modal_type_dict = config[KEY.MODAL_MAP + key_add]
    erase_modal_indices = range(len(modal_type_dict.keys()))  # starts with 0

    if ref_modal != 'common':
        try:
            ref_modal_index = modal_type_dict[ref_modal]
        except:
            raise KeyError(
                f'{ref_modal} not in modal type. Use one of'
                f' {modal_type_dict.keys()}.'
            )

    for module_key in model._modules.keys():
        for (
            use_modal_module_key,
            modal_module_name,
        ) in modal_module_dict.items():
            irreps_out = Irreps(model.get_irreps_in(module_key, 'irreps_out'))
            # TODO: directly using "irreps_in" might not be compatible
            # when changing `nn/linear.py`
            output_dim = irreps_out.count('0e')
            if (
                config[use_modal_module_key]
                and modal_module_name in module_key
            ):  # this module is used for giving modality

                irreps_in = Irreps(
                    model.get_irreps_in(module_key, 'irreps_in')
                )

                new_bias = (
                    torch.zeros(output_dim)
                    if ref_modal == 'common'
                    else _get_modal_weight_as_bias(
                        model_state_dct,
                        module_key,
                        ref_modal_index,
                        irreps_in,  # type: ignore
                        irreps_out,  # type: ignore
                    )
                )
                erased_modal_weight = _erase_linear_modal_params(
                    model_state_dct,
                    erase_modal_indices,
                    module_key,
                    irreps_in,  # type: ignore
                    irreps_out,  # type: ignore
                )

                model_state_dct[module_key + '.linear.weight'] = (
                    erased_modal_weight
                )
                model_state_dct[module_key + '.linear.bias'] = new_bias
            elif modal_module_name in module_key:
                model_state_dct[module_key + '.linear.bias'] = torch.zeros(
                    output_dim,
                    dtype=model_state_dct[module_key + '.linear.weight'].dtype,
                )

    final_block_key = 'reduce_hidden_to_energy'
    model_state_dct[final_block_key + '.linear.bias'] = torch.tensor(
        [0], dtype=model_state_dct[final_block_key + '.linear.weight'].dtype
    )

    if config[KEY.USE_MODAL_WISE_SHIFT] or config[KEY.USE_MODAL_WISE_SHIFT]:
        rescaler_names = []
        if config[KEY.USE_MODAL_WISE_SHIFT]:
            rescaler_names.append('shift')
        if config[KEY.USE_MODAL_WISE_SCALE]:
            rescaler_names.append('scale')
        config[KEY.USE_MODAL_WISE_SHIFT] = False
        config[KEY.USE_MODAL_WISE_SCALE] = False
        for rescaler_name in rescaler_names:
            rescaler_key = 'rescale_atomic_energy.' + rescaler_name
            rescaler = model_state_dct[rescaler_key][ref_modal_index]
            model_state_dct.update({rescaler_key: rescaler})
            config.update({rescaler_name: rescaler})

    config[KEY.USE_MODALITY] = False

    return model_state_dct


def append_modality_to_model_dct(
    model_state_dct: dict,
    config: dict,
    orig_num_modal: int,
    append_modal_length: int,
):
    """
    Append modal-wise parameters to the original linear layers.
    This enables expanding modal to single/multi modal model checkpoint.

    `model_state_dct`: model state dictionary from multimodal checkpoint file
    `config`: dictionary containing configuration of the checkpoint model
            + modality appended
    `orig_num_modal`: Number of modality used in original checkpoint
    `append_modal_length`: Number of modality to be appended in new checkpoint.
    """
    config_num_modal = config[KEY.NUM_MODALITIES]
    config.update({KEY.NUM_MODALITIES: orig_num_modal, KEY.USE_MODALITY: True})

    model = build_E3_equivariant_model(config)

    for module_key in model._modules.keys():
        for (
            use_modal_module_key,
            modal_module_name,
        ) in modal_module_dict.items():
            if (
                config[use_modal_module_key]
                and modal_module_name in module_key
            ):  # this module is used for giving modality
                irreps_in = model.get_irreps_in(
                    module_key, 'irreps_in'
                )
                # TODO: directly using "irreps_in" might not be compatible
                # when changing `nn/linear.py`
                irreps_out = model.get_irreps_in(module_key, 'irreps_out')
                irreps_in, irreps_out = Irreps(irreps_in), Irreps(irreps_out)

                append_weight = _append_modal_weight(
                    model_state_dct,
                    module_key,
                    irreps_in,  # type: ignore
                    irreps_out,  # type: ignore
                    append_modal_length,
                )
                model_state_dct[module_key + '.linear.weight'] = append_weight
    config[KEY.NUM_MODALITIES] = config_num_modal

    return model_state_dct