57 / 90

第57日: 耐障害性、破損チェックポイントの復旧とクラウド課金防御

不完全なチェックポイント破損の復旧、strict=Falseによる語彙拡張、SIGTERMスポット中断ハンドリング、クラウド課金トラップの回避。

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

破損・途絶・互換性のないチェックポイントの診断と自動フォールバック

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

前提条件

第55日チェックポイント知識および例外処理の仕組み。

学習目標

書き込み途中で中断した不完全ファイルの破損(EOFError、UnpicklingError)を検出し、SHA256ハッシュ検証および過去の健全なスナップショットへの自動フォールバックを実装する。

手順

1. 書き込み途絶によるファイル障害モード(非ゼロサイズだが途中で切断されたpickleデータ)を学習します。
2. 不完全なチェックポイントに対する torch.load は _pickle.UnpicklingError や EOFError を送出します。
3. 'ckpt.pt.sha256' ハッシュファイルの併用により、ロード前に破損を瞬時に検知する仕組みを実装します。
4. 最新スナップショットが破損している場合に、1世代前の健全なスナップショットへ自動的にフォールバックするロジックを構築します。

演習と予測記録

意図的に末尾を切り詰めた破損チェックポイントが存在する状況下で、過去の健全なチェックポイントを自動検出して安全にロードする safe_load_checkpoint 関数を実装してください。

独立習得基準

破損ファイルを安全にスキップし、直近の健全なチェックポイントから復旧できる。

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

linking BSD-3-Clause

PyTorch Tutorial: Saving and Loading Models

著者・提供機関: PyTorch Documentation Team | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Saving & Loading a General Checkpoint for Inference and/or Resuming Training

検証バージョンと日付: PyTorch 2.x Documentation (Verified: 2026-10-30)

選定理由:

Official documentation detailing state_dict serialization, optimizer state saving, and safe loading patterns.

ソース前提知識:

Python, PyTorch tensors and nn.Module basics.

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

All checkpoint serialization semantics, dictionary schema keys, and safe recovery steps are self-contained in lesson code.

比較対象候補リソースと選定評価 (2)
TensorFlow tf.train.Checkpoint Guide ↗ compared_alternative [Apache 2.0]

TensorFlow trackable checkpoint architecture; PyTorch's explicit dictionary is our target implementation standard.

Hugging Face Safetensors Documentation ↗ compared_alternative [Apache 2.0]

Safetensors provides safe zero-copy tensor storage without pickle, compared directly when discussing pickle execution risks.

linking MIT

nanoGPT: train.py pretraining loop

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: train.py: checkpoint dict {'model', 'optimizer', 'model_args', 'iter_num', 'best_val_loss', 'config'} written with torch.save to out_dir/ckpt.pt; the init_from == 'resume' branch reloads it

検証バージョンと日付: Git master commit 71c3905 (Verified: 2026-10-30)

選定理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

ソース前提知識:

Transformer architecture from Days 46-48 and training loop from Days 49-51.

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

Complete checkpoint saving patterns and resuming loops are directly implemented in lesson exercises.

比較対象候補リソースと選定評価 (2)
Megatron-LM Checkpoint Utility ↗ compared_alternative [BSD-3-Clause]

High-complexity multi-GPU distributed model-parallel checkpointing; nanoGPT provides accessible single-GPU clarity.

PyTorch Lightning ModelCheckpoint ↗ compared_alternative [Apache 2.0]

High-level framework callback abstraction; nanoGPT keeps explicit control over state dict dictionaries.

02 120 分

部分重み読み込み、語彙拡張とstrictモードの適切な制御

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

前提条件

第57日アクティビティ1およびPyTorchモジュールパラメータ管理。

学習目標

strict=False を用いて、構造を変更したアーキテクチャ間で部分的な重み転送を行い、新規トークン用に埋め込み層を安全に拡張し、missing_keys と unexpected_keys を監査せよ。

手順

1. load_state_dict(..., strict=False) の戻り値を理解せよ:incompatible_keys は missing_keys と unexpected_keys の属性を持つ。
2. 語彙拡張を扱え:特殊トークンを追加して新しいドメインでモデルをファインチューニングする際、vocab_size は V_old から V_new に変化する。
3. 旧埋め込みを移行せよ:既存の重みを新しい [V_new, d] テンソルにコピーし、新規トークンの埋め込みは小さなランダムなガウスノイズ、または平均埋め込みで初期化する。
4. サイレントバグを避けよ:欠落キーが新たに追加されたモジュールだけであることを検証し、層名の綴り誤りによるものではないことを確認せよ。

演習と予測記録

vocab_size=100 の事前学習済みモデルを取り上げよ。語彙を 105 に拡張せよ。strict=False を用いて旧重みを安全に読み込み、新規の 5 個のトークン埋め込みを初期化し、既存の 100 トークンに対する出力が完全に変化していないことを検証せよ。

独立習得基準

語彙拡張時に既存トークンの潜在表現を完全保持しつつ安全に重み移行できる。

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

linking BSD-3-Clause

PyTorch Tutorial: Saving and Loading Models

著者・提供機関: PyTorch Documentation Team | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Warmstarting Model Using Parameters from a Different Model (strict=False)

検証バージョンと日付: PyTorch 2.x Documentation (Verified: 2026-10-30)

選定理由:

Official documentation detailing state_dict serialization, optimizer state saving, and safe loading patterns.

ソース前提知識:

Python, PyTorch tensors and nn.Module basics.

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

All checkpoint serialization semantics, dictionary schema keys, and safe recovery steps are self-contained in lesson code.

比較対象候補リソースと選定評価 (2)
TensorFlow tf.train.Checkpoint Guide ↗ compared_alternative [Apache 2.0]

TensorFlow trackable checkpoint architecture; PyTorch's explicit dictionary is our target implementation standard.

Hugging Face Safetensors Documentation ↗ compared_alternative [Apache 2.0]

Safetensors provides safe zero-copy tensor storage without pickle, compared directly when discussing pickle execution risks.

linking MIT

nanoGPT: train.py pretraining loop

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: train.py: init_from == 'resume' branch (strips the '_orig_mod.' prefix, then model.load_state_dict)

検証バージョンと日付: Git master commit 71c3905 (Verified: 2026-10-30)

選定理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

ソース前提知識:

Transformer architecture from Days 46-48 and training loop from Days 49-51.

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

Complete checkpoint saving patterns and resuming loops are directly implemented in lesson exercises.

比較対象候補リソースと選定評価 (2)
Megatron-LM Checkpoint Utility ↗ compared_alternative [BSD-3-Clause]

High-complexity multi-GPU distributed model-parallel checkpointing; nanoGPT provides accessible single-GPU clarity.

PyTorch Lightning ModelCheckpoint ↗ compared_alternative [Apache 2.0]

High-level framework callback abstraction; nanoGPT keeps explicit control over state dict dictionaries.

03 120 分

クラウドスポットインスタンス中断シグナル(SIGTERM)の安全な捕捉とコスト防御

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

前提条件

第57日アクティビティ1、Linux POSIXシグナルとスポット課金構造の理解。

学習目標

スポットGPU回収時の SIGTERM/SIGINT シグナルを捕捉して即座に緊急アトミック保存を実行し、計算資源とクラウドアカウントの課金浪費を完全に防ぐ。

手順

1. クラウドのスポットインスタンスは70〜90%安価ですが、中断通知(SIGTERM後30〜120秒で強制停止)が届く特性を把握します。
2. Python の signal モジュールを用いて signal.signal(signal.SIGTERM, handler) を登録します。
3. ハンドラ内で終了フラグを設定し、現行ステップ終了時に即座に緊急チェックポイントを保存して終了コード0で離脱します。
4. ディスクI/O負荷と再開損失のトレードオフを計算し、最適なチェックポイント間隔を設計します。

演習と予測記録

SIGTERMハンドラを登録した学習ループを作成し、疑似シグナルを受信した際に最新ステップの緊急チェックポイントを保存して正常終了することを実証してください。

独立習得基準

SIGTERMシグナルを捕捉し、安全に緊急退避保存を完了してプロセスを終了できる。

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

linking MIT

nanoGPT: train.py pretraining loop

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: train.py: checkpoint dict {'model', 'optimizer', 'model_args', 'iter_num', 'best_val_loss', 'config'} written with torch.save to out_dir/ckpt.pt; the init_from == 'resume' branch reloads it

検証バージョンと日付: Git master commit 71c3905 (Verified: 2026-10-30)

選定理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

ソース前提知識:

Transformer architecture from Days 46-48 and training loop from Days 49-51.

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

Complete checkpoint saving patterns and resuming loops are directly implemented in lesson exercises.

比較対象候補リソースと選定評価 (2)
Megatron-LM Checkpoint Utility ↗ compared_alternative [BSD-3-Clause]

High-complexity multi-GPU distributed model-parallel checkpointing; nanoGPT provides accessible single-GPU clarity.

PyTorch Lightning ModelCheckpoint ↗ compared_alternative [Apache 2.0]

High-level framework callback abstraction; nanoGPT keeps explicit control over state dict dictionaries.

linking CC-BY 4.0

Transformer Math 101

著者・提供機関: Quentin Anthony, Stella Biderman, Hailey Schoelkopf | オリジナル出所URL ↗

該当セクション・タイムスタンプ: N/A

検証バージョンと日付: EleutherAI Research Blog (Verified: 2026-10-30)

選定理由:

Widely cited, practical industry guide deriving 6ND compute FLOPs, activation memory, KV cache size, and VRAM estimation.

ソース前提知識:

Transformer matrix dimensions and matrix multiplication FLOP counting.

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

The 6ND derivation (2ND forward, 4ND backward) and peak VRAM estimation models are fully worked out in the curriculum.

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

Rigorous academic formulation; Transformer Math 101 provides accessible step-by-step arithmetic equations.

Kaplan et al. Section 2.1 Compute Budgeting ↗ compared_alternative [Open Access]

Original 6ND approximation; EleutherAI expands on activation memory and KV cache.

04 120 分

評価課題: プリエンプション強制終了とデータ破損注入シミュレーション試験

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

前提条件

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

学習目標

予期せぬSIGTERM終了およびチェックポイントファイル破損を意図的に注入し、自律的な自癒復旧プロセスを実証する。

手順

1. 30ステップの訓練ジョブを実行するシミュレーションスクリプトを作成します。
2. ステップ10で疑似SIGTERMを注入し、緊急保存が正しく発動することを確認します。
3. ステップ20で最新のチェックポイントの末尾512バイトをゼロクリアして破損させます。
4. 訓練ランナーを再起動し、破損を自動検知して過去の健全なスナップショットから再開し、30ステップを完走することを検証します。

演習と予測記録

カオス障害注入テストを実行し、破損ファイルの検出・破棄と正常な訓練継続を確認してください。

独立習得基準

障害注入(プロセス強制終了およびファイル破損)に対し、人的介入なしで自動復旧を完遂できる。

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

linking BSD-3-Clause

PyTorch Tutorial: Saving and Loading Models

著者・提供機関: PyTorch Documentation Team | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Saving & Loading a General Checkpoint for Inference and/or Resuming Training

検証バージョンと日付: PyTorch 2.x Documentation (Verified: 2026-10-30)

選定理由:

Official documentation detailing state_dict serialization, optimizer state saving, and safe loading patterns.

ソース前提知識:

Python, PyTorch tensors and nn.Module basics.

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

All checkpoint serialization semantics, dictionary schema keys, and safe recovery steps are self-contained in lesson code.

比較対象候補リソースと選定評価 (2)
TensorFlow tf.train.Checkpoint Guide ↗ compared_alternative [Apache 2.0]

TensorFlow trackable checkpoint architecture; PyTorch's explicit dictionary is our target implementation standard.

Hugging Face Safetensors Documentation ↗ compared_alternative [Apache 2.0]

Safetensors provides safe zero-copy tensor storage without pickle, compared directly when discussing pickle execution risks.

linking MIT

nanoGPT: train.py pretraining loop

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: train.py: checkpoint dict {'model', 'optimizer', 'model_args', 'iter_num', 'best_val_loss', 'config'} written with torch.save to out_dir/ckpt.pt; the init_from == 'resume' branch reloads it

検証バージョンと日付: Git master commit 71c3905 (Verified: 2026-10-30)

選定理由:

Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.

ソース前提知識:

Transformer architecture from Days 46-48 and training loop from Days 49-51.

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

Complete checkpoint saving patterns and resuming loops are directly implemented in lesson exercises.

比較対象候補リソースと選定評価 (2)
Megatron-LM Checkpoint Utility ↗ compared_alternative [BSD-3-Clause]

High-complexity multi-GPU distributed model-parallel checkpointing; nanoGPT provides accessible single-GPU clarity.

PyTorch Lightning ModelCheckpoint ↗ compared_alternative [Apache 2.0]

High-level framework callback abstraction; nanoGPT keeps explicit control over state dict dictionaries.