| |
| |
| |
| |
| |
| |
| |
|
|
| import torch |
| import torch.nn as nn |
|
|
|
|
| |
| |
| |
| class PyTorchMultiHeadAttention(nn.Module): |
| def __init__(self, d_in, d_out, num_heads, dropout=0.0, qkv_bias=False): |
| super().__init__() |
|
|
| assert d_out % num_heads == 0, "d_out is indivisible by num_heads" |
|
|
| self.num_heads = num_heads |
| self.head_dim = d_out // num_heads |
| self.d_out = d_out |
|
|
| self.qkv = nn.Linear(d_in, 3 * d_out, bias=qkv_bias) |
| self.proj = nn.Linear(d_out, d_out) |
| self.dropout = dropout |
|
|
| def forward(self, x): |
| batch_size, num_tokens, embed_dim = x.shape |
|
|
| |
| qkv = self.qkv(x) |
|
|
| |
| qkv = qkv.view(batch_size, num_tokens, 3, self.num_heads, self.head_dim) |
|
|
| |
| qkv = qkv.permute(2, 0, 3, 1, 4) |
|
|
| |
| queries, keys, values = qkv |
|
|
| use_dropout = 0. if not self.training else self.dropout |
|
|
| context_vec = nn.functional.scaled_dot_product_attention( |
| queries, keys, values, attn_mask=None, dropout_p=use_dropout, is_causal=True) |
|
|
| |
| context_vec = context_vec.transpose(1, 2).contiguous().view(batch_size, num_tokens, self.d_out) |
|
|
| context_vec = self.proj(context_vec) |
|
|
| return context_vec |
|
|
|
|
| |
| |
| |
|
|
|
|
| class FeedForward(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| self.layers = nn.Sequential( |
| nn.Linear(cfg["emb_dim"], 4 * cfg["emb_dim"]), |
| nn.GELU(approximate="tanh"), |
| nn.Linear(4 * cfg["emb_dim"], cfg["emb_dim"]), |
| ) |
|
|
| def forward(self, x): |
| return self.layers(x) |
|
|
|
|
| class TransformerBlock(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| self.att = PyTorchMultiHeadAttention( |
| d_in=cfg["emb_dim"], |
| d_out=cfg["emb_dim"], |
| num_heads=cfg["n_heads"], |
| dropout=cfg["drop_rate"], |
| qkv_bias=cfg["qkv_bias"]) |
| self.ff = FeedForward(cfg) |
| self.norm1 = nn.LayerNorm(cfg["emb_dim"]) |
| self.norm2 = nn.LayerNorm(cfg["emb_dim"]) |
| self.drop_shortcut = nn.Dropout(cfg["drop_rate"]) |
|
|
| def forward(self, x): |
| |
| shortcut = x |
| x = self.norm1(x) |
| x = self.att(x) |
| x = self.drop_shortcut(x) |
| x = x + shortcut |
|
|
| |
| shortcut = x |
| x = self.norm2(x) |
| x = self.ff(x) |
| x = self.drop_shortcut(x) |
| x = x + shortcut |
|
|
| return x |
|
|
|
|
| class GPTModel(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| self.tok_emb = nn.Embedding(cfg["vocab_size"], cfg["emb_dim"]) |
| self.pos_emb = nn.Embedding(cfg["context_length"], cfg["emb_dim"]) |
| self.drop_emb = nn.Dropout(cfg["drop_rate"]) |
|
|
| self.trf_blocks = nn.Sequential( |
| *[TransformerBlock(cfg) for _ in range(cfg["n_layers"])]) |
|
|
| self.final_norm = nn.LayerNorm(cfg["emb_dim"]) |
| self.out_head = nn.Linear(cfg["emb_dim"], cfg["vocab_size"], bias=False) |
|
|
| def forward(self, in_idx): |
| batch_size, seq_len = in_idx.shape |
| tok_embeds = self.tok_emb(in_idx) |
| pos_embeds = self.pos_emb(torch.arange(seq_len, device=in_idx.device)) |
| x = tok_embeds + pos_embeds |
| x = self.drop_emb(x) |
| x = self.trf_blocks(x) |
| x = self.final_norm(x) |
| logits = self.out_head(x) |
| return logits |
|
|