MCPcopy Create free account
hub / github.com/RosettaCommons/RFdiffusion / MSAColGlobalAttention

Class MSAColGlobalAttention

rfdiffusion/Attention_module.py:282–332  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

280 return out
281
282class MSAColGlobalAttention(nn.Module):
283 def __init__(self, d_msa=64, n_head=8, d_hidden=8):
284 super(MSAColGlobalAttention, self).__init__()
285 self.norm_msa = nn.LayerNorm(d_msa)
286 #
287 self.to_q = nn.Linear(d_msa, n_head*d_hidden, bias=False)
288 self.to_k = nn.Linear(d_msa, d_hidden, bias=False)
289 self.to_v = nn.Linear(d_msa, d_hidden, bias=False)
290 self.to_g = nn.Linear(d_msa, n_head*d_hidden)
291 self.to_out = nn.Linear(n_head*d_hidden, d_msa)
292
293 self.scaling = 1/math.sqrt(d_hidden)
294 self.h = n_head
295 self.dim = d_hidden
296
297 self.reset_parameter()
298
299 def reset_parameter(self):
300 # query/key/value projection: Glorot uniform / Xavier uniform
301 nn.init.xavier_uniform_(self.to_q.weight)
302 nn.init.xavier_uniform_(self.to_k.weight)
303 nn.init.xavier_uniform_(self.to_v.weight)
304
305 # gating: zero weights, one biases (mostly open gate at the begining)
306 nn.init.zeros_(self.to_g.weight)
307 nn.init.ones_(self.to_g.bias)
308
309 # to_out: right before residual connection: zero initialize -- to make it sure residual operation is same to the Identity at the begining
310 nn.init.zeros_(self.to_out.weight)
311 nn.init.zeros_(self.to_out.bias)
312
313 def forward(self, msa):
314 B, N, L = msa.shape[:3]
315 #
316 msa = self.norm_msa(msa)
317 #
318 query = self.to_q(msa).reshape(B, N, L, self.h, self.dim)
319 query = query.mean(dim=1) # (B, L, h, dim)
320 key = self.to_k(msa) # (B, N, L, dim)
321 value = self.to_v(msa) # (B, N, L, dim)
322 gate = torch.sigmoid(self.to_g(msa)) # (B, N, L, h*dim)
323 #
324 query = query * self.scaling
325 attn = einsum('bihd,bkid->bihk', query, key) # (B, L, h, N)
326 attn = F.softmax(attn, dim=-1)
327 #
328 out = einsum('bihk,bkid->bihd', attn, value).reshape(B, 1, L, -1) # (B, 1, L, h*dim)
329 out = gate * out # (B, N, L, h*dim)
330 #
331 out = self.to_out(out)
332 return out
333
334# Instead of triangle attention, use Tied axail attention with bias from coordinates..?
335class BiasedAxialAttention(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected