新規内容: パラメータ・メモリ・計算量の算術
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、時間、費用を求めよ。
ブロックごと: 12 * 768^2 + 4 * 768。共有された埋め込みは 1 回だけ数える。静的な学習メモリは N * 16 バイト。実効スループットは 312e12 * 0.40 FLOP/s。
1. ブロックごと = 12 * 768^2 + 4 * 768 = 7,080,960、12 ブロックで 84,971,520。埋め込み = 50,257 * 768 + 1,024 * 768 = 39,383,808。最終 LayerNorm = 2 * 768 = 1536。合計 N = 124,356,864。線形層バイアス(12 * 9 * 768 = 82,944)を加えると 124,439,808 で、bias=True の GPT-2 で PyTorch が報告する数と一致する。N = 124,356,864 の FP16 重み: 248,713,728 バイト = 237.2 MiB(248.7 MB)。
2. 静的 = 1e9 * 16 バイト = 16 GB。合計 = 16 + 8(活性化)+ 1(CUDA コンテキスト)= 25 GB > 24 GB なので、勾配チェックポイント、小さいマイクロバッチと勾配累積、ZeRO オフロードのいずれかを使わない限りメモリ不足になる。
3. C = 6 * N * D = 6 * 125e6 * 10e9 = 7.5e18 FLOPs。実効スループット = 1.248e14 FLOP/s。時間 = 60,096 秒 = 16.69 時間。費用 = 16.69 * $1.50 = $25.04。
線形層バイアスの有無それぞれで正確なパラメータ数を再現し、16 バイト/パラメータ則でメモリ不足を予測し、6ND と MFU から時間と費用を求められる。
公式ソースリソースと権利証跡
Scaling Laws for Neural Language Models
該当セクション・タイムスタンプ: 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.
Transformer Math 101
該当セクション・タイムスタンプ: 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.
Original 6ND approximation; EleutherAI expands on activation memory and KV cache.
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
該当セクション・タイムスタンプ: 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.
Foundational mixed-precision paper; ZeRO formalizes the exact 16-byte Adam state footprint.