File size: 9,395 Bytes
868a5eb | 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 | #!/bin/bash
#SBATCH --job-name=droid_replace_loop
#SBATCH --partition=c6-128-main
#SBATCH --cpus-per-task=2
#SBATCH --mem=8G
#SBATCH --time=48:00:00
#SBATCH --output=/home/wujiayir/robot_tools/droid/out/slurm/replace_loop_%j.log
#
# Selection QA / replacement driver (scripts/select_qa.py). Each round:
# 1. pending -> active episodes that have data/<uuid>/calib_refined_v2.json but no fresh score
# 2. score -> sbatch --wait array (8 CPU / 16 GB per task, <= SCORE_PER_TASK episodes each) rendering the two
# exterior cameras at every usable GT step -> data/selection_qa/scores/<uuid>.json
# 3. decide -> decisions.csv + per-task table (out/selection_qa/decide_summary.json); stop when pass == TARGET
# 4. replace -> data/selection_qa/replacements_round<k>.tsv for every FAILED slot (+1 extra success on round 1)
# 5. fetch the new episodes (fetch_episode.py), GT masks on GPU (scripts/sbatch_gt_masks.sh, sbatch --wait, chunks of
# GT_CHUNK episodes), then scripts/sbatch_pipeline.sh over the replacement manifest (sbatch --wait, throttle THROTTLE)
# until pass == 1000 or MAX_ROUNDS (default 6) replacement rounds. Only acts on episodes that already have a v2
# calibration; never re-runs refine_v2 on an existing episode (the pipeline is only submitted for the new uuids), so it
# is safe while another sbatch_pipeline.sh array is still finishing the original selection: rounds that find failed
# slots are counted, rounds that only wait for pending episodes are not (WAIT_S between polls, MAX_WAIT_LOOPS cap).
#
# Usage: sbatch scripts/replace_loop.sh (or bash scripts/replace_loop.sh on a compute node)
# MANIFEST_ONLY=data/selection_qa/reselect_round1.tsv ROUND_TAG=reselect1 sbatch scripts/replace_loop.sh
# one-shot: fetch -> GT masks -> pipeline for that manifest, then score every pending episode, then exit
# (used by `select_qa.py reselect`; no decide/replace loop)
# MAX_ROUNDS=3 DRY_RUN=1 sbatch scripts/replace_loop.sh (score + decide + replace --dry-run, no submissions)
# Knobs: MAX_ROUNDS (6) WAIT_S (600) MAX_WAIT_LOOPS (36) THROTTLE (40) SCORE_PER_TASK (20) GT_CHUNK (40) SEED (0)
# IOU_MIN (0.80) R2G_MAX (6.0) MIN_FRAMES (6) R2G_METRIC (r2g | r2g_near) GT_SET (strict | ext) GT_POLICY (pipeline default 'skip') PIPE_ENV (extra env for the
# pipeline, e.g. "REFINE_FAIL=render") SKIP_GT=1 (no GT job; the pipeline then refines with Canny -> unverifiable
# -> the episode fails next round) FINALIZE (1)
set -u
DROID=/home/wujiayir/robot_tools/droid
PY=/home/wujiayir/miniconda3/envs/droid/bin/python
SQ="$PY $DROID/scripts/select_qa.py"
QA=$DROID/data/selection_qa
OUTQA=$DROID/out/selection_qa
LOGS=$DROID/out/slurm
MAX_ROUNDS=${MAX_ROUNDS:-6}
WAIT_S=${WAIT_S:-600}
MAX_WAIT_LOOPS=${MAX_WAIT_LOOPS:-36}
THROTTLE=${THROTTLE:-40}
SCORE_PER_TASK=${SCORE_PER_TASK:-20}
GT_CHUNK=${GT_CHUNK:-40}
SEED=${SEED:-0}
IOU_MIN=${IOU_MIN:-0.80}
R2G_MAX=${R2G_MAX:-6.0}
MIN_FRAMES=${MIN_FRAMES:-6}
R2G_METRIC=${R2G_METRIC:-r2g}
GT_SET=${GT_SET:-strict}
DRY_RUN=${DRY_RUN:-0}
FINALIZE=${FINALIZE:-1}
TARGET=1000
export OMP_NUM_THREADS=1 MKL_NUM_THREADS=1
mkdir -p "$QA" "$OUTQA" "$LOGS"
cd "$DROID"
say() { echo "[replace_loop $(date -u +%Y-%m-%dT%H:%M:%S)] $*"; }
jget() { $PY -c "import json,sys; d=json.load(open('$OUTQA/decide_summary.json')); v=d$1; print(v if not isinstance(v,bool) else int(v))"; }
score_pending() { # score every active episode that has a v2 file but no fresh score (sbatch --wait array)
TS="$OUTQA/to_score_$(date -u +%Y%m%d_%H%M%S).txt"
$SQ pending --out "$TS"
N=$(grep -c . "$TS" 2>/dev/null || true); N=${N:-0}
if [ "$N" -gt 0 ]; then
NT=$(( (N + SCORE_PER_TASK - 1) / SCORE_PER_TASK ))
say "scoring $N episodes in $NT array tasks ($TS)"
sbatch --wait --parsable --job-name=droid_qa_score --partition=c6-128-main --cpus-per-task=8 --mem=16G --time=04:00:00 \
--array=0-$((NT - 1))%$THROTTLE --output="$LOGS/qa_score_%A_%a.log" \
--wrap "export OMP_NUM_THREADS=1 MKL_NUM_THREADS=1; cd $DROID && $SQ score --uuids $TS --chunk \$SLURM_ARRAY_TASK_ID --nchunks $NT --workers 8" \
|| say "WARNING: scoring array returned non-zero (see $LOGS/qa_score_*.log)"
else
rm -f "$TS"; say "nothing new to score"
fi
}
run_new_episodes() { # $1 = manifest (uuid<TAB>rel), $2 = tag: fetch -> GT masks (GPU) -> sbatch_pipeline.sh, all with --wait
local REP=$1 TAG=$2
local NR; NR=$(grep -c . "$REP" 2>/dev/null || true); NR=${NR:-0}
[ "$NR" -eq 0 ] && { say "run_new_episodes: empty manifest $REP"; return 0; }
say "$TAG: $NR new episodes ($REP): fetch"
while IFS=$'\t' read -r U R; do
[ -z "$U" ] && continue
[ -f "$DROID/data/$U/trajectory.h5" ] && continue
$PY scripts/fetch_episode.py "$U" "$R" || say "WARNING: fetch failed for $U"
done < "$REP"
if [ "${SKIP_GT:-0}" != "1" ]; then
local CH="$OUTQA/gt_chunks_${TAG}.json"
$PY - "$REP" "$CH" "$GT_CHUNK" <<'EOF'
import json, sys, os
rep, out, size = sys.argv[1], sys.argv[2], int(sys.argv[3])
root = "/home/wujiayir/robot_tools/droid"
us = [l.split("\t")[0].strip() for l in open(rep) if l.strip()]
us = [u for u in us if not os.path.exists(f"{root}/out/gt/{u}/.gt_done") and os.path.exists(f"{root}/data/{u}/trajectory.h5")]
chunks = [us[i:i + size] for i in range(0, len(us), size)] or [[]]
json.dump(chunks, open(out, "w"), indent=0)
EOF
local NCH; NCH=$($PY -c "import json; c=json.load(open('$CH')); print(len([x for x in c if x]))")
if [ "$NCH" -gt 0 ]; then
say "GT masks for $TAG: $NCH GPU chunk job(s) (sbatch --wait scripts/sbatch_gt_masks.sh)"
CHUNKS="$CH" TAG="qa_${TAG}" sbatch --wait --parsable --array=0-$((NCH - 1)) scripts/sbatch_gt_masks.sh \
|| say "WARNING: GT mask array returned non-zero (episodes without .gt_done will refine with Canny and fail as unverifiable)"
else
say "GT masks already present for all new episodes"
fi
fi
say "pipeline for $TAG: $NR episodes (sbatch --wait scripts/sbatch_pipeline.sh, throttle $THROTTLE)"
env MANIFEST="$REP" ${GT_POLICY:+GT_POLICY=$GT_POLICY} ${PIPE_ENV:-} \
sbatch --wait --parsable --array=0-$((NR - 1))%$THROTTLE scripts/sbatch_pipeline.sh \
|| say "WARNING: pipeline array returned non-zero (see $LOGS/pipe_v2_*.log); episodes without v2 stay pending"
say "$TAG pipeline finished"
}
if [ -n "${MANIFEST_ONLY:-}" ]; then
case "$MANIFEST_ONLY" in /*) ;; *) MANIFEST_ONLY="$DROID/$MANIFEST_ONLY" ;; esac
run_new_episodes "$MANIFEST_ONLY" "${ROUND_TAG:-manual}"
score_pending
say "MANIFEST_ONLY done ($MANIFEST_ONLY); re-run: $SQ reselect"
exit 0
fi
round=0
waits=0
while :; do
# ---- 1+2. score everything that has a v2 calibration but no fresh score ---------------------------------------------
score_pending
# ---- 3. decide ----------------------------------------------------------------------------------------------------
say "decide (iou_min $IOU_MIN r2g_max $R2G_MAX min_frames $MIN_FRAMES r2g_metric $R2G_METRIC gt_set $GT_SET)"
$SQ decide --iou-min "$IOU_MIN" --r2g-max "$R2G_MAX" --min-frames "$MIN_FRAMES" --r2g-metric "$R2G_METRIC" --gt-set "$GT_SET" \
|| { say "decide failed"; exit 1; }
NPASS=$(jget "['n_pass']"); NFAIL=$(jget "['n_fail']"); NPEND=$(jget "['n_pending']"); NACT=$(jget "['n_active']"); COMPLETE=$(jget "['complete']")
say "ROUND TABLE after $round replacement round(s): active $NACT pass $NPASS fail $NFAIL pending $NPEND (target $TARGET)"
cp "$QA/decision_table.md" "$OUTQA/round_table_${round}.md"
if [ "$COMPLETE" = "1" ]; then
say "all $TARGET episodes pass"
[ "$FINALIZE" = "1" ] && $SQ finalize
exit 0
fi
# ---- 4. replace ----------------------------------------------------------------------------------------------------
EXTRA_FLAG=""
if [ "$NFAIL" -eq 0 ] && [ "$NPEND" -gt 0 ] && [ "$round" -gt 0 ]; then
# nothing failed yet, the +1 extra was already added on round 1 -> just wait for the pending ones
waits=$((waits + 1)); [ "$waits" -gt "$MAX_WAIT_LOOPS" ] && { say "giving up after $MAX_WAIT_LOOPS wait loops"; exit 2; }
say "no failed slots; $NPEND pending (other arrays still running?) -> sleep $WAIT_S s"; sleep "$WAIT_S"; continue
fi
if [ "$round" -ge "$MAX_ROUNDS" ]; then say "MAX_ROUNDS=$MAX_ROUNDS reached (pass $NPASS fail $NFAIL pending $NPEND)"; exit 3; fi
round=$((round + 1))
if [ "$DRY_RUN" = "1" ]; then
$SQ replace --seed "$SEED" --round "$round" --dry-run
say "DRY_RUN: stopping after the dry-run replacement of round $round"; exit 0
fi
$SQ replace --seed "$SEED" --round "$round" || { say "replace failed"; exit 1; }
REP="$QA/replacements_round${round}.tsv"
NR=$(grep -c . "$REP" 2>/dev/null || true); NR=${NR:-0}
if [ "$NR" -eq 0 ]; then
if [ "$NPEND" -gt 0 ]; then
waits=$((waits + 1)); [ "$waits" -gt "$MAX_WAIT_LOOPS" ] && { say "giving up after $MAX_WAIT_LOOPS wait loops"; exit 2; }
round=$((round - 1)); say "no replacements possible now; $NPEND pending -> sleep $WAIT_S s"; sleep "$WAIT_S"; continue
fi
say "no replacements possible and nothing pending (pool dry for the remaining failed slots) -> stop"; exit 4
fi
# ---- 5. fetch -> GT masks (GPU) -> pipeline for the new episodes -----------------------------------------------------
run_new_episodes "$REP" "round${round}"
done
|