56 / 90

第56日: 第8週の総復習: オプティマイザからチェックポイントまで + リソース計画

第50〜55日(AdamW、スケジュール、学習ステップ、サンプリング、評価、チェックポイント)を想起してつなげ、クラウド実行に必要な唯一の新しい道具であるパラメータ・メモリ・計算量の算術を加える。

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

新規内容: パラメータ・メモリ・計算量の算術

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

前提条件

第47日のブロック計数、第48日の重み共有、第50日の AdamW、第55日のチェックポイント。

学習目標

GPT のパラメータ数を正確に数え、推論時と学習時のメモリに換算し、6ND 則で学習 FLOPs・時間・費用を見積もる。

手順

今日の新しい内容はこれだけで、残りは第8週の復習です。第58、59、64、65日がこの式を再利用します。
1. パラメータ(d = n_embd、V = 語彙数、L = コンテキスト長): 埋め込み V*d + L*d、ブロックごとに 12*d^2(Attention 4*d^2、MLP 8*d^2)+ 4*d(LayerNorm 2 個)、最終 LayerNorm 2*d、重み共有のヘッドは追加なし。この式は線形層のバイアスを含まない。GPT-2 ではブロックごとに 9*d 個ある(QKV 3d、Attention 出力 d、MLP 4d + d)。結果は PyTorch の sum(p.numel()) で確かめる。同じ式で GPT-2 Medium(350M: d = 1,024、24 ブロック)は 354,601,984、バイアス込みで 354,823,168。
2. 重みのみのメモリ: 1 パラメータあたり 4 バイト(FP32)、2(FP16/BF16)、1(INT8)、0.5(INT4)。
3. 混合精度 AdamW 学習は 1 パラメータあたり 16 バイト(ZeRO 論文): FP16 重み 2 + FP16 勾配 2 + FP32 のマスター重み・1 次モーメント・2 次モーメント 4 + 4 + 4。これに加えて活性化メモリがバッチサイズとコンテキストに比例して増える。CUDA コンテキストに約 1 GB を見込む。
4. 計算量: C = 6 * N * D FLOPs(順伝播 2ND、逆伝播 4ND)。時間 = C / (GPU 数 * ピーク FLOP/s * MFU * 3600)。MFU は通常 30〜50%。

演習と予測記録

1. GPT-2 Small: V = 50,257、L = 1,024、n_layer = 12、n_embd = 768、重み共有ヘッド。上の式でパラメータ数を数え、GPT-2 の線形層バイアスを含めた場合も求めよ。FP16 の重みは何バイトか。
2. 10 億パラメータのモデルを混合精度 AdamW で学習し、このバッチサイズで活性化に 8 GB 必要とする。24 GB の GPU に収まるか。
3. 125M モデルを 10B トークンで、A100 1 枚(BF16 ピーク 312 TFLOP/s)、MFU 40%、1 時間 $1.50 で学習する。FLOPs、時間、費用を求めよ。

独立習得基準

線形層バイアスの有無それぞれで正確なパラメータ数を再現し、16 バイト/パラメータ則でメモリ不足を予測し、6ND と MFU から時間と費用を求められる。

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

linking arXiv non-exclusive license / Open Access

Scaling Laws for Neural Language Models

著者・提供機関: Jared Kaplan, Sam McCandlish, Tom Henighan, Tom B. Brown, Benjamin Chess, Rewon Child, Scott Gray, Alec Radford, Jeffrey Wu, Dario Amodei | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2.1 Parameter and Compute Scaling of Transformers (C ≈ 6N FLOPs per token)

検証バージョンと日付: arXiv:2001.08361v1 (Verified: 2026-10-30)

選定理由:

Foundational empirical study formulating power laws governing cross-entropy loss with respect to model parameters N, dataset size D, and compute C.

ソース前提知識:

Basic calculus, logarithms, and parameter counting fundamentals.

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

Scaling formulas, power-law exponents, and parameter compute trade-offs are derived in the lesson text.

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

Early formulation of power-law scaling; Kaplan focuses explicitly on autoregressive transformer language models.

General vision/translation scaling; Kaplan specifically establishes modern decoder-only LLM compute budgeting.

linking CC-BY 4.0

Transformer Math 101

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

該当セクション・タイムスタンプ: Compute Requirements (C = 6PD) and Memory Requirements: Training

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

linking arXiv non-exclusive license / Open Access

ZeRO: Memory Optimizations Toward Training Trillion Parameter Models

著者・提供機関: Samyam Rajbhandari, Jeff Rasley, Olatunji Ruwase, Yuxiong He | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3.1 Model States: Optimizer States, Gradients and Parameters (2Ψ + 2Ψ + KΨ = 16Ψ bytes, K = 12)

検証バージョンと日付: SC20 / arXiv:1910.02054v3 (Verified: 2026-10-30)

選定理由:

Source of the 16 bytes per parameter accounting for mixed-precision Adam that the lesson's memory arithmetic uses.

ソース前提知識:

Adam optimizer mechanics from Day 34 and parameter memory arithmetic.

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

Complete analytical formulas for bytes-per-parameter across AdamW vs SGD and mixed precision are fully provided.

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

Focuses on tensor slicing and inter-GPU communication; ZeRO provides the standard breakdown of optimizer and parameter memory footprints.

Micikevicius et al. Mixed Precision Training ↗ compared_alternative [Open Access]

Foundational mixed-precision paper; ZeRO formalizes the exact 16-byte Adam state footprint.

02 120 分

復習ドリル: AdamW・スケジュール・クリッピング・サンプリング(第50〜52日)

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

前提条件

第50日の AdamW と学習率スケジュール、第51日の学習ステップとクリッピング、第52日のサンプリング。クローズドブックで解き、第50〜52日のノートは後で確認に使う。

学習目標

AdamW の 1 ステップ、スケジュールの節目、クリッピング係数、温度と top-p の分布を記憶から再計算する。

手順

想起練習です。先に答え、その後ノートで確認します。1) 第50日と同様に、バイアス補正と分離された重み減衰を含む AdamW の更新式を書き、減衰しないテンソルを思い出す。2) 第50日の線形ウォームアップとコサイン減衰の get_lr(it) を書く。3) 第51日の 5 行の学習ステップとクリッピング規則を書く。4) 第52日と同様に温度と top-p を適用する。

演習と予測記録

1. AdamW の 1 ステップ: w0 = 2.0、g = 0.4、lr = 0.01、weight_decay = 0.1、beta1 = 0.9、beta2 = 0.999、eps は無視。m_hat、v_hat、w1 を求めよ。
2. 第50日の規則で重み減衰がかかるのはどれか: Attention の W^Q、LayerNorm のゲイン、MLP のバイアス、トークン埋め込み。
3. 100 ステップで lr_max = 1e-3 までウォームアップし、ステップ 1,100 で lr_min = 1e-4 までコサイン減衰する。ステップ 50、100、600、1,100、2,000 の lr を答えよ。
4. 全体の勾配ノルムが 4.0、max_norm = 1.0。clip_grad_norm_ はすべての勾配に何を掛けるか。学習ステップを順に書け。
5. ロジット [2, 1, 0]、温度 0.5: 確率を求め、次に top-p = 0.9 適用後の分布を答えよ。

独立習得基準

ノートなしで w1 = 1.988、スケジュールの 5 つの値、クリップ係数 0.25、top-p 後の分布を正しく求め、減衰のグループ分けを正しく答えられる。

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

linking arXiv non-exclusive license / Open Access

Decoupled Weight Decay Regularization

著者・提供機関: Ilya Loshchilov, Frank Hutter | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2 Decoupling the Weight Decay from the Gradient-Based Update (Algorithm 2, AdamW)

検証バージョンと日付: ICLR 2019 / arXiv:1711.05101v3 (Verified: 2026-10-30)

選定理由:

Foundational primary literature defining AdamW, demonstrating that L2 regularization does not equal decoupled weight decay in adaptive gradient algorithms.

ソース前提知識:

Stochastic gradient descent, L2 regularization, and momentum concepts from Days 31-33.

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

All update equations and mathematical proofs are self-contained in the curriculum lessons.

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

Original Adam algorithm implementing L2 regularization via gradient addition, leading to sub-optimal generalization compared to decoupled weight decay.

PyTorch torch.optim.AdamW documentation ↗ compared_alternative [Modified BSD]

Standard library reference; Loshchilov & Hutter provides theoretical mathematical derivations and empirical justification.

linking MIT

nanoGPT: train.py pretraining loop

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

該当セクション・タイムスタンプ: train.py: optimizer = model.configure_optimizers(weight_decay, learning_rate, (beta1, beta2), device_type); train.py: get_lr(it): linear warmup over warmup_iters, cosine decay to min_lr by lr_decay_iters; clip_grad_norm_(grad_clip) in the training loop

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

ソース前提知識:

Python, PyTorch, and Day 46-48 Transformer model architecture.

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

Runnable pretraining scripts and step-by-step tensor mechanics are fully self-contained in the curriculum lessons.

比較対象候補リソースと選定評価 (2)
Hugging Face Accelerate / Trainer ↗ compared_alternative [Apache-2.0]

Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.

Megatron-LM (NVIDIA) ↗ compared_alternative [BSD-3-Clause]

High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.

03 120 分

復習演習: 誠実な評価と再開可能なチェックポイント(第53〜55日)

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

前提条件

第53日の評価とパープレキシティ、第54日の失敗ギャラリーとアブレーション、第55日のチェックポイント、サイズ計算には第56日の活動 1。

学習目標

第8週の評価とチェックポイントの規則を 1 つの小さな事前学習に当てはめ、数値を誠実に読み、失敗を診断し、再開可能なチェックポイントのサイズを求める。

手順

同僚の 124M パラメータの学習(活動 1 の N = 124,356,864)をレビューする。1) 第53日と同様に検証損失をパープレキシティに換算し、過学習を判定する。2) 第54日の失敗ギャラリーでサンプルの失敗を分類し、Distinct-2 を計算する。3) 第55日の規則でチェックポイントを確認し、今日の算術でサイズを求める。

演習と予測記録

1. 最終の学習損失 1.62、最良の検証損失はステップ 3,000 で 2.0794、ステップ 5,000 の検証損失 2.31。最良のパープレキシティを求め、過学習かどうかと、どのチェックポイントを残すかを答えよ。
2. 同僚はトークンをシャッフルしてから 90/10 に分割した。何が問題で、正しい分割はどうするか。
3. 貪欲デコードのサンプルが「a b a b a b」となった。失敗の名前を答え、Distinct-2 を計算せよ。
4. 同僚のチェックポイントは model.state_dict() だけを保存しており、再開後に損失が跳ね上がる。理由を説明し、アトミックな書き込みできれいに再開できるペイロードを書け。
5. FP32 の重みと AdamW の 2 つのモーメントを含めると、そのチェックポイントの大きさはいくらか。

独立習得基準

PPL 8.0 と正しいチェックポイント選択を示し、漏洩とループを名指しし、Distinct-2 = 0.4 を計算し、再開時の跳ね上がりを説明し、完全なアトミックなペイロードを書き、その大きさを約 1.49 GB と求められる。

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

linking MIT

nanoGPT: model.py architecture implementation

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

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

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

ソース前提知識:

Python, PyTorch, and Day 43-45 attention fundamentals.

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

Local models and classes in the curriculum mirror nanoGPT semantics directly.

比較対象候補リソースと選定評価 (2)
GPT-2 TensorFlow source (OpenAI) ↗ compared_alternative [Modified MIT License]

Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.

Llama 2 / 3 PyTorch source (Meta) ↗ compared_alternative [Llama 2 Community License]

Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

著者・提供機関: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | オリジナル出所URL ↗

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

検証バージョンと日付: ICLR 2020 / arXiv:1904.09751v2 (Verified: 2026-10-30)

選定理由:

Primary source introducing Nucleus (Top-p) sampling and analyzing the failure of greedy decoding (loops, repetition).

ソース前提知識:

Softmax probabilities, cumulative probability distributions from Days 25-27 and 41-42.

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

Top-p truncation algorithms and worked probability cutoffs are completely explained in the lesson.

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

Introduces top-k sampling; Holtzman et al. dynamic nucleus threshold is compared directly against top-k.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.

04 120 分

第8週の評価: 124M 事前学習の事前計画(第50〜56日)

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

前提条件

第50〜55日と第56日の活動 1〜3。クローズドブック、AI 不使用を申告。電卓は使用可。

学習目標

第8週のオプティマイザ・ループ・評価・チェックポイントの規則と今日のリソース算術を組み合わせ、検証可能な完全な事前計画を独力で書く。

手順

活動 1 のモデル(N = 124,356,864)を 2.5B トークンで事前学習する計画を立てる。A100 40 GB 1 枚(ピーク 312 TFLOP/s、MFU は 35% と仮定)、1 時間 $1.20。各オプティマイザステップは 524,288 トークン(マイクロバッチ 16 x 1,024 トークン、累積 32 ステップ)。計画中のすべての数値に式を示し、最初の実ステップで確認すべき予測値がどれかを明記すること。

演習と予測記録

1. 2.5B トークンをカバーするオプティマイザステップ数は。ウォームアップ 2% なら何ステップか。lr_max = 6e-4、lr_min = 6e-5 のとき、コサイン区間の中間点の lr はいくらか(第50日)。
2. 16 バイト/パラメータ則での静的な学習メモリ。40 GB に収まるか。代わりにマイクロバッチを制限するのは何か。
3. 総 FLOPs、時間、費用。
4. 評価とチェックポイントの方針を書け: estimate_loss を呼ぶ頻度、残すチェックポイントの選び方、チェックポイントの中身、報告する内容(第51、53、55日)。
5. 最終報告に載せる、この規模のモデルで示せることと示せないことを 2 文で書け(第54日)。

独立習得基準

4,769 ステップ、約 95 のウォームアップ、中間点の lr 3.3e-4、約 1.99 GB の静的メモリ、1.865e18 FLOPs、約 4.74 時間と $5.69、完全な評価・チェックポイント方針、誠実な能力の記述を、各数値の式とともに示せる。

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

linking arXiv non-exclusive license / Open Access

Decoupled Weight Decay Regularization

著者・提供機関: Ilya Loshchilov, Frank Hutter | オリジナル出所URL ↗

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

検証バージョンと日付: ICLR 2019 / arXiv:1711.05101v3 (Verified: 2026-10-30)

選定理由:

Foundational primary literature defining AdamW, demonstrating that L2 regularization does not equal decoupled weight decay in adaptive gradient algorithms.

ソース前提知識:

Stochastic gradient descent, L2 regularization, and momentum concepts from Days 31-33.

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

All update equations and mathematical proofs are self-contained in the curriculum lessons.

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

Original Adam algorithm implementing L2 regularization via gradient addition, leading to sub-optimal generalization compared to decoupled weight decay.

PyTorch torch.optim.AdamW documentation ↗ compared_alternative [Modified BSD]

Standard library reference; Loshchilov & Hutter provides theoretical mathematical derivations and empirical justification.

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.