Chapter 1

LLM Fundamentals

NLP → Transformer → training → applications

Full path: 9-chapter hub · ? RAG

Tap right · Space · F fullscreen

Scope

Chapter 1 of 9

  1. ? LLM — this deck
  2. ? RAG — look up first, then think
  3. ?—? Agent Core + Retrieval
  4. ?—? LangGraph, MCP/A2A, OpenClaw
  5. ?—? Multi-agent/KG, Multimodal

present-hub.html

Chapter 1

Why did LLMs emerge?

From NLP to LLM: why traditional AI was not enough

Ch 1

Two questions to start

Why did Siri feel dumb?

Short replies, lost context, brittle rules

Why did ChatGPT feel smart?

Long context, fluent reasoning, general tasks

Classical NLP: cannot model long-range context
Ch 1

NLP = everything is prediction

A Language Model predicts the next token given all previous tokens.

ChatGPT "understands" — but under the hood: next-token prediction, one token at a time.

TaskWhat model predicts
Chat / QAP(answer token | question + history)
TranslationP(target token | source + target so far)
ASRP(text token | audio frames)
Ch 1

Prediction loop

Context Model P(next) Pick Append Train: compare vs truth. Infer: loop until EOS.
Ch 1

Four failures of traditional AI

Rules Infinite variation — rules break
ML Hand-crafted features; not end-to-end
RNN Serial, slow; distant tokens fade
CNN Local window; many layers for global view
Transformer (2017): any token ↔ any token — parallel & global
Ch 1

Long-range dependency

I grew up in Germany ... (50 words) ... I speak fluent ______

RNN: 49 hops "Germany" signal fades May predict wrong language Attention: 1 hop Correct: German / Deutsch
Ch 1 · Math

Language Model definition

$$\mathcal{P}(s_1 \ldots s_n) = \prod_{j=1}^{n} \mathcal{P}(s_j \mid s_1, \ldots, s_{j-1})$$

Modeling language = modeling next-token probability, repeatedly.

GPT approximates $\mathcal{P}(s_{t+1} \mid s_{1:t})$ with a Transformer.
Ch 1 · Example

Numeric example

Vocabulary {I, love, Paris}. Sentence "I love Paris":

$$\mathcal{P}(\text{I love Paris}) = \mathcal{P}(\text{I}) \cdot \mathcal{P}(\text{love}\mid\text{I}) \cdot \mathcal{P}(\text{Paris}\mid\text{I love})$$

$0.02 \times 0.12 \times 0.09 = \mathbf{0.000216}$

Generation reverses this: sample next token, append, repeat.
Chapter 2

Why Transformer changed the world

Before formulas — understand the revolution

Ch 2

RNN vs CNN vs Attention

ArchPatternProblem
RNNT1 → T2 → ... → T50Serial; forgets distant tokens
CNNLocal windowsCannot see global context in one layer
AttentionT1 ↔ T2 ↔ ... ↔ T50Direct links; parallel; global
Token1 ↔ Token50 in one step, not 49 hops.
Ch 2

Attention: "Who matters most?"

The animal did not cross the street because it was tired. it animal street weight 0.84 → "animal"
Ch 2 · Math

Scaled dot-product attention

$$\text{Attention}(Q,K,V)=\text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V$$
  • Q Query — what am I looking for?
  • K Key — how matchable is each token?
  • V Value — what information do I take?
Ch 2 · Math

Self-attention with projections

$$\text{SelfAttn}(X)=\text{softmax}\!\left(\frac{XW_Q(XW_K)^\top}{\sqrt{d_k}}\right)XW_V$$

Mini example (cat, sat, mat) for "sat": scores → softmax [0.26, 0.58, 0.16]

Multi-Head = several $(W_Q,W_K,W_V)$ in parallel → syntax, coreference, semantics.
Ch 2 · Architecture

Three Transformer families

ModelStackExamples
Encoder-DecoderBoth; cross-attentionTranslation, Whisper
Decoder-onlyCausal masked self-attnChatGPT, Llama
Encoder-onlyBidirectionalBERT, embeddings
ChatGPT path: GPT = decoder-only, autoregressive next-token loop.
Ch 2 · GPT

GPT = Decoder-only

GPT (Decoder-only) Prompt + generated tokens Embedding + RoPE Decoder Block x L Causal Masked Multi-Head Self-Attention FFN + LayerNorm (no cross-attn) LM Head → Softmax Sample next token → append Autoregressive loop
Ch 2 · Inference

How an LLM answers

Q: "What is the capital of France?"

  1. Tokenize → embed + positional encoding
  2. Forward through L Transformer layers
  3. LM Head → logits over vocabulary
  4. Softmax → P(next token | context)
  5. Pick token → "Paris"
  6. Append → repeat until <EOS>
Autoregressive: one token at a time.
Ch 2 · Example

Decode: predicting "Paris"

TokenParisLondonthe<EOS>
logit3.21.10.3-1.0
softmax77.9%9.5%4.3%1.2%

Context grows: "Paris" "." <EOS>

$$\mathcal{P}_i=\frac{e^{z_i/T}}{\sum_j e^{z_j/T}} \quad (T=\text{temperature})$$
Ch 2 · Sampling

Inference parameters

ParamEffectUse when
temperatureLow = peaked; high = random0 facts/code; 0.7 chat; >1 creative
top-pSample from nucleus set~0.9 default
top-kKeep k highest tokensk=1 greedy; 40-50 chat
TokenT=0.1T=1.0T=2.0
Paris99.2%77.9%52.1%
Chapter 3

Why intelligence emerges

Emergent abilities & Scaling Laws

Ch 3

Emergence & scaling

< 1B params

Advanced autocomplete. Weak reasoning.

10B - 100B+

CoT, coding, planning, translation jump.

$$L(N,D) \approx \left(\frac{N_c}{N}\right)^{\alpha_N} + \left(\frac{D_c}{D}\right)^{\alpha_D}$$

Loss decreases predictably with params N, data D, compute C.

Ch 3

What is an LLM?

Definition: Transformer LM with 10B+ parameters, trained on massive text.

Solves tasks via text generation (next-token prediction at scale).

TechniqueExample
ScalingGPT-1 117M → GPT-3 175B
Ability elicitingCoT: "Think step by step"
AlignmentRLHF, DPO
Tool manipulationAPI calls (see RAG course)
Chapter 4

Modern LLM Training

Pretrain → SFT → RLHF

Ch 4 · Pretrain

Pretraining

$$\mathcal{L}_{\text{PT}} = -\frac{1}{N}\sum_{t=1}^{N} \log \mathcal{P}_\theta(x_t \mid x_{1:t-1})$$

Data: web crawl, books, Wikipedia, code. Distributed GPU training.

After pretrain: model continues text — cannot follow instructions yet.
Ch 4 · SFT

Instruction tuning (SFT)

After pretrain

Q: 2+2=?
May ramble about math...

After SFT

Q: 2+2=?
Answer: 4.

Train only on assistant reply tokens. Methods: Full FT, LoRA, QLoRA.

Ch 4 · RLHF

RLHF alignment

  1. SFT — imitation on human-written answers
  2. Reward model — humans rank responses; train RM
  3. PPO — maximize reward + KL penalty vs SFT
$$\max_\theta \; \mathbb{E}[r(x,y)] - \beta\, D_{\text{KL}}(\pi_\theta \,\|\, \pi_{\text{SFT}})$$
ChatGPT = Pretrain + SFT + RLHF. Eval: MMLU, win rate, TruthfulQA.
Chapter 1 complete

LLM Fundamentals done

Scroll: course-full-llm.html (appendix sections for agent preview)

Next: ? RAG
Full path: 9-chapter hub

Intro
1 / 1 Hub Space F