Phase III · Days 37–44 · ~20 hours Prerequisites: 06-llm-training-alignment Learning Objectives: Understand quantization, ICL, long context, RAG; connect LLMs to robotics
Day 37 · ~3 hours
The Memory Wall:
| Model | Params | FP16 Memory | INT8 Memory | INT4 Memory |
|---|---|---|---|---|
| 7B | 7B | 14 GB | 7 GB | 3.5 GB |
| 13B | 13B | 26 GB | 13 GB | 6.5 GB |
| 70B | 70B | 140 GB | 70 GB | 35 GB |
Robotics Motivation: - Robots have limited onboard compute (Jetson Oort, embedded GPUs) - Inference latency matters for real-time control - Power budget constraints on mobile robots - Can’t stream to cloud for every decision - Quantization makes LLM-powered robotics feasible on the edge
What Is Quantization? Map high-precision floating-point weights to lower-precision integers.
Linear (Affine) Quantization:
where: - $s$ = scale factor - $z$ = zero point - $x_q$ = quantized integer
Dequantization:
Symmetric vs Asymmetric: - Symmetric: $z = 0$, range $[-\alpha, \alpha]$ - Asymmetric: $z \neq 0$, range $[\text{min}, \text{max}]$
Post-Training Quantization (PTQ): Quantize after training is complete — no retraining needed.
| Method | Precision | Approach | Quality |
|---|---|---|---|
| GPTQ | 3-4 bit | Layer-wise, uses calibration data | Good |
| AWQ | 4 bit | Activation-aware, protects salient channels | Better |
| GGUF | 2-8 bit | CPU-friendly format (llama.cpp) | Good |
| SqueezeLLM | 3-4 bit | Sensitivity-based mixed precision | Good |
| QuIP# | 2-4 bit | Incoherence processing + lattice codebooks | Excellent at 2-bit |
GPTQ (GPT-Quantized):
For each layer:
1. Compute Hessian from calibration data
2. Quantize weights column-by-column
3. Update remaining columns to compensate for error
4. Move to next layer
AWQ (Activation-Aware Weight Quantization): - Key insight: 1% of weights are “salient” (much larger activation magnitudes) - Protect salient channels by scaling them up before quantization - Searching for optimal per-channel scales using calibration data - Often better quality than GPTQ at same bit-width
GGUF Format (llama.cpp):
- Designed for CPU inference (no GPU required)
- Mixed precision: different layers can use different bit-widths
- Excellent for edge deployment and consumer hardware
- Naming: Q4_K_M = 4-bit, K-quant, medium quality
Benchmark: LLaMA 2 7B on Common Benchmarks
| Precision | Memory | Tokens/sec (A100) | MMLU | Perplexity |
|---|---|---|---|---|
| FP16 | 14 GB | 40 t/s | 45.3 | 5.47 |
| INT8 | 7 GB | 55 t/s | 45.1 | 5.49 |
| INT4 (GPTQ) | 3.5 GB | 65 t/s | 44.2 | 5.68 |
| INT4 (AWQ) | 3.5 GB | 65 t/s | 44.8 | 5.55 |
| INT3 | 2.6 GB | 70 t/s | 41.5 | 6.31 |
| INT2 | 1.8 GB | 75 t/s | 35.2 | 8.44 |
Key Observations: - INT8 is nearly lossless (<0.5% quality drop) - INT4 is the sweet spot (75% memory reduction, <2% quality drop) - Below INT4, quality degrades rapidly - AWQ consistently beats GPTQ at same bit-width
Idea: Simulate quantization during training so the model adapts.
# Straight-Through Estimator (STE)
# Forward: quantize
x_q = quantize(x)
# Backward: pass gradient through as if no quantization happened
x.grad = x_q.grad # STE approximation
QAT vs PTQ: - QAT: better quality, but requires retraining (expensive) - PTQ: no retraining, but slightly lower quality - For most LLM use cases, PTQ is sufficient (INT4/INT8)
Decision Tree:
Need to run LLM on edge device?
├── GPU available (Jetson, etc.)
│ ├── Memory > 8GB → INT8 (bitsandbytes)
│ └── Memory < 8GB → INT4 (AWQ or GPTQ)
├── CPU only
│ └── Use GGUF (llama.cpp)
│ ├── RAM > 16GB → Q5_K_M
│ └── RAM < 16GB → Q4_K_M or Q3_K_M
└── Cloud/datacenter
├── Latency critical → INT8 or FP8
└── Quality critical → FP16 or BF16
Day 38 · ~3 hours
Definition: The ability of LLMs to learn new tasks from examples provided in the prompt, without any gradient updates.
Zero-shot:
"Translate English to French: Hello world"
One-shot:
"Translate English to French:
Good morning → Bonjour
Hello world →"
Few-shot:
"Translate English to French:
Good morning → Bonjour
How are you? → Comment allez-vous?
Thank you → Merci
Hello world →"
The Remarkable Property: The model was never trained on this specific translation task format, yet it learns the pattern from examples in the prompt.
The Bayesian Interpretation (Xie et al., 2022):
Pretraining exposes the model to many “concepts” (latent variables). At inference time, the demonstrations help the model identify the correct concept:
where $c$ is the latent concept inferred from demonstrations.
Intuition: The model doesn’t literally learn a new algorithm. It uses the examples to identify which of its pretrained capabilities to activate.
Observations: - Small models (< 1B) show weak or no ICL ability - ICL appears as an emergent capability at ~1B+ parameters - More demonstrations help, but with diminishing returns - ICL improves continuously with model scale
Hypotheses for Emergence: 1. Mesa-optimization: The transformer has learned an internal optimization algorithm (gradient descent in the forward pass via attention) 2. Bayesian inference: Larger models have better concept coverage from pretraining 3. Distributional matching: More parameters = better distribution matching from context
Standard Prompting:
Q: Roger has 5 tennis balls. He buys 2 cans of 3 balls each.
How many tennis balls does he have now?
A: 11
Chain-of-Thought:
Q: Roger has 5 tennis balls. He buys 2 cans of 3 balls each.
How many tennis balls does he have now?
A: Roger starts with 5 balls. He buys 2 cans × 3 balls = 6 balls.
Total = 5 + 6 = 11.
Why CoT Works: - Decomposes complex reasoning into sequential steps - Each step is within the model’s capability - The chain serves as working memory (since transformers have no explicit memory) - Dramatically improves math, logic, and multi-step reasoning
Variants: - Zero-shot CoT: “Let’s think step by step” - Few-shot CoT: Provide examples with reasoning chains - Self-consistency: Sample multiple chains, take majority vote - Tree of Thought: Explore multiple reasoning branches
Surprising Findings: 1. Label correctness matters less than expected — random labels still help format 2. Example order matters — different orderings can swing accuracy by 20%+ 3. Format matters more than content — the structure of examples matters 4. Recency bias — the model weighs later examples more heavily
Practical Implications: - Carefully curate and order few-shot examples - Include diverse examples covering edge cases - Test multiple orderings and templates - Use self-consistency to reduce variance
Day 39 · ~3 hours
| Model | Year | Context Length |
|---|---|---|
| GPT-3 | 2020 | 2,048 |
| GPT-3.5 | 2023 | 4,096 → 16,384 |
| GPT-4 | 2023 | 8,192 → 32,768 → 128K |
| Claude 3 | 2024 | 200K |
| Gemini 1.5 | 2024 | 1M → 2M |
| LLaMA 3.1 | 2024 | 128K |
| GPT-4.1 | 2025 | 1M |
Why Long Context Matters: - Entire codebases in one prompt - Full documents without chunking - Multi-turn conversations without forgetting - Robotics: Full environment history, task specifications, sensor logs
Standard Attention Complexity:
At 1M tokens: Attention matrix = $10^6 \times 10^6 = 10^{12}$ entries — infeasible.
Flash Attention: - Same exact computation as standard attention - But done in tiles/blocks that fit in GPU SRAM - IO-aware: minimizes data movement between HBM and SRAM - Result: 2-4× faster, $O(n)$ memory
Sliding Window Attention (Mistral): - Each token attends to only $w$ nearest tokens - Window size $w$ = 4096 typically - Information propagates through layers: effective receptive field = $w \times L$ - Dramatically reduces memory for long sequences
Multi-Query/Grouped-Query Attention (GQA):
| Variant | Key-Value Heads | Memory |
|---|---|---|
| Multi-Head (MHA) | $h$ heads (= query heads) | Full |
| Multi-Query (MQA) | 1 head (shared) | $1/h$ of KV cache |
| Grouped-Query (GQA) | $g$ groups ($1 < g < h$) | $g/h$ of KV cache |
RoPE Review: Encodes position by rotating query and key vectors:
where $m$ is position and $\theta$ depends on dimension.
Extending Context with RoPE Scaling:
| Method | Approach | Quality |
|---|---|---|
| Linear scaling | Divide position by factor $s$ | Decent, some quality loss |
| NTK-aware | Scale the base frequency | Better perplexity |
| YaRN | NTK + attention scaling + temperature | Best overall |
| Dynamic NTK | Adjust scaling during inference | Adaptive |
Example (extending 4K → 32K):
# Linear scaling: simple but lossy
position = position / 8.0 # scale factor = target / original = 32K / 4K
# NTK-aware: scale the base frequency
base = 10000 * (scale_factor ** (dim / (dim - 2)))
| Aspect | RAG | Long Context |
|---|---|---|
| Latency | Retrieval overhead | Higher (quadratic attention) |
| Cost | Retrieval infra + short prompt | Long prompt = more tokens |
| Accuracy | Depends on retrieval quality | All info available (no retrieval errors) |
| Freshness | Can update index anytime | Must re-prompt |
| Scalability | Handles millions of docs | Limited by context window |
| Complexity | Chunking, embedding, retrieval pipeline | Just a longer prompt |
Guidance: Use RAG for large document collections; use long context for documents that fit in the window and where you need holistic understanding.
Day 40 · ~3 hours
Retrieval-Augmented Generation augments LLM generation with external knowledge by retrieving relevant documents at inference time.
Pipeline:
User Query
│
↓
┌──────────┐ ┌──────────────┐ ┌──────────────┐
│ Embed │────→│ Vector Search│────→│ Top-K Results│
│ Query │ │ (similarity) │ │ │
└──────────┘ └──────────────┘ └──────┬───────┘
│
↓
┌────────────────┐
│ LLM Generation │
│ (query + docs) │
└────────┬───────┘
│
↓
Response
Text Embedding:
- Map text chunks to dense vectors
- Models: OpenAI text-embedding-3-small, BGE, E5, GTE
- Dimension: 384–3072
- Trained with contrastive learning: similar texts → close vectors
Vector Databases: - FAISS (Meta) — in-memory, fast - Pinecone — managed cloud - Weaviate — open source - Chroma — lightweight, local - pgvector — PostgreSQL extension
Similarity Search:
Retrieve top-$k$ documents with highest cosine similarity.
How you split documents matters enormously:
| Strategy | Chunk Size | Overlap | Best For |
|---|---|---|---|
| Fixed-size | 512 tokens | 50 tokens | Simple, general purpose |
| Sentence-based | Variable | Sentence boundary | Structured text |
| Paragraph-based | Variable | Paragraph boundary | Well-formatted docs |
| Recursive | 512–1024 | Hierarchical | Code, nested structures |
| Semantic | Variable | Meaning-based | Complex documents |
Key Principle: Chunks should be self-contained units of meaning.
Naive RAG → Advanced RAG → Modular RAG:
Use RAG when: - Document collection is large (>1M tokens total) - Documents update frequently - Cost per query matters (shorter prompts = cheaper) - Need attribution (which document answered the question) - Latency requirements are moderate
Use Long Context when: - All documents fit in context window - Need holistic understanding across documents - Simplicity is paramount - Documents are interdependent
Day 41 · ~3 hours
LLMs are powerful reasoners but: - They operate in token space (text) - They can’t perceive the physical world - They can’t take physical actions - They lack grounding (words ≠ physics)
Robots have complementary strengths: - Can sense the environment (cameras, lidar, touch) - Can take physical actions (motors, grippers) - But struggle with high-level reasoning and planning
The opportunity: Combine LLM reasoning with robot perception and action.
Paper: “Do As I Can, Not As I Say” (Google, 2022)
Key Idea: LLM provides task planning; a learned affordance model grounds the plan in what the robot can actually do.
Architecture:
User: "I spilled my drink, can you help?"
│
↓
┌─────────────────┐
│ LLM (SayCan) │ → suggests actions with probabilities
│ P(useful | task) │ "Pick up sponge" (0.8)
└────────┬────────┘ "Go to kitchen" (0.3)
│ "Pick up cup" (0.5)
│
↓
┌─────────────────┐
│ Affordance Model │ → which actions are physically feasible
│ P(success | env) │ "Pick up sponge" (0.9) ← sponge visible
└────────┬────────┘ "Go to kitchen" (0.7)
│ "Pick up cup" (0.1) ← no cup nearby
│
↓
Combined Score: P(useful) × P(success)
→ "Pick up sponge" (0.72) ✓ SELECTED
→ "Go to kitchen" (0.21)
→ "Pick up cup" (0.05)
Scoring:
Paper: “Code as Policies” (Google, 2023)
Key Idea: Instead of outputting actions directly, the LLM writes Python code that calls robot APIs.
# LLM generates this code from natural language instruction
# "Stack the red blocks on the blue plate"
def stack_red_on_blue():
blue_plate = detect_object("blue plate")
red_blocks = detect_objects("red block")
# Sort by size for stable stacking
red_blocks.sort(key=lambda b: b.size, reverse=True)
for i, block in enumerate(red_blocks):
target_pos = blue_plate.position + [0, 0, i * BLOCK_HEIGHT]
pick(block)
place(target_pos)
Advantages: - Compositional (can combine primitives) - Interpretable (code is readable) - Precise (exact coordinates, loops, conditionals) - Debuggable (can inspect code before execution)
Paper: “Inner Monologue: Embodied Reasoning through Planning with Language Models” (Google, 2022)
Key Idea: Continuous feedback loop between LLM planner and robot perception.
LLM: "First, pick up the red cup"
Robot: [attempts pick] → Success detector: FAILED (cup slipped)
LLM: "The pick failed. Let me try a different grasp angle."
Robot: [re-attempts] → Success detector: SUCCESS
LLM: "Good. Now place the cup on the table."
...
Feedback Types: - Success/failure detection - Scene description (from vision model) - Human feedback (verbal corrections) - Object state changes
The Fundamental Problem:
LLM alone: text → text (no perception, no action)
SayCan: text → text → skill selection (limited actions)
Code-as-Policies: text → code → API calls (requires predefined APIs)
Inner Monologue: text ↔ text + perception (feedback but still text-mediated)
What we really want:
VLA: vision + text → continuous actions (end-to-end)
Why VLAs Are the Next Step: 1. End-to-end: No hand-designed skill libraries or APIs 2. Grounded: Vision provides direct perception 3. Continuous: Output motor commands, not discrete text 4. Generalizable: One model, many tasks and environments
The Bridge from LLMs to VLAs: - LLMs provide the language understanding and reasoning backbone - Vision encoders (from CLIP, SigLIP) provide visual grounding - Action heads output continuous motor commands - All three are combined in a Vision-Language-Action model
| System | Approach | Limitations |
|---|---|---|
| SayCan | LLM + affordances | Needs predefined skills |
| Code-as-Policies | LLM → code | Needs predefined APIs |
| Inner Monologue | LLM + feedback | Text-mediated, slow |
| TidyBot | LLM + preferences | Category-level planning only |
| PROGPROMPT | LLM → programs | Limited to scripted actions |
| Voyager (Minecraft) | LLM + code library | Virtual environment only |
Common Limitation: All require predefined action primitives. The LLM plans, but doesn’t act. VLAs will close this gap.
Days 42–44 · ~8 hours
Goal: Fine-tune a small LLM (1-3B parameters) to be a knowledgeable robotics assistant using LoRA, combining all Phase III skills.
See: projects/03-robotics-assistant/README.md
LoRA Fine-tuning: - Apply QLoRA to a base model (LLaMA 3.2 1B or similar) - Train on robotics domain data - Compare base vs fine-tuned outputs
Evaluation: - Create a robotics QA evaluation set (50+ questions) - Benchmark: MMLU subset + domain-specific questions - Compare: base model vs LoRA model vs GPT-4
Deployment: - Quantize the fine-tuned model (INT4) - Measure inference latency - Run on consumer hardware
Report: - Training curves (loss, eval metrics) - Example conversations showing improvement - Resource usage analysis
knowledge/ directory)| Paper | Year | Key Contribution |
|---|---|---|
| SayCan (Ahn et al.) | 2022 | LLM + affordance grounding for robot planning |
| Code as Policies (Liang et al.) | 2023 | LLM generates robot control code |
| Inner Monologue (Huang et al.) | 2022 | Feedback loop between LLM and robot |
| GPTQ (Frantar et al.) | 2022 | Post-training quantization for LLMs |
| AWQ (Lin et al.) | 2023 | Activation-aware weight quantization |
| Chain-of-Thought (Wei et al.) | 2022 | Step-by-step reasoning in LLMs |
| An Explanation of ICL (Xie et al.) | 2022 | ICL as implicit Bayesian inference |
| Flash Attention (Dao et al.) | 2022 | IO-aware exact attention |
| RoPE (Su et al.) | 2021 | Rotary position embeddings |
| YaRN (Peng et al.) | 2023 | Efficient context extension |
| Self-RAG (Asai et al.) | 2023 | Self-reflective RAG |
| Voyager (Wang et al.) | 2023 | LLM-powered agent in Minecraft |
Quantization as Lossy Compression: - FP16 → INT4: compress each weight from 16 bits to 4 bits (4× compression) - The model’s knowledge is remarkably robust to this compression - Why? Because the information is distributed across billions of parameters - Analogy: JPEG compression — lose some details, preserve the structure
ICL as Runtime Compression: - During pretraining, the model compressed patterns from training data - At inference, ICL examples activate the right compressed representation - The model doesn’t learn new weights — it selects which compressed knowledge to use - ICL = indexing into a compressed knowledge base using demonstrations
The Full Path to VLAs:
Phase I: Math foundations (the language of compression)
Phase II: Vision models (compress visual information)
Phase III: LLMs (compress language, reasoning, and knowledge)
├── Training pipeline: pretrain → SFT → DPO
├── Efficiency: LoRA, quantization
├── Capabilities: ICL, CoT, long context
└── Robotics connection: SayCan, Code-as-Policies
Phase IV: Multimodal (bridge vision and language) ← NEXT
Phase V: VLAs (bridge language, vision, and action)
The progression from LLMs to VLAs:
Phase III gives us: - Understanding of how LLMs encode knowledge (for VLA backbone) - LoRA for efficient adaptation (VLAs will be fine-tuned with LoRA) - Quantization for edge deployment (VLAs must run on robots) - ICL/CoT for flexible reasoning (VLAs inherit this from the LLM backbone)
Next: Phase IV will bridge vision and language (CLIP, Flamingo, LLaVA) → then Phase V combines everything into VLAs (RT-2, OpenVLA, π₀).
These notes are part of the LLM-to-VLA curriculum. Previous: 06 — LLM Training & Alignment Next: 08 — Vision Transformers