File size: 6,352 Bytes
84af0a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# Pull selected pi0.5 checkpoints off the Brev instance, resumably, params+assets only.
#
#   scripts/fetch_checkpoints.sh --dry-run 7500 15000 22500
#   scripts/fetch_checkpoints.sh 7500 15000 22500
#
# Why a script and not a bare rsync:
#
#  1. Brev's generated ssh_config sets `RequestTTY yes`. rsync over a pty corrupts its own
#     protocol stream ("protocol version mismatch" / garbage). We force -T off.
#  2. `train_state/` is optimizer state -- 30-40 GiB per checkpoint that the robot never reads.
#     Excluded unconditionally. Use --with-train-state only if you intend to RESUME training.
#  3. 12.5 GiB files over a home connection will drop. --partial --append-verify resumes
#     instead of restarting, and re-checksums the bytes already on disk.
#  4. A preflight refuses to start a transfer that cannot fit in local free space, because
#     filling a root filesystem is a much worse afternoon than a clear error message.
set -euo pipefail

HOST="${HOST:-pi-05-pickplace-fft}"
REMOTE_ROOT="${REMOTE_ROOT:-/data/checkpoints/pi05_g1_pickplace/pickplace_full_v1}"
DEST="${DEST:-$HOME/pi05_checkpoints}"
RESERVE_GIB="${RESERVE_GIB:-15}"   # local headroom to leave untouched

DRY=0
WITH_TS=0
STEPS=()
while [ $# -gt 0 ]; do
  case "$1" in
    --dry-run) DRY=1 ;;
    --with-train-state) WITH_TS=1 ;;
    --dest) DEST="$2"; shift ;;
    --host) HOST="$2"; shift ;;
    -h|--help) sed -n '2,20p' "$0"; exit 0 ;;
    -*) echo "unknown flag: $1" >&2; exit 2 ;;
    *) STEPS+=("$1") ;;
  esac
  shift
done

if [ ${#STEPS[@]} -eq 0 ]; then
  echo "usage: $0 [--dry-run] [--with-train-state] [--dest DIR] STEP [STEP ...]" >&2
  echo "       e.g. $0 7500 15000 22500 29999" >&2
  exit 2
fi

RS_HELP=$(rsync --help 2>&1 || true)
MISSING_FLAGS=""
for f in append-verify info partial exclude; do
  case "$RS_HELP" in *"--$f"*) ;; *) MISSING_FLAGS="$MISSING_FLAGS --$f" ;; esac
done
if [ -n "$MISSING_FLAGS" ]; then
  echo "FAIL: this rsync does not support:$MISSING_FLAGS" >&2
  echo "      $(rsync --version 2>&1 | head -1)" >&2
  echo "      Need GNU rsync 3.1+. Debian/Ubuntu: sudo apt install rsync" >&2
  echo "      (stock macOS ships openrsync, which lacks these: brew install rsync)" >&2
  exit 1
fi

SSH_OPTS=(-o RequestTTY=no -o ConnectTimeout=20)
ssh_run() { ssh "${SSH_OPTS[@]}" "$HOST" "$@"; }

echo "host        : $HOST"
echo "remote root : $REMOTE_ROOT"
echo "destination : $DEST"
echo "steps       : ${STEPS[*]}"
echo

if ! ssh_run true 2>/dev/null; then
  echo "Host aliases Brev has configured on this machine:" >&2
  grep -h '^Host ' ~/.brev/ssh_config 2>/dev/null | sed 's/^/  /' >&2 || true
  cat >&2 <<'MSG'
FAIL: cannot reach the instance over ssh.

Brev rotates the instance IP, and ~/.brev/ssh_config is only rewritten when the CLI is logged
in. Refresh it, then re-run:

    brev login
    brev ls          # confirm the instance is RUNNING
    brev refresh     # rewrites ~/.brev/ssh_config with the current IP
MSG
  exit 1
fi

# ---- preflight: what exists remotely, how big, and does it fit locally ----
echo "== remote inventory =="
ssh_run "cd '$REMOTE_ROOT' 2>/dev/null || { echo 'MISSING: $REMOTE_ROOT'; exit 1; }; \
         for s in ${STEPS[*]}; do \
           if [ -d \"\$s\" ]; then \
             printf '%8s  params=%-8s assets=%-7s train_state=%s\n' \"\$s\" \
               \"\$(du -sh \$s/params 2>/dev/null | cut -f1)\" \
               \"\$(du -sh \$s/assets 2>/dev/null | cut -f1)\" \
               \"\$([ -d \$s/train_state ] && du -sh \$s/train_state | cut -f1 || echo none)\"; \
           else echo \"\$s  MISSING\"; fi; \
         done"

# Bytes we are actually about to move.
if [ "$WITH_TS" -eq 1 ]; then SUBDIRS="params assets train_state"; else SUBDIRS="params assets"; fi
NEED_KB=$(ssh_run "cd '$REMOTE_ROOT'; t=0; for s in ${STEPS[*]}; do for d in $SUBDIRS; do \
            [ -d \"\$s/\$d\" ] && t=\$((t + \$(du -sk \"\$s/\$d\" | cut -f1))); done; done; echo \$t")
NEED_GIB=$(( NEED_KB / 1024 / 1024 ))

mkdir -p "$DEST"
AVAIL_KB=$(df -Pk "$DEST" | tail -1 | awk '{print $4}')
AVAIL_GIB=$(( AVAIL_KB / 1024 / 1024 ))

echo
echo "transfer size : ${NEED_GIB} GiB  (train_state $([ "$WITH_TS" -eq 1 ] && echo INCLUDED || echo excluded))"
echo "local free    : ${AVAIL_GIB} GiB at $DEST"
echo "reserve       : ${RESERVE_GIB} GiB"

if [ $(( NEED_GIB + RESERVE_GIB )) -gt "$AVAIL_GIB" ]; then
  cat >&2 <<MSG

FAIL: will not fit. Need ${NEED_GIB} GiB + ${RESERVE_GIB} GiB reserve, have ${AVAIL_GIB} GiB.

Options, cheapest first:
  * fetch fewer steps -- each params+assets pair is ~12.5 GiB
  * --dest /path/on/a/bigger/volume
  * push to the Hugging Face Hub from the instance instead, and pull one at a time later
  * lower the reserve with RESERVE_GIB=5 (only if you know what is on that volume)
MSG
  exit 1
fi

RSYNC=(rsync -a --info=progress2 --partial --append-verify --human-readable
       -e "ssh ${SSH_OPTS[*]}")
[ "$WITH_TS" -eq 1 ] || RSYNC+=(--exclude 'train_state/')
[ "$DRY" -eq 1 ] && RSYNC+=(--dry-run)

echo
for s in "${STEPS[@]}"; do
  echo "== $s =="
  "${RSYNC[@]}" "$HOST:$REMOTE_ROOT/$s" "$DEST/"
done

[ "$DRY" -eq 1 ] && { echo; echo "dry run -- nothing written."; exit 0; }

# ---- verify: norm_stats present, and remote/local byte counts agree ----
echo
echo "== verify =="
fail=0
for s in "${STEPS[@]}"; do
  if ! find "$DEST/$s/assets" -name norm_stats.json 2>/dev/null | grep -q .; then
    echo "  $s  FAIL: no assets/**/norm_stats.json -- policy would serve garbage"; fail=1; continue
  fi
  r=$(ssh_run "cd '$REMOTE_ROOT/$s'; du -sk $SUBDIRS 2>/dev/null | awk '{t+=\$1} END {print t}'")
  l=$(cd "$DEST/$s" && du -sk $SUBDIRS 2>/dev/null | awk '{t+=$1} END {print t}')
  # du can differ by a few blocks across filesystems; 1% is generous but still catches truncation.
  if [ "$r" -gt 0 ] && [ $(( (r - l) * 100 / r )) -gt 1 ] ; then
    echo "  $s  FAIL: remote ${r}KB vs local ${l}KB -- incomplete, re-run to resume"; fail=1
  else
    echo "  $s  OK ($(du -sh "$DEST/$s" | cut -f1))"
  fi
done

echo
if [ "$fail" -eq 1 ]; then
  echo "Some checkpoints did not verify. Re-run the same command -- rsync resumes."
  exit 1
fi
echo "All requested checkpoints verified in $DEST."
echo "Do NOT terminate the instance until you have confirmed at least one loads locally:"
echo "  uv run python scripts/load_checkpoint_test.py $DEST/${STEPS[0]}"