richeddy shared this post · Oct 10
Codewithbrij

Most explanations of LLMs stop at "it predicts the next word." Here is what actually happens inside the model.

  1. Tokenizer: "What is gravity?" is split into subword tokens and mapped to integer IDs.
  2. Embeddings: each ID looks up a learned vector. In Llama 3 8B, that vector has 4,096 numbers.
  3. Transformer blocks: multi-head self-attention computes softmax(QKᵀ/√d)·V, so every token weighs every earlier token. A feed-forward network (SwiGLU in Llama) transforms the result. This repeats across N layers, 32 in Llama 3 8B.
  4. LM head: the final vector is projected onto the whole vocabulary (about 128K tokens in Llama 3) and turned into probabilities.
  5. Sampling: greedy, top-k, top-p and temperature decide which token is picked.
  6. Speculative decoding: a small draft model proposes several tokens, and the large model verifies them all in one pass.

Why some models feel fast and others slow:
Prefill processes your whole prompt in parallel and is compute-bound.
Decode generates one token per step and is memory-bandwidth-bound.
The KV cache grows with every token, and is often the real bottleneck.
FlashAttention and INT8/INT4 quantization exist to fight exactly these limits.

Save this one. It is the mental model behind every inference optimization you will read about.

#LLM #DeepLearning #AIEngineering

541 13 247
Ashish Nayak Super Oct 10
AI & Cloud Insights 🙌🏼 Oct 9