基於 Tmux 的無頭後台持久化訓練與即時遙測流構建
Agent 輔助工程檢查
前置要求
第 58 天行前檢查通過,Linux tmux/nohup 進程管理原理。
學習目標
在持久化 tmux 會話中解耦啟動無頭預訓練任務,將運行指標重定向至結構化遙測日誌,恪守不將掛機等待時間算作有效學習時長的工程原則。
操作指引
1. 以分離狀態啟動:tmux new-session -d -s pretrain '...' 建立會話但不連接。透過 tee -a 輸出,使重啟時 training.log 只追加、不被截斷。
2. 確認會話(tmux ls)和進程(ps aux | grep '[t]rain.py')都存在。
3. 相隔一分鐘取兩次日誌末尾,確認步數在增長。
4. 斷連測試:不要在 tmux 裡輸入 exit,直接關閉 SSH 用戶端,重新連接後重複第 2、3 步。解釋進程為何存活:它的父進程是 tmux 伺服器,而不是你的登入 shell。
5. 用 tmux attach -t pretrain 連接觀察,再按 Ctrl-B 然後 d 分離。
6. 保留證據(兩次末尾、斷連前後的 ps 行、實際耗時),只把閱讀遙測的分鐘記為研習時間;等待 GPU 不算研習。
時間安排(120 分鐘):閱讀 20 + 練習 70 + 檢查 30。 閱讀:nanoGPT train.py 的日誌迴圈和 W&B 實驗追蹤概述。練習:在模擬或真實執行上完成第 1-5 步。檢查:憑記憶重做斷連測試,並將證據與參考答案對照。
自主實作與記錄
在脫機 tmux 會話中啟動模擬訓練。關閉終端重新登錄,利用 ps aux | grep python 確認任務仍在運行且 training.log 持續更新。
使用 nohup python3 train.py >> training.log 2>&1 & 或 tmux 分離會話。
# 1. Start the run detached. tee -a appends, so a restart never truncates earlier steps.
tmux new-session -d -s pretrain 'python3 train.py 2>&1 | tee -a training.log'
# 2. Confirm that the session and the training process both exist.
tmux ls # expect: pretrain: 1 windows (created ...)
ps aux | grep '[t]rain.py' # the [t] pattern stops grep from matching itself
# 3. Record two log tails a minute apart; the step number must grow.
tail -n 2 training.log; sleep 60; tail -n 2 training.log
# 4. Disconnect test: close the SSH client (do not type exit inside tmux), reconnect, then repeat
# step 2 and step 3. The process survives because the tmux server, not your SSH login shell,
# is its parent. nohup python3 train.py >> training.log 2>&1 & is the no-tmux fallback.
tmux attach -t pretrain # watch live; Ctrl-B then d detaches again without stopping training
# 5. Stop on purpose at the end. Ctrl-C sends SIGINT; train.py should save a checkpoint and exit.
tmux send-keys -t pretrain C-c
# Evidence to keep: the two tails (step grew), the ps line before and after the disconnect,
# and the wall-clock hours. Only the minutes you spent reading telemetry count as study time.
成功構建不受本地網絡終端中斷影響的持久化後台訓練管道與遙測日誌流。
權威來源資源與權利證明
Experiment Tracking and Real-Time ML Telemetry Guide
對應章節 / 時間戳: Experiments overview: How it works (wandb.init, run.log)
驗證版本與日期: W&B Documentation (Verified: 2026-10-30)
Widely adopted concepts for decoupling active human study time from passive unattended GPU training runs.
Loss functions, learning rate schedules, and step loops.
Offline local CSV/JSONL telemetry logging is fully provided in lesson code.
對比候選資源與選型分析 (2)
Local self-hosted visualizer; compared alongside cloud telemetry dashboards.
Enterprise lifecycle manager; W&B is preferred for quick LLM run telemetry.
nanoGPT: train.py pretraining loop
對應章節 / 時間戳: train.py: logging (wandb_log, wandb.init) and the per-iteration print of loss, time and mfu
驗證版本與日期: 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)
Heavy abstraction layer wrapping distributed training; nanoGPT exposes raw PyTorch loops without framework overhead.
High-performance enterprise distributed framework; unsuitable for local educational first-principles comprehension.