67 / 90

第 67 天:微调配方行前预检、零样本基线基准与支出熔断

锁定 LoRA 训练超参配方、评测微调前零样本基线基准并建立刚性支出熔断防线。

模型与预训练 480 分钟有效研习(8 小时)
01 120 分钟

微调配方参数审计与训练超参数基准校准

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 且数值合规。

独立掌握标准

独立构建符合行业最高标准且张量约束明确的 LoRA 微调超参数配方。

权威源资源与权利证明

linking Apache-2.0

Hugging Face LLM Course: Fine-tuning LLMs

作者 / 机构: Hugging Face course contributors | 原始出处链接 ↗

对应章节 / 时间戳: 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)
LoRA original paper ↗ compared_alternative [arXiv.org perpetual non-exclusive license / Open Access]

Retained for initialization and the low-rank method, not a beginner teaching sequence.

Hugging Face PEFT ↗ compared_alternative [Apache-2.0]

Retained for actual API and implementation defaults; reference documentation is not a complete teaching course.

linking Copyright Stanford CRFM (blog post; no open license stated)

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: 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.

Databricks Dolly 15k Dataset & Training Recipe ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive crowdsourced dataset; Alpaca established the academic instruction tuning standard.

linking arXiv non-exclusive license / Open Access

LoRA: Low-Rank Adaptation of Large Language Models

作者 / 机构: Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen | 原始出处链接 ↗

对应章节 / 时间戳: 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.

02 120 分钟

目标评估 Prompt 集合上的零样本基线基准测算

独立掌握检查(无 AI 辅助)

前置要求

第 67 天活动 1 与大语言模型生成评测基础。

学习目标

在未挂载任何微调适配器的冻结基座模型上,执行固定留出测试 Prompt 集合评测,客观测定初始任务准确率、格式合规率并留存经典错误样例。

操作指引

1. 固定一组留出的领域提示(例如 50 道结构化抽取题)。每项包含 id、提示,以及正确回答必须包含的 JSON 键。
2. 加载不挂任何适配器的基座模型。用 temperature=0.1、max_new_tokens=256 和固定种子解码,并准确记录这些参数;Day 69 会原样复用。
3. 自动给每个输出打分:必须能被 json.loads 解析、是 JSON 对象,并包含全部期望的键。为每个失败记录原因。
4. 原样保存失败的输出。基座模型的典型失败:JSON 前有寒暄式前缀、对象在中途截断、缺少键。
5. 把准确率、提示数量、解码参数和提示集的 SHA-256 哈希写入 baseline_results.json,这样 Day 69 能证明用的是同一组提示。
6. 记下测量的分辨率:只有 10 道题时,每道题会让准确率变化 10 个百分点。

时间安排(120 分钟):阅读 15 + 练习 75 + 检查 30。 阅读:InstructGPT(Ouyang et al.)第 1 节关于基座模型为何不遵循指令的讨论,以及 Alpaca 的评测说明。练习:在 10 道题上完成第 1-6 步。检查:手工重新给一个保存的失败样例打分,并确认哈希与提示集一致。

自主演练与记录

编写 zero_shot_eval.py 评测脚本并在基座模型上执行 10 道测试题。核算初始准确率(通常仅为 10%-20%),并截留其未输出有效 JSON 的代表性失败文本。

独立掌握标准

在启动微调前建立严密、透明且可复核的零样本性能锚定凭证。

权威源资源与权利证明

linking Open Access

Training language models to follow instructions with human feedback

作者 / 机构: Long Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida, Carroll Wainwright, Pamela Mishkin, et al. | 原始出处链接 ↗

对应章节 / 时间戳: Section 3.6 Evaluation and Section 4.1 Results on the API distribution

验证版本与日期: arXiv:2203.02155v1 (Verified: 2026-10-30)

入选理由:

Foundational InstructGPT paper defining the SFT objective: maximizing log-likelihood of demonstrator completions conditioned on prompts.

来源前置条件:

Supervised fine-tuning, conditional autoregressive generation.

安全回退方案(离线弹性):

The conditional likelihood objective and prompt-conditioning derivations are completely formulated in the review.

对比候选资源与选型分析 (2)

Earlier RLHF formulation on summarization; InstructGPT standardizes the prompt-response SFT training phase.

Focuses on massive multi-task scaling; InstructGPT details the exact prompt-conditioned loss setup.

linking Copyright Stanford CRFM (blog post; no open license stated)

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: Preliminary evaluation

验证版本与日期: 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.

Databricks Dolly 15k Dataset & Training Recipe ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive crowdsourced dataset; Alpaca established the academic instruction tuning standard.

03 120 分钟

云端支出上限锁定、工时封顶与前置熔断触发器

Agent 辅助工程检查

前置要求

第 67 天活动 1 与第 58 天云端开销预算控制标准。

学习目标

为本次 LoRA 微调任务确立刚性的财务与工时安全红线:硬性预算上限 <= $10.00,最大允许工时 < 2 小时,预先植入自动化熔断停机触发器。

操作指引

1. 估算这次运行:LoRA 只训练约 0.5% 的参数,批次放得进单张 GPU,一次指令微调通常需要 1 到 2 个 GPU 小时。
2. 设定上限:支出上限为 $10.00。计划运行 2.0 小时:A10G 按 $0.60/小时为 $1.20,A100 按 $1.50/小时为 $3.00。
3. 设定停止触发器:
- 步数上限:在第 1000 步或 3 个 epoch 后停止。
- 停滞:验证损失连续 3 次评估没有改善时提前停止。
- 超时守护:墙钟时间超过 2.5 小时即终止进程。A100 上的最坏情况:2.5 * $1.50 = $3.75,仍低于上限。
4. 编写 check_budget_limits(max_cost, max_hours)。它拒绝费用超过 $10.00 或时长超过 3 小时(校验用的硬上限,高于 2.5 小时的守护)的配置。
5. 同样拒绝非法输入:NaN、无穷大、负数和布尔值。NaN > 10.0 为 False,所以简单比较会放过 NaN。
6. 没有这些上限时,绝不启动后台任务。

时间安排(120 分钟):阅读 20 + 练习 70 + 检查 30。 阅读:Hu et al.(LoRA)关于训练参数之少的说明、Alpaca 的成本说明,以及你在 Day 58 定下的预算规则。练习:第 2-5 步,并为每种非法输入写一个测试。检查:不看笔记重新计算三个费用($1.20、$3.00、$3.75)。

自主演练与记录

编写预算合规门禁函数 check_budget_limits(max_cost, max_hours)。验证其对超出 $10.00 额度或超过 3 小时的请求能够精准拦截并抛出异常。

独立掌握标准

能独立部署不可突破的预算与工时防御策略,杜绝算力资源无谓闲置消耗。

权威源资源与权利证明

linking arXiv non-exclusive license / Open Access

LoRA: Low-Rank Adaptation of Large Language Models

作者 / 机构: Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen | 原始出处链接 ↗

对应章节 / 时间戳: N/A

验证版本与日期: 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.

linking Copyright Stanford CRFM (blog post; no open license stated)

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: N/A

验证版本与日期: 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.

Databricks Dolly 15k Dataset & Training Recipe ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive crowdsourced dataset; Alpaca established the academic instruction tuning standard.

04 120 分钟

考核评估:微调行前自动检查门禁与基线核算台账审计

独立掌握检查(无 AI 辅助)

前置要求

第 67 天活动 1 至 3 全部内容。

学习目标

独立实现工程级 FinetunePreflightValidator 预检门禁类,全自动校验数据损失掩码规范、基线测算文件完整性与支出上限,出具具备防篡改特性的行前决策报告。

操作指引

1. 创建 FinetunePreflightValidator(dataset_path, baseline_path, budget_limit)。
2. verify_data():读取分词后的 JSONL。每行的 input_ids 与 labels 长度必须相同,第一个标签必须是 -100(提示被掩码),且至少有一个标签不是 -100(确实有内容被训练)。报告行数和最先出现的错误行。
3. verify_baseline():活动 2 的 baseline_results.json 必须存在,其准确率必须是 0 到 1 之间的数,n 必须为正,并且记录了解码参数。
4. verify_budget():上限必须是不大于 10.0 的有限正数。
5. validate():运行全部三项检查,只有全部通过时才返回 verdict='GO',否则返回 'NO-GO'。报告中包含每项检查的细节、baseline_score 和 data_samples_count。
6. 两种结果都要测试:一个合法的测试样例得到 GO,每项检查各用一个错误输入(超过 10 的预算、缺失的基线、NaN 预算)得到 NO-GO。不会给出 NO-GO 的校验器什么也证明不了。

时间安排(120 分钟):阅读 10 + 练习 80 + 检查 30。 阅读:活动 1-3 的笔记和 Alpaca 的数据格式。练习:这个类及其测试样例。检查:在没有 AI 帮助的情况下,完成所有 GO 和 NO-GO 断言都通过的掌握运行。

自主演练与记录

在合法的测试样例上运行 FinetunePreflightValidator,确认它返回 'GO',且报告中包含 baseline_score 和 data_samples_count。然后依次破坏每项检查(预算超过 $10.00、基线文件缺失、预算为 NaN),确认每种情况都返回 'NO-GO'。

独立掌握标准

构建出能够百分之百阻断超支或缺陷配置的微调行前质量与安全检查门禁。

权威源资源与权利证明

linking Copyright Stanford CRFM (blog post; no open license stated)

Stanford Alpaca: An Instruction-following LLaMA Model

作者 / 机构: Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li, Carlos Guestrin, Percy Liang, Tatsunori B. Hashimoto | 原始出处链接 ↗

对应章节 / 时间戳: N/A

验证版本与日期: 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.

Databricks Dolly 15k Dataset & Training Recipe ↗ compared_alternative [CC-BY-SA 3.0]

Commercially permissive crowdsourced dataset; Alpaca established the academic instruction tuning standard.

linking arXiv non-exclusive license / Open Access

LoRA: Low-Rank Adaptation of Large Language Models

作者 / 机构: Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen | 原始出处链接 ↗

对应章节 / 时间戳: N/A

验证版本与日期: 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.