66 / 90

第66日: アダプタライフサイクル: シリアライズ、重み融合とゼロ遅延推論

ゼロ遅延推論のための重みマージ、マルチタスク差し替えのためのアンマージ、および軽量アダプタの永続化。

モデルと事前学習 480 分(8時間)
01 120 分

重み融合(マージ)の数理とゼロ遅延推論デプロイの実現

Agent支援エンジニアリングチェック

前提条件

第64〜65日LoRA数学および行列加算の基本。

学習目標

W_merged = W0 + (alpha/r)*BA による重みマージを定式化・実装し、推論時の分岐カーネルを消去して完全なゼロ遅延デプロイを達成する。

手順

1. マージ前の順伝播を書く:y = W0 @ x + b + (alpha/r) * B @ (A @ x)。これを提供すると、層ごとに小さな行列積が 2 つとメモリ読み込みが余分にかかる。
2. マージ後の重みを導く:W_merged = W0 + (alpha/r) * B @ A。形状を確かめる:B @ A は (out, r) @ (r, in) = (out, in) で、W0 と同じ形状である。
3. 線形性で等価性を示す:W_merged @ x = W0 @ x + (alpha/r) * B @ A @ x。
4. 2 回マージしても差分が 2 回加わらないよう、ガードのフラグ付きで merge() を実装し、マージ後は forward() がアダプタの経路を飛ばすようにする。
5. ランダムな入力でマージ前後の出力を比べる。厳密な算術では等しいが、fp32 は和を異なる順序で丸めるので、最大差を測って目標の 1e-6 と比べる。
6. in = out = 4096、r = 16 で節約される遅延を数える:マージ前の経路が層・トークンごとに加える FLOPs。

時間配分(120 分):読解 25 + 演習 65 + 確認 30。 読解:Hu et al. 4.1 節(推論遅延の追加なし)と PEFT の merge_and_unload のドキュメント。演習:手順 1-6。確認:マージの式と形状を記憶から導く。

演習と予測記録

LoRALinear に merge() メソッドを実装し、マージ後の線形層の出力がマージ前の出力と許容誤差 1e-6 内で完全一致することを検証してください。

独立習得基準

重み融合処理を実装し、マージ前後での数値的等価性を単体テストで証明できる。

公式ソースリソースと権利証跡

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4.1, paragraph 'No Additional Inference Latency' (store W = W0 + BA)

検証バージョンと日付: 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 Apache-2.0

PEFT: State-of-the-art Parameter-Efficient Fine-Tuning Methods

著者・提供機関: Sourab Mangrulkar, Sylvain Gugger, Lysandre Debut, Younes Belkada, Sayak Paul, Benjamin Bossan | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README 'Quickstart' (LoraConfig, get_peft_model, print_trainable_parameters)

検証バージョンと日付: PEFT 0.10.x / GitHub (Verified: 2026-10-30)

選定理由:

Widely adopted open-source library implementing LoRA configuration classes (LoraConfig), get_peft_model, and adapter serialization.

ソース前提知識:

PyTorch nn.Module, Hugging Face transformers, and LoRA equations.

安全フォールバック(オフライン回復性):

Standalone, zero-dependency PyTorch implementations of LoRALinear layers and save/reload logic are provided in curriculum scripts.

比較対象候補リソースと選定評価 (2)

Original reference code by Hu et al.; PEFT expands this to broader multi-model ecosystem integration.

Lit-GPT LoRA implementation (Lightning AI) ↗ compared_alternative [Apache 2.0]

High-performance clean implementation; compared alongside PEFT for modular code architecture.

02 120 分

アンマージ操作とマルチテナント環境におけるアダプタ高速差し替え

独立習得チェック(AI 支援なし)

前提条件

第66日アクティビティ1、可逆的な重み演算の知識。

学習目標

W0 = W_merged - (alpha/r)*BA によるアンマージ操作を実装し、単一GPU上の共有基座モデルで複数タスクのアダプタを動的に高速交換する。

手順

1. 提供の経済性を計算する:bf16 の 7B モデルを 10 個別々に置くと 10 * 14 GB = 140 GB が必要。共有の 14 GB ベース 1 つとアダプタ 10 個ならはるかに少ない:7 射影すべてに r=16 なら 39,976,960 パラメータで、bf16 で 1 つ約 80 MB。
2. unmerge() を merge() の厳密な逆演算として実装する:同じ A、B、スケールで (alpha/r) * B @ A を引き、マージされていない層を再びアンマージしないようガードする。
3. 差し替えサイクルを実行する:Adapter_Medical を読み込み、merge、推論、unmerge。Adapter_Legal を読み込み、merge、推論、unmerge。これを数サイクル繰り返す。
4. unmerge のたびに、ベース重みと元の W0 の最大差を測り、1e-6 以内にとどまることを確認する。
5. ドリフトが小さいが常にゼロではない理由を説明する:浮動小数点では (w0 + d) - d が常に w0 になるとは限らない。
6. 1 サイクルを bf16 で繰り返してドリフトを記録する。アンマージする予定の重みにマージするのが危険なのはどんな場合かを結論づける。

時間配分(120 分):読解 20 + 演習 70 + 確認 30。 読解:複数アダプタの読み込みと切り替えに関する PEFT のドキュメントと活動 1 のノート。演習:unmerge()、差し替えサイクル、bf16 との比較。確認:ドリフトの結果をノートなしで説明する。

演習と予測記録

マージ -> アンマージの反復サイクルを実行し、元の基座重みとの間に浮動小数点ドリフトが生じないことを確認してください。

独立習得基準

可逆的なアンマージ処理とマルチテナント対応の高速アダプタ切り替えを実装できる。

公式ソースリソースと権利証跡

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4.1, paragraph 'No Additional Inference Latency' (store W = W0 + BA)

検証バージョンと日付: 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 Apache-2.0

PEFT: State-of-the-art Parameter-Efficient Fine-Tuning Methods

著者・提供機関: Sourab Mangrulkar, Sylvain Gugger, Lysandre Debut, Younes Belkada, Sayak Paul, Benjamin Bossan | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README 'Quickstart' (LoraConfig, get_peft_model, print_trainable_parameters)

検証バージョンと日付: PEFT 0.10.x / GitHub (Verified: 2026-10-30)

選定理由:

Widely adopted open-source library implementing LoRA configuration classes (LoraConfig), get_peft_model, and adapter serialization.

ソース前提知識:

PyTorch nn.Module, Hugging Face transformers, and LoRA equations.

安全フォールバック(オフライン回復性):

Standalone, zero-dependency PyTorch implementations of LoRALinear layers and save/reload logic are provided in curriculum scripts.

比較対象候補リソースと選定評価 (2)

Original reference code by Hu et al.; PEFT expands this to broader multi-model ecosystem integration.

Lit-GPT LoRA implementation (Lightning AI) ↗ compared_alternative [Apache 2.0]

High-performance clean implementation; compared alongside PEFT for modular code architecture.

03 120 分

軽量アダプタのシリアライズ規格と設定スキーマ(adapter_config)の標準化

Agent支援エンジニアリングチェック

前提条件

第66日アクティビティ1〜2およびJSON/Safetensorsシリアライズ技術。

学習目標

アダプタのチェックポイントの保存と再読み込みを標準化します: 基座の重みからアダプタのテンソルを分離し、adapter_config.json のメタデータを出力して、安全な再読み込みを検証します。

手順

1. 成果物の構成を決める:adapter_model.safetensors と adapter_config.json だけ。古いエクスポートは adapter_model.bin(pickle)を使っていたが、safetensors を使う(Day 60)。
2. 設定に r、lora_alpha、target_modules、lora_dropout、base_model_name_or_path を書く。
3. 状態辞書を 'lora_' を含むキーに絞り、凍結したベースの重みがファイルに入らないようにする。
4. save_adapter(model, path, ...) と load_adapter(model, path) を書く。ファイルにはわざとベースの重みがないので strict=False で読み込み、アダプタのキーが欠けていないこと、予期しないものが読み込まれていないことを assert する。
5. ディレクトリの大きさを測り、予測値と比べる:r * (in + out) 個の値 × fp32 の 4 バイト。
6. 新しいモデルに読み込み直し、出力が訓練済みモデルの出力と一致することを確かめる。

時間配分(120 分):読解 20 + 演習 70 + 確認 30。 読解:PEFT の save_pretrained/from_pretrained のドキュメントと safetensors の形式の説明。演習:2 つの関数、サイズ確認、再読み込み確認。確認:1024 x 1024 の層、r=8 のファイルサイズを予測し、確かめる。

演習と予測記録

学習済みアダプタを保存し、ディレクトリ全体の容量が1MB未満であること、および adapter_config.json の整合性を確認してください。

独立習得基準

基座モデルとアダプタを完全に分離した標準シリアライズ規格を実装できる。

公式ソースリソースと権利証跡

linking Apache-2.0

PEFT: State-of-the-art Parameter-Efficient Fine-Tuning Methods

著者・提供機関: Sourab Mangrulkar, Sylvain Gugger, Lysandre Debut, Younes Belkada, Sayak Paul, Benjamin Bossan | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README 'Quickstart' (LoraConfig, get_peft_model, print_trainable_parameters)

検証バージョンと日付: PEFT 0.10.x / GitHub (Verified: 2026-10-30)

選定理由:

Widely adopted open-source library implementing LoRA configuration classes (LoraConfig), get_peft_model, and adapter serialization.

ソース前提知識:

PyTorch nn.Module, Hugging Face transformers, and LoRA equations.

安全フォールバック(オフライン回復性):

Standalone, zero-dependency PyTorch implementations of LoRALinear layers and save/reload logic are provided in curriculum scripts.

比較対象候補リソースと選定評価 (2)

Original reference code by Hu et al.; PEFT expands this to broader multi-model ecosystem integration.

Lit-GPT LoRA implementation (Lightning AI) ↗ compared_alternative [Apache 2.0]

High-performance clean implementation; compared alongside PEFT for modular code architecture.

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4.2 Applying LoRA to Transformer (frozen W0, VRAM and checkpoint-size reduction)

検証バージョンと日付: 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.

04 120 分

評価課題: LoRA学習、重み融合の数値一致性検証とエクスポート完遂

独立習得チェック(AI 支援なし)

前提条件

第66日アクティビティ1〜3の全知識。

学習目標

LoRAアダプタの訓練、成果物エクスポート、新規モデルへのロード、重み融合、および許容誤差1e-6以内の出力一致性をエンドツーエンドで実証する。

手順

1. 部品を再利用する:Day 64 の LoRALinear、活動 1-2 の merge() と unmerge()、活動 3 の save_adapter/load_adapter。
2. 合成データで lora_A と lora_B だけを 15 ステップ訓練し、各ステップの損失を記録する。
3. アダプタを 'exported_adapter/' にエクスポートする。
4. 同じベースの重みを持つ新しいモデルを作り、アダプタを読み込み、y_unmerged = model(x) を計算する。
5. merge() を呼び、y_merged = model(x) を計算する。
6. 損失が減少したこと、マージ後にベースの重みが変化したこと、max(abs(y_unmerged - y_merged)) < 1e-5 であること(fp32 での merge は加算順序が変わるため 1e-5 を許容する)、読み込み直したモデルが訓練済みモデルを再現することを assert する。

時間配分(120 分):読解 15 + 演習 75 + 確認 30。 読解:活動 1-3 のノート。演習:ライフサイクル全体のスクリプト。確認:すべての assert が通る習熟実行と、新しいプロセスでの再実行。

演習と予測記録

完全なLoRAライフサイクル検証スイートを実行し、学習収束、エクスポート、および重み融合の等価性を確認してください。

独立習得基準

LoRAの訓練・保存・ロード・マージの全ライフサイクルを完遂し、数値的等価性を証明できる。

公式ソースリソースと権利証跡

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 | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 4.1, paragraph 'No Additional Inference Latency' (store W = W0 + BA)

検証バージョンと日付: 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 Apache-2.0

PEFT: State-of-the-art Parameter-Efficient Fine-Tuning Methods

著者・提供機関: Sourab Mangrulkar, Sylvain Gugger, Lysandre Debut, Younes Belkada, Sayak Paul, Benjamin Bossan | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README 'Quickstart' (LoraConfig, get_peft_model, print_trainable_parameters)

検証バージョンと日付: PEFT 0.10.x / GitHub (Verified: 2026-10-30)

選定理由:

Widely adopted open-source library implementing LoRA configuration classes (LoraConfig), get_peft_model, and adapter serialization.

ソース前提知識:

PyTorch nn.Module, Hugging Face transformers, and LoRA equations.

安全フォールバック(オフライン回復性):

Standalone, zero-dependency PyTorch implementations of LoRALinear layers and save/reload logic are provided in curriculum scripts.

比較対象候補リソースと選定評価 (2)

Original reference code by Hu et al.; PEFT expands this to broader multi-model ecosystem integration.

Lit-GPT LoRA implementation (Lightning AI) ↗ compared_alternative [Apache 2.0]

High-performance clean implementation; compared alongside PEFT for modular code architecture.