#!/usr/bin/env bash # Train DiscoLoop or a looped / stacked baseline on the synthetic natural-language # two-hop datasets with train_twin_hyper_real.py. The defaults are the DiscoLoop # configuration on the direct format (K=2, learned gate, 2 GPUs x 512 samples); # scripts/nl_2hop.sh launches the configurations of the experiments. # # Usage: # ./train_twin_hyper_real.sh \ # --gpus 0,1 \ # --data multi-hop/data/custom/two_graph_direct \ # --gate-mode learned # # With more than one GPU id in --gpus the trainer runs under torchrun (one process # per GPU), otherwise as a single process. # Relative paths are resolved from the repository root. set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" GPUS="0,1" DATA_DIR="multi-hop/data/custom/two_graph_direct" TOKENIZER_NAME="multi-hop/tokenizers/gpt2_names_relations" WANDB_PROJECT="" # empty = no W&B logging BATCH_SIZE=512 # per GPU MAX_LENGTH=8 # 16 for two_graph_reverse LOSS_COMPUTE_METHOD="answer" # answer | full EPOCHS=5000 STAGE1_EPOCHS=10 # stacking only: GPT warm-up epochs before stage 2 SKIP_STAGE1=0 LR=1e-3 WEIGHT_DECAY=0.01 WARMUP_STEPS=200 LOGGING_STEPS=20 SAVE_STEPS=500 EVALUATE_EPOCH=10 SEED=7 DESIGN="looping" # looping | stacking NUM_STAGES=2 GATE_MODE="learned" # fixed | learned FIXED_ALPHA=0.5 FIXED_BETA=0.5 # OUT_DIR / RUN_NAME defaults are built after argument parsing (below) so they # reflect the final option values. Pass --out / --run-name to override them. OUT_DIR="" RUN_NAME="" while [[ $# -gt 0 ]]; do case "$1" in --gpus) GPUS="$2"; shift 2 ;; --data) DATA_DIR="$2"; shift 2 ;; --tokenizer-name) TOKENIZER_NAME="$2"; shift 2 ;; --out) OUT_DIR="$2"; shift 2 ;; --wandb-project) WANDB_PROJECT="$2"; shift 2 ;; --run-name) RUN_NAME="$2"; shift 2 ;; --batch-size) BATCH_SIZE="$2"; shift 2 ;; --max-length) MAX_LENGTH="$2"; shift 2 ;; --loss-compute-method) LOSS_COMPUTE_METHOD="$2"; shift 2 ;; --epochs) EPOCHS="$2"; shift 2 ;; --stage1-epochs) STAGE1_EPOCHS="$2"; shift 2 ;; --skip-stage1) SKIP_STAGE1=1; shift 1 ;; --lr) LR="$2"; shift 2 ;; --weight-decay) WEIGHT_DECAY="$2"; shift 2 ;; --warmup-steps) WARMUP_STEPS="$2"; shift 2 ;; --logging-steps) LOGGING_STEPS="$2"; shift 2 ;; --save-steps) SAVE_STEPS="$2"; shift 2 ;; --evaluate-epoch) EVALUATE_EPOCH="$2"; shift 2 ;; --seed) SEED="$2"; shift 2 ;; --design) DESIGN="$2"; shift 2 ;; --num-stages) NUM_STAGES="$2"; shift 2 ;; --gate-mode) GATE_MODE="$2"; shift 2 ;; --fixed-alpha) FIXED_ALPHA="$2"; shift 2 ;; --fixed-beta) FIXED_BETA="$2"; shift 2 ;; -h|--help) cat <<'USAGE' Usage: ./train_twin_hyper_real.sh [options] Training with train_twin_hyper_real.py. Defaults (in brackets) are the natural-language DiscoLoop configuration on the direct format. GPUs: --gpus LIST comma-separated GPU ids, exported as CUDA_VISIBLE_DEVICES [0,1]; more than one id runs torchrun with one process per GPU Core options: --data DIR dataset dir [multi-hop/data/custom/two_graph_direct] --tokenizer-name PATH GPT-2-based tokenizer [multi-hop/tokenizers/gpt2_names_relations] --out DIR output dir [outputs/..] --design looping|stacking shared blocks, or K untied copies (no-loop baseline) [looping] --num-stages K number of loops (or stacked copies) [2] --gate-mode fixed|learned [learned] Two-stage (stacking): --stage1-epochs N GPT warm-up epochs before stage 2 [10] --skip-stage1 train the stacked model from scratch Gates (--gate-mode fixed): --fixed-alpha A fixed gate on the decoded embeddings [0.5] --fixed-beta B fixed gate on the input embeddings H0 [0.5] Data: --max-length N sequence length in tokens; 16 for two_graph_reverse [8] --loss-compute-method answer|full [answer] Optimization: --batch-size N batch size per GPU [512] --epochs N [5000] --lr X [1e-3] --weight-decay X [0.01] --warmup-steps N [200] --seed N [7] Logging: --logging-steps N [20] --save-steps N [500] --evaluate-epoch N stage-wise eval every N epochs (<=0 disables) [10] --run-name NAME [.] --wandb-project NAME log to Weights & Biases [off] Notes: --batch-size is per GPU: with --gpus 0,1 the global batch is 2 x 512. Under torchrun the HF Trainer multiplies the training loss of this model (and so its gradients, before clipping at norm 1.0) by the number of processes, so runs with different GPU counts are not equivalent. USAGE exit 0 ;; *) echo "Unknown arg: $1" >&2 exit 1 ;; esac done if [[ -z "$DATA_DIR" ]]; then echo "Required: --data " >&2 exit 1 fi # Build default OUT_DIR / RUN_NAME from the FINAL argument values. DATA_TAG="$(basename "$DATA_DIR")" CONFIG_TAG="${DESIGN}.K${NUM_STAGES}.${GATE_MODE}" if [[ "$GATE_MODE" == "fixed" ]]; then CONFIG_TAG="${CONFIG_TAG}.alpha${FIXED_ALPHA}_beta${FIXED_BETA}" fi if [[ "$DESIGN" == "stacking" ]]; then if [[ "$SKIP_STAGE1" -eq 1 ]]; then CONFIG_TAG="${CONFIG_TAG}.nostage1" else CONFIG_TAG="${CONFIG_TAG}.stage1ep${STAGE1_EPOCHS}" fi fi CONFIG_TAG="${CONFIG_TAG}.loss${LOSS_COMPUTE_METHOD}.lr${LR}.wd${WEIGHT_DECAY}.warmup${WARMUP_STEPS}.seed${SEED}" if [[ -z "$OUT_DIR" ]]; then OUT_DIR="outputs/${DATA_TAG}.${CONFIG_TAG}.$(date +%Y%m%d_%H%M%S)" fi if [[ -z "$RUN_NAME" ]]; then RUN_NAME="${DATA_TAG}.${CONFIG_TAG}" fi NPROC_PER_NODE=$(printf '%s\n' "$GPUS" | awk -F',' '{print NF}') export CUDA_VISIBLE_DEVICES="$GPUS" if [[ "$NPROC_PER_NODE" -gt 1 ]]; then CMD=(python3 -m torch.distributed.run --standalone --nproc_per_node "$NPROC_PER_NODE" train_twin_hyper_real.py ) else CMD=(python3 train_twin_hyper_real.py) fi CMD+=( --data-dir "$DATA_DIR" --output-dir "$OUT_DIR" --tokenizer-name "$TOKENIZER_NAME" --batch-size "$BATCH_SIZE" --max-length "$MAX_LENGTH" --loss-compute-method "$LOSS_COMPUTE_METHOD" --epochs "$EPOCHS" --stage1-epochs "$STAGE1_EPOCHS" --lr "$LR" --weight-decay "$WEIGHT_DECAY" --warmup-steps "$WARMUP_STEPS" --logging-steps "$LOGGING_STEPS" --save-steps "$SAVE_STEPS" --evaluate-epoch "$EVALUATE_EPOCH" --seed "$SEED" --design "$DESIGN" --num-stages "$NUM_STAGES" --gate-mode "$GATE_MODE" --fixed-alpha "$FIXED_ALPHA" --fixed-beta "$FIXED_BETA" --run-name "$RUN_NAME" ) if [[ "$SKIP_STAGE1" -eq 1 ]]; then CMD+=(--skip-stage1) fi if [[ -n "$WANDB_PROJECT" ]]; then CMD+=(--wandb-project "$WANDB_PROJECT") fi echo "CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES" echo "NPROC_PER_NODE=$NPROC_PER_NODE" echo "Running: ${CMD[*]}" "${CMD[@]}"