File size: 3,808 Bytes
74b7543 | 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 | # Multi-Hop Implicit Reasoning in Transformers
## Overview
This repository contains the code for reproducing the experiments in our paper **"How Does Transformer Learn Implicit Reasoning?"** . In this work, we explore how implicit multi-hop reasoning emerges during training in transformers. We also propose several novel techniques, such as *cross-query semantic patching* and a *cosine-based representational lens*, to analyze the behavior and internal representations of transformers during multi-hop reasoning.
## Structure of the Repository
- **notebooks/**: Original exploratory notebooks (data prep, training dynamics, logit lens, cosine metrics, semantic patching).
- **data_preparation.py**: CLI to generate datasets (base, 3-hop, ablations, twin two-hop).
- **train_twin.py / train_twin.sh**: Train a GPT-2 style LM on the twin two-hop dataset (with optional custom tokenizer, wandb logging, scratch init).
- **analyze_training.py**: Aggregate checkpoint accuracies (reads `all_items.json`) and optionally log to WandB.
- **visualize_twin_twohop.py**: Render twin graphs side by side to verify isomorphism.
- **environment.yml**: Conda environment for the scripted workflow.
## Quick Start (one-click install)
Recreate the exact working environment (`implicitreasoning`, Python 3.10, torch 2.9.1+cu128)
and run the main hyper-real training sweep:
```
./install.sh # build the conda env
conda activate implicitreasoning
./train_twin_hyper_real_multi_sweep.sh --gpus 0,1 # launch training
```
Use `./install.sh --cpu` for a CPU-only torch build, or `./install.sh --name <env>` for a
different env name. Pinned dependency versions live in `requirements.txt`.
## Setup Instructions (scripted)
### 1) Environment
```
./install.sh # recommended (pinned versions), or the loose conda spec below:
conda env create -f environment.yml
conda activate implicitreasoning
```
### 2) Data generation
Base (paper) dataset:
```
python data_preparation.py --config base
```
Twin two-hop dataset (structural transfer):
```
python data_preparation.py --config twin_twohop --dataset-name twin_twohop.2000.200.20
```
Twin two-hop (true OOD, different graphs):
```
python data_preparation.py --config twin_twohop_truood_diff --dataset-name twin_twohop_truood_diff.2000.200.20
```
Smaller twin example:
```
python data_preparation.py --config twin_twohop \
--num-entities 500 --num-relations 50 --out-degree 10 \
--dataset-name twin_twohop.small
```
Outputs: `data/<dataset>/train.json`, `valid.json`, `test.json`, `vocab.json`.
### 3) Training on twin dataset
Using the bash wrapper:
```
chmod +x train_twin.sh
./train_twin.sh \
--gpu 0 \
--data data/twin_twohop.2000.200.20 \
--out outputs/twin_run \
--from-scratch \
--create-special-token \
--wandb-project myproj
```
Key flags:
- `--gpu`: select GPU via CUDA_VISIBLE_DEVICES
- `--create-special-token`: add dataset vocab so each entity/relation is its own token
- `--from-scratch`: fresh GPT-2 config (8 layers, 768 dim)
- Other knobs: `--batch-size`, `--max-length`, `--lr`, `--epochs`, `--warmup-steps`
Direct python call:
```
python train_twin.py --data-dir data/twin_twohop.2000.200.20 \
--output-dir outputs/twin_run \
--create-special-token --from-scratch --wandb-project myproj
```
### 4) Visualize the twin graphs
```
python visualize_twin_twohop.py \
--dataset-dir data/twin_twohop.2000.200.20 \
--max-nodes 50 \
--output twin_twohop.png
```
### 5) Analyze checkpoints
```
python analyze_training.py \
--base-dir /path/to/checkpoints \
--x-log \
--output training_dynamics.png \
--wandb-project myproj
```
Requires `all_items.json` in each `checkpoint-*` directory.
## Contact Information
For any questions or issues, please feel free to contact the authors.
|