9/29
Part 2 of 5 · The blockLearn and do
The block in code
Here is the block from the picture, in code:
class Block(nn.Module):
def __init__(self, n_embd: int, n_head: int, block_size: int) -> None:
super().__init__()
self.ln1 = nn.LayerNorm(n_embd)
self.attn = CausalSelfAttention(n_embd, n_head, block_size)
self.ln2 = nn.LayerNorm(n_embd)
self.mlp = nn.Sequential(
# up to 4C numbers
nn.Linear(n_embd, 4 * n_embd),
nn.GELU(),
# back down to C
nn.Linear(4 * n_embd, n_embd),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""x: (B, T, C), the stream. Returns (B, T, C)."""
...
forward runs the two steps, and you write it a few steps from now. Each step reads a normalized copy of the stream, changes it, and adds the change back. The block's output has the same shape as its input, (B, T, C), so blocks stack like bricks.
The model keeps its blocks in nn.ModuleList, not in a plain Python list.
You keep the blocks in a plain Python list instead. What happens when you train on the CPU?
Pick an answer above to see the rest