Editing
Attention Mechanisms
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Implementing scaled dot-product attention from scratch:''' <syntaxhighlight lang="python"> import torch import torch.nn as nn import torch.nn.functional as F import math class MultiHeadAttention(nn.Module): def __init__(self, d_model=512, n_heads=8, dropout=0.1): super().__init__() assert d_model % n_heads == 0 self.d_k = d_model // n_heads self.n_heads = n_heads self.W_q = nn.Linear(d_model, d_model) self.W_k = nn.Linear(d_model, d_model) self.W_v = nn.Linear(d_model, d_model) self.W_o = nn.Linear(d_model, d_model) self.dropout = nn.Dropout(dropout) def split_heads(self, x): B, T, D = x.shape return x.view(B, T, self.n_heads, self.d_k).transpose(1, 2) # (B, H, T, d_k) def forward(self, query, key, value, mask=None): B, T, _ = query.shape Q = self.split_heads(self.W_q(query)) K = self.split_heads(self.W_k(key)) V = self.split_heads(self.W_v(value)) # Scaled dot-product attention scores = torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(self.d_k) if mask is not None: scores = scores.masked_fill(mask == 0, float('-inf')) attn = self.dropout(F.softmax(scores, dim=-1)) out = torch.matmul(attn, V) # (B, H, T, d_k) out = out.transpose(1, 2).contiguous().view(B, T, -1) return self.W_o(out), attn </syntaxhighlight> ; Attention variant selection guide : '''Standard self-attention''' β Transformers, BERT, GPT (seq len β€ 4096) : '''Flash Attention 2''' β Any modern transformer; same output, 2-4Γ faster, O(n) memory : '''Grouped Query Attention (GQA)''' β LLaMA 2/3, Mistral β reduces KV cache in inference : '''Sliding window attention''' β Longformer, Mistral β O(nΒ·w) complexity for long docs : '''Cross-attention''' β Encoder-decoder models (T5, Whisper, LLaVA projection) : '''Linear attention''' β Mamba alternative, sub-quadratic, trades quality for speed </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information