Repository navigation
Expand file tree
/
Copy pathlaunch_b200_training.sh
More file actions
executable file
·484 lines (445 loc) · 16.9 KB
/
Copy pathlaunch_b200_training.sh
File metadata and controls
executable file
·484 lines (445 loc) · 16.9 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env bash
# =============================================================================
# B200 Training Launcher — Conditional-GQE
# =============================================================================
# Strategy: Supervised warm-start → DAPO RL (main run)
# Direct RL from scratch (ablation, secondary)
#
# Hardware: 1× NVIDIA B200 (180 GB HBM3e, sm_100, CUDA 13.2)
# 2× Intel Xeon 6960P (288 threads), 3 TiB RAM
#
# Molecule inventory:
# Stage 1 SFT : hamiltonians_merged.json (21 mol, 4–40q)
# Stage 2 RL : hamiltonians_gic2026/hamiltonians.json (35 mol, 4–28q)
# Stage 2 RL xl: hamiltonians_40plus/hamiltonians.json (10 mol, 4–40q)
#
# Usage:
# bash scripts/launch_b200_training.sh [sft|rl|both|cache|ablation|ablation-smoke|cache+ablation]
# Default: both (sft then rl)
#
# Fast RL path:
# bash scripts/launch_b200_training.sh cache # precompute energies (once)
# bash scripts/launch_b200_training.sh cache-remaining # finish 32–40q only (resume-safe)
# bash scripts/launch_b200_training.sh ablation # train with cache + reuse-iters=16
# # or: bash scripts/launch_b200_training.sh cache+ablation
# =============================================================================
set -euo pipefail
STAGE="${1:-both}"
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
RESULTS="$ROOT/results"
DATA="$ROOT/results/data"
# ------------------------------------------------------------------
# Blackwell / B200 acceleration (cuBLAS BF16x9 + CUDA-Q FP32 emulation)
# MUST be sourced before any Python that imports cudaq.
# ------------------------------------------------------------------
# shellcheck source=/dev/null
source "$ROOT/scripts/env_b200_blackwell.sh"
echo "=== Blackwell env ==="
echo " CUBLAS_EMULATE_SINGLE_PRECISION=$CUBLAS_EMULATE_SINGLE_PRECISION strategy=$CUBLAS_EMULATION_STRATEGY"
echo " CUDAQ_ALLOW_FP32_EMULATED=$CUDAQ_ALLOW_FP32_EMULATED FUSION_MAX_QUBITS=$CUDAQ_FUSION_MAX_QUBITS"
echo " CUDAQ_FUSION_DIAGONAL_GATE_MAX_QUBITS=$CUDAQ_FUSION_DIAGONAL_GATE_MAX_QUBITS"
echo " CUDAQ_ENABLE_MEMPOOL=$CUDAQ_ENABLE_MEMPOOL FUSION_HOST_THREADS=$CUDAQ_FUSION_NUM_HOST_THREADS"
echo " CUDAQ_MAX_GPU_MEMORY_GB=$CUDAQ_MAX_GPU_MEMORY_GB"
echo " TORCH_ALLOW_TF32_CUBLAS_OVERRIDE=$TORCH_ALLOW_TF32_CUBLAS_OVERRIDE NVIDIA_TF32_OVERRIDE=$NVIDIA_TF32_OVERRIDE"
# ------------------------------------------------------------------
# Portable CUDA library path resolution (qBraid / containerised env)
# ------------------------------------------------------------------
NVIDIA_SITE="$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null)/nvidia"
CUDA_LIBS=""
for subdir in cu13 cublas cudnn cufft curand cusolver cusparse nccl cuda_runtime; do
libdir="$NVIDIA_SITE/$subdir/lib"
[ -d "$libdir" ] && CUDA_LIBS="$libdir:$CUDA_LIBS"
done
# Include stub dir for missing optional libs (libcufile, libcupti, libnvshmem)
STUB_DIR="$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null)"
export LD_LIBRARY_PATH="${CUDA_LIBS}${STUB_DIR}:${LD_LIBRARY_PATH:-}"
# ------------------------------------------------------------------
# Verify GPU
# ------------------------------------------------------------------
echo "=== GPU check ==="
python3 -c "
import torch
assert torch.cuda.is_available(), 'CUDA not available — check LD_LIBRARY_PATH'
cap = torch.cuda.get_device_capability(0)
name = torch.cuda.get_device_name(0)
vram = torch.cuda.get_device_properties(0).total_memory / 1e9
print(f' {name} sm_{cap[0]}{cap[1]} {vram:.0f} GB VRAM torch={torch.__version__}')
"
# ------------------------------------------------------------------
# Stage 1: Supervised Fine-Tuning (warm-start)
# ------------------------------------------------------------------
run_sft() {
echo ""
echo "=== Stage 1: Supervised warm-start ==="
echo " Dataset : $RESULTS/train/gqe_supervised_dataset.pt"
echo " Output : $RESULTS/train/h_cgqe_model_b200_sft.pt"
echo " GPU : B200 (BF16, batch 1024, val-every 2, fast commute, 500 epochs)"
echo ""
python3 "$ROOT/src/gqe/models/train_h_cgqe.py" \
--dataset "$RESULTS/train/gqe_supervised_dataset.pt" \
--out "$RESULTS/train/h_cgqe_model_b200_sft.pt" \
--epochs 500 \
--batch-size 1024 \
--lr 5e-4 \
--d-model 256 \
--nhead 8 \
--enc-layers 4 \
--dec-layers 4 \
--dim-ff 1024 \
--dropout 0.1 \
--train-split 0.8 \
--use-cuda \
--use-bf16 \
--num-workers 0 \
--grad-accum 1 \
--val-every 2 \
--commutator-weight 0.1 \
--commutator-ramp-epochs 100 \
--label-smoothing 0.1 \
--patience 60 \
--min-delta 1e-4 \
2>&1 | tee "$RESULTS/train/sft_b200.log"
echo "SFT complete → $RESULTS/train/h_cgqe_model_b200_sft.pt"
}
# ------------------------------------------------------------------
# Stage 2: DAPO RL fine-tuning (main run)
# ------------------------------------------------------------------
run_rl_main() {
echo ""
echo "=== Stage 2: DAPO RL (main — GIC 35-molecule set) ==="
echo " Checkpoint: $RESULTS/train/h_cgqe_model_b200_sft.pt"
echo " Molecules : 35 GIC2026 systems (4–28q)"
echo " Output : $RESULTS/train/h_cgqe_model_b200_rl_main.pt"
echo ""
# Build molecule list from GIC2026 dataset
MOLECULES=$(python3 -c "
import json
d = json.load(open('$DATA/hamiltonians_gic2026/hamiltonians.json'))
names = [r['name'] for r in d['records']]
print(' '.join(names))
")
python3 "$ROOT/src/gqe/models/train_rl_dapo.py" \
--checkpoint "$RESULTS/train/h_cgqe_model_b200_sft.pt" \
--hamiltonians "$DATA/hamiltonians_gic2026/hamiltonians.json" \
--molecules $MOLECULES \
--out "$RESULTS/train/h_cgqe_model_b200_rl_main.pt" \
--epochs 300 \
--n-samples 64 \
--n-iters 5 \
--lr 1e-5 \
--clip-low 0.2 \
--clip-high 0.28 \
--use-cuda \
--use-bf16 \
--single-gpu \
--target nvidia \
--target-option fp32 \
--max-qubits 28 \
--mps-threshold 24 \
--mps-bond 64 \
--curriculum \
--curriculum-warmup 30 \
--force-entanglement \
--entropy-coef 0.01 \
--adaptive-temp \
--explore-eps 0.3 \
--kl-coef 0.05 \
--w-creativity 0.1 \
2>&1 | tee "$RESULTS/train/rl_main_b200.log"
echo "RL main run complete → $RESULTS/train/h_cgqe_model_b200_rl_main.pt"
}
# ------------------------------------------------------------------
# Stage 2 XL: DAPO RL on 40q scaling systems
# ------------------------------------------------------------------
run_rl_xl() {
echo ""
echo "=== Stage 2 XL: DAPO RL (40q scaling set) ==="
echo " Checkpoint: $RESULTS/train/h_cgqe_model_b200_rl_main.pt"
echo " Molecules : 10 records up to 40q"
echo " Output : $RESULTS/train/h_cgqe_model_b200_rl_40q.pt"
echo ""
MOLECULES_40=$(python3 -c "
import json
d = json.load(open('$DATA/hamiltonians_40plus/hamiltonians.json'))
names = [r['name'] for r in d['records']]
print(' '.join(names))
")
python3 "$ROOT/src/gqe/models/train_rl_dapo.py" \
--checkpoint "$RESULTS/train/h_cgqe_model_b200_rl_main.pt" \
--hamiltonians "$DATA/hamiltonians_40plus/hamiltonians.json" \
--molecules $MOLECULES_40 \
--out "$RESULTS/train/h_cgqe_model_b200_rl_40q.pt" \
--epochs 150 \
--n-samples 32 \
--n-iters 3 \
--lr 5e-6 \
--use-cuda \
--use-bf16 \
--single-gpu \
--target nvidia \
--target-option fp32 \
--max-qubits 40 \
--mps-threshold 24 \
--mps-bond 128 \
--curriculum \
--force-entanglement \
2>&1 | tee "$RESULTS/train/rl_40q_b200.log"
echo "RL 40q complete → $RESULTS/train/h_cgqe_model_b200_rl_40q.pt"
}
# ------------------------------------------------------------------
# Precompute persistent circuit→energy cache (CUDA-Q once, reuse in RL)
# ------------------------------------------------------------------
run_energy_cache() {
echo ""
echo "=== Precompute RL energy cache (CUDA-Q → SQLite) ==="
echo " This is the expensive one-time step; training then hits the cache."
echo ""
HAMILTONIANS="$DATA/hamiltonians_rl_b200/hamiltonians.json"
if [ ! -f "$HAMILTONIANS" ]; then
HAMILTONIANS="$DATA/hamiltonians_merged.json"
fi
CACHE_OUT="$RESULTS/train/rl_energy_cache.sqlite"
N_PER_MOL="${CACHE_N_PER_MOL:-512}"
CACHE_MAX_QUBITS="${CACHE_MAX_QUBITS:-28}"
MPS_THRESHOLD="${CACHE_MPS_THRESHOLD:-33}"
N_PER_MOL_LARGE="${CACHE_N_PER_MOL_LARGE:-128}"
MPS_BOND="${CACHE_MPS_BOND:-32}"
echo " Hamiltonians: $HAMILTONIANS"
echo " Output : $CACHE_OUT"
echo " max-qubits : $CACHE_MAX_QUBITS (RL cache; use QSCI/FMO2 for 32–40q)"
echo " n-per-mol : $N_PER_MOL (≤${MPS_THRESHOLD}q) / $N_PER_MOL_LARGE (>${MPS_THRESHOLD}q MPS)"
echo " mps-threshold: $MPS_THRESHOLD mps-bond: $MPS_BOND"
echo ""
python3 "$ROOT/src/gqe/data/precompute_rl_energy_cache.py" \
--hamiltonians "$HAMILTONIANS" \
--out "$CACHE_OUT" \
--n-per-mol "$N_PER_MOL" \
--n-per-mol-large "$N_PER_MOL_LARGE" \
--max-qubits "$CACHE_MAX_QUBITS" \
--max-seq-len 64 \
--theta 0.01 \
--eval-async-chunk 24 \
--target nvidia \
--target-option fp32 \
--mps-threshold "$MPS_THRESHOLD" \
--mps-bond "$MPS_BOND" \
2>&1 | tee -a "$RESULTS/train/rl_energy_cache_precompute.log"
echo "Energy cache ready → $CACHE_OUT"
}
# ------------------------------------------------------------------
# Resume: only the 32–40q molecules missing from cache (append-safe)
# ------------------------------------------------------------------
run_energy_cache_remaining() {
echo ""
echo "=== Resume RL energy cache (32–40q only, append to existing SQLite) ==="
echo " DEPRECATED: prefer QSCI/FMO2 for 32–40q. RL cache should stop at 28q."
echo " Keeps all previously cached 4–28q entries."
echo ""
HAMILTONIANS="$DATA/hamiltonians_rl_b200/hamiltonians.json"
CACHE_OUT="$RESULTS/train/rl_energy_cache.sqlite"
if [ ! -f "$CACHE_OUT" ]; then
echo "ERROR: No existing cache at $CACHE_OUT — run 'cache' first."
exit 1
fi
BACKUP="$CACHE_OUT.bak-$(date -u +%Y%m%dT%H%M%S)"
cp -a "$CACHE_OUT" "$BACKUP"
echo " Backup : $BACKUP"
REMAINING_MOLS="beh2_ccpvdz n2_ccpvdz benzene_cas20 n2_ccpvdz_cas20"
echo " Molecules : $REMAINING_MOLS"
echo " 32q backend : nvidia statevector (mps-threshold=33)"
echo " 40q backend : tensornet-mps, n-per-mol=128, bond=32"
echo ""
CACHE_N_PER_MOL=512 \
CACHE_N_PER_MOL_LARGE=128 \
CACHE_MPS_THRESHOLD=33 \
CACHE_MPS_BOND=32 \
python3 "$ROOT/src/gqe/data/precompute_rl_energy_cache.py" \
--hamiltonians "$HAMILTONIANS" \
--molecules $REMAINING_MOLS \
--out "$CACHE_OUT" \
--n-per-mol 512 \
--n-per-mol-large 128 \
--max-qubits 40 \
--max-seq-len 64 \
--theta 0.01 \
--eval-async-chunk 24 \
--target nvidia \
--target-option fp32 \
--mps-threshold 33 \
--mps-bond 32 \
2>&1 | tee -a "$RESULTS/train/rl_energy_cache_precompute_remaining.log"
echo "Energy cache updated → $CACHE_OUT"
}
# ------------------------------------------------------------------
# Ablation: Direct RL from scratch (secondary comparison)
# ------------------------------------------------------------------
run_rl_ablation() {
echo ""
echo "=== Ablation: Direct RL from scratch (no SFT warm-start) ==="
echo " Compact policy d_model=256 (~8–10M, SFT-scale), BF16, energy cache + reuse-iters=16,"
echo " size curriculum, chunked async eval, max-qubits 40"
echo " Output : $RESULTS/train/h_cgqe_model_b200_rl_scratch.pt"
echo ""
HAMILTONIANS="$DATA/hamiltonians_rl_b200/hamiltonians.json"
if [ ! -f "$HAMILTONIANS" ]; then
HAMILTONIANS="$DATA/hamiltonians_merged.json"
fi
CACHE_OUT="$RESULTS/train/rl_energy_cache.sqlite"
echo " Hamiltonians: $HAMILTONIANS"
if [ -f "$CACHE_OUT" ]; then
echo " Energy cache: $CACHE_OUT (write-through)"
ENERGY_CACHE_ARGS=(--energy-cache "$CACHE_OUT")
else
echo " Energy cache: MISSING — run: bash scripts/launch_b200_training.sh cache"
echo " Continuing without cache (slow CUDA-Q path)."
ENERGY_CACHE_ARGS=()
fi
# Ascending qubit order so curriculum stages grow small → large
MOLECULES=$(python3 -c "
import json
d = json.load(open('$HAMILTONIANS'))
recs = sorted(
[r for r in d['records'] if r.get('n_qubits', 99) <= 40],
key=lambda r: (r.get('n_qubits', 99), r.get('name', '')),
)
print(' '.join(r['name'] for r in recs))
")
echo " Molecules : $MOLECULES (sorted by n_qubits; SV <=28, MPS 29-40)"
echo ""
python3 "$ROOT/src/gqe/models/train_rl_dapo.py" \
--from-scratch \
--hamiltonians "$HAMILTONIANS" \
--molecules $MOLECULES \
--out "$RESULTS/train/h_cgqe_model_b200_rl_scratch.pt" \
--epochs 200 \
--n-samples 128 \
--n-iters 8 \
--reuse-iters 16 \
--lr 1e-4 \
--d-model 256 \
--nhead 8 \
--encoder-layers 4 \
--decoder-layers 4 \
--dim-feedforward 1024 \
--dropout 0.1 \
--use-cuda \
--use-bf16 \
--single-gpu \
--target nvidia \
--target-option fp32 \
--max-qubits 40 \
--mps-threshold 28 \
--mps-bond 64 \
--force-entanglement \
--no-adaptive-theta \
--curriculum \
--curriculum-warmup 15 \
--curriculum-steps 4 \
--max-resample-attempts 1 \
--no-dynamic-sampling \
--eval-async \
--eval-async-chunk 24 \
"${ENERGY_CACHE_ARGS[@]}" \
2>&1 | tee "$RESULTS/train/rl_scratch_b200.log"
echo "Ablation (scratch RL) complete → $RESULTS/train/h_cgqe_model_b200_rl_scratch.pt"
}
run_rl_ablation_smoke() {
echo ""
echo "=== Ablation smoke: Direct RL from scratch (~2 min sanity check) ==="
echo " Compact policy d_model=256 (~8–10M), BF16, chunked async eval"
echo " Output : $RESULTS/train/h_cgqe_model_b200_rl_scratch_smoke.pt"
echo ""
CACHE_OUT="$RESULTS/train/rl_energy_cache.sqlite"
HAMILTONIANS="$DATA/hamiltonians_rl_b200/hamiltonians.json"
if [ ! -f "$HAMILTONIANS" ]; then
HAMILTONIANS="$DATA/hamiltonians_merged.json"
MOLECULES="h2_0.74 lih_1.6_full"
else
MOLECULES=$(python3 -c "
import json
d = json.load(open('$HAMILTONIANS'))
recs = sorted(
[r for r in d['records'] if r.get('n_qubits', 99) <= 40],
key=lambda r: r.get('n_qubits', 99),
)
print(' '.join(r['name'] for r in recs[:2]))
")
fi
echo " Hamiltonians: $HAMILTONIANS"
echo " Molecules : $MOLECULES (2 smallest, 2 epochs, 32 samples)"
echo ""
python3 "$ROOT/src/gqe/models/train_rl_dapo.py" \
--from-scratch \
--hamiltonians "$HAMILTONIANS" \
--molecules $MOLECULES \
--out "$RESULTS/train/h_cgqe_model_b200_rl_scratch_smoke.pt" \
--epochs 2 \
--n-samples 32 \
--n-iters 8 \
--reuse-iters 4 \
--lr 1e-4 \
--d-model 256 \
--nhead 8 \
--encoder-layers 4 \
--decoder-layers 4 \
--dim-feedforward 1024 \
--dropout 0.1 \
--use-cuda \
--use-bf16 \
--single-gpu \
--target nvidia \
--target-option fp32 \
--max-qubits 40 \
--mps-threshold 28 \
--mps-bond 64 \
--force-entanglement \
--no-adaptive-theta \
--no-curriculum \
--max-resample-attempts 1 \
--no-dynamic-sampling \
--eval-async \
--eval-async-chunk 16 \
${CACHE_OUT:+--energy-cache "$CACHE_OUT"} \
2>&1 | tee "$RESULTS/train/rl_scratch_b200_smoke.log"
echo "Ablation smoke complete → $RESULTS/train/h_cgqe_model_b200_rl_scratch_smoke.pt"
}
# ------------------------------------------------------------------
# Dispatch
# ------------------------------------------------------------------
mkdir -p "$RESULTS/train"
case "$STAGE" in
sft)
run_sft
;;
rl)
run_rl_main
run_rl_xl
;;
both)
run_sft
run_rl_main
run_rl_xl
;;
cache)
run_energy_cache
;;
cache-remaining)
run_energy_cache_remaining
;;
ablation)
run_rl_ablation
;;
ablation-smoke)
run_rl_ablation_smoke
;;
cache+ablation)
run_energy_cache
run_rl_ablation
;;
*)
echo "Usage: $0 [sft|rl|both|cache|cache-remaining|ablation|ablation-smoke|cache+ablation]"
exit 1
;;
esac
echo ""
echo "=== All requested stages complete ==="