MCPcopy Create free account
hub / github.com/geekcomputers/Python / forward

Method forward

ML/src/python/neuralforge/nn/attention.py:21–39  ·  view source on GitHub ↗
(self, x, mask=None)

Source from the content-addressed store, hash-verified

19 self.dropout = nn.Dropout(dropout)
20
21 def forward(self, x, mask=None):
22 B, N, C = x.shape
23
24 qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
25 q, k, v = qkv[0], qkv[1], qkv[2]
26
27 attn = (q @ k.transpose(-2, -1)) * self.scale
28
29 if mask is not None:
30 attn = attn.masked_fill(mask == 0, float('-inf'))
31
32 attn = F.softmax(attn, dim=-1)
33 attn = self.dropout(attn)
34
35 x = (attn @ v).transpose(1, 2).reshape(B, N, C)
36 x = self.proj(x)
37 x = self.dropout(x)
38
39 return x
40
41class CrossAttention(nn.Module):
42 def __init__(self, embed_dim, num_heads, dropout=0.1):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected