Jungsoo Lee1, Alexi Gladstone2, Yilun Du3 1MIT 2UIUC 3Harvard University
This repository contains the PyTorch implementation of the Transformer Energy-Based Model (TEM). TEM is a contrastively trained energy-based model (EBM) that generates high-fidelity ImageNet images using Langevin MCMC without classifier-free guidance.
Class-conditional ImageNet samples, no classifier-free guidance.
256×256
512×512
Class-conditional ImageNet, FID-50K, no classifier-free guidance:
| Model | Params | Resolution | Sampling steps | FID ↓ |
|---|---|---|---|---|
| TEM-L/2 | 415M | 256×256 | 60 | 4.74 |
| TEM-XL/2 | 609M | 256×256 | 60 | 3.82 |
| TEM-XL/2 | 609M | 512×512 | 60 | 8.24 |
Inference-time scaling for TEM-XL/2 at 256×256 (step size 500):
| Sampling steps | FID ↓ | IS ↑ | Precision ↑ | Recall ↑ |
|---|---|---|---|---|
| 40 | 7.63 | 106.0 | 0.65 | 0.65 |
| 60 | 3.82 | 176.2 | 0.76 | 0.57 |
| 80 | 4.72 | 221.1 | 0.81 | 0.51 |
Langevin sampling trajectory. Each row is one chain started from Gaussian noise, shown every 20 steps (steps 0, 20, …, 120).
| Model | Resolution | Training | Download | SHA-256 |
|---|---|---|---|---|
| TEM-XL/2 (EMA weights) | 256×256 | 12.8M images | Google Drive (2.44 GB) | 02eee5f8d90a633055ae0441559ee5ae89928bc1cb4dfa931ae3bae2ba55e4f8 |
pip install gdown && gdown 1xBJOjGM-Wrdr-hEUNAdLt9Kj__TKzLHs # -> TEM-XL-2_imagenet256_ema.pth
sha256sum TEM-XL-2_imagenet256_ema.pth # should match the hash above
torchrun --nproc_per_node=4 --standalone inference.py \
--checkpoint TEM-XL-2_imagenet256_ema.pth --model_size XL --resolution 256The checkpoint records its energy-head layout, so inference.py needs no extra flags.
git clone https://github.com/int-lee/TEM.git && cd TEM
conda env create -f environment.yml && conda activate tem
# or, in an existing Python 3.10 environment (install the torch build for your CUDA first):
pip install -r requirements.txtThese pretrained networks are downloaded automatically on first use; set HF_HOME / TORCH_HOME to choose the cache location:
| Network | Used for |
|---|---|
stabilityai/sd-vae-ft-mse |
the latent space (4×32×32 at 256px, 4×64×64 at 512px) |
facebook/dinov2-large |
the PRA target (training only) |
| torchvision Inception-v3 | FID monitor during training |
Point --data_path at the ImageNet-1k train directory: one sub-folder per class (n01440764/, ...), as for torchvision.datasets.ImageFolder. Only folders named n######## are treated as classes.
Images are preprocessed as in ADM/DiT: center crop with box-downsampling and a bicubic resize, then a random horizontal flip.
Training uses 4 GPUs with the paper's defaults: batch 256 (8 per GPU × 8 accumulation steps × 4 GPUs), Adam with learning rate 2e-4 and betas (0, 0.999), 40 Langevin steps (step size 500, noise 0.05), and EMA decay 0.9999.
# TEM-XL/2, ImageNet 256x256
torchrun --nproc_per_node=4 --standalone train.py \
--data_path /path/to/imagenet/train --model_size XL --resolution 256
# TEM-XL/2, ImageNet 512x512
torchrun --nproc_per_node=4 --standalone train.py \
--data_path /path/to/imagenet/train --model_size XL --resolution 512
# other sizes: --model_size S | B | LEvery option is a command-line flag (python train.py -h):
Steps. The step counter (checkpoint names, --max_steps, logging intervals) counts micro-batches. With the defaults, one micro-batch is 32 images across 4 GPUs, so the paper's 50K iterations at batch 256 correspond to --max_steps 400000.
Outputs. Each run writes to <output_dir>/<experiment_name>/ (default ./checkpoints/TEM-<size>-2_in<res>/):
model_step_N.pth: model, EMA, optimizer and replay buffer.fid_scores.txt: a quick FID monitor during training (1k samples). Useinference.pyand the ADM evaluator for real evaluations.
Checkpoints are written atomically. A restarted job resumes automatically from the newest complete checkpoint, including the data order, so preemptible cluster queues are safe to use.
Compute. Training TEM-XL/2 at 256×256 for 50K iterations takes about 6 days on 4 H200 GPUs. Training at 512×512 is about 3–4× slower.
inference.py follows the paper's persistent-chain protocol:
- The replay buffer starts empty, with a capacity of 5,000.
- While it fills, chains start from 𝒩(0, I); afterwards they start from buffered states.
- Each chain runs
--total_langevin_stepssteps (default 60). Its state after step 40 goes into the buffer, so the buffer stays matched to the training horizon. - Every chain gets an independently sampled class label.
# 1. Generate 50K samples -> samples/<run>_<step>_s60_n50000.npz
torchrun --nproc_per_node=4 --standalone inference.py \
--checkpoint /path/to/model_step_N.pth --model_size XL --resolution 256Inference-time scaling. No retraining is needed: increase --total_langevin_steps (e.g. 40 → 60 → 80) or --step_size (e.g. 500 → 1000) to move along the fidelity–diversity curve.
@article{lee2026scaling,
title = {Scaling Transformer Energy-Based Models for High Quality Image Synthesis},
author = {Lee, Jungsoo and Gladstone, Alexi and Du, Yilun},
journal = {arXiv preprint arXiv:2610.31889},
year = {2026},
}






































