TinyStoryGPT2 / model.py
MLGOATKOJI's picture
Upload model.py
e22d6d8 verified
Raw
History Blame Contribute Delete
5.03 kB
import torch
import torch.nn as nn
from torch.nn import functional as F
import math
class CasualSelfAttention(nn.Module):
def __init__(self,config):
super().__init__()
self.c_attn = nn.Linear(config.n_embd, config.n_embd * 3)
self.c_proj = nn.Linear(config.n_embd, config.n_embd)
self.n_head = config.n_head
self.n_embd = config.n_embd
self.c_proj.NANOGPT_SCALE_INIT = 1
def forward(self,x):
B,T, C = x.size()
qkv = self.c_attn(x)
q,k,v = qkv.split(self.n_embd,dim=2)
q = q.view(B,T, self.n_head, C // self.n_head).transpose(1,2)
k = k.view(B,T, self.n_head, C // self.n_head).transpose(1,2)
v = v.view(B,T, self.n_head, C // self.n_head).transpose(1,2)
y = F.scaled_dot_product_attention(q,k,v,is_causal=True)
y = y.transpose(1,2).contiguous().view(B,T,C)
y = self.c_proj(y)
return y
class MLP(nn.Module):
def __init__(self,config):
super().__init__()
self.increase = nn.Linear(config.n_embd,config.n_embd * 4)
self.gelu = nn.GELU(approximate='tanh')
self.reduce = nn.Linear(config.n_embd * 4,config.n_embd)
def forward(self,x):
x = self.increase(x)
x = self.gelu(x)
x = self.reduce(x)
return x
class Block(nn.Module):
def __init__(self,config):
super().__init__()
self.sa = CasualSelfAttention(config)
self.mlp = MLP(config)
self.ln1 = nn.LayerNorm(config.n_embd)
self.ln2 = nn.LayerNorm(config.n_embd)
def forward(self,x):
x = x + self.sa(self.ln1(x))
x = x + self.mlp(self.ln2(x))
return x
class GPT(nn.Module):
def __init__(self,config):
super().__init__()
self.config = config
self.transformer = nn.ModuleDict(dict(
wte = nn.Embedding(config.vocab_size,config.n_embd),
wpe = nn.Embedding(config.block_size,config.n_embd),
h = nn.ModuleList([Block(config) for _ in range(config.n_layer)]),
ln_f = nn.LayerNorm(config.n_embd)
))
self.lm_head = nn.Linear(config.n_embd,config.vocab_size,bias=False)
self.transformer.wte.weight = self.lm_head.weight
self.apply(self._init_weights)
#
def _init_weights(self, module):
if isinstance(module, nn.Linear):
std = 0.02
if hasattr(module, "NANOGPT_SCALE_INIT"):
std *= (2 * self.config.n_layer) ** -0.5
torch.nn.init.normal_(module.weight, mean=0.0, std=std)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
def forward(self,idx,targets=None):
B,T = idx.size()
pos = torch.arange(0,T,dtype=torch.long,device=idx.device)
tok_emb = self.transformer.wte(idx)
pos_emb = self.transformer.wpe(pos)
x = tok_emb + pos_emb
for block in self.transformer.h:
x = block(x)
x = self.transformer.ln_f(x)
logits = self.lm_head(x)
loss = None
if targets is not None:
loss = F.cross_entropy(logits.view(-1,logits.size(-1)),targets.view(-1))
return logits,loss
@torch.no_grad()
def generate(self, idx, max_new_tokens, temperature=1.0, top_k=None):
"""
Take a conditioning sequence of indices idx (LongTensor of shape (b,t)) and complete
the sequence max_new_tokens times, feeding the predictions back into the model each time.
Most likely you'll want to make sure to be in model.eval() mode of operation for this.
"""
for _ in range(max_new_tokens):
# if the sequence context is growing too long we must crop it at block_size
idx_cond = idx if idx.size(1) <= self.config.block_size else idx[:, -self.config.block_size:]
# forward the model to get the logits for the index in the sequence
logits, _ = self(idx_cond)
# pluck the logits at the final step and scale by desired temperature
logits = logits[:, -1, :] / temperature
# optionally crop the logits to only the top k options
if top_k is not None:
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = -float('Inf')
# apply softmax to convert logits to (normalized) probabilities
probs = F.softmax(logits, dim=-1)
# sample from the distribution
idx_next = torch.multinomial(probs, num_samples=1)
# append sampled index to the running sequence and continue
idx = torch.cat((idx, idx_next), dim=1)
return idx