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

Method forward

ML/src/python/neuralforge/nn/attention.py:55–74  ·  view source on GitHub ↗
(self, query, key, value, mask=None)

Source from the content-addressed store, hash-verified

53 self.dropout = nn.Dropout(dropout)
54
55 def forward(self, query, key, value, mask=None):
56 B, N_q, C = query.shape
57 N_k = key.shape[1]
58
59 q = self.q_proj(query).reshape(B, N_q, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
60 k = self.k_proj(key).reshape(B, N_k, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
61 v = self.v_proj(value).reshape(B, N_k, self.num_heads, self.head_dim).permute(0, 2, 1, 3)
62
63 attn = (q @ k.transpose(-2, -1)) * self.scale
64
65 if mask is not None:
66 attn = attn.masked_fill(mask == 0, float('-inf'))
67
68 attn = F.softmax(attn, dim=-1)
69 attn = self.dropout(attn)
70
71 x = (attn @ v).transpose(1, 2).reshape(B, N_q, C)
72 x = self.out_proj(x)
73
74 return x
75
76class FeedForward(nn.Module):
77 def __init__(self, embed_dim, hidden_dim, dropout=0.1, activation='gelu'):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected