Commit ·
e9355d6
1
Parent(s): a352a6f
Initial commit
Browse files- checkpoint_epoch_199999_cpu.pt +3 -0
- checkpoint_epoch_199999_gpu.pt +3 -0
- gpt_model.py +142 -0
- requirements.txt +1 -0
checkpoint_epoch_199999_cpu.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1c2de40fd6ad825e496319f577d1e76908b2e07b10180a375cc07025e0295f89
|
| 3 |
+
size 1168678
|
checkpoint_epoch_199999_gpu.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:632e8cd0b027699ce7d13cda2e632720b9ad0c25f6b43e9671b29fa98b96d96e
|
| 3 |
+
size 1169758
|
gpt_model.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torch.nn import functional as F
|
| 4 |
+
|
| 5 |
+
n_embd = 64
|
| 6 |
+
block_size = 64 # what is the maximum context length for predictions?
|
| 7 |
+
n_layer = 4
|
| 8 |
+
n_head = 4
|
| 9 |
+
dropout = 0.0
|
| 10 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 11 |
+
|
| 12 |
+
class Head(nn.Module):
|
| 13 |
+
""" one head of self-attention """
|
| 14 |
+
|
| 15 |
+
def __init__(self, head_size):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.key = nn.Linear(n_embd, head_size, bias=False)
|
| 18 |
+
self.query = nn.Linear(n_embd, head_size, bias=False)
|
| 19 |
+
self.value = nn.Linear(n_embd, head_size, bias=False)
|
| 20 |
+
self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size)))
|
| 21 |
+
|
| 22 |
+
self.dropout = nn.Dropout(dropout)
|
| 23 |
+
|
| 24 |
+
def forward(self, x):
|
| 25 |
+
B,T,C = x.shape
|
| 26 |
+
k = self.key(x) # (B,T,C)
|
| 27 |
+
q = self.query(x) # (B,T,C)
|
| 28 |
+
# compute attention scores ("affinities")
|
| 29 |
+
wei = q @ k.transpose(-2,-1) * C**-0.5 # (B, T, C) @ (B, C, T) -> (B, T, T)
|
| 30 |
+
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) # (B, T, T)
|
| 31 |
+
wei = F.softmax(wei, dim=-1) # (B, T, T)
|
| 32 |
+
wei = self.dropout(wei)
|
| 33 |
+
# perform the weighted aggregation of the values
|
| 34 |
+
v = self.value(x) # (B,T,C)
|
| 35 |
+
out = wei @ v # (B, T, T) @ (B, T, C) -> (B, T, C)
|
| 36 |
+
return out
|
| 37 |
+
|
| 38 |
+
class MultiHeadAttention(nn.Module):
|
| 39 |
+
""" multiple heads of self-attention in parallel """
|
| 40 |
+
|
| 41 |
+
def __init__(self, num_heads, head_size):
|
| 42 |
+
super().__init__()
|
| 43 |
+
self.heads = nn.ModuleList([Head(head_size) for _ in range(num_heads)])
|
| 44 |
+
self.proj = nn.Linear(n_embd, n_embd)
|
| 45 |
+
self.dropout = nn.Dropout(dropout)
|
| 46 |
+
|
| 47 |
+
def forward(self, x):
|
| 48 |
+
out = torch.cat([h(x) for h in self.heads], dim=-1)
|
| 49 |
+
out = self.dropout(self.proj(out))
|
| 50 |
+
return out
|
| 51 |
+
|
| 52 |
+
class FeedFoward(nn.Module):
|
| 53 |
+
""" a simple linear layer followed by a non-linearity """
|
| 54 |
+
|
| 55 |
+
def __init__(self, n_embd):
|
| 56 |
+
super().__init__()
|
| 57 |
+
self.net = nn.Sequential(
|
| 58 |
+
nn.Linear(n_embd, 4 * n_embd),
|
| 59 |
+
nn.ReLU(),
|
| 60 |
+
nn.Linear(4 * n_embd, n_embd),
|
| 61 |
+
nn.Dropout(dropout),
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
def forward(self, x):
|
| 65 |
+
return self.net(x)
|
| 66 |
+
|
| 67 |
+
class Block(nn.Module):
|
| 68 |
+
""" Transformer block: communication followed by computation """
|
| 69 |
+
|
| 70 |
+
def __init__(self, n_embd, n_head):
|
| 71 |
+
# n_embd: embedding dimension, n_head: the number of heads we'd like
|
| 72 |
+
super().__init__()
|
| 73 |
+
head_size = n_embd // n_head
|
| 74 |
+
self.sa = MultiHeadAttention(n_head, head_size)
|
| 75 |
+
self.ffwd = FeedFoward(n_embd)
|
| 76 |
+
self.ln1 = nn.LayerNorm(n_embd)
|
| 77 |
+
self.ln2 = nn.LayerNorm(n_embd)
|
| 78 |
+
|
| 79 |
+
def forward(self, x):
|
| 80 |
+
x = x + self.sa(self.ln1(x))
|
| 81 |
+
x = x + self.ffwd(self.ln2(x))
|
| 82 |
+
return x
|
| 83 |
+
|
| 84 |
+
# gpt model
|
| 85 |
+
class gptModel(nn.Module):
|
| 86 |
+
|
| 87 |
+
def __init__(self):
|
| 88 |
+
super().__init__()
|
| 89 |
+
# each token directly reads off the logits for the next token from a lookup table
|
| 90 |
+
self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
|
| 91 |
+
self.position_embedding_table = nn.Embedding(block_size, n_embd)
|
| 92 |
+
self.blocks = nn.Sequential(*[Block(n_embd, n_head=n_head) for _ in range(n_layer)])
|
| 93 |
+
self.ln_f = nn.LayerNorm(n_embd) # final layer norm
|
| 94 |
+
self.lm_head = nn.Linear(n_embd, vocab_size)
|
| 95 |
+
|
| 96 |
+
def forward(self, idx, targets=None):
|
| 97 |
+
B, T = idx.shape
|
| 98 |
+
|
| 99 |
+
# idx and targets are both (B,T) tensor of integers
|
| 100 |
+
tok_emb = self.token_embedding_table(idx) # (B,T,C)
|
| 101 |
+
pos_emb = self.position_embedding_table(torch.arange(T, device=device)) # (T,C)
|
| 102 |
+
x = tok_emb + pos_emb # (B,T,C)
|
| 103 |
+
x = self.blocks(x) # (B,T,C)
|
| 104 |
+
x = self.ln_f(x) # (B,T,C)
|
| 105 |
+
logits = self.lm_head(x) # (B,T,vocab_size)
|
| 106 |
+
|
| 107 |
+
if targets is None:
|
| 108 |
+
loss = None
|
| 109 |
+
else:
|
| 110 |
+
B, T, C = logits.shape
|
| 111 |
+
logits = logits.view(B*T, C)
|
| 112 |
+
targets = targets.view(B*T)
|
| 113 |
+
loss = F.cross_entropy(logits, targets)
|
| 114 |
+
|
| 115 |
+
return logits, loss
|
| 116 |
+
|
| 117 |
+
def generate(self, idx, max_new_tokens):
|
| 118 |
+
# idx is (B, T) array of indices in the current context
|
| 119 |
+
context_length = idx.shape[-1]
|
| 120 |
+
max_new_tokens -= context_length
|
| 121 |
+
for k in range(max_new_tokens):
|
| 122 |
+
# crop idx to the last block_size tokens
|
| 123 |
+
idx_cond = idx[:, -block_size:]
|
| 124 |
+
# get the predictions
|
| 125 |
+
logits, loss = self(idx_cond)
|
| 126 |
+
# focus only on the last time step
|
| 127 |
+
logits = logits[:, -1, :] # becomes (B, C)
|
| 128 |
+
# apply softmax to get probabilities
|
| 129 |
+
probs = F.softmax(logits, dim=-1) # (B, C)
|
| 130 |
+
# sample from the distribution
|
| 131 |
+
idx_next = torch.multinomial(probs, num_samples=1) # (B, 1)
|
| 132 |
+
# append sampled index to the running sequence
|
| 133 |
+
idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
|
| 134 |
+
return idx
|
| 135 |
+
|
| 136 |
+
model = gptModel()
|
| 137 |
+
model = model.to(device)
|
| 138 |
+
if device == 'cpu':
|
| 139 |
+
model_pth = './content/checkpoint_epoch_199999_cpu.pt'
|
| 140 |
+
else:
|
| 141 |
+
model_pth = './content/checkpoint_epoch_199999_gpu.pt'
|
| 142 |
+
model.load_state_dict(torch.load(model_pth))
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
torch==2.1.0
|