File size: 5,939 Bytes
fd684da | 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 | # wAR-Tok: Learning Discrete Autoregressive Priors with Wasserstein Gradient Flow
CIFAR-10 implementation of [our paper](https://arxiv.org/abs/2605.06148).
## Features
- Discrete image tokenization with Wasserstein gradient flow regularization.
- Transformer encoder and decoder with cross-attention, RMSNorm and GEGLU.
- Six variants: baseline, tail dropout, flow matching, FM + tail dropout, FM + tied tail dropout, and wAR-Tok.
- Two-stage training: tokenizer training followed by an autoregressive prior.
- Mixed precision, multi-GPU training and optional W&B logging.
## Quick Start
### Installation
Use Python 3.12 and CUDA. The training environment uses PyTorch 2.8.0 / CUDA 12.8.
```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
### Data Preparation
```bash
python scripts/prepare_cifar10.py --download
```
This prepares the CIFAR-10 training split under `data/cifar10`. Download the [FID reference](https://huggingface.co/Zyriix/wARTok/resolve/main/refs/cifar10-32x32_local.npz?download=true) to `refs/cifar10-32x32_local.npz`.
### Training
The default configuration uses 8 GPUs, global batch size 512 and BF16. Each method has a `*_tokenizer.yaml` and a `*_ar.yaml` in [configs/cifar10](configs/cifar10): train the tokenizer for 400K steps, then the AR prior for 200K steps.
**Stage 1: Train the tokenizer**
```bash
python train.py --config=configs/cifar10/war_tokenizer.yaml
```
**Stage 2: Train the AR prior**
```bash
python train.py --config=configs/cifar10/war_ar.yaml \
ae_ckpt_path=./outputs/war_tokenizer/000/ckpts/last.ckpt
```
The AR configuration defaults to the matching tokenizer's `000/ckpts/last.ckpt`. Set `WANDB_MODE=online` to enable experiment logging.
### Evaluation
Use the matching tokenizer configuration for reconstruction and the AR configuration for generation. FM reconstruction uses 25 Euler steps; generation uses a single decoder step. Evaluation uses FP32; generation uses AR EMA, temperature 1, CFG 0 and no top-k/top-p filtering.
**Reconstruction**
```bash
CUDA_VISIBLE_DEVICES=0 python evaluate.py \
--config configs/cifar10/war_tokenizer.yaml --checkpoint checkpoints/war.pt \
--mode reconstruct --data data/cifar10 --num-samples 50000 \
--output outputs/war-reconstruction --fid-reference refs/cifar10-32x32_local.npz
```
**Generation**
```bash
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 torchrun --standalone --nproc-per-node=8 evaluate.py \
--config configs/cifar10/war_ar.yaml --checkpoint checkpoints/war.pt \
--num-samples 50000 --batch-size 64 --seed 42 --no-legacy \
--output outputs/war-generation --fid-reference refs/cifar10-32x32_local.npz
```
Non-legacy mode (`--no-legacy`) uses a different seed on each GPU and generally yields [lower gFID scores](#checkpoints), without changing the relative ranking of the methods or the conclusions supported by the results.
To evaluate all six checkpoints with `--no-legacy`:
```bash
python scripts/evaluate_all.py --gpus 0,1,2,3,4,5,6,7 \
--output outputs/cifar10-nonlegacy
```
Use `--methods baseline war` for a subset or `--resume` to reuse completed evaluations.
### Exporting Checkpoints
```bash
python scripts/export_checkpoint.py --source path/to/raw-ar.ckpt \
--config configs/cifar10/war_ar.yaml --output checkpoints/war.pt
```
The export includes tokenizer and raw/EMA AR weights without optimizer states.
## Checkpoints
Name checkpoints after their configuration prefix (e.g. `baseline_ar.yaml` β `checkpoints/baseline.pt`, `war_ar.yaml` β `checkpoints/war.pt`); each includes the tokenizer and raw/EMA AR weights. Hugging Face repository: [wARTok](https://huggingface.co/Zyriix/wARTok).
| Method | rFID | Historical gFID | Non-legacy gFID | Download |
|---|---:|---:|---:|---|
| [Baseline](configs/cifar10/baseline_ar.yaml) | 2.3015 | 52.5343 | 49.4191 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/baseline.pt?download=true) |
| [Tail Dropout](configs/cifar10/tail_dropout_ar.yaml) | 3.3567 | 23.3713 | 19.8997 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/tail_dropout.pt?download=true) |
| [FM](configs/cifar10/flow_matching_ar.yaml) | 3.4194 | 38.6309 | 35.8593 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/flow_matching.pt?download=true) |
| [FM + Tail](configs/cifar10/flow_matching_tail_dropout_ar.yaml) | 4.4247 | 22.7613 | 19.0569 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/flow_matching_tail_dropout.pt?download=true) |
| [FM + Tail + Tied](configs/cifar10/flow_matching_tied_tail_dropout_ar.yaml) | 6.4911 | 44.7971 | 41.1334 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/flow_matching_tied_tail_dropout.pt?download=true) |
| [wAR-Tok](configs/cifar10/war_ar.yaml) | 3.0648 | 11.3059 | 7.3749 | [Download](https://huggingface.co/Zyriix/wARTok/resolve/main/checkpoints/war.pt?download=true) |
Download the [FID reference](https://huggingface.co/Zyriix/wARTok/resolve/main/refs/cifar10-32x32_local.npz?download=true) to `refs/cifar10-32x32_local.npz`.
## Project Structure
```text
wAR/
βββ configs/cifar10/ # Training configurations
βββ checkpoints/ # Downloaded weights
βββ scripts/ # Data preparation and evaluation tools
βββ models.py # Model architectures
βββ train.py # Tokenizer and AR training
βββ evaluate.py # Reconstruction and generation
βββ dataset.py # Data loading
βββ lpips.py # Perceptual loss
βββ calc_fid.py # ADM FID evaluation
```
## References
This project builds on [EDM](https://github.com/NVlabs/edm), [ADM](https://github.com/openai/guided-diffusion), and [LPIPS](https://github.com/richzhang/PerceptualSimilarity).
## License
Project-authored code is covered by [MIT](LICENSE). Third-party components retain their original licenses.
|