Files

140 lines
5.1 KiB
Markdown
Raw Permalink Normal View History

2025-05-18 20:02:45 -05:00
# enso
## Scaling Diffusion Transformers with Mixture of Experts
2024-07-16 05:19:54 -05:00
2024-07-16 20:17:12 -05:00
[![arXiv](https://img.shields.io/badge/arXiv-2407.11633-b31b1b.svg)](https://arxiv.org/abs/2407.11633)
2024-07-16 05:29:42 -05:00
This repo contains PyTorch model definitions, pre-trained weights and training/sampling code for our paper scaling Diffusion Transformers to 16 billion parameters (DiT-MoE).
2025-05-18 20:02:45 -05:00
DiT-MoE as a sparse version of the diffusion Transformer, is scalable and competitive with dense networks while exhibiting highly optimized inference.
2024-07-16 05:19:54 -05:00
2025-05-18 20:02:45 -05:00
![DiT-MoE framework](visuals/framework.png)
2024-07-16 03:44:32 -05:00
2025-05-18 22:55:02 -05:00
* 🪐 A PyTorch [implementation](models.py) of Enso and pre-trained checkpoints in paper
2025-05-18 20:02:45 -05:00
* 🌋 **Rectified flow**-based training and sampling scripts
* 💥 A [sampling script](sample.py) for running pre-trained DiT-MoE
2024-07-19 03:17:48 -05:00
* 🛸 A DiT-MoE training script using PyTorch [DDP](train.py) and [deepspeed](train_deepspeed.py)
2026-02-26 09:58:21 -08:00
* ⚡️ A **upcycle** [scripts](https://github.com/feizc/DiT-MoE/blob/main/upcycle.py) to convert dense to MoE ckpts referring [link](#)
2024-08-18 06:22:41 -05:00
2024-07-16 05:29:42 -05:00
2024-07-16 20:30:09 -05:00
### To-do list
2024-07-16 05:29:42 -05:00
2024-07-19 03:17:48 -05:00
- [x] training / inference scripts
- [x] experts routing analysis
2024-08-13 20:48:44 -05:00
- [x] huggingface ckpts
2024-07-16 03:44:32 -05:00
2025-05-18 20:02:45 -05:00
### 1. Training
2024-07-16 05:29:42 -05:00
2026-02-26 19:42:49 -08:00
You can refer to the [link](https://github.com/zenlm/enso/blob/main/environment.yml) to build the running environment.
2024-07-18 01:19:07 -05:00
2024-07-16 20:30:09 -05:00
To launch DiT-MoE-S/2 (256x256) in the latent space training with `N` GPUs on one node with pytorch DDP:
```bash
torchrun --nnodes=1 --nproc_per_node=N train.py \
--model DiT-S/2 \
2024-07-18 01:19:07 -05:00
--num_experts 8 \
--num_experts_per_tok 2 \
2024-07-16 20:30:09 -05:00
--data-path /path/to/imagenet/train \
--image-size 256 \
2024-07-18 01:19:07 -05:00
--global-batch-size 256 \
--vae-path /path/to/vae
2024-07-16 20:30:09 -05:00
```
2024-07-16 05:29:42 -05:00
2024-07-18 01:19:07 -05:00
2026-02-26 19:42:49 -08:00
For multiple node training, we solve the [bug](https://github.com/zenlm/enso/blob/main/train.py#L149) at original DiT repository, and you can run with 8 nodes as:
2024-07-17 02:22:29 -05:00
```bash
torchrun --nnodes=8 \
--node_rank=0 \
--nproc_per_node=8 \
--master_addr="10.0.0.0" \
--master_port=1234 \
train.py \
--model DiT-B/2 \
2024-07-18 01:19:07 -05:00
--num_experts 8 \
--num_experts_per_tok 2 \
2024-07-17 02:22:29 -05:00
--global-batch-size 1024 \
2024-07-18 01:19:07 -05:00
--data-path /path/to/imagenet/train \
--vae-path /path/to/vae
2024-07-17 02:22:29 -05:00
```
2024-07-16 05:29:42 -05:00
2025-05-18 20:02:45 -05:00
For larger model size training, we recommand to use deepspeed with flash attention scripts, and different stage settings including zero2 and zero3 can be seen in config file.
2024-07-19 03:17:48 -05:00
You can run as:
```bash
python -m torch.distributed.launch --nnodes=1 --nproc_per_node=8 train_deepspeed.py \
--deepspeed_config config/zero2.json \
--model DiT-XL/2 \
--num_experts 8 \
--num_experts_per_tok 2 \
2024-07-19 03:32:01 -05:00
--data-path /path/to/imagenet/train \
--vae-path /path/to/vae \
2024-07-19 03:17:48 -05:00
--train_batch_size 32
```
2026-02-26 19:42:49 -08:00
For rectified flow training as rectified flow models and [SD3](https://stability.ai/news/stable-diffusion-3), you can run as:
2024-08-13 20:48:44 -05:00
```bash
python -m torch.distributed.launch --nnodes=1 --nproc_per_node=8 train_deepspeed.py \
--deepspeed_config config/zero2.json \
--model DiT-XL/2 \
--rf True \
--num_experts 8 \
--num_experts_per_tok 2 \
--data-path /path/to/imagenet/train \
--vae-path /path/to/vae \
--train_batch_size 32
```
2025-05-18 20:02:45 -05:00
Our experiments show that rectified flow training leads to a better performance as well as faster convergence.
2024-08-13 20:48:44 -05:00
2025-05-18 20:02:45 -05:00
We also provide all shell scripts for different model size training in file folder *scripts*.
2024-07-19 03:17:48 -05:00
2025-05-18 20:02:45 -05:00
### 2. Inference
2024-07-16 05:29:42 -05:00
2025-05-18 20:02:45 -05:00
We include a [`sample.py`](sample.py) script which samples images from a DiT-MoE model. Take care that we use torch.float16 for large model inference.
2024-07-16 20:30:09 -05:00
```bash
python sample.py \
2024-07-20 21:50:47 -05:00
--model DiT-XL/2 \
2024-07-16 20:30:09 -05:00
--ckpt /path/to/model \
2024-07-20 21:50:47 -05:00
--vae-path /path/to/vae \
2024-07-16 20:30:09 -05:00
--image-size 256 \
--cfg-scale 1.5
```
2025-05-18 20:02:45 -05:00
### 3. Download Models and Data
2024-07-16 20:30:09 -05:00
2025-05-18 20:02:45 -05:00
The model weights, data and used scripts for results reproduce are listed as follows.
2024-07-18 01:19:07 -05:00
2025-05-18 20:02:45 -05:00
We use sd vae in this [link](https://huggingface.co/feizhengcong/DiT-MoE/tree/main/sd-vae-ft-mse).
2024-07-23 01:20:22 -05:00
2024-08-13 20:48:44 -05:00
| DiT-MoE Model | Image Resolution | Url | Scripts | Loss curve |
|---------------|------------------|---------|---------|-------|
| DiT-MoE-S/2-8E2A | 256x256 | [link](https://huggingface.co/feizhengcong/DiT-MoE/blob/main/dit_moe_s_8E2A.pt) | DDIM | -|
| DiT-MoE-S/2-16E2A | 256x256 | [link](https://huggingface.co/feizhengcong/DiT-MoE/blob/main/dit_moe_s_16E2A.pt) | DDIM| -|
| DiT-MoE-B/2-8E2A | 256x256 | [link](https://huggingface.co/feizhengcong/DiT-MoE/blob/main/dit_moe_b_8E2A.pt) | DDIM | -|
| DiT-MoE-XL/2-8E2A | 256x256 | [link](https://huggingface.co/feizhengcong/DiT-MoE/blob/main/dit_moe_xl_8E2A.pt) | RF|-|
2024-09-01 21:35:44 -05:00
| DiT-MoE-G/2-16E2A | 256x256 | [link](https://huggingface.co/feizhengcong/DiT-MoE/blob/main/dit_moe_g_16E2A.pt) | RF|-|
2024-07-16 20:30:09 -05:00
### 4. Expert Specialization Analysis Tools
2025-05-18 20:02:45 -05:00
We provide all the analysis scripts used in the paper.
You can use [`expert_data.py`](analysis/expert_data.py) to sample data points towards experts ids across different class-conditional.
Then, a series of files headmap_xx.py are used to visualize the frequency of expert selection for different scenarios.
Quick validation can be achieved by adjusting the number of sampled data and the save path.
2024-07-18 01:41:59 -05:00
2024-07-18 01:19:07 -05:00
2024-07-16 20:30:09 -05:00
### 5. BibTeX
2024-07-16 05:29:42 -05:00
```bibtex
@article{FeiDiTMoE2024,
title={Scaling Diffusion Transformers to 16 Billion Parameters},
author={Zhengcong Fei, Mingyuan Fan, Changqian Yu, Debang Li, Jusnshi Huang},
year={2024},
journal={arXiv preprint},
}
```