Appearance
實驗紀錄
建置與執行
最低要求
- CPU 版本: 任意 C 編譯器 + OpenMP, 無 GPU 也可執行
- CUDA 版本: NVIDIA GPU (Compute Capability 7.0+), CUDA Toolkit 11+
下載資源
bash
# 下載 GPT-2 124M 權重、tokenizer、tiny shakespeare 資料集
chmod u+x ./dev/download_starter_pack.sh
./dev/download_starter_pack.sh
# 或手動從 PyTorch 生成
pip install -r requirements.txt
python dev/data/tinyshakespeare.py
python train_gpt2.py編譯與執行
bash
# CPU 版 (FP32, 建議 8+ 執行緒)
make train_gpt2
OMP_NUM_THREADS=8 ./train_gpt2
# GPU 版 (BF16)
make train_gpt2cu
./train_gpt2cu
# GPU 版 (FP32)
make train_gpt2cu PRECISION=FP32
./train_gpt2cu
# GPU 版 + cuDNN Flash Attention
make train_gpt2cu USE_CUDNN=1
./train_gpt2cu除錯
編譯時將 -O3 取代為 -g 即可在 IDE 中單步除錯。
硬體需求
| 精度 | GPU 記憶體 (B=4, T=1024) | GPU 記憶體 (B=8, T=1024) |
|---|---|---|
| FP32 | ~8 GB | ~14 GB |
| BF16 | ~5 GB | ~8 GB |
| BF16 + recompute=2 | ~4 GB | ~7 GB |
測試
bash
# CPU 版測試 (與 PyTorch 比對)
make test_gpt2
./test_gpt2
# GPU 版測試
make test_gpt2cu PRECISION=FP32 && ./test_gpt2cu
make test_gpt2cu USE_CUDNN=1 && ./test_gpt2cu成功輸出: overall okay: 1
CLI 參數
| 參數 | 說明 | 預設值 |
|---|---|---|
-b <int> | Micro batch size B | 4 |
-t <int> | Sequence length T | 1024 |
-d <int> | Total batch size | BTnum_processes |
-l <float> | Learning rate | 3e-4 |
-x <int> | Max steps | -1 (1 epoch) |
-v <int> | Val loss frequency | 20 |
-s <int> | Sample frequency | 20 |
-g <int> | Gen steps | 64 |
-r <int> | Recompute (0/1/2) | 1 |
超參數探索
bash
# 範例 sweep 腳本
learning_rates=(3e-5 1e-4 3e-4 1e-3)
for i in {0..3}; do
export CUDA_VISIBLE_DEVICES=$i
screen -dmS "tr$i" bash -c \
"./train_gpt2cu -i data/TinyStories -v 250 -s 250 -g 144 -l ${learning_rates[$i]} -o stories$i.log"
done