|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# Block-Recurrent Dynamics in ViTs (Raptor) |
| 4 | + |
| 5 | +<img src="assets/raptor_logo.png" width="33%" /> |
| 6 | + |
| 7 | +[](https://github.com/KempnerInstitute/raptor/actions/workflows/tests.yml) |
| 8 | +[](https://arxiv.org/abs/1234.56789) |
| 9 | + |
| 10 | +[**Mozes Jacobs**](https://scholar.google.com/citations?user=8Dm0KfQAAAAJ&hl=en)$^{\star1}$ [**Thomas Fel**](https://thomasfel.me)$^{\star1}$ [**Richard Hakim**](https://richhakim.com/)$^{\star1}$ |
| 11 | +<br> |
| 12 | +[**Alessandra Brondetta**](https://alessandrabrondetta.github.io/)$^{2}$ [**Demba Ba**](https://scholar.google.com/citations?user=qHiACEgAAAAJ&hl=en)$^{1,3}$ [**T. Andy Keller**](https://akandykeller.github.io/)$^{1}$ |
| 13 | + |
| 14 | +<small> |
| 15 | + |
| 16 | +$^1$**Kempner Institute, Harvard University** $^2$**Osnabrück University** $^3$**Harvard University** |
| 17 | + |
| 18 | +</small> |
| 19 | + |
| 20 | +</div> |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +**tl;dr** Our work introduces the Block-Recurrent Hypothesis (BRH), by noticing that foundation models like DINOv2 can be rewritten using only two recurrent blocks to recover 96% of the original accuracy. We leverage our framework and explore a Dynamical Interpretability approach where we interpret token evolution through layers as trajectories and show that they converge into class-dependent angular basins while late-stage updates collapse into low-rank attractors. |
| 25 | + |
| 26 | +Ultimately, the study reveals that Vision Transformers seems to naturally converge toward compact, iterative programs instead of unique layer-by-layer transformations (indicating a lower algorithmic complexity / Kolmogorov complexity). |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Setup |
| 31 | + |
| 32 | +### Environment |
| 33 | +To run the code, you will need to create a mamba (or conda) environment from the `environment.yml` file. |
| 34 | +Create and activate the environment with |
| 35 | +```bash |
| 36 | +mamba env create -f environment.yml |
| 37 | +mamba activate raptor |
| 38 | +``` |
| 39 | + |
| 40 | +### Paths |
| 41 | +Edit `src/paths.py` to have the correct absolute paths to different datasets. |
| 42 | + |
| 43 | +### Extracting DINOv2 Activations for ImageNet-1k |
| 44 | +For ImageNet, we precompute the DINOv2 activations so that `Raptor` can train faster. |
| 45 | +We provide a script to extract the activations from the ImageNet-1k dataset. This script is available in the `data` directory. |
| 46 | +This script takes around 5 hours to run on 1 H100 GPU, and storing the activations requires a lot of disk space. |
| 47 | +```bash |
| 48 | +cd data |
| 49 | +python precompute_dinov2_act.py |
| 50 | +``` |
| 51 | + |
| 52 | +### Download Pretrained Classifiers |
| 53 | +Download the DINOv2 linear heads from Meta's [repository](https://github.com/facebookresearch/dinov2). |
| 54 | +These are used during training of `Raptor`. |
| 55 | + |
| 56 | +```bash |
| 57 | +cd src |
| 58 | +wget https://dl.fbaipublicfiles.com/dinov2/dinov2_vitb14/dinov2_vitb14_reg4_linear_head.pth |
| 59 | +wget https://dl.fbaipublicfiles.com/dinov2/dinov2_vits14/dinov2_vits14_reg4_linear_head.pth |
| 60 | +cp dinov2_vitb14_reg4_linear_head.pth imagenet_probes/dinov2_vitb14_reg4_linear_head.pth |
| 61 | +cp dinov2_vits14_reg4_linear_head.pth imagenet_probes/dinov2_vits14_reg4_linear_head.pth |
| 62 | +``` |
| 63 | + |
| 64 | +## Usage Example |
| 65 | +`Raptor` training follows 4 main steps. Here, we show example usage for a 3-block `Raptor`: |
| 66 | + |
| 67 | +1. Determine max-cut segmentations. This has been done for you in src/000_max_cut_dinov2_base.ipynb. |
| 68 | +2. Train each block independently. |
| 69 | +```bash |
| 70 | +cd src |
| 71 | +python trainer.py --teacher_force --mse --sigma 0 --lr 3e-4 --wandb --t_scale --swiglu --start_layer 0 --end_layer 7 --seed 100 |
| 72 | +python trainer.py --teacher_force --mse --sigma 0 --lr 3e-4 --wandb --t_scale --swiglu --start_layer 7 --end_layer 10 --seed 101 |
| 73 | +python trainer.py --teacher_force --weighted --sigma 0 --lr 3e-4 --wandb --t_scale --swiglu --start_layer 10 --end_layer 12 --seed 104 |
| 74 | +``` |
| 75 | +3. Train the full model with the pretrained blocks. |
| 76 | +```bash |
| 77 | +cd src |
| 78 | +BP1="final_weighted_False_autoregressive_False_distillation_False_teacher_True_mse_True_cosine_False_t_scale_True_swiglu_True_sigma_0.0_start_0_end_7_lr_0.0003_cls_weight_0.34_reg_weight_0.33_patch_weight_0.33_seed_100_step_312500.pt" |
| 79 | +BP2="final_weighted_False_autoregressive_False_distillation_False_teacher_True_mse_True_cosine_False_t_scale_True_swiglu_True_sigma_0.0_start_7_end_10_lr_0.0003_cls_weight_0.34_reg_weight_0.33_patch_weight_0.33_seed_101_step_312500.pt" |
| 80 | +BP3="final_weighted_True_autoregressive_False_distillation_False_teacher_True_mse_False_cosine_False_t_scale_True_swiglu_True_sigma_0.0_start_10_end_12_lr_0.0003_cls_weight_0.34_reg_weight_0.33_patch_weight_0.33_seed_104_step_312500.pt" |
| 81 | +python trainer.py --raptor3 --autoreg --weighted --sigma 0 --lr 3e-4 --wandb --t_scale --swiglu --start_layer 0 --end_layer 12 --cls_weight 0.45 --reg_weight 0.10 --patch_weight 0.45 --bp1 $BP1 --bp2 $BP2 --bp3 $BP3 --seed 1101 |
| 82 | +``` |
| 83 | +4. Train linear probes on the frozen pretrained checkpoints. |
| 84 | +```bash |
| 85 | +cd src/imagenet_probes |
| 86 | +python train_probe.py --variant raptor3 --model_seed 1101 --seed 4005 |
| 87 | +``` |
| 88 | +```bash |
| 89 | +cd src/ade20k_probes |
| 90 | +python train_probe.py --variant raptor3 --model_seed 1101 --seed 5005 |
| 91 | +``` |
| 92 | +```bash |
| 93 | +cd src/nyud_probes |
| 94 | +python train_probe.py --variant raptor3 --model_seed 1101 --seed 6005 |
| 95 | +``` |
| 96 | + |
| 97 | +## Reproducing Foundation Models Results (Section 3) |
| 98 | +To reproduce the results for the foundation models section (Table 1 and Figure 7), do the following: |
| 99 | + |
| 100 | +1. Determine max-cut segmentations. This has been done for you in src/max_cut_dinov2_base.ipynb. |
| 101 | +2. Train each block independently. |
| 102 | +```bash |
| 103 | +cd src/runs |
| 104 | +sbatch blocks.sh |
| 105 | +``` |
| 106 | +3. Train the full model with the pretrained blocks. |
| 107 | +```bash |
| 108 | +cd src/runs |
| 109 | +sbatch 002_raptor2_pretrained.sh |
| 110 | +sbatch 003_raptor3_pretrained.sh |
| 111 | +sbatch 004_raptor4_pretrained.sh |
| 112 | +``` |
| 113 | +4. Train linear probes on the frozen pretrained checkpoints. |
| 114 | +```bash |
| 115 | +cd src/ade20k_probes |
| 116 | +sbatch run_all.sh |
| 117 | +``` |
| 118 | +```bash |
| 119 | +cd src/imagenet_probes |
| 120 | +sbatch run_all.sh |
| 121 | +``` |
| 122 | +```bash |
| 123 | +cd src/nyud_probes |
| 124 | +sbatch run_all.sh |
| 125 | +``` |
| 126 | +5. Table 1 |
| 127 | +```bash |
| 128 | +cd src |
| 129 | +python aggregate_results.py |
| 130 | +``` |
| 131 | +6. Figure 7 |
| 132 | +Run the notebook in src/imagenet_probes/101_eval_error_bars.ipynb. |
0 commit comments