Fine-Tuning Recipe Configuration and Hyperparameter Audit
Agent-Assisted Engineering Check
Prerequisites
Day 64-66 LoRA mathematics and Day 49-51 pretraining loops.
Learning Objective
Audit learning rate schedules (cosine decay with warmup to 2e-4), rank r=16, alpha=32, batch size, and target linear projections against standard LoRA recipes.
Instructions
Study source: within this activity's existing reading/practice time, use Hugging Face LLM Course: Fine-tuning LLMs, Chapter 11.3: Training Configuration, then explain one connection to the local task in your own words. Keep the local exercise and acceptance checks; use its original explanation if the external source is unavailable. 1. Review the published recipes (Alpaca, the LoRA paper, PEFT) and write down the value each uses:
- Learning rate: 1e-4 to 3e-4 for LoRA, about 10x the usual full fine-tuning rate (1e-5 to 2e-5), because only the small adapter is updated.
- Rank and scale: r=16, alpha=32, so the adapter output is scaled by alpha/r = 2.0.
- Target modules: all attention projections (q_proj, k_proj, v_proj, o_proj), or all linear layers.
- Schedule: linear warmup over 3% to 5% of the steps, then cosine decay to 10% of the peak.
2. Write config.json with these values plus max_steps=1000, seq_len=1024 and min_lr_ratio=0.1.
3. Implement lr_at(step) and check three points by hand: the end of warmup (step 29 of 30 warmup steps) is 2e-4, the last step is 2e-5, and the rate rises during warmup.
4. Check the sequence format: every pair is packed into a fixed length (512 or 1024 tokens) and ends with the EOS token.
5. Check loss masking on one example: prompt tokens get label -100, and only the answer tokens and EOS count in the loss.
Time plan (120 min): reading 30 + exercise 60 + check 30. Reading: the Alpaca training recipe and Hu et al. (LoRA) Section 7 on which weight matrices to adapt and which rank to use. Exercise: steps 2-5. Check: recompute alpha/r and the warmup step count, and explain from memory why the LoRA rate is 10x higher.
Practice & Prediction Log
Construct a configuration dictionary config.json for training a 7B model on Alpaca-format data. Assert that rank=16, alpha=32, lr=2e-4, warmup_ratio=0.03, and loss_masking=True.
Check dictionary keys match standard PEFT LoraConfig.
import math
config = {
'r': 16,
'lora_alpha': 32,
'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],
'lr': 2e-4,
'warmup_ratio': 0.03,
'min_lr_ratio': 0.1, # cosine decays to 10% of the peak
'max_steps': 1000,
'seq_len': 1024,
'loss_masking': True # prompt tokens get label -100 and add nothing to the loss
}
assert config['r'] == 16 and config['lora_alpha'] == 32
assert config['lr'] == 2e-4 and config['warmup_ratio'] == 0.03
assert config['loss_masking'] is True
assert config['lora_alpha'] / config['r'] == 2.0 # the adapter output is scaled by 2
def lr_at(step, c=config):
warmup = round(c['warmup_ratio'] * c['max_steps']) # 30 steps
if step < warmup:
return c['lr'] * (step + 1) / warmup
progress = (step - warmup) / (c['max_steps'] - warmup)
floor = c['min_lr_ratio'] * c['lr']
return floor + 0.5 * (c['lr'] - floor) * (1 + math.cos(math.pi * progress))
assert abs(lr_at(29) - 2e-4) < 1e-12 # warmup ends at the peak
assert abs(lr_at(1000) - 2e-5) < 1e-12 # cosine ends at 10% of the peak
assert lr_at(0) < lr_at(10) < lr_at(29)
# Loss masking, worked on one example: 5 prompt tokens and 3 answer tokens plus EOS.
input_ids = [101, 7, 8, 9, 102, 55, 56, 57, 2]
labels = [-100] * 5 + input_ids[5:]
assert sum(l != -100 for l in labels) == 4 and labels[-1] == 2 # only the answer and EOS train
# Peak lr 2e-4 is about 10x a typical full fine-tuning lr (1e-5 to 2e-5) because only the adapter moves.
Formulate a complete, mathematically sound instruction fine-tuning configuration.
Authoritative Source Resources & Provenance
Hugging Face LLM Course: Fine-tuning LLMs
Relevant Section / Range: Chapter 11.3: Training Configuration
Verified Version & Date: Public edition checked 2026-11-01; moving pages may change
Sequenced templates, supervised fine-tuning and evaluation teaching with exercises/quizzes. LoRA method and memory claims are checked against the original paper, PEFT and the local parameter ledger, not accepted wholesale from Chapter 11.4.
Python, PyTorch, language-model training and the preceding chat-format lessons.
Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.
Compared Candidate Sources & Selection Analysis (2)
Retained for initialization and the low-rank method, not a beginner teaching sequence.
Retained for actual API and implementation defaults; reference documentation is not a complete teaching course.
Stanford Alpaca: An Instruction-following LLaMA Model
Relevant Section / Range: Training recipe
Verified Version & Date: Stanford CRFM Release (Verified: 2026-10-30)
Original open recipe demonstrating lightweight instruction fine-tuning of open models, establishing prompt/response templates, batch scheduling, and learning rate ranges.
Supervised fine-tuning, instruction formatting, and LoRA fundamentals.
Complete instruction templates, JSONL formatting scripts, and learning rate schedules are self-contained in curriculum materials.
Compared Candidate Sources & Selection Analysis (2)
Multi-turn ShareGPT tuning recipe; Alpaca provides simpler, single-turn clarity ideal for foundational instruction tuning study.
Commercially permissive crowdsourced dataset; Alpaca established the academic instruction tuning standard.
LoRA: Low-Rank Adaptation of Large Language Models
Relevant Section / Range: Appendix D Hyperparameters Used in Experiments
Verified Version & Date: ICLR 2022 / arXiv:2106.09685v2 (Verified: 2026-10-30)
Primary literature introducing Low-Rank Adaptation, establishing parameter decomposition W = W0 + (alpha/r)*BA, initialization strategies, and zero inference latency merging.
Matrix factorization, rank, linear layers, and transformer attention.
Complete matrix derivations, tensor dimensions, and pure PyTorch LoRALinear modules are implemented directly in lesson code.
Compared Candidate Sources & Selection Analysis (2)
Introduces bottleneck adapter layers in series, which introduces unavoidable inference latency overhead; LoRA's parallel formulation merges with zero latency.
Prepends virtual prompt tokens; reduces available context length, unlike LoRA which preserves full sequence capacity.