lhallee commited on
Commit
5433a4f
·
verified ·
1 Parent(s): 989ec3c

Upload vb_modules_diffusion_conditioning.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. vb_modules_diffusion_conditioning.py +116 -116
vb_modules_diffusion_conditioning.py CHANGED
@@ -1,116 +1,116 @@
1
- from __future__ import annotations
2
-
3
- import torch
4
- from torch import nn
5
- from torch.nn import Module
6
-
7
- from .vb_modules_encodersv2 import (
8
- AtomEncoder,
9
- PairwiseConditioning,
10
- )
11
-
12
-
13
- class DiffusionConditioning(Module):
14
- def __init__(
15
- self,
16
- token_s: int,
17
- token_z: int,
18
- atom_s: int,
19
- atom_z: int,
20
- atoms_per_window_queries: int = 32,
21
- atoms_per_window_keys: int = 128,
22
- atom_encoder_depth: int = 3,
23
- atom_encoder_heads: int = 4,
24
- token_transformer_depth: int = 24,
25
- token_transformer_heads: int = 8,
26
- atom_decoder_depth: int = 3,
27
- atom_decoder_heads: int = 4,
28
- atom_feature_dim: int = 128,
29
- conditioning_transition_layers: int = 2,
30
- use_no_atom_char: bool = False,
31
- use_atom_backbone_feat: bool = False,
32
- use_residue_feats_atoms: bool = False,
33
- ) -> None:
34
- super().__init__()
35
-
36
- self.pairwise_conditioner = PairwiseConditioning(
37
- token_z=token_z,
38
- dim_token_rel_pos_feats=token_z,
39
- num_transitions=conditioning_transition_layers,
40
- )
41
-
42
- self.atom_encoder = AtomEncoder(
43
- atom_s=atom_s,
44
- atom_z=atom_z,
45
- token_s=token_s,
46
- token_z=token_z,
47
- atoms_per_window_queries=atoms_per_window_queries,
48
- atoms_per_window_keys=atoms_per_window_keys,
49
- atom_feature_dim=atom_feature_dim,
50
- structure_prediction=True,
51
- use_no_atom_char=use_no_atom_char,
52
- use_atom_backbone_feat=use_atom_backbone_feat,
53
- use_residue_feats_atoms=use_residue_feats_atoms,
54
- )
55
-
56
- self.atom_enc_proj_z = nn.ModuleList()
57
- for _ in range(atom_encoder_depth):
58
- self.atom_enc_proj_z.append(
59
- nn.Sequential(
60
- nn.LayerNorm(atom_z),
61
- nn.Linear(atom_z, atom_encoder_heads, bias=False),
62
- )
63
- )
64
-
65
- self.atom_dec_proj_z = nn.ModuleList()
66
- for _ in range(atom_decoder_depth):
67
- self.atom_dec_proj_z.append(
68
- nn.Sequential(
69
- nn.LayerNorm(atom_z),
70
- nn.Linear(atom_z, atom_decoder_heads, bias=False),
71
- )
72
- )
73
-
74
- self.token_trans_proj_z = nn.ModuleList()
75
- for _ in range(token_transformer_depth):
76
- self.token_trans_proj_z.append(
77
- nn.Sequential(
78
- nn.LayerNorm(token_z),
79
- nn.Linear(token_z, token_transformer_heads, bias=False),
80
- )
81
- )
82
-
83
- def forward(
84
- self,
85
- s_trunk, # Float['b n ts']
86
- z_trunk, # Float['b n n tz']
87
- relative_position_encoding, # Float['b n n tz']
88
- feats,
89
- ):
90
- z = self.pairwise_conditioner(
91
- z_trunk,
92
- relative_position_encoding,
93
- )
94
-
95
- q, c, p, to_keys = self.atom_encoder(
96
- feats=feats,
97
- s_trunk=s_trunk, # Float['b n ts'],
98
- z=z, # Float['b n n tz'],
99
- )
100
-
101
- atom_enc_bias = []
102
- for layer in self.atom_enc_proj_z:
103
- atom_enc_bias.append(layer(p))
104
- atom_enc_bias = torch.cat(atom_enc_bias, dim=-1)
105
-
106
- atom_dec_bias = []
107
- for layer in self.atom_dec_proj_z:
108
- atom_dec_bias.append(layer(p))
109
- atom_dec_bias = torch.cat(atom_dec_bias, dim=-1)
110
-
111
- token_trans_bias = []
112
- for layer in self.token_trans_proj_z:
113
- token_trans_bias.append(layer(z))
114
- token_trans_bias = torch.cat(token_trans_bias, dim=-1)
115
-
116
- return q, c, to_keys, atom_enc_bias, atom_dec_bias, token_trans_bias
 
1
+ from __future__ import annotations
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import Module
6
+
7
+ from .vb_modules_encodersv2 import (
8
+ AtomEncoder,
9
+ PairwiseConditioning,
10
+ )
11
+
12
+
13
+ class DiffusionConditioning(Module):
14
+ def __init__(
15
+ self,
16
+ token_s: int,
17
+ token_z: int,
18
+ atom_s: int,
19
+ atom_z: int,
20
+ atoms_per_window_queries: int = 32,
21
+ atoms_per_window_keys: int = 128,
22
+ atom_encoder_depth: int = 3,
23
+ atom_encoder_heads: int = 4,
24
+ token_transformer_depth: int = 24,
25
+ token_transformer_heads: int = 8,
26
+ atom_decoder_depth: int = 3,
27
+ atom_decoder_heads: int = 4,
28
+ atom_feature_dim: int = 128,
29
+ conditioning_transition_layers: int = 2,
30
+ use_no_atom_char: bool = False,
31
+ use_atom_backbone_feat: bool = False,
32
+ use_residue_feats_atoms: bool = False,
33
+ ) -> None:
34
+ super().__init__()
35
+
36
+ self.pairwise_conditioner = PairwiseConditioning(
37
+ token_z=token_z,
38
+ dim_token_rel_pos_feats=token_z,
39
+ num_transitions=conditioning_transition_layers,
40
+ )
41
+
42
+ self.atom_encoder = AtomEncoder(
43
+ atom_s=atom_s,
44
+ atom_z=atom_z,
45
+ token_s=token_s,
46
+ token_z=token_z,
47
+ atoms_per_window_queries=atoms_per_window_queries,
48
+ atoms_per_window_keys=atoms_per_window_keys,
49
+ atom_feature_dim=atom_feature_dim,
50
+ structure_prediction=True,
51
+ use_no_atom_char=use_no_atom_char,
52
+ use_atom_backbone_feat=use_atom_backbone_feat,
53
+ use_residue_feats_atoms=use_residue_feats_atoms,
54
+ )
55
+
56
+ self.atom_enc_proj_z = nn.ModuleList()
57
+ for _ in range(atom_encoder_depth):
58
+ self.atom_enc_proj_z.append(
59
+ nn.Sequential(
60
+ nn.LayerNorm(atom_z),
61
+ nn.Linear(atom_z, atom_encoder_heads, bias=False),
62
+ )
63
+ )
64
+
65
+ self.atom_dec_proj_z = nn.ModuleList()
66
+ for _ in range(atom_decoder_depth):
67
+ self.atom_dec_proj_z.append(
68
+ nn.Sequential(
69
+ nn.LayerNorm(atom_z),
70
+ nn.Linear(atom_z, atom_decoder_heads, bias=False),
71
+ )
72
+ )
73
+
74
+ self.token_trans_proj_z = nn.ModuleList()
75
+ for _ in range(token_transformer_depth):
76
+ self.token_trans_proj_z.append(
77
+ nn.Sequential(
78
+ nn.LayerNorm(token_z),
79
+ nn.Linear(token_z, token_transformer_heads, bias=False),
80
+ )
81
+ )
82
+
83
+ def forward(
84
+ self,
85
+ s_trunk, # Float['b n ts']
86
+ z_trunk, # Float['b n n tz']
87
+ relative_position_encoding, # Float['b n n tz']
88
+ feats,
89
+ ):
90
+ z = self.pairwise_conditioner(
91
+ z_trunk,
92
+ relative_position_encoding,
93
+ )
94
+
95
+ q, c, p, to_keys = self.atom_encoder(
96
+ feats=feats,
97
+ s_trunk=s_trunk, # Float['b n ts'],
98
+ z=z, # Float['b n n tz'],
99
+ )
100
+
101
+ atom_enc_bias = []
102
+ for layer in self.atom_enc_proj_z:
103
+ atom_enc_bias.append(layer(p))
104
+ atom_enc_bias = torch.cat(atom_enc_bias, dim=-1)
105
+
106
+ atom_dec_bias = []
107
+ for layer in self.atom_dec_proj_z:
108
+ atom_dec_bias.append(layer(p))
109
+ atom_dec_bias = torch.cat(atom_dec_bias, dim=-1)
110
+
111
+ token_trans_bias = []
112
+ for layer in self.token_trans_proj_z:
113
+ token_trans_bias.append(layer(z))
114
+ token_trans_bias = torch.cat(token_trans_bias, dim=-1)
115
+
116
+ return q, c, to_keys, atom_enc_bias, atom_dec_bias, token_trans_bias