File size: 7,180 Bytes
12c2325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/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