AI Model Internals · Training Workshop
Tokens · Attention · Inference — a hands-on field guide, taught by one of the people who wrote the paper.
An engineering training for ML engineers & curious builders · ~90 min
Agenda · 14 slides
What the model actually reads & the cost of text.
Vectors, Q/K/V, and why "attention is all you need".
Encoder & decoder blocks, multi-head self-attention.
Autoregressive generation, KV cache, batching, latency.
Module 01 · Tokens
Roughly 3–4 characters for English. "transformers" ≈ trans|form|ers. A sub-word, not always a whole word.
Byte-Pair Encoding merges the most common character pairs into vocabulary entries. Bigger vocab → fewer tokens per word.
Billing & context are measured in tokens, not words.
Module 02 · Representations
Each token ID looks up a row in a big matrix → a vector in d_model dims (e.g. 4096).
king − man + woman ≈ queen. Directions in vector space encode semantics.
Attention is order-agnostic, so we add sine-wave position signals so the model knows word order.
No hard recurrence or convolution — just lookup tables and position math. That's part of why it parallelizes so well.
Module 02 · The core idea
Query asks a question, Key says "what I am", Value is "my content". Similarity(Q,K) → weight for each Value.
Weights are normalized so each position pays attention that sums to 1.
Every token weighs every other token — including itself — in a single forward pass.
Module 02 · Scaling attention
Split the d_model into h subspaces (e.g. 8–32). Each head attends to a different relationship — syntax, coreference, proximity, subject–object.
All heads' outputs are concatenated and linearly projected back to d_model.
Think of it as several "attention experts" each monitoring a different type of relationship, then voting together.
Module 03 · Architecture
Bidirectional self-attention — reads the whole input at once. e.g. BERT-style encoders.
Masked self-attention (can't see the future) + cross-attention into the encoder. e.g. GPT-style decoders.
Each block: multi-head attention → add & norm → feed-forward → add & norm. Stack N of them.
Module 04 · Inference
The model predicts the next token, appends it, and repeats — a loop until <EOS>.
The first token (prefill) is the slow part. After that, streaming feels fast.
Output length = number of forward passes. That's why "long outputs are slow."
Module 04 · Faster inference
Each step recomputes attention over ALL previous tokens. Naive: O(n²) work total.
Keys & Values from past steps are cached. Only the new Query needs computing → roughly O(n) per step.
Memory cost: KV cache grows with sequence length × layers × heads × head-dim — a big chunk of a model's VRAM during long chats.
Module 04 · Throughput
Group N requests and run them as one matrix multiply → far higher GPU utilization.
More throughput per token, but every request waits for the slowest in the batch → higher latency.
Modern servers evict finished sequences and slot new ones in mid-step — key to fast chat.
Module 04 · Memory & speed
A 7B-parameter model in FP16 ≈ 14 GB of weights.
Weights dropped to 4 bits ≈ 3.5 GB — ~4× smaller, runs on a single consumer GPU, small quality hit.
Post-training quantization rounds weights to fewer bits. Bigger speed & memory win for a small accuracy cost.
Module 04 · Advanced speed
A small fast model guesses the next K tokens in one shot.
The big model checks all K at once in a single parallel pass.
Accepted guesses are free; on a miss, resample. Net: ~2–3× decode speed-up.
Recap
| Text → input | Tokenizer → token IDs → embeddings + position |
| Understanding | Stacked blocks: multi-head self-attention + MLP + add & norm |
| Output | Autoregressive next-token loop until <EOS> |
| Speed | KV cache (O(n) per step) + batching (throughput) + quantization (memory) + speculative (latency) |
| The mantra | Attention is all you need. |
Wrap-up · Hands-on challenge
Homework: pick a model, measure its KV cache for a 4K-token conversation, and rough out its FP16 vs INT4 memory — then bring the numbers and we'll compare.
← → navigate · S presenter · T themes · F fullscreen