template.py 8.76 KB
Newer Older
Gustaf Ahdritz's avatar
Gustaf Ahdritz 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
302
303
304
305
306
307
308
309
310
# Copyright 2021 AlQuraishi Laboratory
# Copyright 2021 DeepMind Technologies Limited
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from functools import partial
import math
import torch
import torch.nn as nn

from alphafold.model.primitives import Linear, Attention
from alphafold.utils.deepspeed import checkpoint_blocks
from alphafold.model.dropout import  (
    DropoutRowwise,
    DropoutColumnwise,
)
from alphafold.model.pair_transition import PairTransition
from alphafold.model.triangular_attention import (
    TriangleAttentionStartingNode,
    TriangleAttentionEndingNode,
)
from alphafold.model.triangular_multiplicative_update import (
    TriangleMultiplicationOutgoing,
    TriangleMultiplicationIncoming,
)
from alphafold.utils.tensor_utils import (
    chunk_layer,
    permute_final_dims, 
    flatten_final_dims,
)


class TemplatePointwiseAttention(nn.Module):
    """
        Implements Algorithm 17.
    """
    def __init__(self, 
        c_t, 
        c_z, 
        c_hidden, 
        no_heads, 
        chunk_size,
        **kwargs
    ):
        """
            Args:
                c_t:
                    Template embedding channel dimension
                c_z:
                    Pair embedding channel dimension
                c_hidden:
                    Hidden channel dimension 
        """
        super(TemplatePointwiseAttention, self).__init__()
        
        self.c_t = c_t
        self.c_z = c_z
        self.c_hidden = c_hidden
        self.no_heads = no_heads
        self.chunk_size = chunk_size

        self.mha = Attention(
            self.c_z, self.c_t, self.c_t, 
            self.c_hidden, self.no_heads,
            gating=False,
        )

    def forward(self, t, z, template_mask=None):
        """
            Args:
                t:
                    [*, N_templ, N_res, N_res, C_t] template embedding
                z:
                    [*, N_res, N_res, C_t] pair embedding
                template_mask:
                    [*, N_templ] template mask
            Returns:
                [*, N_res, N_res, C_z] pair embedding update
        """
        if(template_mask is None):
            # NOTE: This is not the "template_mask" from the supplement, but a
            # [*, N_templ] mask from the code. I'm pretty sure it's always just 1,
            # but not sure enough to remove it. It's nice to have, I guess.
            template_mask = torch.ones(t.shape[:-3], device=t.device)
        
        bias = (1e9 * (template_mask[..., None, None, None, None, :] - 1))

        # [*, N_res, N_res, 1, C_z]
        z = z.unsqueeze(-2)

        # [*, N_res, N_res, N_temp, C_t]
        t = permute_final_dims(t, 1, 2, 0, 3)

        # [*, N_res, N_res, 1, C_z]
        mha_inputs = {
            "q_x": z,
            "k_x": t,
            "v_x": t,
            "biases": [bias],
        }
        if(not self.training and self.chunk_size is not None):
            z = chunk_layer(
                self.mha,
                mha_inputs,
                chunk_size=self.chunk_size,
                no_batch_dims=len(z.shape[:-2])
            )
        else:
            z = self.mha(**mha_inputs)

        # [*, N_res, N_res, C_z]
        z = z.squeeze(-2)
         
        return z


class TemplatePairStackBlock(nn.Module):
    def __init__(self, 
        c_t, 
        c_hidden_tri_att,
        c_hidden_tri_mul,
        no_heads, 
        pair_transition_n, 
        dropout_rate,
        chunk_size,
    ):
        super(TemplatePairStackBlock, self).__init__()
        
        self.c_t = c_t
        self.c_hidden_tri_att = c_hidden_tri_att
        self.c_hidden_tri_mul = c_hidden_tri_mul
        self.no_heads = no_heads
        self.pair_transition_n = pair_transition_n
        self.dropout_rate = dropout_rate
        self.chunk_size = chunk_size

        self.dropout_row = DropoutRowwise(self.dropout_rate)
        self.dropout_col = DropoutColumnwise(self.dropout_rate)
        
        self.tri_att_start = TriangleAttentionStartingNode(
            self.c_t, 
            self.c_hidden_tri_att, 
            self.no_heads, 
            chunk_size=chunk_size,
        )
        self.tri_att_end = TriangleAttentionEndingNode(
            self.c_t,
            self.c_hidden_tri_att,
            self.no_heads,
            chunk_size=chunk_size,
        )

        self.tri_mul_out = TriangleMultiplicationOutgoing(
            self.c_t,
            self.c_hidden_tri_mul,
        )
        self.tri_mul_in = TriangleMultiplicationIncoming(
            self.c_t,
            self.c_hidden_tri_mul,
        )

        self.pair_transition = PairTransition(
            self.c_t,
            self.pair_transition_n,
            chunk_size=chunk_size,
        )

    def forward(self, z, mask, _mask_trans=True):
        z = z + self.dropout_row(self.tri_att_start(z, mask=mask))
        z = z + self.dropout_col(self.tri_att_end(z, mask=mask))
        z = z + self.dropout_row(self.tri_mul_out(z, mask=mask))
        z = z + self.dropout_row(self.tri_mul_in(z, mask=mask))
        z = z + self.pair_transition(z, mask=mask if _mask_trans else None)
        
        return z



class TemplatePairStack(nn.Module):
    """
        Implements Algorithm 16.
    """
    def __init__(self, 
        c_t, 
        c_hidden_tri_att,
        c_hidden_tri_mul,
        no_blocks, 
        no_heads, 
        pair_transition_n, 
        dropout_rate,
        blocks_per_ckpt,
        chunk_size,
        **kwargs,
    ):
        """
            Args:
                c_t:
                    Template embedding channel dimension
                c_hidden_tri_att:
                    Per-head hidden dimension for triangular attention
                c_hidden_tri_att:
                    Hidden dimension for triangular multiplication
                no_blocks:
                    Number of blocks in the stack
                pair_transition_n:
                    Scale of pair transition (Alg. 15) hidden dimension
                dropout_rate:
                    Dropout rate used throughout the stack
                blocks_per_ckpt:
                    Number of blocks per activation checkpoint. None disables
                    activation checkpointing
                chunk_size:
                    Size of subbatches. A higher value increases throughput at
                    the cost of memory
        """  
        super(TemplatePairStack, self).__init__()

        self.blocks_per_ckpt = blocks_per_ckpt

        self.blocks = nn.ModuleList()
        for i in range(no_blocks):
            block = TemplatePairStackBlock(
                c_t=c_t,
                c_hidden_tri_att=c_hidden_tri_att,
                c_hidden_tri_mul=c_hidden_tri_mul,
                no_heads=no_heads,
                pair_transition_n=pair_transition_n,
                dropout_rate=dropout_rate,
                chunk_size=chunk_size,
            )
            self.blocks.append(block)

        self.layer_norm = nn.LayerNorm(c_t)

    def forward(self, 
        t: torch.tensor,
        mask: torch.tensor,
        _mask_trans: bool = True,
    ):
        """
            Args:
                t:
                    [*, N_res, N_res, C_t] template embedding
                mask:
                    [*, N_res, N_res] mask
            Returns:
                [*, N_res, N_res, C_t] template embedding update
        """
        t, = checkpoint_blocks(
            blocks=[
                partial(
                    b, 
                    mask=mask, 
                    _mask_trans=_mask_trans,
                ) for b in self.blocks
            ], 
            args=(t),
            blocks_per_ckpt=self.blocks_per_ckpt,
        )

        t = self.layer_norm(t)

        return t


if __name__ == "__main__":
    template_angle_dim = 51
    c_m = 256
    batch_size = 4
    n_templ = 4
    n_res = 256

    tae = TemplateAngleEmbedder(
        template_angle_dim,
        c_m,
    )
 
    x = torch.rand((batch_size, n_templ, n_res, template_angle_dim))
    x.shape_before = x.shape
    x = tae(x)
    x.shape_after = x.shape

    assert(shape_before == shape_after)

    batch_size = 2
    s_t = 4
    c_t = 64
    c_z = 128
    c = 32
    no_heads = 3
    n = 100
    
    tpa = TemplatePointwiseAttention(c_t, c_z, c)

    t = torch.rand((batch_size, s_t, n, n, c_t))
    z = torch.rand((batch_size, n, n, c_z))

    z_update = tpa(t, z)

    assert(z_update.shape == z.shape)