| import torch |
| import torch.nn as nn |
| from torchtune.modules import RotaryPositionalEmbeddings |
| from torch.nn.attention.flex_attention import flex_attention |
| from torch.nn.attention import sdpa_kernel, SDPBackend |
|
|
| class Attention(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
| |
| self.config = config |
| |
| |
| self.W_q = nn.Linear(config.embed_dim, config.hidden_dim, bias=False) |
| self.W_k = nn.Linear(config.embed_dim, config.hidden_dim, bias=False) |
| self.W_v = nn.Linear(config.embed_dim, config.hidden_dim, bias=True) |
| self.W_o = nn.Linear(config.hidden_dim, config.embed_dim, bias=True) |
| |
| |
| d_k = config.hidden_dim // config.n_heads |
| self.rotary_embeddings = RotaryPositionalEmbeddings(d_k, max_seq_len=config.max_seq_len + 10) |
| |
| self.drop_resid = nn.Dropout(0.1) |
| |
| def forward(self, x): |
| |
| B, S, E = x.shape |
| H = self.config.n_heads |
| D_h = self.config.hidden_dim // H |
| |
| |
| q = self.W_q(x).view(B, S, H, D_h) |
| k = self.W_k(x).view(B, S, H, D_h) |
| v = self.W_v(x).view(B, S, H, D_h) |
| |
| |
| q = self.rotary_embeddings(q).transpose(1, 2).contiguous() |
| k = self.rotary_embeddings(k).transpose(1, 2).contiguous() |
| v = v.transpose(1, 2).contiguous() |
| |
| |
| def _score_mod(scores, b, h, i, j): |
| |
| |
| return scores |
|
|
| |
| |
| with sdpa_kernel(SDPBackend.FLASH_ATTENTION): |
| attn_output = flex_attention( |
| q, k, v, |
| score_mod=_score_mod, |
| scale=None, |
| ) |
| |
| |
| |
| attn_output = attn_output.transpose(1, 2).contiguous().view(B, S, self.config.hidden_dim) |
| attn_output = self.W_o(attn_output) |
| attn_output = self.drop_resid(attn_output) |
| |
| return attn_output |