Upload 3 files
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
import tiktoken
|
4 |
import numpy as np
|
5 |
-
from
|
6 |
|
7 |
def load_quantized_model():
|
8 |
model = GPT(GPTConfig())
|
|
|
2 |
import torch
|
3 |
import tiktoken
|
4 |
import numpy as np
|
5 |
+
from model import GPT, GPTConfig # Changed from train to model
|
6 |
|
7 |
def load_quantized_model():
|
8 |
model = GPT(GPTConfig())
|
model.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
from dataclasses import dataclass
|
6 |
+
|
7 |
+
@dataclass
|
8 |
+
class GPTConfig:
|
9 |
+
block_size: int = 1024
|
10 |
+
vocab_size: int = 50257
|
11 |
+
n_layer: int = 12
|
12 |
+
n_head: int = 12
|
13 |
+
n_embd: int = 768
|
14 |
+
|
15 |
+
class CausalSelfAttention(nn.Module):
|
16 |
+
def __init__(self, config):
|
17 |
+
super().__init__()
|
18 |
+
assert config.n_embd % config.n_head == 0
|
19 |
+
self.c_attn = nn.Linear(config.n_embd, 3 * config.n_embd)
|
20 |
+
self.c_proj = nn.Linear(config.n_embd, config.n_embd)
|
21 |
+
self.c_proj.NANGPT_SCALE_INIT = 1
|
22 |
+
self.n_head = config.n_head
|
23 |
+
self.n_embd = config.n_embd
|
24 |
+
self.register_buffer("bias", torch.tril(torch.ones(config.block_size, config.block_size)).view(1, 1, config.block_size, config.block_size))
|
25 |
+
|
26 |
+
def forward(self, x):
|
27 |
+
B, T, C = x.size()
|
28 |
+
qkv = self.c_attn(x)
|
29 |
+
q, k, v = qkv.split(self.n_embd, dim=2)
|
30 |
+
k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
31 |
+
q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
32 |
+
v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
|
33 |
+
|
34 |
+
att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
|
35 |
+
att = att.masked_fill(self.bias[:, :, :T, :T] == 0, float('-inf'))
|
36 |
+
att = F.softmax(att, dim=-1)
|
37 |
+
y = att @ v
|
38 |
+
y = y.transpose(1, 2).contiguous().view(B, T, C)
|
39 |
+
y = self.c_proj(y)
|
40 |
+
return y
|
41 |
+
|
42 |
+
class MLP(nn.Module):
|
43 |
+
def __init__(self, config):
|
44 |
+
super().__init__()
|
45 |
+
self.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd)
|
46 |
+
self.gelu = nn.GELU(approximate='tanh')
|
47 |
+
self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd)
|
48 |
+
self.c_proj.NANOGPT_SCALE_INIT = 1
|
49 |
+
|
50 |
+
def forward(self, x):
|
51 |
+
x = self.c_fc(x)
|
52 |
+
x = self.gelu(x)
|
53 |
+
x = self.c_proj(x)
|
54 |
+
return x
|
55 |
+
|
56 |
+
class Block(nn.Module):
|
57 |
+
def __init__(self, config):
|
58 |
+
super().__init__()
|
59 |
+
self.ln_1 = nn.LayerNorm(config.n_embd)
|
60 |
+
self.attn = CausalSelfAttention(config)
|
61 |
+
self.ln_2 = nn.LayerNorm(config.n_embd)
|
62 |
+
self.mlp = MLP(config)
|
63 |
+
|
64 |
+
def forward(self, x):
|
65 |
+
x = x + self.attn(self.ln_1(x))
|
66 |
+
x = x + self.mlp(self.ln_2(x))
|
67 |
+
return x
|
68 |
+
|
69 |
+
class GPT(nn.Module):
|
70 |
+
def __init__(self, config):
|
71 |
+
super().__init__()
|
72 |
+
self.config = config
|
73 |
+
|
74 |
+
self.transformer = nn.ModuleDict(dict(
|
75 |
+
wte = nn.Embedding(config.vocab_size, config.n_embd),
|
76 |
+
wpe = nn.Embedding(config.block_size, config.n_embd),
|
77 |
+
h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
|
78 |
+
ln_f = nn.LayerNorm(config.n_embd),
|
79 |
+
))
|
80 |
+
self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
|
81 |
+
self.transformer.wte.weight = self.lm_head.weight
|
82 |
+
self.apply(self._init_weights)
|
83 |
+
|
84 |
+
def _init_weights(self, module):
|
85 |
+
if isinstance(module, nn.Linear):
|
86 |
+
std = 0.02
|
87 |
+
if hasattr(module, 'NANGPT_SCALE_INIT'):
|
88 |
+
std *= (2 * self.config.n_layer) ** -0.5
|
89 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
|
90 |
+
if module.bias is not None:
|
91 |
+
torch.nn.init.zeros_(module.bias)
|
92 |
+
elif isinstance(module, nn.Embedding):
|
93 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
94 |
+
|
95 |
+
def forward(self, idx, targets=None):
|
96 |
+
B, T = idx.size()
|
97 |
+
assert T <= self.config.block_size, f"Cannot forward sequence of length {T}, block size is only {self.config.block_size}"
|
98 |
+
pos = torch.arange(0, T, dtype=torch.long, device=idx.device)
|
99 |
+
pos_emb = self.transformer.wpe(pos)
|
100 |
+
tok_emb = self.transformer.wte(idx)
|
101 |
+
x = tok_emb + pos_emb
|
102 |
+
for block in self.transformer.h:
|
103 |
+
x = block(x)
|
104 |
+
x = self.transformer.ln_f(x)
|
105 |
+
logits = self.lm_head(x)
|
106 |
+
loss = None
|
107 |
+
if targets is not None:
|
108 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1))
|
109 |
+
return logits, loss
|
110 |
+
|
111 |
+
def generate(self, idx, max_new_tokens):
|
112 |
+
for _ in range(max_new_tokens):
|
113 |
+
idx_cond = idx[:, -self.config.block_size:]
|
114 |
+
logits, _ = self(idx_cond)
|
115 |
+
logits = logits[:, -1, :]
|
116 |
+
probs = F.softmax(logits, dim=-1)
|
117 |
+
idx_next = torch.multinomial(probs, num_samples=1)
|
118 |
+
idx = torch.cat((idx, idx_next), dim=1)
|
119 |
+
return idx
|