Every knob you can turn, explained with one running example
If you've ever tinkered with an LLM API and wondered "What does temperature actually do?" or "When do I use LoRA rank?" — this post is for you.
We'll split every important LLM parameter into two clean buckets and then walk through a single concrete example to see them in action.
📚 What We'll Learn
🎨 Bucket 1: Inference Parameters (used at generation time)
Control how the model produces text from an already-trained model.
- 🌡️ Temperature
- 🎯 Top-K
- 🎯 Top-P (Nucleus Sampling)
- 🚫 Frequency Penalty
- 🚫 Presence Penalty
- 📏 Max Tokens
- 🛑 Stop Sequences
- 🎲 Seed
🏋️ Bucket 2: Training / Fine-Tuning Parameters (used while teaching the model)
Control how the model learns from data.
- 📈 Learning Rate
- 📦 Batch Size
- 🔄 Epochs
- 📉 Warmup Steps
- 🎯 Weight Decay
- ⏹️ Gradient Clipping
- 🎯 LoRA Rank (
r) - ⚖️ LoRA Alpha (
α) - 💧 LoRA Dropout
- 💾 Quantization Bits (QLoRA)
Let's go! 🚀
🎨 Part 1: Inference Parameters
A Step-by-Step Walkthrough with ONE Example
🎬 Our Running Example
Input prompt:
Task: Predict the next word.
📊 Step 0: What Are Logits?
After the LLM processes your input, its final layer spits out logits — raw, unnormalized scores for every word in the vocabulary. They're NOT probabilities yet — just numbers indicating how "excited" the model is about each word.
For our prompt, let's say the model produces:
Higher logit = model thinks it's a better next word. Now the sampling pipeline kicks in.
🚫 Step 1: Apply Frequency Penalty
What it does: Penalizes words based on how many times they already appeared.
Formula: new_logit = logit - frequency_penalty × count
Our example — count from prompt: "love" appeared 2 times.
With frequency_penalty = 1.0:
💡 Intuition: A "tax" that grows each time you reuse a word. Repeat "love" 5 times? Big fat penalty.
🚫 Step 2: Apply Presence Penalty
What it does: Flat penalty for any word that has appeared at all (regardless of count).
Formula: new_logit = logit - presence_penalty (if seen)
With presence_penalty = 0.5:
💡 Intuition: A "one-time fee" that encourages exploring new words. It doesn't care how many times, just "have you seen this before?"
New ranking:
Notice: "cooking" is now the leader! 🎉
🌡️ Step 3: Apply Temperature
What it does: Reshapes the "sharpness" of the distribution. Divides logits before softmax.
Formula: adjusted_logit = logit / temperature
With temperature = 1.0 (default, no change):
With temperature = 0.5 (COLD — sharpens):
Gaps grow → top choice dominates more.
With temperature = 2.0 (HOT — flattens):
Gaps shrink → other words get a real chance.
💡 Intuition: Temperature is a "volume knob for randomness".
0.0→ always pick #1 (deterministic, greedy)1.0→ natural randomness2.0→ wild, creative, occasionally bizarre
Let's continue with temperature = 1.0.
🎯 Step 4: Apply Top-K Filter
What it does: Keep only the K highest-scoring tokens, discard the rest.
With top_k = 4:
💡 Intuition: "Only consider the top 4 candidates."
🎯 Step 5: Apply Top-P (Nucleus) Filter
What it does: Keep the smallest set of tokens whose probabilities sum to P.
First, we peek at probabilities (softmax of remaining logits):
With top_p = 0.9:
💡 Intuition: "Keep just enough candidates to cover 90% of the probability mass." Adaptively shrinks or grows the pool.
🎲 Step 6: Softmax → Probabilities
Convert the remaining logits into a proper probability distribution:
Visually:
🎲 Step 7: Sample (Weighted Random Pick)
The model spins a "weighted wheel" and picks based on probabilities.
Wait — but "cooking" had the highest probability! Why "love"?
Because sampling is random, weighted by probability. "cooking" wins 45% of the time, but the other 55% goes to "love" or "eating". That's what creates variety.
💡 Set
seed = 42to fix the random number generator → same input always gives same output. Great for testing! 🧪
🛑 Step 8: Check Stop Sequences
Not modifying logits — this is a post-check on the generated text.
📏 Step 9: Check Max Tokens & Loop
Update our tracking:
- Now
"love"count = 3 - Next iteration,
"love"will have an even bigger frequency penalty
📊 The Full Inference Pipeline (Visual)
🎯 Inference Params — Quick Cheat Sheet
| Parameter | Typical Value | Effect |
|---|---|---|
| 🌡️ Temperature | 0.0 – 1.0 | Randomness (0 = deterministic) |
| 🎯 Top-P | 0.9 | Diversity via nucleus |
| 🎯 Top-K | 40 | Diversity via cutoff |
| 🚫 Frequency Penalty | 0.0 – 1.0 | Anti-repetition (grows with count) |
| 🚫 Presence Penalty | 0.0 – 1.0 | Encourages new words |
| 📏 Max Tokens | 500 – 4096 | Output length |
| 🛑 Stop Sequences | ["\n\n"] | Halt condition |
| 🎲 Seed | 42 | Reproducibility |
🏋️ Part 2: Training / Fine-Tuning Parameters
Now, before an LLM can generate anything, it has to be trained. These parameters control that phase.
Imagine we're fine-tuning an LLM on cooking recipes. Here's what each parameter does.
📈 1. Learning Rate
How big a step to take when updating weights after each training example.
Analogy: Rolling a ball down a hill to find the bottom. Small steps = careful. Big steps = might bounce past the valley.
Typical values:
- Pre-training:
1e-4 - Fine-tuning:
2e-5 - LoRA:
2e-4
📦 2. Batch Size
Number of examples processed together per training step.
Trade-off: Bigger batch = smoother learning but more memory. Smaller batch = noisier but fits on small GPUs.
🔄 3. Epochs
Number of times the model sees the entire dataset.
Analogy: Rereading a textbook. Once = get the gist. 3 times = solid grasp. 10 times = you memorize page numbers but stop learning.
📉 4. Warmup Steps
Gradually ramp up learning rate at the start to avoid early instability.
Analogy: A car doesn't slam into 5th gear from a standstill — it eases up through the gears.
🎯 5. Weight Decay
Slightly shrinks weights each step to prevent them from growing too large.
Analogy: A gentle "pruning" of the model's memory to prevent hoarding useless details.
⏹️ 6. Gradient Clipping
Caps how big any gradient update can be to prevent training explosions.
🎯 7. LoRA Rank (r) — The Star of Fine-Tuning!
Instead of updating all billions of weights, LoRA adds tiny adapter matrices.
Visually:
Rank r | Capacity | Use Case |
|---|---|---|
| 4 | Tiny | Style tweaks |
| 8 | Small | Simple tasks (default) |
| 16 | Medium | Domain adaptation |
| 64 | Large | Complex domains |
| 128+ | Huge | Near full fine-tuning |
💡 Intuition: Rank = "how much room" the adapter has to store new knowledge.
⚖️ 8. LoRA Alpha (α)
Scaling factor for LoRA updates.
Rule of thumb: alpha = 2 × rank. Higher alpha = stronger LoRA influence on the model.
💧 9. LoRA Dropout
Randomly zero out some LoRA activations during training to prevent overfitting.
Analogy: A basketball team practicing with random players sitting out — makes everyone versatile.
💾 10. Quantization Bits (QLoRA)
Compress model weights to fewer bits to fit in less GPU memory.
Trade-off: Less precision = slight quality loss, but massive memory savings.
🎯 Training Params — Quick Cheat Sheet
| Parameter | Typical Value | Effect |
|---|---|---|
| 📈 Learning Rate | 1e-5 – 2e-4 | Speed of learning |
| 📦 Batch Size | 8 – 64 | Stability of gradient |
| 🔄 Epochs | 1 – 3 | Passes over data |
| 📉 Warmup Steps | 100 – 500 | Ease into training |
| 🎯 Weight Decay | 0.01 | Prevent overfitting |
| ⏹️ Gradient Clipping | 1.0 | Prevent explosions |
| 🎯 LoRA Rank | 8 – 64 | Adapter capacity |
| ⚖️ LoRA Alpha | 2 × rank | Adapter strength |
| 💧 LoRA Dropout | 0.05 – 0.1 | Prevent overfit |
| 💾 Quantization | 4-bit / 8-bit | Memory savings |
🎬 The Big Picture
💡 Real-World Recipes
🤖 Production Chatbot (with RAG)
✍️ Creative Writing
🧮 Code / Math
🔧 LoRA Fine-Tuning on Custom Data
🎯 TL;DR
- Inference params shape logits → probabilities → sampling. Master
temperature,top_p,frequency_penaltyand you can control 90% of output behavior. - Training params shape how weights update. Master
learning_rate,batch_size,LoRA rankand you can fine-tune any model on any domain. - Same architecture underneath — you're just tweaking dials on the machine. 🎛️
✨ Pro tip: Start with defaults (temp=0.7, top_p=0.9, LoRA rank=8). Only change one knob at a time and observe. That's how the pros tune LLMs. 🚀
No comments:
Post a Comment