|
import os |
|
import math |
|
import time |
|
import inspect |
|
from dataclasses import dataclass |
|
import torch |
|
import torch.nn as nn |
|
from torch.nn import functional as F |
|
|
|
class Attention(nn.Module): |
|
|
|
def __init__(self, config): |
|
super().__init__() |
|
|
|
assert config.nn_embed % config.nn_head == 0 |
|
|
|
self.nn_head = config.nn_head |
|
self.nn_embed = config.nn_embed |
|
|
|
|
|
self.w_qkv = nn.Linear(config.nn_embed, config.nn_embed * 3) |
|
|
|
|
|
self.proj = nn.Linear(config.nn_embed, config.nn_embed) |
|
|
|
self.register_buffer("bias", torch.tril(torch.ones(config.nn_max_tok_seq, config.nn_max_tok_seq)).view(1, 1, config.nn_max_tok_seq, config.nn_max_tok_seq)) |
|
|
|
|
|
def forward(self, x): |
|
B, T, E = x.size() |
|
q, k, v = self.w_qkv(x).split(self.nn_embed, dim=2) |
|
|
|
|
|
q = q.view(B, T, self.nn_head, E//self.nn_head).transpose(1,2) |
|
k = k.view(B, T, self.nn_head, E//self.nn_head).transpose(1,2) |
|
v = v.view(B, T, self.nn_head, E//self.nn_head).transpose(1,2) |
|
|
|
att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1))) |
|
att = att.masked_fill(self.bias[:, :, :T, :T] == 0, float('-inf')) |
|
att = F.softmax(att, dim = -1) |
|
y = att @ v |
|
|
|
y = y.transpose(1,2).contiguous().view(B, T, E) |
|
|
|
y = self.proj(y) |
|
return y |
|
|
|
|
|
class MLP(nn.Module): |
|
|
|
def __init__(self, config): |
|
super().__init__() |
|
|
|
self.fc = nn.Linear(config.nn_embed, config.nn_embed * config.nn_mlp_expansion) |
|
self.gelu = nn.GELU(approximate='tanh') |
|
self.proj = nn.Linear(config.nn_embed * config.nn_mlp_expansion, config.nn_embed) |
|
self.proj.NANGPT_SCALE_INIT = 1 |
|
|
|
def forward(self, x): |
|
x = self.fc(x) |
|
x = self.gelu(x) |
|
x = self.proj(x) |
|
return x |
|
|
|
class Block(nn.Module): |
|
def __init__(self, config): |
|
super().__init__() |
|
self.ln_1 = nn.LayerNorm(config.nn_embed) |
|
self.att = Attention(config) |
|
self.ln_2 = nn.LayerNorm(config.nn_embed) |
|
self.mlp = MLP(config) |
|
|
|
def forward(self, x): |
|
x = x + self.att(self.ln_1(x)) |
|
x = x + self.mlp(self.ln_2(x)) |
|
return x |
|
|
|
|
|
class DecoderTransformer(nn.Module): |
|
def __init__(self, config): |
|
super().__init__() |
|
self.config = config |
|
|
|
self.wte = nn.Embedding(config.vocab_size, config.nn_embed) |
|
self.wpe = nn.Embedding(config.nn_max_tok_seq, config.nn_embed) |
|
self.blocks = nn.ModuleList([Block(config) for _ in range(0, config.nn_layer)]) |
|
self.lm_head = nn.Linear(config.nn_embed, config.vocab_size, bias=False) |
|
|
|
|
|
self.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, 'NANGPT_SCALE_INIT'): |
|
std *= (2 * self.config.nn_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() |
|
assert T <= self.config.nn_max_tok_seq, f"Token length ({T}) can not exceed the max allowed sequence size (block size) ({self.config.nn_max_tok_seq})" |
|
|
|
|
|
pos = torch.arange(0, T, dtype=torch.long, device=idx.device) |
|
pos_embed = self.wpe(pos) |
|
tok_embed = self.wte(idx) |
|
|
|
x = pos_embed + tok_embed |
|
|
|
|
|
for block in self.blocks: |
|
x = block(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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|