ML coding
Machine learning algorithms and model parts written from scratch in NumPy and PyTorch, as ML coding rounds ask.
Machine learning algorithms and model parts written from scratch in NumPy and PyTorch, as ML coding rounds ask.
9 write-ups
| Title |
|---|
| Self-Attention from Scratch Scaled dot-product self-attention in NumPy and then PyTorch, with causal and padding masks, a stable softmax, tests, and the why-scale and why-norm questions. |
| Multi-Head and Grouped-Query Attention Multi-head causal self-attention in PyTorch, from a loop over heads to one fused projection and reshapes, then grouped-query and multi-query attention, tested against PyTorch's own. |
| Positional Encoding and RoPE Why attention needs positions, then learned and sinusoidal embeddings and rotary embeddings (RoPE) on queries and keys, with the relative-position property tested and the long-context follow-ups. |
| A Transformer Block A transformer encoder layer in PyTorch, from a block without residuals to post-norm and pre-norm, matched against nn.TransformerEncoderLayer, plus a one-layer NumPy transformer and an attention-free variant. |
| KV Cache and Incremental Decoding A KV cache for a small decoder in PyTorch, from recomputing the prefix to a growing cache to a preallocated one, with prefill vs decode, offset masks, an exactness test and the memory math. |
| Decoding: Greedy, Sampling and Beam Search Turning next-token logits into text in PyTorch: greedy, temperature, top-k and top-p sampling as masks over a batch, beam search checked against exhaustive search, and repetition penalties. |
| LoRA Low-rank adapters in PyTorch, from full fine-tuning to a LoRA linear layer to injecting, merging and saving adapters, with a rank experiment, exactness tests and the memory math. |
| Train a Tiny GPT A character-level GPT trained end to end in PyTorch: data and batches, a bigram baseline, the transformer, a training recipe with warmup, clipping and weight decay, sampling, and the checks that prove it learns. |
| Policy Gradients and GRPO Policy-gradient training for sequence models in PyTorch, from raw REINFORCE to a group-relative baseline to GRPO with ratio clipping and a KL penalty, plus the NaN and ratio bugs of the debugging variant. |