微调配方参数审计与训练超参数基准校准
Agent 辅助工程检查
前置要求
第 64-66 天 LoRA 数学工程基础与第 49-51 天预训练训练循环。
学习目标
系统审计微调学习率调度(带线性预热的余弦衰减,峰值 2e-4)、秩 r=16、alpha=32、批大小及目标投影层,严格与主流开源标准配方对齐。
操作指引
学习来源:在本活动现有的阅读与练习时段内,参考 Hugging Face LLM Course: Fine-tuning LLMs 的 Chapter 11.3: Training Configuration,用自己的话解释一处与本地任务的联系。保留现有练习与验收检查;若外部资料无法访问,使用课程自写的解释。1. 查阅公开的配方(Alpaca、LoRA 论文、PEFT),写下各自使用的值:
- 学习率:LoRA 取 1e-4 至 3e-4,约为常见全量微调学习率(1e-5 至 2e-5)的 10 倍,因为只更新很小的适配器。
- 秩与缩放:r=16,alpha=32,因此适配器输出乘以 alpha/r = 2.0。
- 目标模块:注意力的全部投影(q_proj、k_proj、v_proj、o_proj),或全部线性层。
- 调度:在 3% 至 5% 的步数内线性预热,然后余弦衰减到峰值的 10%。
2. 用这些值加上 max_steps=1000、seq_len=1024 和 min_lr_ratio=0.1 写出 config.json。
3. 实现 lr_at(step),并手算检查三个点:预热结束时(30 个预热步中的第 29 步)为 2e-4,最后一步为 2e-5,预热期间学习率上升。
4. 检查序列格式:每个样本对被打包到固定长度(512 或 1024 个词元),并以 EOS 词元结尾。
5. 用一个样例检查损失掩码:提示词元的标签为 -100,只有回答词元和 EOS 计入损失。
时间安排(120 分钟):阅读 30 + 练习 60 + 检查 30。 阅读:Alpaca 的训练配方,以及 Hu et al.(LoRA)第 7 节关于适配哪些权重矩阵、选用多大秩的讨论。练习:第 2-5 步。检查:重新计算 alpha/r 和预热步数,并凭记忆解释 LoRA 学习率为何高 10 倍。
自主演练与记录
构造微调配置字典 config.json。断言其显式包含 rank=16, alpha=32, lr=2e-4, warmup_ratio=0.03 以及 loss_masking=True 且数值合规。
核对字典键值与 Hugging Face PEFT 规范一致。
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.
独立构建符合行业最高标准且张量约束明确的 LoRA 微调超参数配方。
权威源资源与权利证明
Hugging Face LLM Course: Fine-tuning LLMs
对应章节 / 时间戳: Chapter 11.3: Training Configuration
验证版本与日期: 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.
对比候选资源与选型分析 (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
对应章节 / 时间戳: Training recipe
验证版本与日期: 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.
对比候选资源与选型分析 (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
对应章节 / 时间戳: Appendix D Hyperparameters Used in Experiments
验证版本与日期: 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.
对比候选资源与选型分析 (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.