-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure_block.py
More file actions
304 lines (253 loc) · 9.75 KB
/
Copy pathstructure_block.py
File metadata and controls
304 lines (253 loc) · 9.75 KB
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
302
303
304
"""AlphaFold-style structure module built from repeated IPA updates.
This module refines the single representation into residue frames through
Invariant Point Attention, transition layers, and backbone updates. It is the
bridge between latent features and explicit 3D geometry. It also exposes the
canonical AlphaFold-style rotation stop-gradient between blocks and an optional
backbone auxiliary loss over intermediate structures.
"""
import torch
import torch.nn as nn
import math
from model.invariant_point_attention import *
from model.ipa_transformations import *
from model.structure_transition import *
from model.losses.fape_loss import FAPELoss
from model.losses.loss_helpers import build_backbone_frames
from model.msa_transitions import zero_init_linear
class StructureModule(nn.Module):
"""
AlphaFold2-style Structure Module.
Canonical AF2 behavior:
- 8 blocks with shared weights
- LayerNorm(s_initial), LayerNorm(z)
- initial linear projection of s_initial
- post-residual LayerNorm after IPA
- post-residual LayerNorm after Transition
- BackboneUpdate after transition
use_block_specific_params = False (default)
Reuses the same IPA / Transition / BackboneUpdate across all 8 blocks.
This matches the shared-weights design in AlphaFold2 Algorithm 20.
use_block_specific_params = True
Uses separate parameters per block via ModuleList.
This is a non-canonical variant kept only for experimentation.
"""
def __init__(
self,
c_s=256,
c_z=128,
num_blocks=8, # AF2 canonical: 8
ipa_heads=8, # AF2 canonical: 12
ipa_scalar_dim=32, # AF2 canonical: 16
ipa_qk_points=4, # AF2 canonical: 4
ipa_v_points=8, # AF2 canonical: 8
dropout=0.1,
trans_scale_factor=10.0,
use_block_specific_params=False, # Canonical
stop_rotation_gradients=True,
aux_fape_enabled=True,
aux_fape_length_scale=10.0,
aux_fape_clamp_distance=10.0,
aux_fape_eps=1e-12,
):
super().__init__()
self.num_blocks = num_blocks
self.trans_scale_factor = trans_scale_factor
self.use_block_specific_params = use_block_specific_params
self.stop_rotation_gradients = bool(stop_rotation_gradients)
self.aux_fape_enabled = bool(aux_fape_enabled)
self.dropout = nn.Dropout(dropout)
self.aux_fape_loss = FAPELoss(
length_scale=aux_fape_length_scale,
clamp_distance=aux_fape_clamp_distance,
eps=aux_fape_eps,
)
self.last_aux_loss = None
self.last_aux_per_block = None
# Canonical Structure Module input preprocessing
self.input_layer_norm_s = nn.LayerNorm(c_s)
self.input_layer_norm_z = nn.LayerNorm(c_z)
self.single_init_proj = nn.Linear(c_s, c_s)
# Canonical post-residual normalizations
self.post_ipa_layer_norm = nn.LayerNorm(c_s)
self.post_transition_layer_norm = nn.LayerNorm(c_s)
if self.use_block_specific_params:
# Non-canonical experimental variant
self.ipas = nn.ModuleList([
InvariantPointAttention(
c_s=c_s,
c_z=c_z,
num_heads=ipa_heads,
c_hidden=ipa_scalar_dim,
num_qk_points=ipa_qk_points,
num_v_points=ipa_v_points,
)
for _ in range(num_blocks)
])
self.transitions = nn.ModuleList([
StructureTransition(
c_s=c_s,
dropout=dropout,
)
for _ in range(num_blocks)
])
self.backbone_updates = nn.ModuleList([
BackboneUpdate(c_s=c_s)
for _ in range(num_blocks)
])
self.translation_heads = nn.ModuleList([
nn.Linear(c_s, 3)
for _ in range(num_blocks)
])
for head in self.translation_heads:
nn.init.zeros_(head.weight)
nn.init.zeros_(head.bias)
for ipa in self.ipas:
zero_init_linear(ipa.output_linear)
else:
# Canonical AF2-style shared-weights path
self.ipa = InvariantPointAttention(
c_s=c_s,
c_z=c_z,
num_heads=ipa_heads,
c_hidden=ipa_scalar_dim,
num_qk_points=ipa_qk_points,
num_v_points=ipa_v_points)
self.transition = StructureTransition(
c_s=c_s,
dropout=dropout)
self.backbone_update = BackboneUpdate(c_s=c_s)
zero_init_linear(self.ipa.output_linear)
def _compute_aux_backbone_fape(
self,
*,
R: torch.Tensor,
t: torch.Tensor,
R_true: torch.Tensor,
t_true: torch.Tensor,
coords_ca: torch.Tensor,
mask: torch.Tensor | None) -> torch.Tensor:
return self.aux_fape_loss(
R_pred=R.float(),
t_pred=t.float(),
x_pred=t.float(),
R_true=R_true.float(),
t_true=t_true.float(),
x_true=coords_ca.float(),
mask=(mask.float() if mask is not None else None))
def forward(
self,
s,
z,
mask=None,
*,
coords_n: torch.Tensor | None = None,
coords_ca: torch.Tensor | None = None,
coords_c: torch.Tensor | None = None,
backbone_mask: torch.Tensor | None = None,
return_aux: bool = False,
return_intermediates: bool = False):
"""
Args
----
s : [B, L, c_s]
z : [B, L, L, c_z]
mask : [B, L]
Optional backbone targets allow accumulation of an intermediate
auxiliary backbone FAPE loss.
Returns
-------
s : [B, L, c_s]
R : [B, L, 3, 3]
t : [B, L, 3]
"""
B, L, _ = s.shape
device, dtype = s.device, s.dtype
aux_mask = backbone_mask if backbone_mask is not None else mask
can_compute_aux = (
self.aux_fape_enabled
and coords_n is not None
and coords_ca is not None
and coords_c is not None
)
R_true = None
t_true = None
if can_compute_aux:
R_true, t_true = build_backbone_frames(
coords_n=coords_n,
coords_ca=coords_ca,
coords_c=coords_c,
mask=aux_mask,
)
# Canonical input preprocessing
s_initial = self.input_layer_norm_s(s)
z = self.input_layer_norm_z(z)
s = self.single_init_proj(s_initial)
# Identity initial frames
R = torch.eye(3, device=device, dtype=dtype).view(1, 1, 3, 3).repeat(B, L, 1, 1)
t = torch.zeros(B, L, 3, device=device, dtype=dtype)
aux_losses = []
s_intermediates = []
R_intermediates = []
t_intermediates = []
for i in range(self.num_blocks):
if self.use_block_specific_params:
ipa_update, _ = self.ipas[i](s, z, R, t, mask)
s = s + self.dropout(ipa_update)
s = self.post_ipa_layer_norm(s)
transition_update = self.transitions[i](s, mask)
s = s + self.dropout(transition_update)
s = self.post_transition_layer_norm(s)
dR, _ = self.backbone_updates[i](s, mask)
dt = self.translation_heads[i](s) * self.trans_scale_factor
if mask is not None:
dt = dt * mask.unsqueeze(-1)
else:
ipa_update, _ = self.ipa(s, z, R, t, mask)
s = s + self.dropout(ipa_update)
s = self.post_ipa_layer_norm(s)
transition_update = self.transition(s, mask)
s = s + self.dropout(transition_update)
s = self.post_transition_layer_norm(s)
dR, dt = self.backbone_update(s, mask)
if mask is not None:
dt = dt * mask.unsqueeze(-1)
R, t = compose_frames(R, t, dR, dt)
if return_intermediates:
s_intermediates.append(s)
R_intermediates.append(R)
t_intermediates.append(t)
if can_compute_aux:
aux_losses.append(
self._compute_aux_backbone_fape(
R=R,
t=t,
R_true=R_true,
t_true=t_true,
coords_ca=coords_ca,
mask=aux_mask,
)
)
if self.stop_rotation_gradients and i < (self.num_blocks - 1):
R = R.detach()
if aux_losses:
aux_per_block = torch.stack(aux_losses)
aux_loss = aux_per_block.mean()
else:
aux_per_block = torch.zeros(0, device=device, dtype=dtype)
aux_loss = torch.zeros((), device=device, dtype=dtype)
self.last_aux_per_block = aux_per_block.detach()
self.last_aux_loss = aux_loss.detach()
intermediates = None
if return_intermediates:
intermediates = {
"single": torch.stack(s_intermediates, dim=0),
"R": torch.stack(R_intermediates, dim=0),
"t": torch.stack(t_intermediates, dim=0),
}
if return_aux and return_intermediates:
return s, R, t, aux_loss, intermediates
if return_aux:
return s, R, t, aux_loss
if return_intermediates:
return s, R, t, intermediates
return s, R, t