File size: 1,995 Bytes
2e52dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# On-instance completion guard, independent of any local watcher.
# Destroys this instance only after verify_uploads.py proves model + results are on the Hub.
# Failure or a vanished driver stops (not destroys) it, so disk, logs and checkpoints survive for inspection.
set -uo pipefail
W=/workspace; LOG=$W/guard.log; PY=$W/code/.venv/bin/python
while IFS= read -r -d "" kv; do
  case "$kv" in HF_TOKEN=*|CONTAINER_API_KEY=*|CONTAINER_ID=*) export "$kv";; esac
done < /proc/1/environ
export HF_HOME=$W/.cache/huggingface
VAST="$HOME/.local/bin/vastai"
log() { echo "$(date -u +%FT%TZ) $*" >> $LOG; }
push_logs() {
  cp -f $W/*.log $W/out/ 2>/dev/null
  $PY -c "
from huggingface_hub import HfApi
HfApi().upload_folder(folder_path='$W/out', repo_id='divakantpandey/code-embed-v3-src', path_in_repo='v2-zeroshot/run',
  allow_patterns=['*.log', 'progress.json', 'stages.json', 'throughput.json'], commit_message='v2-zeroshot: guard $1')" >> $LOG 2>&1 \
  || log "log upload failed"
}
halt() {  # $1 = stop|destroy
  push_logs "$1"
  log "vastai $1 instance $CONTAINER_ID"
  for i in 1 2 3; do
    "$VAST" "$1" instance "$CONTAINER_ID" --api-key "$CONTAINER_API_KEY" >> $LOG 2>&1 && exit 0
    sleep 60
  done
  log "vastai $1 failed 3 times"; exit 1
}

log "armed for instance $CONTAINER_ID"
tries=0; gone=0
while true; do
  note=$(cat $W/out/progress.json 2>/dev/null)
  case "$note" in
    *'"all done"'*)
      if $PY $W/verify_uploads.py >> $LOG 2>&1; then log "outputs verified"; halt destroy; fi
      tries=$((tries + 1)); log "verification failed ($tries/3)"
      [ $tries -ge 3 ] && { log "cannot verify uploads, stopping instead of destroying"; halt stop; }
      sleep 600; continue ;;
    *'"FAILED'*)
      log "run failed: $note"; halt stop ;;
  esac
  if pgrep -f "bash $W/run.sh" >/dev/null; then gone=0
  else gone=$((gone + 1)); log "driver not running ($gone/5)"
    [ $gone -ge 5 ] && { log "driver gone without a final status"; halt stop; }
  fi
  sleep 120
done