File size: 12,552 Bytes
48ecd01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | #!/usr/bin/env bash
# =============================================================================
# monitor_3b.sh โ 3B ํ์ต ์ค์๊ฐ ๋ชจ๋ํฐ๋ง + ์ด์ ๊ฐ์ง + ์๋ ์ฒดํฌํฌ์ธํธ ์ ๋ฆฌ
#
# Usage:
# bash scripts/monitor_3b.sh # ๊ธฐ๋ณธ ๊ฐ์
# bash scripts/monitor_3b.sh --check-once # 1ํ ๊ฒ์ฌ
# bash scripts/monitor_3b.sh --auto-cleanup # ์๋ ์ค๋๋ ์ฒดํฌํฌ์ธํธ ์ญ์
#
# 3B ํนํ ์ฌํญ:
# - ์ฒดํฌํฌ์ธํธ 27GB/๊ฐ โ ๋์คํฌ ๊ฐ์ ๊ฐํ
# - NCCL hang ๊ฐ์ง + ์๋ ์ฌ์์ ์ต์
# - ์์ ์๋ฃ ์๊ฐ ์ค์๊ฐ ๊ณ์ฐ
# - ํ๋ก์ธ์ค ์ค๋ณต ์คํ ๋ฐฉ์ง
# =============================================================================
set -euo pipefail
# ---- Configuration ----------------------------------------------------------
RUN_NAME="${RUN_NAME:-korean_3b_fp8_run1}"
LOG_FILE="${1:-checkpoints/${RUN_NAME}/train.log}"
CKPT_DIR="checkpoints/${RUN_NAME}"
CHECK_INTERVAL=60 # 3B๋ step ๊ฐ๊ฒฉ ๋ ๊น โ 60์ด
ZERO_LOSS_THRESHOLD=3
GNORM_WARN=10.0
GNORM_CRITICAL=50.0
LOSS_SPIKE_FACTOR=3.0
STALL_TIMEOUT=600 # 10๋ถ (3B๋ step ๋ ์ค๋ ๊ฑธ๋ฆผ)
DISK_WARN_PCT=85
DISK_CRITICAL_PCT=92
GPU_UTIL_WARN=50
MAX_CHECKPOINTS=15 # ์ต๋ ๋ณด๊ด ์ฒดํฌํฌ์ธํธ ์ (15 ร 27GB = 405GB)
CHECK_ONCE=false
AUTO_CLEANUP=false
AUTO_RESTART=false
# Parse args
for arg in "$@"; do
case "$arg" in
--check-once) CHECK_ONCE=true ;;
--auto-cleanup) AUTO_CLEANUP=true ;;
--auto-restart) AUTO_RESTART=true ;;
esac
done
# Fix LOG_FILE if first arg was a flag
if [[ "$LOG_FILE" == --* ]]; then
LOG_FILE="checkpoints/${RUN_NAME}/train.log"
fi
# ---- Colors -----------------------------------------------------------------
RED='\033[0;31m'; YELLOW='\033[1;33m'; GREEN='\033[0;32m'
CYAN='\033[0;36m'; MAGENTA='\033[0;35m'; NC='\033[0m'
timestamp() { date '+%Y-%m-%d %H:%M:%S'; }
alert() {
local level="$1" msg="$2"
case "$level" in
CRITICAL) echo -e "${RED}๐ด [$(timestamp)] [CRITICAL] ${msg}${NC}" ;;
WARNING) echo -e "${YELLOW}๐ [$(timestamp)] [WARNING] ${msg}${NC}" ;;
INFO) echo -e "${CYAN}๐ก [$(timestamp)] [INFO] ${msg}${NC}" ;;
OK) echo -e "${GREEN}โ
[$(timestamp)] [OK] ${msg}${NC}" ;;
esac
}
# ---- Parse metrics ----------------------------------------------------------
parse_metrics() {
local n="${1:-20}"
[[ -f "$LOG_FILE" ]] || return
tail -n "$n" "$LOG_FILE" | grep "step.*loss.*gnorm" || true
}
extract_field() {
echo "$1" | grep -oP "${2}\s+\K[0-9]+\.[0-9e+\-]+" | head -1
}
extract_step() {
echo "$1" | grep -oP "step\s+\K[0-9]+" | head -1
}
# ---- Check: Loss = 0 -------------------------------------------------------
check_loss_zero() {
local lines
lines=$(parse_metrics "$ZERO_LOSS_THRESHOLD")
[[ -z "$lines" ]] && return 0
local zero_count=0
while IFS= read -r line; do
local loss=$(extract_field "$line" "loss")
if [[ -n "$loss" ]] && (( $(echo "$loss < 0.001" | bc -l 2>/dev/null || echo 0) )); then
((zero_count++))
fi
done <<< "$lines"
if [[ $zero_count -ge $ZERO_LOSS_THRESHOLD ]]; then
alert CRITICAL "Loss๊ฐ ${zero_count}ํ ์ฐ์ ~0! Labels ๋ฒ๊ทธ. ์ฆ์ ์ค๋จ!"
return 1
fi
}
# ---- Check: Loss spike -----------------------------------------------------
check_loss_spike() {
local lines=$(parse_metrics 20)
[[ -z "$lines" ]] && return 0
local losses=()
while IFS= read -r line; do
local loss=$(extract_field "$line" "loss")
[[ -n "$loss" ]] && losses+=("$loss")
done <<< "$lines"
local count=${#losses[@]}
[[ $count -lt 5 ]] && return 0
local last="${losses[$((count-1))]}"
local sum=0
for ((i=0; i<count-1; i++)); do
sum=$(echo "$sum + ${losses[$i]}" | bc -l 2>/dev/null || echo "$sum")
done
local avg=$(echo "$sum / ($count - 1)" | bc -l 2>/dev/null || echo "0")
if [[ "$avg" != "0" ]]; then
local ratio=$(echo "$last / $avg" | bc -l 2>/dev/null || echo "1")
if (( $(echo "$ratio > $LOSS_SPIKE_FACTOR" | bc -l 2>/dev/null || echo 0) )); then
alert WARNING "Loss spike! ํ์ฌ=${last}, ํ๊ท =${avg}, ๋น์จ=${ratio}x"
fi
fi
}
# ---- Check: Gradient norm ---------------------------------------------------
check_gnorm() {
local lines=$(parse_metrics 5)
[[ -z "$lines" ]] && return 0
local gnorm=$(extract_field "$(echo "$lines" | tail -1)" "gnorm")
[[ -z "$gnorm" ]] && return 0
if (( $(echo "$gnorm > $GNORM_CRITICAL" | bc -l 2>/dev/null || echo 0) )); then
alert CRITICAL "GNorm=${gnorm} > ${GNORM_CRITICAL}! ๋ฐ์ฐ ์ง์ ."
elif (( $(echo "$gnorm > $GNORM_WARN" | bc -l 2>/dev/null || echo 0) )); then
alert WARNING "GNorm=${gnorm} ๋ถ์์ ."
fi
}
# ---- Check: Stall / NCCL hang ----------------------------------------------
check_stall() {
[[ ! -f "$LOG_FILE" ]] && return 0
local last_mod=$(stat -c %Y "$LOG_FILE" 2>/dev/null || echo 0)
local now=$(date +%s)
local diff=$((now - last_mod))
if [[ $diff -gt $STALL_TIMEOUT ]]; then
alert CRITICAL "๋ก๊ทธ ${diff}์ด ($(( diff/60 ))๋ถ) ๋ฉ์ถค! NCCL hang ๊ฐ๋ฅ์ฑ."
# NCCL hang ์๋ ์ฌ์์
if $AUTO_RESTART; then
alert WARNING "์๋ ์ฌ์์ ์๋..."
local pid=$(pgrep -f "pretrain.py.*korean_3b" | head -1 || true)
if [[ -n "$pid" ]]; then
kill -9 "$pid" 2>/dev/null || true
sleep 5
alert INFO "์ด์ ํ๋ก์ธ์ค ์ข
๋ฃ. launch_3b_pretrain.sh ์ฌ์คํ ํ์."
fi
fi
fi
}
# ---- Check: Disk (3B ๊ฐํ) --------------------------------------------------
check_disk() {
local usage=$(df /PROJECT 2>/dev/null | awk 'NR==2 {print $5}' | tr -d '%')
if [[ -n "$usage" && "$usage" -gt "$DISK_CRITICAL_PCT" ]]; then
alert CRITICAL "๋์คํฌ ${usage}% > ${DISK_CRITICAL_PCT}%! ์ฆ์ ์ ๋ฆฌ ํ์!"
$AUTO_CLEANUP && cleanup_old_checkpoints
elif [[ -n "$usage" && "$usage" -gt "$DISK_WARN_PCT" ]]; then
alert WARNING "๋์คํฌ ${usage}% > ${DISK_WARN_PCT}%. ์ฒดํฌํฌ์ธํธ ์ ๋ฆฌ ๊ถ์ฅ."
fi
}
# ---- Check: GPU utilization -------------------------------------------------
check_gpu() {
command -v nvidia-smi &>/dev/null || return 0
local low=0 total=0
while IFS= read -r util; do
((total++))
[[ "$util" -lt "$GPU_UTIL_WARN" ]] && ((low++))
done < <(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits 2>/dev/null)
[[ $total -gt 0 && $low -gt 0 ]] && alert INFO "${low}/${total} GPU util < ${GPU_UTIL_WARN}%"
}
# ---- Check: ์ฒดํฌํฌ์ธํธ ๋ฌด๊ฒฐ์ฑ -----------------------------------------------
check_checkpoint_integrity() {
local latest=$(ls -d "${CKPT_DIR}"/checkpoint-* 2>/dev/null | sort -V | tail -1 || true)
[[ -z "$latest" ]] && return 0
# ์ต์ ํ์ผ ์กด์ฌ ํ์ธ
if [[ ! -f "${latest}/model.pt" ]] && [[ ! -f "${latest}/model.safetensors" ]]; then
alert WARNING "์ต๊ทผ ์ฒดํฌํฌ์ธํธ์ ๋ชจ๋ธ ํ์ผ ์์: ${latest}"
fi
# ํฌ๊ธฐ ํ์ธ (3B model.pt๋ ์ต์ 2GB)
local size=$(du -sb "${latest}" 2>/dev/null | awk '{print $1}')
if [[ -n "$size" && "$size" -lt 2000000000 ]]; then
alert WARNING "์ฒดํฌํฌ์ธํธ ํฌ๊ธฐ ๋น์ ์ (${size} bytes < 2GB): ${latest}"
fi
}
# ---- Cleanup: ์ค๋๋ ์ฒดํฌํฌ์ธํธ ์๋ ์ญ์ ------------------------------------
cleanup_old_checkpoints() {
local ckpts=($(ls -d "${CKPT_DIR}"/checkpoint-* 2>/dev/null | sort -V))
local count=${#ckpts[@]}
if [[ $count -le $MAX_CHECKPOINTS ]]; then
alert OK "์ฒดํฌํฌ์ธํธ ${count}๊ฐ โค ${MAX_CHECKPOINTS}. ์ ๋ฆฌ ๋ถํ์."
return
fi
# ์ด์ ํ ์ฒดํฌํฌ์ธํธ ๋ณด์กด (๋งค 10K step)
local deletable=()
local preserved=()
for ckpt in "${ckpts[@]}"; do
local step_num=$(basename "$ckpt" | grep -oP '\d+' || echo "0")
if (( step_num % 10000 == 0 && step_num > 0 )); then
preserved+=("$ckpt")
else
deletable+=("$ckpt")
fi
done
# ์ต๊ทผ MAX_CHECKPOINTS๊ฐ๋ ๋ฌด์กฐ๊ฑด ๋ณด์กด
local n_deletable=${#deletable[@]}
local total_keep=$(( ${#preserved[@]} + MAX_CHECKPOINTS ))
local to_delete=$(( count - total_keep ))
[[ $to_delete -le 0 ]] && { alert OK "์ ๋ฆฌ ๋ถํ์ (์ด์ ํ ${#preserved[@]}๊ฐ + ์ต๊ทผ ${MAX_CHECKPOINTS}๊ฐ ๋ณด์กด)."; return; }
alert INFO "${count}๊ฐ ์ฒดํฌํฌ์ธํธ โ ${to_delete}๊ฐ ์ญ์ (์ด์ ํ ${#preserved[@]}๊ฐ ์๊ตฌ ๋ณด์กด)"
local deleted=0
for ckpt in "${deletable[@]}"; do
[[ $deleted -ge $to_delete ]] && break
local ckpt_size=$(du -sh "$ckpt" 2>/dev/null | awk '{print $1}')
echo " ์ญ์ : $ckpt (${ckpt_size})"
rm -rf "$ckpt"
((deleted++))
done
alert OK "์ฒดํฌํฌ์ธํธ ์ ๋ฆฌ ์๋ฃ. (${deleted}๊ฐ ์ญ์ )"
}
# ---- ETA ๊ณ์ฐ ---------------------------------------------------------------
estimate_eta() {
[[ ! -f "$LOG_FILE" ]] && return
# ์ต๊ทผ step ๋ฒํธ + ์๊ฐ
local lines=$(parse_metrics 50)
[[ -z "$lines" ]] && return
local last_line=$(echo "$lines" | tail -1)
local first_line=$(echo "$lines" | head -1)
local cur_step=$(extract_step "$last_line")
local max_steps=$(grep -oP "max_steps.*?(\d+)" "${CKPT_DIR}/train.log" 2>/dev/null | head -1 | grep -oP '\d+$' || echo "57000")
[[ -z "$cur_step" || "$cur_step" == "0" ]] && return
# step/sec from log timestamps (approximate)
local remaining=$((max_steps - cur_step))
if [[ $remaining -le 0 ]]; then
echo -e "${MAGENTA}๐ ์งํ: ${cur_step}/${max_steps} (์๋ฃ!)${NC}"
return
fi
# ํ์ผ ์์ ์๊ฐ ๊ธฐ๋ฐ rough ETA
local first_time=$(head -20 "$LOG_FILE" 2>/dev/null | grep -oP '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}' | head -1 || true)
if [[ -n "$first_time" ]]; then
local start_epoch=$(date -d "$first_time" +%s 2>/dev/null || echo 0)
local now=$(date +%s)
if [[ $start_epoch -gt 0 && $cur_step -gt 0 ]]; then
local elapsed=$((now - start_epoch))
local sec_per_step=$(echo "$elapsed / $cur_step" | bc -l 2>/dev/null || echo "0")
local eta_sec=$(echo "$remaining * $sec_per_step" | bc 2>/dev/null | cut -d. -f1 || echo "0")
local eta_hours=$(echo "$eta_sec / 3600" | bc 2>/dev/null || echo "?")
local pct=$(echo "scale=1; $cur_step * 100 / $max_steps" | bc 2>/dev/null || echo "?")
echo -e "${MAGENTA}๐ ์งํ: ${cur_step}/${max_steps} (${pct}%) | ๋จ์ ์๊ฐ: ~${eta_hours}h | ${sec_per_step}s/step${NC}"
fi
else
echo -e "${MAGENTA}๐ ์งํ: ${cur_step}/${max_steps}${NC}"
fi
}
# ---- Status summary ---------------------------------------------------------
print_status() {
local lines=$(parse_metrics 1)
[[ -n "$lines" ]] && echo -e "${GREEN}์ต๊ทผ:${NC} $lines"
estimate_eta
if command -v nvidia-smi &>/dev/null; then
echo -e "${CYAN}GPU:${NC}"
nvidia-smi --query-gpu=index,memory.used,memory.total,utilization.gpu,temperature.gpu \
--format=csv,noheader 2>/dev/null | head -8
fi
local ckpt_count=$(ls -d "${CKPT_DIR}"/checkpoint-* 2>/dev/null | wc -l)
local ckpt_size=$(du -sh "${CKPT_DIR}" 2>/dev/null | awk '{print $1}')
echo -e "${CYAN}์ฒดํฌํฌ์ธํธ:${NC} ${ckpt_count}๊ฐ (${ckpt_size})"
local disk=$(df -h /PROJECT 2>/dev/null | awk 'NR==2 {print $3"/"$2" ("$5")"}')
echo -e "${CYAN}๋์คํฌ:${NC} ${disk}"
}
# ---- Main -------------------------------------------------------------------
echo "=================================================================="
echo " 3B Training Monitor"
echo " Run: ${RUN_NAME}"
echo " Log: ${LOG_FILE}"
echo " Interval: ${CHECK_INTERVAL}s"
echo " Auto-cleanup: ${AUTO_CLEANUP} | Auto-restart: ${AUTO_RESTART}"
echo " Ctrl+C to stop"
echo "=================================================================="
run_all_checks() {
check_loss_zero || true
check_loss_spike || true
check_gnorm || true
check_stall || true
check_disk || true
check_gpu || true
check_checkpoint_integrity || true
echo "---"
print_status
echo ""
}
if $CHECK_ONCE; then
run_all_checks
exit 0
fi
while true; do
run_all_checks
sleep "$CHECK_INTERVAL"
done
|