Skip to content

base_train.py 中文註解版本

scripts/base_train.py 是 nanochat 的預訓練主腳本。以下為關鍵區段的中文註解:

CLI 參數

--depth: Transformer 層數(唯一必要的複雜度旋鈕)
--aspect-ratio: model_dim = depth × aspect_ratio(預設 64)
--device-batch-size: 每張 GPU 的 batch size(VRAM 不足時調降)
--total-batch-size: 全局 batch size(-1 = 自動計算)
--num-iterations: 訓練步數(-1 = 由 scaling laws 決定)
--target-param-data-ratio: 目標資料:參數比(預設 12,Chinchilla=20)

Scaling Law 計算流程

  1. depth 計算 model_dimn_head
  2. 查 d12 參考模型的參數,推算最佳 token 數量
  3. 由 Power Lines 論文公式計算最佳 batch size
  4. 依 batch ratio 調整 learning rate
  5. 依 T_epoch 框架調整 weight decay

訓練循環

每步包含:

  1. grad_accum_steps 次 micro-batch 的 forward/backward
  2. 更新 LR、momentum、weight decay scheduler
  3. optimizer step + zero_grad
  4. 定期執行 val bpb、CORE metric、sample generation、checkpoint save