-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinference.py
231 lines (198 loc) · 8.34 KB
/
inference.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
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
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from ofold.utils import rigid_utils as ru
from flowmatch.data import utils as du
from flowmatch.data import all_atom
def self_conditioning_fn(args, model, batch):
model_sc = model.forward_frame(batch)
batch["sc_ca_t"] = model_sc["rigids"][..., 4:]
sc_aa_t = model_sc["amino_acid"]
batch["sc_aa_t"] = sc_aa_t
return batch
def set_t_feats(feats, t, t_placeholder):
feats["t"] = t * t_placeholder
return feats
def inference_fn(
args,
init_feats,
gen_model,
main_network,
min_t = 0.,
max_t = 1.,
num_t = 100,
center = True,
self_condition = True,
aa_do_purity = True,
msa_do_purity = False,
ec_do_purity = False,
rot_sample_schedule = 'exp',
trans_sample_schedule = 'linear',
):
sample_feats = copy.deepcopy(init_feats)
if sample_feats["rigids_t"].ndim == 2:
t_placeholder = torch.ones((1,)).to(args.device)
else:
t_placeholder = torch.ones((sample_feats["rigids_t"].shape[0],)).to(args.device)
forward_steps = np.linspace(min_t, max_t, num_t)
all_rigids = [du.move_to_np(copy.deepcopy(sample_feats["rigids_t"]))]
all_aa = [du.move_to_np(copy.deepcopy(sample_feats["aatype_t"]))]
all_bb_atom37 = [du.move_to_np(all_atom.to_atom37(ru.Rigid.from_tensor_7(sample_feats["rigids_t"].type(torch.float32)))[0])]
if args.flow_ec:
all_ec = [du.move_to_np(copy.deepcopy(sample_feats["ec_t"]))]
t_1 = forward_steps[0]
with torch.no_grad():
for t_2 in forward_steps[1:]:
if args.embed.embed_self_conditioning and self_condition:
sample_feats["t"] = t_1 * t_placeholder
sample_feats = self_conditioning_fn(args, main_network, sample_feats)
sample_feats["t"] = t_1 * t_placeholder
dt = t_2 - t_1
model_out = main_network(sample_feats)
aa_pred = model_out["amino_acid"]
rot_pred = model_out["rigids_tensor"].get_rots().get_rot_mats()
trans_pred = model_out["rigids_tensor"].get_trans()
if args.embed.embed_self_conditioning:
sample_feats["sc_ca_t"] = model_out["rigids"][..., 4:]
sample_feats["sc_aa_t"] = model_out["amino_acid"]
if args.flow_msa:
msa_pred = model_out["msa"]
if args.flow_ec:
ec_pred = model_out["ec"]
rots_t, trans_t, rigids_t = gen_model.reverse_euler(
rigid_t=ru.Rigid.from_tensor_7(sample_feats["rigids_t"]),
rot=du.move_to_np(rot_pred),
trans=du.move_to_np(trans_pred),
flow_mask=None,
t=t_1,
dt=dt,
center=center,
center_of_mass=None,
rot_sample_schedule=rot_sample_schedule,
trans_sample_schedule=trans_sample_schedule,
)
if args.eval.discrete_purity and aa_do_purity:
aa_t = gen_model.reverse_masking_euler_purity(
feat_t=sample_feats["aatype_t"],
feat=aa_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.num_aa_type,
mask_token_idx=args.masked_aa_token_idx,
temp=args.eval.aa_temp,
noise=args.eval.aa_noise,
)
else:
aa_t = gen_model.reverse_masking_euler(
feat_t=sample_feats["aatype_t"],
feat=aa_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.num_aa_type,
mask_token_idx=args.masked_aa_token_idx,
temp=args.eval.aa_temp,
noise=args.eval.aa_noise,
)
if args.flow_msa:
if args.eval.discrete_purity and msa_do_purity:
msa_t = gen_model.reverse_masking_euler_purity(
feat_t=sample_feats["msa_t"],
feat=msa_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.msa.num_msa_vocab,
mask_token_idx=args.msa.masked_msa_token_idx,
temp=args.eval.msa_temp,
noise=args.eval.msa_noise,
)
else:
msa_t = gen_model.reverse_masking_euler(
feat_t=sample_feats["msa_t"],
feat=msa_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.msa.num_msa_vocab,
mask_token_idx=args.msa.masked_msa_token_idx,
temp=args.eval.msa_temp,
noise=args.eval.msa_noise,
)
if args.flow_ec:
if args.eval.discrete_purity and ec_do_purity:
ec_t = gen_model.reverse_masking_euler_purity(
feat_t=sample_feats["ec_t"],
feat=ec_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.ec.num_ec_class,
mask_token_idx=args.ec.masked_ec_token_idx,
temp=args.eval.ec_temp,
noise=args.eval.ec_noise,
)
else:
ec_t = gen_model.reverse_masking_euler(
feat_t=sample_feats["ec_t"],
feat=ec_pred,
flow_mask=None,
t=t_1,
dt=dt,
n_token=args.ec.num_ec_class,
mask_token_idx=args.ec.masked_ec_token_idx,
temp=args.eval.ec_temp,
noise=args.eval.ec_noise,
)
sample_feats["rigids_t"] = rigids_t.to_tensor_7().to(args.device)
sample_feats["aatype_t"] = aa_t.long().to(args.device)
if args.flow_msa:
sample_feats["msa_t"] = msa_t.long().to(args.device)
if args.flow_ec:
sample_feats["ec_t"] = ec_t.long().to(args.device)
all_aa.append(du.move_to_np(aa_t))
all_rigids.append(du.move_to_np(rigids_t.to_tensor_7()))
if args.flow_ec:
all_ec.append(du.move_to_np(ec_t))
atom37_t = all_atom.to_atom37(rigids_t)[0]
all_bb_atom37.append(du.move_to_np(atom37_t))
t_1 = t_2
t_1 = max_t
sample_feats["t"] = t_1 * t_placeholder
n_batch, n_res = sample_feats["aatype_t"].size(0), sample_feats["aatype_t"].size(1)
with torch.no_grad():
model_out = main_network(sample_feats)
aa_logits = model_out['amino_acid']
aa_logits[..., args.masked_aa_token_idx] = -1e10
aa_pred = aa_logits.argmax(-1)
rigid_pred = model_out['rigids_tensor']
atom37_pred = all_atom.to_atom37(rigid_pred)[0]
if args.flow_ec:
ec_logits = model_out['ec']
ec_logits[..., args.ec.masked_ec_token_idx] = -1e10
ec_pred = ec_logits.argmax(-1).reshape(-1, 1)
all_aa.append(du.move_to_np(aa_pred))
all_bb_atom37.append(du.move_to_np(atom37_pred))
all_rigids.append(du.move_to_np(rigid_pred.to_tensor_7()))
if args.flow_ec:
all_ec.append(du.move_to_np(ec_pred))
# Flip trajectory
flip = lambda x: np.flip(np.stack(x), (0,))
time_steps = flip(forward_steps)
all_bb_atom37 = flip(all_bb_atom37)
all_aa = flip(all_aa)
all_rigids = flip(all_rigids)
if args.flow_ec:
all_ec = flip(all_ec)
out = {
"t": time_steps,
"coord_traj": all_bb_atom37,
"aa_traj": all_aa,
"rigid_traj": all_rigids
}
if args.flow_ec:
out["ec_traj"] = all_ec
return out