File size: 6,839 Bytes
3970207 | 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 | #!/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/<data>.<config>.<timestamp>]
--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 [<data>.<config>]
--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 <dir>" >&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[@]}"
|