微調レシピの設定監査とハイパーパラメータ整合性検証
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) を実装し、3 点を手で確かめる:ウォームアップの終わり(30 ステップ中のステップ 29)は 2e-4、最後のステップは 2e-5、ウォームアップ中は学習率が上がる。
4. 系列の形式を確かめる:各ペアは固定長(512 または 1024 トークン)に詰められ、EOS トークンで終わる。
5. 1 つの例で損失マスクを確かめる:プロンプトのトークンはラベル -100 になり、損失に数えるのは回答のトークンと EOS だけである。
時間配分(120 分):読解 30 + 演習 60 + 確認 30。 読解:Alpaca の訓練レシピと Hu et al.(LoRA)7 節(どの重み行列を適応させ、どのランクを使うか)。演習:手順 2-5。確認:alpha/r とウォームアップのステップ数を計算し直し、LoRA の学習率が 10 倍高い理由を記憶から説明する。
演習と予測記録
Alpaca 形式のデータで 7B モデルの学習を行うための設定辞書 config.json を作成してください。rank=16、alpha=32、lr=2e-4、warmup_ratio=0.03、loss_masking=True であることをアサートしてください。
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.
微調ハイパーパラメータの妥当性を数理的に検証・設定できる。
公式ソースリソースと権利証跡
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.