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.