| import torch |
| import torch.nn as nn |
| from torch.nn import functional as F |
|
|
|
|
| class KairoGPTConfig: |
| def __init__(self, vocab_size, block_size=4096, n_layer=14, n_head=14, n_embd=896, dropout=0.1): |
| self.vocab_size = vocab_size |
| self.block_size = block_size |
| self.n_layer = n_layer |
| self.n_head = n_head |
| self.n_embd = n_embd |
| self.dropout = dropout |
|
|
|
|
| class CausalSelfAttention(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| assert cfg.n_embd % cfg.n_head == 0 |
| self.qkv = nn.Linear(cfg.n_embd, 3 * cfg.n_embd) |
| self.proj = nn.Linear(cfg.n_embd, cfg.n_embd) |
| self.attn_drop_p = cfg.dropout |
| self.resid_drop = nn.Dropout(cfg.dropout) |
| self.n_head = cfg.n_head |
|
|
| def forward(self, x): |
| B, T, C = x.shape |
| q, k, v = self.qkv(x).split(C, 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, |
| dropout_p=self.attn_drop_p if self.training else 0.0, |
| is_causal=True, |
| ) |
| y = y.transpose(1, 2).contiguous().view(B, T, C) |
| return self.resid_drop(self.proj(y)) |
|
|
|
|
| class Block(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| self.ln1 = nn.LayerNorm(cfg.n_embd) |
| self.attn = CausalSelfAttention(cfg) |
| self.ln2 = nn.LayerNorm(cfg.n_embd) |
| self.mlp = nn.Sequential( |
| nn.Linear(cfg.n_embd, 4 * cfg.n_embd), |
| nn.GELU(), |
| nn.Linear(4 * cfg.n_embd, cfg.n_embd), |
| nn.Dropout(cfg.dropout), |
| ) |
|
|
| def forward(self, x): |
| x = x + self.attn(self.ln1(x)) |
| x = x + self.mlp(self.ln2(x)) |
| return x |
|
|
|
|
| class KairoGPT(nn.Module): |
| def __init__(self, cfg): |
| super().__init__() |
| self.cfg = cfg |
| self.tok_emb = nn.Embedding(cfg.vocab_size, cfg.n_embd) |
| self.pos_emb = nn.Parameter(torch.zeros(1, cfg.block_size, cfg.n_embd)) |
| self.drop = nn.Dropout(cfg.dropout) |
| self.blocks = nn.Sequential(*[Block(cfg) for _ in range(cfg.n_layer)]) |
| self.ln_f = nn.LayerNorm(cfg.n_embd) |
| self.head = nn.Linear(cfg.n_embd, cfg.vocab_size, bias=False) |
| self.apply(self._init_weights) |
|
|
| def _init_weights(self, module): |
| if isinstance(module, (nn.Linear, nn.Embedding)): |
| nn.init.normal_(module.weight, mean=0.0, std=0.02) |
| if isinstance(module, nn.Linear) and module.bias is not None: |
| nn.init.zeros_(module.bias) |
|
|
| def forward(self, idx, targets=None): |
| B, T = idx.shape |
| x = self.drop(self.tok_emb(idx) + self.pos_emb[:, :T, :]) |
| x = self.blocks(x) |
| x = self.ln_f(x) |
| logits = self.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=0.8, top_k=40): |
| for _ in range(max_new_tokens): |
| idx_cond = idx[:, -self.cfg.block_size:] |
| logits, _ = self(idx_cond) |
| logits = logits[:, -1, :] / temperature |
| if top_k is not None: |
| v, _ = torch.topk(logits, top_k) |
| logits[logits < v[:, [-1]]] = float("-inf") |
| probs = F.softmax(logits, dim=-1) |
| next_id = torch.multinomial(probs, num_samples=1) |
| idx = torch.cat((idx, next_id), dim=1) |
| return idx |
|
|
| @torch.no_grad() |
| def generate_stream(self, idx, max_new_tokens, temperature=0.8, top_k=40): |
| for _ in range(max_new_tokens): |
| idx_cond = idx[:, -self.cfg.block_size:] |
| logits, _ = self(idx_cond) |
| logits = logits[:, -1, :] / temperature |
| if top_k is not None: |
| v, _ = torch.topk(logits, top_k) |
| logits[logits < v[:, [-1]]] = float("-inf") |
| probs = F.softmax(logits, dim=-1) |
| next_id = torch.multinomial(probs, num_samples=1) |
| idx = torch.cat((idx, next_id), dim=1) |
| yield next_id.item() |
|
|