File size: 4,089 Bytes
a221eb4
b663ba3
a221eb4
 
 
 
 
 
 
b663ba3
a221eb4
b663ba3
a221eb4
b663ba3
 
 
 
 
 
a221eb4
 
 
 
 
 
b663ba3
 
 
 
 
 
 
 
 
 
 
a221eb4
 
b663ba3
a221eb4
b663ba3
a221eb4
 
 
 
b663ba3
a221eb4
 
 
 
 
b663ba3
a221eb4
 
 
 
 
 
 
 
 
 
 
b663ba3
 
a221eb4
9456045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a221eb4
 
 
 
 
 
 
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
#!/bin/bash
# Fix torch/transformers mismatch on RunPod pytorch images, then restart training.
# curl -fsSL "https://huggingface.co/datasets/datamatters24/scriptwriter-runpod/resolve/main/fix-and-train.sh" | bash
set -euo pipefail

echo "=== Killing old train session (if any) ==="
tmux kill-session -t scriptwriter-train 2>/dev/null || true
pkill -f 'train_runpod.py' 2>/dev/null || true

echo "=== Restoring torch 2.4.1 + matching torchvision (RunPod image stack) ==="
python3 -m pip install -q --upgrade pip
# Critical: do NOT let pip float torch to 2.13 — it breaks torchvision::nms on this image
python3 -m pip install -q --force-reinstall \
  "torch==2.4.1" "torchvision==0.19.1" "torchaudio==2.4.1" \
  --index-url https://download.pytorch.org/whl/cu124

echo "=== Installing pinned HF train stack (no torch upgrade) ==="
python3 -m pip uninstall -y transformers peft trl accelerate 2>/dev/null || true
python3 -m pip install -q \
  "transformers==4.46.3" \
  "peft==0.13.2" \
  "trl==0.12.1" \
  "accelerate==0.34.2" \
  "datasets>=2.19.0,<3.1" \
  "huggingface_hub>=0.24.0,<0.30" \
  "safetensors>=0.4.0" \
  "sentencepiece>=0.2.0" \
  "protobuf>=4.25.0" \
  "pyyaml>=6.0.0" \
  "tqdm>=4.66.0" \
  "bitsandbytes>=0.43.0,<0.46"

# Freeze torch again in case a dep tried to bump it
python3 -m pip install -q \
  "torch==2.4.1" "torchvision==0.19.1" "torchaudio==2.4.1" \
  --index-url https://download.pytorch.org/whl/cu124

python3 - <<'PY'
import torch, torchvision, transformers, peft, trl
print("torch", torch.__version__, "cuda", torch.cuda.is_available(), "gpus", torch.cuda.device_count())
print("torchvision", torchvision.__version__)
print("transformers", transformers.__version__)
print("peft", peft.__version__)
print("trl", trl.__version__)
from peft import LoraConfig
from transformers import AutoModelForCausalLM
print("imports ok")
PY

DEST=/workspace/scriptwriter-runpod
TGZ=/tmp/scriptwriter-runpod-ready.tgz
echo "=== Refreshing trainer scripts ==="
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}"
# Prevent the trainer from force-reinstalling a newer torch
export RUNPOD_SKIP_FORCE_PIP=1

if [[ ! -f /workspace/data/processed/train.jsonl ]]; then
  echo "=== train.jsonl missing — downloading dataset ${HF_DATASET_REPO} ==="
  mkdir -p /workspace/data/processed
  python3 - <<'PY'
import os
from pathlib import Path
from huggingface_hub import snapshot_download, login
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
if not token:
    raise SystemExit("HF_TOKEN not set in environment — set it in RunPod and retry")
login(token=token, add_to_git_credential=False)
dest = Path("/workspace/data/processed")
snapshot_download(
    repo_id=os.environ.get("HF_DATASET_REPO", "datamatters24/scriptwriter-corpus-ia"),
    repo_type="dataset",
    local_dir=str(dest),
    token=token,
)
train = dest / "train.jsonl"
if not train.exists():
    for alt in ("train-ia.jsonl", "train-local.jsonl", "checkpoint-ia.jsonl"):
        src = dest / alt
        if src.exists() and src.stat().st_size > 0:
            train.write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
            print(f"Created train.jsonl from {alt}")
            break
if not train.exists():
    raise SystemExit("Download finished but train.jsonl still missing")
print(f"train.jsonl lines: {sum(1 for _ in train.open() if _.strip())}")
PY
fi

if [[ ! -f /workspace/data/processed/train.jsonl ]]; then
  echo "ERROR: /workspace/data/processed/train.jsonl missing"
  exit 1
fi

cd "$DEST"
bash scripts/runpod_train_tmux.sh