#!/bin/bash # Enhancement-track queue: runs everything that is left, unattended, and publishes to the Hugging Face repo. # # 1. keep-alive : the three PLANTEUR branch chains (A refinement->SAM, B structured SSL, C temporal CNNs); # every trainer is resumable / skips finished work, so a restart continues, it does not repeat # 2. partial : when SAM and both CNNs are done -> PLANTEUR ladder WITHOUT the structured-SSL branch # -> results/planteur_ladder_partial/ (published at once) # 3. final : when the structured-SSL fine-tune is done -> the full PLANTEUR ladder -> results/planteur_ladder/ # 4. publish : regenerate reports + README and push every PUBLISH_MIN minutes (only while the older # hf_work/queue.sh is not running, which publishes on its own), plus immediately after 2 and 3; # a full push + restore kit at the end # 5. disk guard : the user's hard cap is 88 % on /. Nothing that writes is started at or above DISK_STOP (86 %); # the HF download cache is cleared first. Caches live in /dev/shm (RAM), not on /. # # Existing results are never overwritten: each ladder stage is skipped if its ladder.json already exists. # HF_TOKEN must be in the environment (launch: HF_TOKEN=... nohup bash foundation_queue.sh &); never written to disk. set -u FD=/home/ubuntu/pro/TSRDA_FOUNDATION; HF=/home/ubuntu/pro/hf_work; R=$FD/runs_planteur LOG=$FD/logs/foundation_queue.log; SYNCLOG=$FD/logs/foundation_sync.log DISK_STOP=${DISK_STOP:-86}; DEADLINE_H=${DEADLINE_H:-14}; CYCLE_MIN=${CYCLE_MIN:-5}; PUBLISH_MIN=${PUBLISH_MIN:-20} SHM=/dev/shm/tsrda_foundation END=$(python3 -c "import time;print(int(time.time()+${DEADLINE_H}*3600))") export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True say () { echo "[fq $(date -u +%H:%M:%S)] $*" >> "$LOG"; } alive () { pgrep -f "$1" > /dev/null 2>&1; } disk () { df --output=pcent / | tail -1 | tr -dc '0-9'; } disk_ok () { local u; u=$(disk) if [ "$u" -ge "$DISK_STOP" ]; then say "disk at ${u}% (>= ${DISK_STOP}%) -> clearing the HF download cache" rm -rf $HF/hfhome/hub 2>/dev/null; u=$(disk); say "disk now ${u}%" fi [ "$u" -lt "$DISK_STOP" ] } publish () { # $1 = reports | all disk_ok || { say "publish postponed: disk"; return 1; } until ! alive "sync_hf.py"; do sleep 30; done # never two uploads at once python3 $HF/make_reports.py >> "$SYNCLOG" 2>&1 && say "reports regenerated" || say "WARNING make_reports failed" timeout 3600 python3 $HF/sync_hf.py "$1" >> "$SYNCLOG" 2>&1 && say "pushed to HF ($1)" || say "WARNING sync $1 failed" } run_ladder () { # tag exclude cache out title local tag=$1 excl=$2 cache=$3 out=$4 title=$5 for i in 1 2 3; do disk_ok || { say "ladder $tag postponed: disk"; return 1; } say "ladder $tag: stage A (readouts + caches), attempt $i" if LADDER_EXCLUDE="$excl" LADDER_CACHE="$cache" python3 -u $FD/planteur_ladder.py >> $FD/logs/planteur_ladder_$tag.log 2>&1; then say "ladder $tag: stage B (evaluation)" if LADDER_CACHE="$cache" LADDER_OUT="$out" LADDER_TITLE="$title" python3 -u $FD/planteur_ladder_eval.py \ >> $FD/logs/planteur_ladder_eval_$tag.log 2>&1; then say "ladder $tag: DONE -> $out"; return 0 fi fi say "ladder $tag: attempt $i failed (see logs/planteur_ladder*_$tag.log); retry in 5 min"; sleep 300 done return 1 } say "==============================================================" say "foundation queue started; deadline ${DEADLINE_H} h; disk stop ${DISK_STOP}% (hard cap 88%); disk now $(disk)%" last_pub=0; partial_failed=0; final_failed=0 while [ "$(date +%s)" -lt "$END" ]; do # ---------- 1. keep-alive ------------------------------------------------------------------------------- for c in A B C; do case $c in A) need=$R/sam/result.json ;; B) need=$R/structured_ft/result.json ;; C) need=$R/cnn_new/completed.json ;; esac if [ ! -f "$need" ] && ! alive "chain$c.sh"; then if disk_ok; then say "chain $c not running and unfinished -> restarting (resumes / skips finished steps)" ( cd $R && nohup ./chain$c.sh >> $FD/logs/planteur_chain$c.log 2>&1 & ) fi fi done # ---------- 2. partial ladder (no structured-SSL branch) ------------------------------------------------- if [ -f $R/sam/result.json ] && [ -f $R/cnn_old/completed.json ] && [ -f $R/cnn_new/completed.json ] \ && [ ! -f $FD/results/planteur_ladder_partial/ladder.json ] && [ ! -f $FD/results/planteur_ladder/ladder.json ] \ && [ ! -f $R/structured_ft/result.json ] && [ $partial_failed -eq 0 ]; then if run_ladder partial structured $SHM/planteur_ladder_partial $FD/results/planteur_ladder_partial \ "PLANTEUR enhancement ladder (100 % labels) — PARTIAL: without the structured-SSL branch"; then publish reports; last_pub=$(date +%s) else partial_failed=1; say "partial ladder gave up after 3 attempts"; fi fi # ---------- 3. final ladder ------------------------------------------------------------------------------ if [ -f $R/sam/result.json ] && [ -f $R/structured_ft/result.json ] && [ -f $R/cnn_new/completed.json ] \ && [ ! -f $FD/results/planteur_ladder/ladder.json ] && [ $final_failed -eq 0 ]; then # the fitted readouts depend only on the SAM network and the training pixels: reuse the partial fit if [ -f $SHM/planteur_ladder_partial/fitted/done ] && [ ! -d $SHM/planteur_ladder/fitted ]; then mkdir -p $SHM/planteur_ladder && cp -r $SHM/planteur_ladder_partial/fitted $SHM/planteur_ladder/fitted fi if run_ladder final "" $SHM/planteur_ladder $FD/results/planteur_ladder "PLANTEUR enhancement ladder (100 % labels)"; then rm -rf $SHM/planteur_ladder_partial/val $SHM/planteur_ladder_partial/test # RAM only; results stay in results/ say "ALL WORK DONE -> final publish" publish all disk_ok && { timeout 2400 python3 $HF/make_restore.py push >> "$SYNCLOG" 2>&1 && say "restore kit pushed" || say "WARNING restore push failed"; } say "foundation queue finished"; exit 0 else final_failed=1; say "final ladder gave up after 3 attempts"; fi fi # ---------- 4. periodic publish (only when the older queue.sh is not doing it) --------------------------- now=$(date +%s) if ! alive "bash queue.sh" && [ $((now - last_pub)) -ge $((PUBLISH_MIN * 60)) ]; then publish reports; last_pub=$(date +%s) fi say "cycle: disk $(disk)%" if [ $partial_failed -eq 1 ] && [ $final_failed -eq 1 ]; then say "both ladders failed; stopping"; break; fi sleep $((CYCLE_MIN * 60)) done say "deadline / stop reached -> final publish" publish all say "foundation queue exited"