| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
| import tiktoken |
|
|
| |
| |
| |
|
|
| |
|
|
| class CausalSelfAttention(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
| assert config.n_embd % config.n_head == 0 |
| self.c_attn = nn.Linear(config.n_embd, 3 * config.n_embd) |
| self.c_proj = nn.Linear(config.n_embd, config.n_embd) |
| self.n_head = config.n_head |
| self.n_embd = config.n_embd |
|
|
| def forward(self, x): |
| B, T, C = x.size() |
| qkv = self.c_attn(x) |
| q, k, v = qkv.split(self.n_embd, dim=2) |
| k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2) |
| q = q.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.c_fc = nn.Linear(config.n_embd, 4 * config.n_embd) |
| self.gelu = nn.GELU(approximate='tanh') |
| self.c_proj = nn.Linear(4 * config.n_embd, config.n_embd) |
|
|
| def forward(self, x): |
| x = self.c_fc(x) |
| x = self.gelu(x) |
| x = self.c_proj(x) |
| return x |
|
|
| class Block(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
| self.ln_1 = nn.LayerNorm(config.n_embd) |
| self.attn = CausalSelfAttention(config) |
| self.ln_2 = nn.LayerNorm(config.n_embd) |
| self.mlp = MLP(config) |
|
|
| def forward(self, x): |
| x = x + self.attn(self.ln_1(x)) |
| x = x + self.mlp(self.ln_2(x)) |
| return x |
|
|
| |
| class GPTConfig: |
| def __init__(self, *, block_size, vocab_size, n_layer, n_head, n_embd): |
| self.block_size = block_size |
| self.vocab_size = vocab_size |
| self.n_layer = n_layer |
| self.n_head = n_head |
| self.n_embd = n_embd |
|
|
| class GPT(nn.Module): |
| def __init__(self, config): |
| super().__init__() |
| self.config = config |
| self.wte = nn.Embedding(config.vocab_size, config.n_embd) |
| self.wpe = nn.Embedding(config.block_size, config.n_embd) |
| self.blocks = nn.ModuleList([Block(config) for _ in range(config.n_layer)]) |
| self.ln_f = nn.LayerNorm(config.n_embd) |
| self.lm_head = nn.Linear(config.n_embd, 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 |
| 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.block_size, f"入力シーケンス長 {T} が block_size {self.config.block_size} を超えています" |
| pos = torch.arange(0, T, dtype=torch.long, device=idx.device) |
| pos_emb = self.wpe(pos) |
| tok_emb = self.wte(idx) |
| x = tok_emb + pos_emb |
| for block in self.blocks: |
| x = block(x) |
| x = self.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 |
|
|
| |
| |
| |
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu" |
| MODEL_PATH = "model_00999.pt" |
|
|
| |
| checkpoint = torch.load(MODEL_PATH, map_location=device) |
| config = checkpoint['config'] |
| if isinstance(config, dict): |
| config = GPTConfig(**config) |
|
|
| |
| model = GPT(config) |
| model.load_state_dict(checkpoint['model']) |
| model.to(device) |
| model.eval() |
|
|
| |
| enc = tiktoken.get_encoding("gpt2") |
|
|
| def generate_text(prompt, max_length=100, top_k=50): |
| tokens = enc.encode(prompt) |
| x = torch.tensor(tokens, dtype=torch.long, device=device).unsqueeze(0) |
| with torch.no_grad(): |
| while x.size(1) < max_length: |
| logits, _ = model(x) |
| logits = logits[:, -1, :] |
| probs = F.softmax(logits, dim=-1) |
| topk_probs, topk_indices = torch.topk(probs, top_k, dim=-1) |
| next_token = torch.multinomial(topk_probs, num_samples=1) |
| next_token = torch.gather(topk_indices, -1, next_token) |
| x = torch.cat((x, next_token), dim=1) |
| output = enc.decode(x[0].tolist()) |
| return output |
|
|
| |
| |
| |
|
|
| |
| |
| def predict(inputs): |
| prompt = inputs.get("prompt", "Hello, I'm a language model,") |
| max_length = int(inputs.get("max_length", 100)) |
| top_k = int(inputs.get("top_k", 50)) |
| generated = generate_text(prompt, max_length=max_length, top_k=top_k) |
| return {"generated_text": generated} |
|
|