-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
163 lines (133 loc) · 5.8 KB
/
utils.py
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
import torch
import os
import logging
import numpy as np
def restore_checkpoint(ckpt_dir, state, device):
if not os.path.exists(ckpt_dir):
if not os.path.exists(os.path.dirname(ckpt_dir)):
os.makedirs(os.path.dirname(ckpt_dir))
logging.warning(f"No checkpoint found at {ckpt_dir}. "
f"Returned the same state as input")
return state
else:
loaded_state = torch.load(ckpt_dir, map_location=device)
state['optimizer'].load_state_dict(loaded_state['optimizer'])
state['model'].load_state_dict(loaded_state['model'], strict=True) # change strict to False?
state['ema'].load_state_dict(loaded_state['ema'])
state['step'] = loaded_state['step']
return state
def save_checkpoint(ckpt_dir, state):
saved_state = {
'optimizer': state['optimizer'].state_dict(),
'model': state['model'].state_dict(),
'ema': state['ema'].state_dict(),
'step': state['step']
}
torch.save(saved_state, ckpt_dir)
def get_data_scaler(config):
"""Data normalizer"""
# not consider bias here
if isinstance(config.model.normalize_factors, str):
normalize_factors = config.model.normalize_factors.split(',')
normalize_factors = [int(normalize_factor) for normalize_factor in normalize_factors]
else:
normalize_factors = config.model.normalize_factors
if len(normalize_factors) == 3:
pos_norm, atom_type_norm, fc_charge_norm = normalize_factors
edge_norm = 1
else:
pos_norm, atom_type_norm, fc_charge_norm, edge_norm = normalize_factors
centered = config.data.centered
def scale_fn(pos, atom_type, fc_charge, node_mask, edge_type=None, edge_mask=None):
if centered:
atom_type = atom_type * 2. - 1.
if pos is not None:
pos = pos / pos_norm * node_mask
atom_type = atom_type / atom_type_norm * node_mask
fc_charge = fc_charge / fc_charge_norm * node_mask
if edge_type is not None:
if centered:
edge_type = edge_type * 2. - 1.
edge_type = edge_type / edge_norm
edge_type = edge_type * edge_mask.reshape(node_mask.size(0), node_mask.size(1), node_mask.size(1), 1)
return pos, atom_type, fc_charge, edge_type
return pos, atom_type, fc_charge
return scale_fn
def get_data_inverse_scaler(config):
"""Inverse data normalizer."""
# not consider bias here
if isinstance(config.model.normalize_factors, str):
normalize_factors = config.model.normalize_factors.split(',')
normalize_factors = [int(normalize_factor) for normalize_factor in normalize_factors]
else:
normalize_factors = config.model.normalize_factors
if len(normalize_factors) == 3:
pos_norm, atom_type_norm, fc_charge_norm = normalize_factors
edge_norm = 1
else:
pos_norm, atom_type_norm, fc_charge_norm, edge_norm = normalize_factors
centered = config.data.centered
def inverse_scale_fn(pos, atom_type, fc_charge, node_mask, edge_type=None, edge_mask=None):
if pos is not None:
pos = pos * pos_norm * node_mask
atom_type = atom_type * atom_type_norm
fc_charge = fc_charge * fc_charge_norm * node_mask
if centered:
atom_type = (atom_type + 1.) / 2. * node_mask
if edge_type is not None:
edge_type = edge_type * edge_norm
if centered:
edge_type = (edge_type + 1.) / 2.
edge_type = edge_type * edge_mask.reshape(node_mask.size(0), node_mask.size(1), node_mask.size(1), 1)
return pos, atom_type, fc_charge, edge_type
return pos, atom_type, fc_charge
return inverse_scale_fn
def get_self_cond_fn(config):
# To simplify: directly return
process_type = config.model.self_cond_type # 'ori', 'clamp'
compress_edge = config.data.compress_edge
atom_types = config.data.atom_types
include_fc = config.model.include_fc_charge
atom_type_scale = np.array([0., 1.])
fc_scale = np.array(config.data.fc_scale)
edge_type_scale = np.array([0., 1.])
if isinstance(config.model.normalize_factors, str):
normalize_factors = config.model.normalize_factors.split(',')
normalize_factors = [int(normalize_factor) for normalize_factor in normalize_factors]
else:
normalize_factors = config.model.normalize_factors
_, atom_type_norm, fc_norm, edge_norm = normalize_factors
# get the value scale
centered = config.data.centered
if centered:
atom_type_scale = atom_type_scale * 2. - 1.
edge_type_scale = edge_type_scale * 2. - 1.
atom_type_scale = atom_type_scale / atom_type_norm
fc_scale = fc_scale / fc_norm
edge_type_scale = edge_type_scale / edge_norm
def process_self_cond(cond_x, cond_edge_x):
if process_type == 'ori':
return cond_x, cond_edge_x
elif process_type == 'clamp':
atom_x = cond_x[:, :, 3:3+atom_types]
atom_x = atom_x.clamp(atom_type_scale[0], atom_type_scale[1])
cond_x[:, :, 3:3+atom_types] = atom_x
if include_fc:
atom_fc = cond_x[:, :, -1:]
atom_fc = atom_fc.clamp(fc_scale[0], fc_scale[1])
cond_x[:, :, -1:] = atom_fc
cond_edge_x = cond_edge_x.clamp(edge_type_scale[0], edge_type_scale[1])
return cond_x, cond_edge_x
else:
raise ValueError("Self-condition data process error.")
return process_self_cond
def expand_dims(v, dims):
"""
Expand the tensor `v` to the dim `dims`.
Args:
`v`: a PyTorch tensor with shape [N].
`dim`: a `int`.
Returns:
a PyTorch tensor with shape [N, 1, 1, ..., 1] and the total dimension is `dims`.
"""
return v[(...,) + (None,) * (dims - 1)]