File size: 8,151 Bytes
b4b069f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Derived from Andrej Karpathy's nanochat project.

MIT License

Copyright (c) 2025 Andrej Karpathy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"""

from __future__ import annotations

from dataclasses import asdict, dataclass
import math

import torch
import torch.nn as nn
import torch.nn.functional as F


@dataclass(frozen=True)
class GPTConfig:
    block_size: int
    vocab_size: int
    n_layer: int
    n_head: int
    n_embd: int
    dropout: float

    def to_dict(self) -> dict[str, int | float]:
        return asdict(self)


def rms_norm(x: torch.Tensor) -> torch.Tensor:
    return F.rms_norm(x, (x.size(-1),))


class Linear(nn.Linear):
    """Bias-free linear layer matching the nanochat architectural style."""

    def __init__(self, in_features: int, out_features: int):
        super().__init__(in_features, out_features, bias=False)


def apply_rotary_emb(
    x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
) -> torch.Tensor:
    half = x.shape[-1] // 2
    x1, x2 = x[..., :half], x[..., half:]
    return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-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
        if self.head_dim % 2 != 0:
            raise ValueError("rotary attention requires an even head dimension")
        self.dropout_p = config.dropout
        self.c_q = Linear(config.n_embd, config.n_embd)
        self.c_k = Linear(config.n_embd, config.n_embd)
        self.c_v = Linear(config.n_embd, config.n_embd)
        self.c_proj = Linear(config.n_embd, config.n_embd)
        self.resid_dropout = nn.Dropout(config.dropout)
        mask = torch.ones(config.block_size, config.block_size, dtype=torch.bool).tril()
        self.register_buffer(
            "causal_mask",
            mask.view(1, 1, config.block_size, config.block_size),
            persistent=False,
        )

    def set_dropout(self, p: float) -> None:
        self.dropout_p = p
        self.resid_dropout.p = p

    def forward(
        self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
    ) -> torch.Tensor:
        batch, seq_len, channels = x.shape
        q = self.c_q(x).view(batch, seq_len, self.n_head, self.head_dim).transpose(1, 2)
        k = self.c_k(x).view(batch, seq_len, self.n_head, self.head_dim).transpose(1, 2)
        v = self.c_v(x).view(batch, seq_len, self.n_head, self.head_dim).transpose(1, 2)

        q = rms_norm(apply_rotary_emb(q, cos, sin)) * 1.2
        k = rms_norm(apply_rotary_emb(k, cos, sin)) * 1.2

        att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
        mask = self.causal_mask[:, :, :seq_len, :seq_len]
        att = att.masked_fill(~mask, float("-inf"))
        att = F.softmax(att.float(), dim=-1).to(dtype=x.dtype)
        att = F.dropout(att, p=self.dropout_p, training=self.training)
        y = att @ v
        y = y.transpose(1, 2).contiguous().view(batch, seq_len, channels)
        return self.resid_dropout(self.c_proj(y))


class MLP(nn.Module):
    def __init__(self, config: GPTConfig):
        super().__init__()
        self.c_fc = Linear(config.n_embd, 4 * config.n_embd)
        self.c_proj = Linear(4 * config.n_embd, config.n_embd)
        self.dropout = nn.Dropout(config.dropout)

    def set_dropout(self, p: float) -> None:
        self.dropout.p = p

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.c_fc(x)
        x = F.relu(x).square()
        x = self.c_proj(x)
        return self.dropout(x)


class Block(nn.Module):
    def __init__(self, config: GPTConfig):
        super().__init__()
        self.attn = CausalSelfAttention(config)
        self.mlp = MLP(config)

    def set_dropout(self, p: float) -> None:
        self.attn.set_dropout(p)
        self.mlp.set_dropout(p)

    def forward(
        self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor
    ) -> torch.Tensor:
        x = x + self.attn(rms_norm(x), cos, sin)
        x = x + self.mlp(rms_norm(x))
        return x


class DropoutGPT(nn.Module):
    """Minimal nanochat-style causal Transformer with dynamic dropout control."""

    def __init__(self, config: GPTConfig):
        super().__init__()
        self.config = config
        self.token_embedding = nn.Embedding(config.vocab_size, config.n_embd)
        self.embedding_dropout = nn.Dropout(config.dropout)
        self.blocks = nn.ModuleList(Block(config) for _ in range(config.n_layer))
        self.lm_head = Linear(config.n_embd, config.vocab_size)
        self.register_buffer(
            "cos",
            torch.empty(1, 1, config.block_size, config.n_embd // config.n_head // 2),
            persistent=False,
        )
        self.register_buffer(
            "sin",
            torch.empty(1, 1, config.block_size, config.n_embd // config.n_head // 2),
            persistent=False,
        )
        self._init_weights()
        self._init_rotary()

    @torch.no_grad()
    def _init_weights(self) -> None:
        nn.init.normal_(self.token_embedding.weight, mean=0.0, std=0.8)
        nn.init.normal_(self.lm_head.weight, mean=0.0, std=0.001)
        scale = (3.0 ** 0.5) * (self.config.n_embd ** -0.5)
        for block in self.blocks:
            nn.init.uniform_(block.attn.c_q.weight, -scale, scale)
            nn.init.uniform_(block.attn.c_k.weight, -scale, scale)
            nn.init.uniform_(block.attn.c_v.weight, -scale, scale)
            nn.init.zeros_(block.attn.c_proj.weight)
            nn.init.uniform_(block.mlp.c_fc.weight, -0.4 * scale, 0.4 * scale)
            nn.init.zeros_(block.mlp.c_proj.weight)

    @torch.no_grad()
    def _init_rotary(self) -> None:
        head_dim = self.config.n_embd // self.config.n_head
        channel_range = torch.arange(
            0,
            head_dim,
            2,
            dtype=torch.float32,
            device=self.cos.device,
        )
        inv_freq = 1.0 / (100000 ** (channel_range / head_dim))
        positions = torch.arange(
            self.config.block_size,
            dtype=torch.float32,
            device=self.cos.device,
        )
        freqs = torch.outer(positions, inv_freq)
        self.cos.copy_(freqs.cos().view(1, 1, self.config.block_size, head_dim // 2))
        self.sin.copy_(freqs.sin().view(1, 1, self.config.block_size, head_dim // 2))

    def set_dropout(self, p: float) -> None:
        if not 0.0 <= p < 1.0:
            raise ValueError("dropout must satisfy 0 <= p < 1")
        self.embedding_dropout.p = p
        for block in self.blocks:
            block.set_dropout(p)

    def num_parameters(self) -> int:
        return sum(p.numel() for p in self.parameters())

    def forward(
        self, idx: torch.Tensor, targets: torch.Tensor | None = None
    ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
        _, seq_len = idx.shape
        if seq_len > self.config.block_size:
            raise ValueError("sequence length exceeds block_size")
        cos = self.cos[:, :, :seq_len, :]
        sin = self.sin[:, :, :seq_len, :]
        x = self.embedding_dropout(rms_norm(self.token_embedding(idx)))
        for block in self.blocks:
            x = block(x, cos, sin)
        logits = self.lm_head(rms_norm(x)).float()
        if targets is None:
            return logits
        loss = F.cross_entropy(
            logits.reshape(-1, logits.size(-1)),
            targets.reshape(-1),
        )
        return logits, loss