Library

Volume 2 · GenAI Foundations & LLM Architecture

Notebook
Sign in

Cheat Sheet — Part 1: GenAI Foundations & LLM Architecture

659 words · about 3 min

Talk in tiers — frontier / mid / small — not in version numbers. Specific model names date within months; the trade-off structure doesn’t.

Key Terms

Term In one line
Self-attention Every token computes relevance scores against every other token — O(N²) in sequence length
Multi-head attention Several attention computations in parallel, each learning a different relation type
Decoder-only Causal, left-to-right masking — the architecture of essentially every modern generative LLM
Encoder-only Bidirectional (BERT-style) — classification, embeddings, NER
Encoder-decoder Cross-attention — translation, seq2seq
RoPE Rotary position embedding — what Llama, Mistral and most modern LLMs use; ALiBi is the linear-bias alternative
MoE Mixture of Experts — route each token to a few of many expert FFNs; huge parameter count, modest active compute
KV-cache Caches key/value tensors so each new token only computes its own query
PagedAttention Virtual-memory-style paging of the KV-cache (vLLM); removes fragmentation
Speculative decoding Small draft model proposes tokens, large model verifies them in one parallel pass
LoRA / QLoRA Freeze base weights, train low-rank adapters; QLoRA adds a quantized base
DPO Direct Preference Optimization — preference alignment without a separate reward model or RL loop

Numbers That Matter

Figure Value Where it bites
Attention complexity O(N²) in sequence length Why long context is expensive, and why FlashAttention exists
LoRA trainable params ~0.1% of base weights The reason adapters are swappable per tenant
Quantization ladder FP16 → INT8 → INT4 Each step roughly halves memory; measure quality loss, don’t assume it
Tier cost spread 10–30× between cheapest and strongest The entire basis of the model-routing argument
Traffic mix ~70% simple · ~25% medium · ~5% complex The routing distribution to quote

Decision Rules

  • The four-way choice: adding factual knowledge → RAG. Teaching domain terminology or style → fine-tune. Complex reasoning patterns → fine-tune. Simple task with examples → few-shot prompt. Cost reduction at scale → fine-tune a smaller model.
  • Escalate cheapest-first: optimize the prompt (1–2 days) → add retrieval (weeks) → fine-tune (weeks–months) → train from scratch (never, in enterprise). State this ladder before proposing a fine-tune.
  • Full fine-tune vs PEFT: full updates all weights, needs multiple GPUs and a large dataset, best ceiling. LoRA/QLoRA freeze the base — cheap, swappable, near-parity for most enterprise tasks. Default to LoRA and justify upward.
  • Decoder-only for generation, encoder-only for embeddings and classification. Using a generative model where a 384-dim encoder would do is a common cost mistake.
  • SLMs earn their place where latency, on-prem/edge deployment, data residency or per-query cost dominate — not because they’re better, but because they’re sufficient and controllable.
  • Serving optimizations compose: continuous batching for throughput, PagedAttention for memory, speculative decoding for latency, quantization for footprint. Name which problem you’re solving before picking one.

Say This Out Loud

“Attention computes relevance between every pair of tokens, which is what lets the model resolve a pronoun to a noun forty tokens back. The cost is that it’s quadratic in sequence length — that’s the whole reason long context is expensive, and why FlashAttention tiles the computation so it never materializes the full N-by-N matrix.”

“I choose between RAG and fine-tuning by asking what’s actually missing. If the model lacks facts — our policies, our documents, anything that changes — that’s retrieval, because you don’t want to retrain every time a document changes. If it lacks behaviour — our terminology, our output format, a reasoning pattern specific to our domain — that’s fine-tuning. Most of the time it’s both, and the mistake is fine-tuning to solve a knowledge problem.”

“I’d default to LoRA rather than a full fine-tune. You freeze the base model and train low-rank adapters that are about a tenth of a percent of the weights, which means it’s cheap, it fits on one GPU, and — the part people miss — you can swap adapters per tenant against a single shared base model. Full fine-tuning has a higher ceiling, but I’d want evidence from a LoRA run before spending that.”