LLM Training & Generation · 训练与生成入门
LLM Training and Generation: Small Implementation Exercises
PaperLeet original curriculum; primary documentation linked per exercise
From next-token targets and response masks to candidate probabilities, token-weighted accumulation and global clipping.
What you will implement
Seven standalone exercises with explicit shifting, masks, ties and reductions; deterministic probability experiments and actual small-model gradient updates.
- 86. Next-token Loss: Shifted Causal Targets
Align predictions with the next token and compute a stable average loss.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
- 87. SFT Loss Mask: Train on Response Tokens
Shift targets, mask prompt and padding tokens, and average loss over valid response tokens.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
- 94. Multi-turn SFT: Assistant Targets and Conversation Boundaries
Select assistant targets across turns while excluding padding and cross-conversation predictions.
Implement one boolean mask; roles, conversation IDs and truncated sequences are supplied. No tokenizer, chat template or attention-mask construction.
- 88. Top-k: Temperature and Candidate Probabilities
Apply temperature, keep exactly k candidates, and renormalize their probabilities.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
- 89. Top-p: Cumulative Probability and Nucleus Candidates
Keep the smallest candidate set that reaches the probability threshold, then restore vocabulary order.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
- 90. Gradient Accumulation: Valid-token Weighting
Weight microbatch gradients by valid token counts to match one combined batch.
Implement one core function; independent tests and deterministic synthetic experiments are provided.
- 91. Global Gradient Clipping: One Norm for All Parameters
Compute one gradient norm across all parameters and apply a shared clipping scale.
Implement one core function; independent tests and deterministic synthetic experiments are provided.