AI Lab — All Lessons Summary
AI Lab is a free, interactive learning site that explains how AI models work and how they are made smaller and faster: 14 lessons, 64 steps, with hands-on diagrams, sliders, and quizzes, in Japanese, English, and Chinese. This page is a static summary of every lesson for readers and crawlers without JavaScript. The interactive version is at lab.militou.com.
1. How Models Are Made
The full pipeline of a modern language model: large-scale pretraining on raw text, then instruction tuning and RLHF to make it follow instructions, then fine-tuning for specific uses.
- Pretraining learns language and knowledge from massive text; it is the most compute-heavy stage.
- Instruction tuning and RLHF turn a text predictor into an assistant that follows instructions.
- Most training done at home (LoRA etc.) is only the final fine-tuning stage.
2. LoRA Basics
LoRA freezes the original weights and learns a low-rank update W + BA instead of updating the full weight matrix.
- With rank r = 8, trainable parameters drop to roughly 0.1% of full fine-tuning.
- Rank 8 is usually enough for style changes; rank 16–32 suits more complex changes.
- The adapter is a small separate file that can be swapped per task on one base model.
3. Quantization Basics
Quantization stores weights in fewer bits, trading a little precision for a much smaller, faster model.
- FP16 → INT8 halves the model size; INT4 quarters it.
- An 8B model needs about 16 GB at FP16, about 8 GB at INT8, about 4–5 GB at INT4.
- Scales and zero-points map small integer ranges back to real values.
4. Knowledge Distillation Basics
A small student model learns from a large teacher's output distribution, not only from the correct answer.
- Temperature T above 1 softens the teacher's probabilities and exposes near-miss knowledge.
- T = 1 is the ordinary probability distribution.
- The student can approach the teacher's behavior at a fraction of the size and cost.
5. Diffusion Model Basics
Image generation models learn to reverse a noising process: noise is added step by step until the image is pure noise, and the model learns to remove it step by step.
- Generation starts from pure noise and denoises for tens of steps, guided by the text prompt.
- Work happens in a compressed latent space, not directly on pixels.
- More steps generally mean better quality and slower generation.
6. Parts of a Model
The building blocks of a Transformer: token embeddings, attention, MLP blocks, layer normalization, and the output head.
- Attention mixes information across positions; MLPs store much of the model's knowledge.
- Parameter counts are dominated by embeddings, attention projections, and MLPs.
- Depth (layers) and width (hidden size) define the model's scale.
7. Three Ways to Shrink a Model, and Pruning
The three main compression routes are quantization, distillation, and pruning, and they can be combined.
- Quantization: fewer bits per weight. Distillation: a smaller student. Pruning: removing unimportant weights.
- Each route trades size and speed against quality differently.
- Pruning can be unstructured (individual weights) or structured (whole channels/heads).
8. Making Inference Faster
Inference speed is often limited by memory bandwidth rather than compute.
- The KV cache reuses attention keys/values instead of recomputing them for every token.
- Batching multiple requests amortizes weight loading across users.
- Quantization speeds inference by reducing the bytes moved per token.
9. Sampling at Generation Time
Temperature and top-p control randomness when a model picks the next token.
- Lower temperature makes output more deterministic; higher temperature makes it more varied.
- Top-p restricts choices to the smallest token set covering p of the probability mass.
- This temperature is a generation setting, different from distillation temperature. Context length is the maximum tokens considered at once.
10. How to Evaluate and Compare
Fair comparison requires fixed conditions and metrics matched to the purpose.
- Use the same prompts and the same generation settings across candidates.
- Choose metrics by purpose: accuracy benchmarks, perplexity, speed, memory use.
- Change one variable at a time so measured differences are meaningful.
11. The PEFT Family
Parameter-efficient fine-tuning methods (LoRA, adapters, prefix/prompt tuning) freeze almost all of the base model and train only a small added part.
- One base model can host many cheap, swappable adaptations.
- Methods differ in where the trainable part is inserted: weights, inputs, or activations.
- Training cost and storage drop by orders of magnitude versus full fine-tuning.
12. QLoRA in Practice
QLoRA quantizes the frozen base model to 4-bit NF4 and trains LoRA adapters on top.
- NF4 is a 4-bit data type designed for normally distributed weights.
- Double quantization also quantizes the quantization constants to save more memory.
- Paged optimizers prevent memory spikes from crashing long training runs.
13. Datasets and Training Tuning
Practical fine-tuning is mostly data work: curation, consistent formatting, and choosing learning rate, epochs, and rank.
- Too many epochs on a small dataset causes overfitting and memorization.
- Data quality usually matters more than dataset size.
- Consistent formatting between training and inference prevents silent quality loss.
14. The Few-Step Family
Few-step generation methods compress a diffusion model's tens of denoising steps into roughly 1–8 steps.
- Consistency models and step distillation (LCM, Lightning-style LoRAs) are the main approaches.
- The trade is a small quality loss for a large speed gain.
- Few-step models are what make near-real-time image generation possible.