|
| 1 | +import torch |
| 2 | +from einops import rearrange |
| 3 | +from torch import nn |
| 4 | + |
| 5 | + |
| 6 | +class LocalMHA(nn.Module): |
| 7 | + def __init__(self, dim=1024, window_size=32, dim_head=64, use_rotary_pos_emb=True): |
| 8 | + super().__init__() |
| 9 | + self.norm = nn.LayerNorm(dim) |
| 10 | + self.heads = dim // dim_head |
| 11 | + self.window_size = window_size |
| 12 | + self.to_qkv = nn.Linear(dim, dim * 3, bias=False) |
| 13 | + if use_rotary_pos_emb: |
| 14 | + self.rel_pos = SinusoidalEmbeddings(dim_head, scale_base=window_size // 2) |
| 15 | + else: |
| 16 | + self.rel_pos = None |
| 17 | + self.to_out = nn.Linear(dim, dim, bias=False) |
| 18 | + |
| 19 | + def forward(self, x): |
| 20 | + B, C, T = x.shape |
| 21 | + residual = x |
| 22 | + x = self.norm(x.transpose(1, 2)) |
| 23 | + windows = T // self.window_size |
| 24 | + q, k, v = self.to_qkv(x).chunk(3, dim=-1) |
| 25 | + q, k, v = map(lambda t: rearrange(t, "b (w n) (h d) -> b h w n d", w=windows, h=self.heads), (q, k, v)) |
| 26 | + if self.rel_pos is not None: |
| 27 | + pos_emb, scale = self.rel_pos(k) |
| 28 | + q, k = apply_rotary_pos_emb(q, k, pos_emb, scale) |
| 29 | + out = torch.nn.functional.scaled_dot_product_attention(q, k, v) |
| 30 | + out = rearrange(out, "b h w n d -> b (w n) (h d)") |
| 31 | + out = self.to_out(out) |
| 32 | + return out.transpose(1, 2) + residual |
| 33 | + |
| 34 | + |
| 35 | +class SinusoidalEmbeddings(nn.Module): |
| 36 | + def __init__(self, dim, scale_base=None, use_xpos=False): |
| 37 | + super().__init__() |
| 38 | + inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim)) |
| 39 | + self.register_buffer("inv_freq", inv_freq) |
| 40 | + # xpos related |
| 41 | + self.use_xpos = use_xpos |
| 42 | + self.scale_base = scale_base |
| 43 | + assert not (use_xpos and scale_base is None), "scale base must be defined if using xpos" |
| 44 | + scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim) |
| 45 | + self.register_buffer("scale", scale, persistent=False) |
| 46 | + |
| 47 | + def forward(self, x): |
| 48 | + seq_len, device = x.shape[-2], x.device |
| 49 | + t = torch.arange(seq_len, device=x.device).type_as(self.inv_freq) |
| 50 | + freqs = torch.einsum("i , j -> i j", t, self.inv_freq) |
| 51 | + freqs = torch.cat((freqs, freqs), dim=-1) |
| 52 | + if not self.use_xpos: |
| 53 | + return freqs, torch.ones(1, device=device) |
| 54 | + power = (t - (seq_len // 2)) / self.scale_base |
| 55 | + scale = self.scale ** rearrange(power, "n -> n 1") |
| 56 | + scale = torch.cat((scale, scale), dim=-1) |
| 57 | + |
| 58 | + return freqs, scale |
| 59 | + |
| 60 | + |
| 61 | +def rotate_half(x): |
| 62 | + x = rearrange(x, "b ... (r d) -> b ... r d", r=2) |
| 63 | + x1, x2 = x.unbind(dim=-2) |
| 64 | + return torch.cat((-x2, x1), dim=-1) |
| 65 | + |
| 66 | + |
| 67 | +def apply_rotary_pos_emb(q, k, freqs, scale=1): |
| 68 | + q_len = q.shape[-2] |
| 69 | + q_freqs = freqs[..., -q_len:, :] |
| 70 | + inv_scale = scale**-1 |
| 71 | + if scale.ndim == 2: |
| 72 | + scale = scale[-q_len:, :] |
| 73 | + q = (q * q_freqs.cos() * scale) + (rotate_half(q) * q_freqs.sin() * scale) |
| 74 | + k = (k * freqs.cos() * inv_scale) + (rotate_half(k) * freqs.sin() * inv_scale) |
| 75 | + return q, k |
0 commit comments