import math from dataclasses import dataclass import torch import torch.nn as nn from torch.nn import functional as F @dataclass class GPTConfig: vocab_size: int = 4096 block_size: int = 256 n_embd: int = 384 n_layer: int = 6 n_head: int = 6 dropout: float = 0.1 class CausalSelfAttention(nn.Module): def __init__(self, config: GPTConfig): super().__init__() if config.n_embd % config.n_head != 0: raise ValueError("n_embd must be divisible by n_head") self.n_head = config.n_head self.head_dim = config.n_embd // config.n_head self.qkv = nn.Linear(config.n_embd, 3 * config.n_embd, bias=False) self.proj = nn.Linear(config.n_embd, config.n_embd, bias=False) self.attn_dropout = nn.Dropout(config.dropout) self.resid_dropout = nn.Dropout(config.dropout) mask = torch.tril(torch.ones(config.block_size, config.block_size)) self.register_buffer( "causal_mask", mask.view(1, 1, config.block_size, config.block_size), persistent=False, ) def forward(self, x: torch.Tensor) -> torch.Tensor: batch_size, seq_len, embed_dim = x.shape q, k, v = self.qkv(x).chunk(3, dim=-1) q = q.view(batch_size, seq_len, self.n_head, self.head_dim).transpose(1, 2) k = k.view(batch_size, seq_len, self.n_head, self.head_dim).transpose(1, 2) v = v.view(batch_size, seq_len, self.n_head, self.head_dim).transpose(1, 2) scores = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim) scores = scores.masked_fill( self.causal_mask[:, :, :seq_len, :seq_len] == 0, float("-inf"), ) weights = F.softmax(scores, dim=-1) weights = self.attn_dropout(weights) out = weights @ v out = out.transpose(1, 2).contiguous().view(batch_size, seq_len, embed_dim) return self.resid_dropout(self.proj(out)) class FeedForward(nn.Module): def __init__(self, config: GPTConfig): super().__init__() self.net = nn.Sequential( nn.Linear(config.n_embd, 4 * config.n_embd, bias=False), nn.GELU(), nn.Linear(4 * config.n_embd, config.n_embd, bias=False), nn.Dropout(config.dropout), ) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.net(x) class Block(nn.Module): def __init__(self, config: GPTConfig): super().__init__() self.ln_1 = nn.LayerNorm(config.n_embd) self.attn = CausalSelfAttention(config) self.ln_2 = nn.LayerNorm(config.n_embd) self.ffn = FeedForward(config) def forward(self, x: torch.Tensor) -> torch.Tensor: x = x + self.attn(self.ln_1(x)) x = x + self.ffn(self.ln_2(x)) return x class GPT(nn.Module): def __init__(self, config: GPTConfig): super().__init__() self.config = config self.token_embedding = nn.Embedding(config.vocab_size, config.n_embd) self.position_embedding = nn.Embedding(config.block_size, config.n_embd) self.dropout = nn.Dropout(config.dropout) self.blocks = nn.Sequential(*[Block(config) for _ in range(config.n_layer)]) self.final_norm = nn.LayerNorm(config.n_embd) self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False) self.lm_head.weight = self.token_embedding.weight self.apply(self._init_weights) def _init_weights(self, module: nn.Module) -> None: if isinstance(module, nn.Linear): nn.init.normal_(module.weight, mean=0.0, std=0.02) if module.bias is not None: nn.init.zeros_(module.bias) elif isinstance(module, nn.Embedding): nn.init.normal_(module.weight, mean=0.0, std=0.02) def forward( self, idx: torch.Tensor, targets: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor | None]: batch_size, seq_len = idx.shape if seq_len > self.config.block_size: raise ValueError( f"Sequence length {seq_len} exceeds block size {self.config.block_size}" ) positions = torch.arange(seq_len, device=idx.device) token_emb = self.token_embedding(idx) pos_emb = self.position_embedding(positions) x = self.dropout(token_emb + pos_emb) x = self.blocks(x) x = self.final_norm(x) logits = self.lm_head(x) loss = None if targets is not None: loss = F.cross_entropy( logits.reshape(-1, logits.size(-1)), targets.reshape(-1), ) return logits, loss @torch.no_grad() def generate( self, idx: torch.Tensor, max_new_tokens: int, temperature: float = 1.0, top_k: int | None = None, eos_token_id: int | None = None, ) -> torch.Tensor: if temperature <= 0: raise ValueError("temperature must be greater than 0") for _ in range(max_new_tokens): idx_cond = idx[:, -self.config.block_size :] logits, _ = self(idx_cond) logits = logits[:, -1, :] / temperature if top_k is not None: values, _ = torch.topk(logits, min(top_k, logits.size(-1))) logits = logits.masked_fill(logits < values[:, [-1]], float("-inf")) probs = F.softmax(logits, dim=-1) next_idx = torch.multinomial(probs, num_samples=1) idx = torch.cat((idx, next_idx), dim=1) if eos_token_id is not None and next_idx.item() == eos_token_id: break return idx def num_parameters(self) -> int: return sum(param.numel() for param in self.parameters()) def main() -> None: torch.manual_seed(42) config = GPTConfig() if torch.backends.mps.is_available(): device = torch.device("mps") elif torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") model = GPT(config).to(device) model.eval() batch_size = 4 seq_len = 64 x = torch.randint( low=0, high=config.vocab_size, size=(batch_size, seq_len), device=device, ) targets = torch.randint( low=0, high=config.vocab_size, size=(batch_size, seq_len), device=device, ) logits, loss = model(x, targets) print(f"Device: {device}") print(f"Parameters: {model.num_parameters():,}") print(f"Input shape: {tuple(x.shape)}") print(f"Logits shape: {tuple(logits.shape)}") print(f"Loss: {loss.item():.4f}") print(f"Expected loss: ~{math.log(config.vocab_size):.4f}") if __name__ == "__main__": main()