| #!/bin/bash |
| |
| |
| set -euo pipefail |
|
|
| echo "=== Force-stopping old training ===" |
| tmux kill-session -t scriptwriter-train 2>/dev/null || true |
| tmux kill-server 2>/dev/null || true |
| pkill -9 -f 'train_runpod.py' 2>/dev/null || true |
| pkill -9 -f 'scripts/train_runpod' 2>/dev/null || true |
| pkill -9 -f torchrun 2>/dev/null || true |
| sleep 2 |
| echo "Old session cleared." |
|
|
| DEST=/workspace/scriptwriter-runpod |
| TGZ=/tmp/scriptwriter-runpod-ready.tgz |
| curl -fsSL "https://huggingface.co/datasets/datamatters24/scriptwriter-runpod/resolve/main/scriptwriter-runpod-ready.tgz" -o "$TGZ" |
| rm -rf "$DEST" |
| mkdir -p "$DEST" |
| tar xzf "$TGZ" -C "$DEST" --strip-components=1 --no-same-owner --no-same-permissions |
|
|
| export WORKDIR="$DEST" |
| export DATA_DIR=/workspace/data/processed |
| export OUTPUT_DIR=/workspace/models/lora |
| export HF_DATASET_REPO="${HF_DATASET_REPO:-datamatters24/scriptwriter-corpus-ia}" |
| export HF_MODEL_REPO="${HF_MODEL_REPO:-datamatters24/scriptwriter-lora-ia}" |
| export BASE_MODEL="${BASE_MODEL:-meta-llama/Llama-3.2-3B-Instruct}" |
| export RUNPOD_SKIP_FORCE_PIP=1 |
| unset CUDA_VISIBLE_DEVICES |
| export MAX_SEQ_LENGTH=2048 |
| export MAX_SEQ_LENGTH_CAP=2048 |
| export PER_DEVICE_TRAIN_BATCH_SIZE=1 |
| export GRADIENT_ACCUMULATION_STEPS=4 |
| export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True |
| export NCCL_IB_DISABLE=1 |
| export NCCL_P2P_DISABLE=1 |
| export TOKENIZERS_PARALLELISM=false |
| export RESUME_FROM_CHECKPOINT="${RESUME_FROM_CHECKPOINT:-1}" |
| export NPROC="${NPROC:-2}" |
|
|
| if [[ ! -f /workspace/data/processed/train.jsonl ]]; then |
| echo "train.jsonl missing — use fix-and-train.sh instead" |
| exit 1 |
| fi |
|
|
| cd "$DEST" |
| mkdir -p /workspace/logs /workspace/models/lora |
| mkdir -p "$DEST/data" |
| ln -sfn /workspace/data/processed "$DEST/data/processed" |
|
|
| apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq tmux >/dev/null || true |
|
|
| WORKER=/workspace/logs/_train_worker.sh |
| cat > "$WORKER" <<'INNER' |
| |
| set -euo pipefail |
| cd /workspace/scriptwriter-runpod |
| export HF_TOKEN="${HF_TOKEN:-}" |
| export HUGGING_FACE_HUB_TOKEN="${HF_TOKEN:-}" |
| export HF_HOME="${HF_HOME:-/workspace/.cache/huggingface}" |
| export CONFIG_PATH=/workspace/scriptwriter-runpod/config/training.yaml |
| export BASE_MODEL="${BASE_MODEL:-meta-llama/Llama-3.2-3B-Instruct}" |
| export OUTPUT_DIR=/workspace/models/lora |
| export HF_MODEL_REPO="${HF_MODEL_REPO:-datamatters24/scriptwriter-lora-ia}" |
| unset CUDA_VISIBLE_DEVICES |
| export MAX_SEQ_LENGTH=2048 |
| export MAX_SEQ_LENGTH_CAP=2048 |
| export PER_DEVICE_TRAIN_BATCH_SIZE=1 |
| export GRADIENT_ACCUMULATION_STEPS=4 |
| export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True |
| export NCCL_IB_DISABLE=1 |
| export NCCL_P2P_DISABLE=1 |
| export TOKENIZERS_PARALLELISM=false |
| export RESUME_FROM_CHECKPOINT="${RESUME_FROM_CHECKPOINT:-1}" |
| NPROC="${NPROC:-2}" |
| exec > >(tee -a /workspace/logs/train_live.log) 2>&1 |
| echo "========== TRAIN START $(date -Is) ==========" |
| echo "DDP on ${NPROC} GPUs: seq=2048 batch=1 accum=4 (NCCL_P2P/IB disabled)" |
| ls /workspace/models/lora/checkpoint-* 2>/dev/null | tail -5 || echo "no prior checkpoint" |
| nvidia-smi || true |
| torchrun --standalone --nproc_per_node="$NPROC" scripts/train_runpod.py |
| echo "========== UPLOAD $(date -Is) ==========" |
| python3 scripts/sync_hf.py upload-model --repo "$HF_MODEL_REPO" --folder /workspace/models/lora |
| echo "========== DONE $(date -Is) ==========" |
| echo "STOP THE POD" |
| INNER |
| chmod +x "$WORKER" |
|
|
| tmux new-session -d -s scriptwriter-train -n train "bash $WORKER" |
| tmux new-window -t scriptwriter-train -n monitor |
| tmux send-keys -t scriptwriter-train:monitor "watch -n 15 'nvidia-smi --query-gpu=index,memory.used,utilization.gpu --format=csv; echo; tail -25 /workspace/logs/train_live.log'" Enter |
| echo "Started 2-GPU DDP — attach: tmux attach -t scriptwriter-train" |
| echo "Expect both GPUs ~6–11 GiB and high util." |
|
|