#!/usr/bin/env bash set -euo pipefail REPO_DIR="${REPO_DIR:-/nfs/ywang29/GmNet/journal_exp}" VENV_DIR="${VENV_DIR:-/tmp/gmnet_venv}" PYTHON="${PYTHON_BIN:-${VENV_DIR}/bin/python}" LOCAL_SCRATCH_DIR="${LOCAL_SCRATCH_DIR:-/tmp/gmnet_scratch/e4_e12_official}" WANDB_PROJECT="${WANDB_PROJECT:-gmnet-journal}" WANDB_ENTITY="${WANDB_ENTITY:-yi-fan-wang1216}" DRY_RUN="${DRY_RUN:-1}" FORCE="${FORCE:-0}" export TMPDIR="${LOCAL_SCRATCH_DIR}/tmp" export TORCH_NCCL_HEARTBEAT_TIMEOUT_SEC=1800 export NCCL_SOCKET_IFNAME="${NCCL_SOCKET_IFNAME:-eth}" # export GLOO_SOCKET_IFNAME="${GLOO_SOCKET_IFNAME:-${NCCL_SOCKET_IFNAME}}" export NCCL_NET="${NCCL_NET:-Socket}" # export NCCL_NET_PLUGIN="${NCCL_NET_PLUGIN:-none}" # export NCCL_IB_DISABLE="${NCCL_IB_DISABLE:-1}" export NCCL_DEBUG="${NCCL_DEBUG:-INFO}" export PYTHONUNBUFFERED=1 export FI_EFA_FORK_SAFE=1 export TORCH_NCCL_ASYNC_ERROR_HANDLING=1 export WANDB_API_KEY='24d6afefd46915ea938dca1af6caf95ae0288a19' export WANDB_USERNAME='yi-fan-wang1216' export WANDB_PROJECT="${WANDB_PROJECT}" export WANDB_ENTITY="${WANDB_ENTITY}" export TORCH_DISTRIBUTED_DEBUG=DETAIL # export HF_TOKEN='__REDACTED_HF_TOKEN__' export AWS_PROFILE=default_mle export LD_LIBRARY_PATH= mkdir -p "${TMPDIR}" cd "${REPO_DIR}" E4_ROOT="/tmp/gmnet_runs/e4_cifar100_relu6_official" LEGACY_ROOT="/tmp/gmnet_runs/e4_legacy_imagenet_clean_official" E12_ROOT="/tmp/gmnet_runs/e12_official" ALL_MODES=(baseline batch_shuffle spatial_shuffle channel_shuffle mean_gate) usage() { cat <<'EOF' Usage: run_e4_e12_official.sh [plan|e4|legacy-clean|e12|aggregate|all] Defaults to plan and DRY_RUN=1. To execute a phase, set DRY_RUN=0. E12 additionally requires CONFIRM_IDLE=1 and refuses to run while nvidia-smi reports any compute process. Run E12 only after E4 and legacy-clean finish. EOF } print_command() { printf ' %q' "$@" printf '\n' } run_command() { print_command "$@" if [[ "${DRY_RUN}" == "0" ]]; then "$@" fi } is_complete() { local path="$1" [[ "${FORCE}" != "1" && -s "${path}" ]] || return 1 "${PYTHON}" -B - "${path}" <<'PY' import json import sys value = json.load(open(sys.argv[1], encoding="utf-8")) if not str(value.get("status", "")).startswith("completed"): raise SystemExit(1) PY } require_file() { [[ -f "$1" ]] || { echo "Required file is missing: $1" >&2; exit 2; } } run_e4() { local seed index checkpoint output status failures=0 local -a command=() pids=() launched_seeds=() for seed in 0 1 2; do checkpoint="/tmp/gmnet_runs/e3_cifar100/e3_c100_s1_relu6_seed${seed}/checkpoint_last.pt" output="${E4_ROOT}/seed${seed}" require_file "${checkpoint}" if is_complete "${output}/results.json"; then echo "Skipping completed E4 seed ${seed}: ${output}/results.json" continue fi command=("${PYTHON}" -B scripts/run_e4_interventions.py \ --checkpoint "${checkpoint}" \ --dataset cifar100 \ --data-root /tmp/gmnet_data/cifar-100 \ --device "cuda:${seed}" \ --max-samples 0 \ --batch-size 256 \ --workers 4 \ --seed "${seed}" \ --modes "${ALL_MODES[@]}" \ --output-dir "${output}") print_command "${command[@]}" if [[ "${DRY_RUN}" == "0" ]]; then "${command[@]}" & pids+=("$!") launched_seeds+=("${seed}") fi done for index in "${!pids[@]}"; do if wait "${pids[${index}]}"; then echo "E4 seed ${launched_seeds[${index}]} completed." else status=$? echo "E4 seed ${launched_seeds[${index}]} failed with status ${status}." >&2 if [[ "${failures}" == "0" ]]; then failures="${status}" fi fi done if [[ "${failures}" != "0" ]]; then echo "E4 aggregation skipped because at least one seed failed." >&2 return "${failures}" fi aggregate_e4 } aggregate_e4() { local inputs=() local seed for seed in 0 1 2; do inputs+=("${E4_ROOT}/seed${seed}/results.json") done if [[ "${DRY_RUN}" == "0" ]]; then run_command "${PYTHON}" -B scripts/aggregate_local_results.py \ --kind e4 --inputs "${inputs[@]}" --output-dir "${E4_ROOT}" else print_command "${PYTHON}" -B scripts/aggregate_local_results.py \ --kind e4 --inputs "${inputs[@]}" --output-dir "${E4_ROOT}" fi } run_legacy_clean() { local output="${LEGACY_ROOT}" require_file /tmp/gmnet_data/imagenet-1k-val/.READY if is_complete "${output}/results.json"; then echo "Skipping completed legacy clean evaluation: ${output}/results.json" return fi run_command "${PYTHON}" -B scripts/run_e4_interventions.py \ --checkpoint /nfs/ywang29/GmNet/gmnet_s3.npy \ --dataset imagenet \ --data-root /tmp/gmnet_data/imagenet-1k-val \ --data-source-uri s3://snap-research-cv-code/ywang29/datasets/imagenet-1k/val \ --data-staging-manifest /tmp/gmnet_data/imagenet-1k-val/.READY \ --device cuda:0 \ --max-samples 0 \ --batch-size 512 \ --workers 16 \ --seed 20260712 \ --modes baseline \ --output-dir "${output}" } assert_idle_for_e12() { [[ "${CONFIRM_IDLE:-0}" == "1" ]] || { echo "E12 requires CONFIRM_IDLE=1 after training and evaluation finish." >&2 exit 2 } local processes processes="$(nvidia-smi --query-compute-apps=pid --format=csv,noheader,nounits | sed '/^[[:space:]]*$/d')" [[ -z "${processes}" ]] || { echo "E12 refused: active GPU compute PIDs: ${processes}" >&2 exit 2 } } run_e12() { assert_idle_for_e12 local process output for process in 0 1 2 3 4; do output="${E12_ROOT}/process${process}" if is_complete "${output}/results.json"; then echo "Skipping completed E12 process ${process}: ${output}/results.json" continue fi run_command "${PYTHON}" -B scripts/run_e12_profile.py \ --checkpoint /nfs/ywang29/GmNet/gmnet_s3.npy \ --output-dir "${output}" \ --cuda-device cuda:0 \ --cuda-batches 1 32 \ --cuda-precisions fp32 bf16 \ --cuda-warmup 50 \ --cuda-iterations 200 \ --cpu \ --cpu-batches 1 32 \ --cpu-precision fp32 \ --cpu-warmup 10 \ --cpu-iterations 50 \ --cpu-threads 16 \ --onnx --onnx-runtime \ --onnx-warmup 20 \ --onnx-iterations 100 done aggregate_e12 } aggregate_e12() { local inputs=() local process for process in 0 1 2 3 4; do inputs+=("${E12_ROOT}/process${process}/results.json") done if [[ "${DRY_RUN}" == "0" ]]; then run_command "${PYTHON}" -B scripts/aggregate_local_results.py \ --kind e12 --inputs "${inputs[@]}" --output-dir "${E12_ROOT}" else print_command "${PYTHON}" -B scripts/aggregate_local_results.py \ --kind e12 --inputs "${inputs[@]}" --output-dir "${E12_ROOT}" fi } plan() { DRY_RUN=1 run_e4 run_legacy_clean echo "E12 command omitted from automatic plan execution guard." echo "Run with: CONFIRM_IDLE=1 DRY_RUN=0 $0 e12" } action="${1:-plan}" case "${action}" in plan) plan ;; e4) run_e4 ;; legacy-clean) run_legacy_clean ;; e12) run_e12 ;; aggregate) aggregate_e4 aggregate_e12 ;; all) run_e4 run_legacy_clean run_e12 ;; -h|--help|help) usage ;; *) usage >&2; exit 2 ;; esac