File size: 7,381 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env bash
# Train a looped GPT-2 (DiscoLoop or vanilla loop) or the stacked no-loop baseline
# on one GPU. The defaults are the symbolic two-hop DiscoLoop configuration
# (K=2, fixed gate alpha=1, beta=0); scripts/symbolic_*.sh launch the configurations
# of the experiments.
#
# Usage:
#   ./train_twin_hyper.sh \
#     --gpu 0 \
#     --data data/twin_twohop_truood_diff.500.50.10 \
#     --loop-scheme vanillaloop
#
# Relative paths are resolved from the repository root.

set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"

GPU=0

DATA_DIR="data/twin_twohop_truood_diff.500.50.10"

WANDB_PROJECT=""           # empty = no W&B logging

MODEL_NAME="gpt2-big"

TRAIN_TWOHOP_COUNT=10000   # -1 = train on all multi-hop samples
EVAL_ID_COUNT=2000
EVAL_OOD_COUNT=2000

BATCH_SIZE=2048
MAX_LENGTH=4
LOSS_COMPUTE_METHOD="answer"  # answer | full
EPOCHS=3000
MAX_STEPS=""               # if set, train exactly this many optimizer steps (overrides EPOCHS)
STAGE1_EPOCHS=50           # 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=14

DESIGN="looping"           # looping | stacking
LOOP_SCHEME="discoloop"    # discoloop | vanillaloop
NUM_STAGES=2
GATE_MODE="fixed"          # fixed | learned
FIXED_ALPHA=1
FIXED_BETA=0
# OUT_DIR / RUN_NAME defaults are built AFTER argument parsing (below) so they
# reflect the final values of --loop-scheme, --design, --lr, etc.
# Pass --out / --run-name to override them entirely.
OUT_DIR=""
RUN_NAME=""
RUN_NAME_SUFFIX=""         # appended to the auto-built RUN_NAME / OUT_DIR

while [[ $# -gt 0 ]]; do
  case "$1" in
    --gpu) GPU="$2"; shift 2 ;;
    --data) DATA_DIR="$2"; shift 2 ;;
    --out) OUT_DIR="$2"; shift 2 ;;
    --wandb-project) WANDB_PROJECT="$2"; shift 2 ;;
    --run-name) RUN_NAME="$2"; shift 2 ;;
    --run-name-suffix) RUN_NAME_SUFFIX="$2"; shift 2 ;;

    --model-name) MODEL_NAME="$2"; shift 2 ;;

    --train-twohop-count) TRAIN_TWOHOP_COUNT="$2"; shift 2 ;;
    --eval-id-count) EVAL_ID_COUNT="$2"; shift 2 ;;
    --eval-ood-count) EVAL_OOD_COUNT="$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 ;;
    --max-steps) MAX_STEPS="$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 ;;
    --loop-scheme) LOOP_SCHEME="$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.sh [options]

Single-GPU training with train_twin_hyper.py. Defaults (in brackets) are the
symbolic two-hop DiscoLoop configuration.

Core options:
  --gpu ID                  GPU to use, exported as CUDA_VISIBLE_DEVICES [0]
  --data DIR                dataset dir [data/twin_twohop_truood_diff.500.50.10]
  --out DIR                 output dir [outputs/<data>.<config>.<timestamp>]
  --design looping|stacking  shared blocks, or K untied copies (no-loop baseline) [looping]
  --loop-scheme discoloop|vanillaloop  [discoloop]
  --num-stages K            number of loops (or stacked copies) [2]
  --gate-mode fixed|learned  [fixed]
  --model-name NAME         GPT-2 config trained from scratch [gpt2-big]

Two-stage (stacking):
  --stage1-epochs N         GPT warm-up epochs before stage 2 [50]
  --skip-stage1             train the stacked model from scratch

Gates:
  --fixed-alpha A           fixed gate on the decoded embeddings [1]
  --fixed-beta B            fixed gate on the input embeddings H0 [0]

Data sampling:
  --train-twohop-count N    train on N random multi-hops; rest = ID test pool (-1 = all) [10000]
  --eval-id-count N         ID eval subset size per hop count (-1 = all) [2000]
  --eval-ood-count N        OOD eval subset size per hop count (-1 = all) [2000]
  --max-length N            sequence length in tokens [4]
  --loss-compute-method answer|full  [answer]

Optimization:
  --batch-size N [2048]   --epochs N [3000]   --max-steps N (overrides --epochs)
  --lr X [1e-3]   --weight-decay X [0.01]   --warmup-steps N [200]   --seed N [14]

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>]
  --run-name-suffix S       appended to the auto-built run name / output dir
  --wandb-project NAME      log to Weights & Biases [off]
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

if [[ "$GPU" == *,* ]]; then
  echo "--gpu takes a single GPU id (training is single-GPU), got: $GPU" >&2
  exit 1
fi

# Build default OUT_DIR / RUN_NAME from the FINAL argument values.
DATA_TAG="$(basename "$DATA_DIR")"
CONFIG_TAG="${DESIGN}.${LOOP_SCHEME}.K${NUM_STAGES}"
if [[ "$LOOP_SCHEME" == "discoloop" ]]; then
  CONFIG_TAG="${CONFIG_TAG}.${GATE_MODE}"
  if [[ "$GATE_MODE" == "fixed" ]]; then
    CONFIG_TAG="${CONFIG_TAG}.alpha${FIXED_ALPHA}_beta${FIXED_BETA}"
  fi
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}.tt${TRAIN_TWOHOP_COUNT}.lr${LR}.wd${WEIGHT_DECAY}.warmup${WARMUP_STEPS}.seed${SEED}"
if [[ -n "$RUN_NAME_SUFFIX" ]]; then
  CONFIG_TAG="${CONFIG_TAG}.${RUN_NAME_SUFFIX}"
fi
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

export CUDA_VISIBLE_DEVICES="$GPU"

CMD=(python3 train_twin_hyper.py
  --data-dir "$DATA_DIR"
  --output-dir "$OUT_DIR"
  --model-name "$MODEL_NAME"
  --batch-size "$BATCH_SIZE"
  --max-length "$MAX_LENGTH"
  --loss-compute-method "$LOSS_COMPUTE_METHOD"
  --train-twohop-count "$TRAIN_TWOHOP_COUNT"
  --eval-id-count "$EVAL_ID_COUNT"
  --eval-ood-count "$EVAL_OOD_COUNT"
  --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"
  --loop-scheme "$LOOP_SCHEME"
  --num-stages "$NUM_STAGES"
  --gate-mode "$GATE_MODE"
  --fixed-alpha "$FIXED_ALPHA"
  --fixed-beta "$FIXED_BETA"
)

if [[ "$SKIP_STAGE1" -eq 1 ]]; then
  CMD+=(--skip-stage1)
fi

if [[ -n "$MAX_STEPS" ]]; then
  CMD+=(--max-steps "$MAX_STEPS")
fi

if [[ -n "$WANDB_PROJECT" ]]; then
  CMD+=(--wandb-project "$WANDB_PROJECT")
fi

if [[ -n "$RUN_NAME" ]]; then
  CMD+=(--run-name "$RUN_NAME")
fi

echo "Running: ${CMD[*]}"
"${CMD[@]}"