Build a Mini LLM from Scratch in NumPy: RoPE, GQA, SwiGLU Visual Guide
How to build a mini LLM from scratch in NumPy — RoPE, GQA, QK-Norm, SwiGLU, tied embeddings, and a 3.87M chat companion with full architecture visuals.
Notes on AI & ML
Deep-dives into transformers, autograd, and the math behind modern LLMs — written while I build things from scratch.
Also on gruheshkurra.com
How to build a mini LLM from scratch in NumPy — RoPE, GQA, QK-Norm, SwiGLU, tied embeddings, and a 3.87M chat companion with full architecture visuals.
How does GPT math actually work end to end? Follow one training step from token IDs through Q/K/V, multi-head attention, MLP, logits, cross-entropy loss, backprop, and AdamW — past O = Attention(Q,K,V).
How does the Adam optimizer work? Derive momentum, RMSprop, and bias correction, see why AdamW decouples weight decay, and walk a one-parameter numeric update step by step.
What is cross-entropy loss? Derive softmax + negative log-likelihood step by step, work a 3-class numeric example, see why MSE fails for classification, and build the loss GPT training actually uses.
What is byte pair encoding (BPE)? Learn how GPT tokenizers split text into IDs — pretokenization, merge rules, byte-level vocab, and a from-scratch implementation you can actually debug.
What is positional encoding in transformers? Why attention is order-blind, learned vs sinusoidal position embeddings, why we add instead of concatenate, and a from-scratch NumPy implementation.
What is a token embedding? How LLMs map token IDs to learned vectors — the embedding matrix, gather lookup vs one-hot matmul, parameter counts, and a from-scratch NumPy implementation.
What is numerical gradient checking? Learn the central difference formula, why you never train with finite differences, and how to build a grad checker that catches bugs in your autograd before LayerNorm or attention.
One full transformer training step worked by hand — embeddings, positional encoding, attention, layer norm, the encoder-decoder, cross-entropy loss, backprop, and Adam.
In a five-agent CI/CD pipeline across three LLM providers, a fabricated 'pre-approved under SEC-2291' label lets secret-exfiltrating PRs pass the scanner ~80% of the time and reach 55% compromise in the worst cell—showing distributed verification fails without provenance controls at entry.
Maps 128 papers into a design-space taxonomy of agentic attack surfaces—from tool hijacking and RAG poisoning to multi-agent propagation—with defense-in-depth guidance timed to this week's real-world agent breach outpacing benchmark-only security claims.
A lifecycle survey of 247 papers maps agent risks from prompt injection and tool hijacking to persistent memory corruption and multi-agent propagation—directly relevant as real-world agent breaches outpace benchmark-only security claims.
Timely systematization of agentic LLM risks—prompt injection, RAG poisoning, tool exploits, and multi-agent threats—with attacker models, Unsafe Action Rate metrics, and a phased deployment checklist as autonomous agents move from benchmarks to real infrastructure.
Systematizes inference-time agent risks—from transient context injection and memory poisoning to the Viral Agent Loop—and argues for zero-trust runtime controls with cryptographic tool provenance instead of semantic trust.
Surveys 1,250 arXiv papers into a what/when-to-evolve taxonomy separating bounded self-refinement from open-ended RSI—mapping evaluator design, verification hierarchies, and the research-direction bottleneck behind this week's frontier-lab automation discourse.
Instead of opaque latent CoT, CoLT emits seed tokens that trigger differentiable external decoders to unpack full reasoning steps—keeping the backbone in explicit text space while beating prior latent baselines on four math benchmarks and staying RL-compatible.
Sakana AI's Error Diffusion trains Dale-compliant dual-stream CNNs and RL agents without backprop or weight transport—reaching 61.7% CIFAR-10 and extending biologically plausible credit assignment into convolutional and PPO settings.
Defines CLI command-composition risk—benign git/npm/bash steps chaining through shared OS state—and shows a CVE-guided MOSAIC framework hits 96.59% success across five real coding agents without any prompt injection.
ALAR defaults to compact latent reasoning on routine agent turns and escalates to explicit CoT only when needed—cutting tool-use tokens up to 84.6% while matching or beating full-CoT accuracy on agentic search and tool benchmarks.
The whole path in order — linear algebra through a working GPT-2.
Start at part 1Full archive — sort by date or filter by tag.
View postsEvery tag on the blog, each with its own page.
Browse topicsBooks, videos, courses, papers, and tools worth your time.
View libraryWho writes this, and how the posts get made.
Read moreSubscribe for new posts in your reader.
Subscribe