|
Download code/README.md from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/README.md
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code/README.md
-
curl -L -o README.md https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/README.md
3.81 kB
| # 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. | |