Tokenization and Batching
Build the vocabulary, encode and decode token ids, and pack padded sequences into batched tensors.
Reimplement the original encoder-decoder Transformer with multi-head attention, scheduling, and beam search.
Every source step is its own lesson with intuition, concepts, correctly rendered MathJax mathematics, implementation, tests, mistakes, and a checkpoint.
Build the vocabulary, encode and decode token ids, and pack padded sequences into batched tensors.
Scale embeddings and construct the sinusoidal positional encoding matrix added to input embeddings.
Build padding and causal masks and assemble scaled dot-product attention step by step.
Split, permute, and merge heads, project Q/K/V, and assemble the full multi-head attention module.
Implement the position-wise feed-forward network, layer normalization, residual add-and-norm, and dropout primitives.
Stack encoder and decoder layers, tie output projections to embeddings, and run the complete forward pass.
Allocate the raw weight tensors (with requires_grad) for encoder/decoder layers and embeddings, and gather them for the optimizer.
Implement teacher forcing, Noam warmup, label-smoothed KL loss, and token-level accuracy.
Build the Adam optimizer step by step: moment buffers, exponential-moving-average updates, bias correction, the parameter update rule, and gradient zeroing.
Tie everything together: run a forward pass, compute the label-smoothed loss, backpropagate, and step Adam across many iterations.
Generate sequences with greedy argmax and a length-penalized beam search over candidate hypotheses.