|
| 1 | +import torch |
| 2 | +import torch.nn.functional as F |
| 3 | +from einops import rearrange |
| 4 | +from torch import einsum, nn |
| 5 | + |
| 6 | +# normalization |
| 7 | +# they use layernorm without bias, something that pytorch does not offer |
| 8 | + |
| 9 | + |
| 10 | +class LayerNorm(nn.Module): |
| 11 | + |
| 12 | + def __init__(self, dim, eps=1e-5): |
| 13 | + super().__init__() |
| 14 | + self.eps = eps |
| 15 | + self.gamma = nn.Parameter(torch.ones(dim)) |
| 16 | + self.register_buffer("beta", torch.zeros(dim)) |
| 17 | + |
| 18 | + def forward(self, x): |
| 19 | + return F.layer_norm(x, x.shape[-1:], self.gamma, self.beta) |
| 20 | + |
| 21 | + |
| 22 | +# parallel with residual |
| 23 | +# discovered by Wang et al + EleutherAI from GPT-J fame |
| 24 | + |
| 25 | + |
| 26 | +class ParallelResidual(nn.Module): |
| 27 | + |
| 28 | + def __init__(self, *fns): |
| 29 | + super().__init__() |
| 30 | + self.fns = nn.ModuleList(fns) |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + return x + sum([fn(x) for fn in self.fns]) |
| 34 | + |
| 35 | + |
| 36 | +# rotary positional embedding |
| 37 | +# https://arxiv.org/abs/2104.09864 |
| 38 | + |
| 39 | + |
| 40 | +class RotaryEmbedding(nn.Module): |
| 41 | + |
| 42 | + def __init__(self, dim): |
| 43 | + super().__init__() |
| 44 | + inv_freq = 1.0 / (10000**(torch.arange(0, dim, 2).float() / dim)) |
| 45 | + self.register_buffer("inv_freq", inv_freq) |
| 46 | + |
| 47 | + def forward(self, max_seq_len, *, device): |
| 48 | + seq = torch.arange(max_seq_len, device=device) |
| 49 | + freqs = einsum("i , j -> i j", seq.type_as(self.inv_freq), self.inv_freq) |
| 50 | + return torch.cat((freqs, freqs), dim=-1) |
| 51 | + |
| 52 | + |
| 53 | +def rotate_half(x): |
| 54 | + x = rearrange(x, "... (j d) -> ... j d", j=2) |
| 55 | + x1, x2 = x.unbind(dim=-2) |
| 56 | + return torch.cat((-x2, x1), dim=-1) |
| 57 | + |
| 58 | + |
| 59 | +def apply_rotary_pos_emb(pos, t): |
| 60 | + return (t * pos.cos()) + (rotate_half(t) * pos.sin()) |
| 61 | + |
| 62 | + |
| 63 | +# feedforward |
| 64 | +# classic Noam Shazeer paper, except here they use SwiGLU instead of the more popular GEGLU |
| 65 | +# https://arxiv.org/abs/2002.05202 |
| 66 | + |
| 67 | + |
| 68 | +class SwiGLU(nn.Module): |
| 69 | + |
| 70 | + def forward(self, x): |
| 71 | + x, gate = x.chunk(2, dim=-1) |
| 72 | + return F.silu(gate) * x |
| 73 | + |
| 74 | + |
| 75 | +def FeedForward(dim, mult=4): |
| 76 | + inner_dim = int(dim * mult) |
| 77 | + return nn.Sequential( |
| 78 | + LayerNorm(dim), |
| 79 | + nn.Linear(dim, inner_dim * 2, bias=False), |
| 80 | + SwiGLU(), |
| 81 | + nn.Linear(inner_dim, dim, bias=False), |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +# attention |
| 86 | + |
| 87 | + |
| 88 | +class Attention(nn.Module): |
| 89 | + |
| 90 | + def __init__(self, dim, dim_head=64, heads=8): |
| 91 | + super().__init__() |
| 92 | + inner_dim = dim_head * heads |
| 93 | + self.norm = LayerNorm(dim) |
| 94 | + self.heads = heads |
| 95 | + self.scale = dim_head**-0.5 |
| 96 | + self.rotary_emb = RotaryEmbedding(dim_head) |
| 97 | + |
| 98 | + self.to_q = nn.Linear(dim, inner_dim, bias=False) |
| 99 | + self.to_kv = nn.Linear(dim, dim_head * 2, bias=False) |
| 100 | + self.to_out = nn.Linear(inner_dim, dim, bias=False) |
| 101 | + |
| 102 | + # for caching causal mask and rotary embeddings |
| 103 | + |
| 104 | + self.register_buffer("mask", None, persistent=False) |
| 105 | + self.register_buffer("pos_emb", None, persistent=False) |
| 106 | + |
| 107 | + def get_mask(self, n, device): |
| 108 | + if self.mask is not None and self.mask.shape[-1] >= n: |
| 109 | + return self.mask[:n, :n] |
| 110 | + |
| 111 | + mask = torch.ones((n, n), device=device, dtype=torch.bool).triu(1) |
| 112 | + self.register_buffer("mask", mask, persistent=False) |
| 113 | + return mask |
| 114 | + |
| 115 | + def get_rotary_embedding(self, n, device): |
| 116 | + if self.pos_emb is not None and self.pos_emb.shape[-2] >= n: |
| 117 | + return self.pos_emb[:n] |
| 118 | + |
| 119 | + pos_emb = self.rotary_emb(n, device=device) |
| 120 | + self.register_buffer("position", pos_emb, persistent=False) |
| 121 | + return pos_emb |
| 122 | + |
| 123 | + def forward(self, x): |
| 124 | + """ |
| 125 | + einstein notation |
| 126 | + b - batch |
| 127 | + h - heads |
| 128 | + n, i, j - sequence length (base sequence length, source, target) |
| 129 | + d - feature dimension |
| 130 | + """ |
| 131 | + |
| 132 | + n, device, h = x.shape[1], x.device, self.heads |
| 133 | + |
| 134 | + # pre layernorm |
| 135 | + |
| 136 | + x = self.norm(x) |
| 137 | + |
| 138 | + # queries, keys, values |
| 139 | + |
| 140 | + q, k, v = (self.to_q(x), *self.to_kv(x).chunk(2, dim=-1)) |
| 141 | + |
| 142 | + # split heads |
| 143 | + # they use multi-query single-key-value attention, yet another Noam Shazeer paper |
| 144 | + # they found no performance loss past a certain scale, and more efficient decoding obviously |
| 145 | + # https://arxiv.org/abs/1911.02150 |
| 146 | + |
| 147 | + q = rearrange(q, "b n (h d) -> b h n d", h=h) |
| 148 | + |
| 149 | + # rotary embeddings |
| 150 | + |
| 151 | + positions = self.get_rotary_embedding(n, device) |
| 152 | + q, k = map(lambda t: apply_rotary_pos_emb(positions, t), (q, k)) |
| 153 | + |
| 154 | + # scale |
| 155 | + |
| 156 | + q = q * self.scale |
| 157 | + |
| 158 | + # similarity |
| 159 | + |
| 160 | + sim = einsum("b h i d, b j d -> b h i j", q, k) |
| 161 | + |
| 162 | + # causal mask |
| 163 | + |
| 164 | + causal_mask = self.get_mask(n, device) |
| 165 | + sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max) |
| 166 | + |
| 167 | + # attention |
| 168 | + |
| 169 | + sim = sim - sim.amax(dim=-1, keepdim=True).detach() |
| 170 | + attn = sim.softmax(dim=-1) |
| 171 | + |
| 172 | + # aggregate values |
| 173 | + |
| 174 | + out = einsum("b h i j, b j d -> b h i d", attn, v) |
| 175 | + |
| 176 | + # merge heads |
| 177 | + |
| 178 | + out = rearrange(out, "b h n d -> b n (h d)") |
| 179 | + return self.to_out(out) |
| 180 | + |
| 181 | + |
| 182 | +# transformer |
| 183 | + |
| 184 | + |
| 185 | +def PaLM(*, dim, num_tokens, depth, dim_head=64, heads=8, ff_mult=4): |
| 186 | + net = nn.Sequential( |
| 187 | + nn.Embedding(num_tokens, dim), *[ |
| 188 | + ParallelResidual( |
| 189 | + Attention(dim=dim, dim_head=dim_head, heads=heads), |
| 190 | + FeedForward(dim=dim, mult=ff_mult), |
| 191 | + ) for _ in range(depth) |
| 192 | + ], LayerNorm(dim), nn.Linear(dim, num_tokens, bias=False)) |
| 193 | + |
| 194 | + # they used embedding weight tied projection out to logits, not common, but works |
| 195 | + net[-1].weight = net[0].weight |
| 196 | + |
| 197 | + nn.init.normal_(net[0].weight, std=0.02) |
| 198 | + return net |
0 commit comments