LLM Basics
Practice LLM Basics in Python with research-based exercises, implementation guides, and tests on PaperLeet.
- Next-token Loss: Shifted Causal Targets
Align predictions with the next token and compute a stable average loss.
- SFT Loss Mask: Train on Response Tokens
Shift targets, mask prompt and padding tokens, and average loss over valid response tokens.
- Multi-turn SFT: Assistant Targets and Conversation Boundaries
Select assistant targets across turns while excluding padding and cross-conversation predictions.
- Top-k: Temperature and Candidate Probabilities
Apply temperature, keep exactly k candidates, and renormalize their probabilities.
- Top-p: Cumulative Probability and Nucleus Candidates
Keep the smallest candidate set that reaches the probability threshold, then restore vocabulary order.
- Gradient Accumulation: Valid-token Weighting
Weight microbatch gradients by valid token counts to match one combined batch.
- Global Gradient Clipping: One Norm for All Parameters
Compute one gradient norm across all parameters and apply a shared clipping scale.