Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

unidiffuser

> 编程语言
Open source

Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion"

1.5K stars0 likes0 views
WebsiteGitHub

About

Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion"

UniDiffuser

Code and models for the paper "One Transformer Fits All Distributions in Multi-Modal Diffusion" UniDiffuser is a unified diffusion framework to fit all distributions relevant to a set of multi-modal data in one model. Its key insight is -- learning diffusion models for marginal, conditional, and joint distributions can be unified as predicting the noise in the perturbed data, where the perturbation levels (i.e. timesteps) can be different for different modalities. Inspired by the unified view, UniDiffuser learns all distributions simultaneously with a minimal modification to the original diffusion model -- perturbs data in all modalities instead of a single modality, inputs individual timesteps in different modalities, and predicts the noise of all modalities instead of a single modality. UniDiffuser is parameterized by a transformer for diffusion models to handle input types of different modalities. Implemented on large-scale paired image-text data, UniDiffuser is able to perform image, text, text-to-image, image-to-text, and image-text pair generation by setting proper timesteps without additional overhead. In particular, UniDiffuser is able to produce perceptually realistic samples in all tasks and its quantitative results (e.g., the FID and CLIP score) are not only superior to existing general-purpose models but also comparable to the bespoken models (e.g., Stable Diffusion and DALL-E 2) in representative tasks (e.g., text-to-image generation).


Dependency

conda create -n unidiffuser python=3.9
conda activate unidiffuser
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu116  # install torch-1.13.1
pip install accelerate==0.12.0 absl-py ml_collections einops ftfy==6.1.1 transformers==4.23.1
pip install -e git+https://github.com/openai/CLIP.git@main#egg=clip

# xformers is optional, but it would greatly speed up the attention computation.
pip install -U xformers
pip install -U --pre triton
  • We highly suggest install xformers, which would greatly speed up the attention computation for both training and inference.

Pretrained Models

UniDiffuser employs a variation of transformer, called U-ViT, which parameterizes the joint noise prediction network. Other components perform as encoders and decoders of different modalities, including a pretrained image autoencoder from Stable Diffusion, a pretrained image ViT-B/32 CLIP encoder, a pretrained text ViT-L CLIP encoder, and a GPT-2 text decoder finetuned by ourselves.

We provide two versions of UniDiffuser, which contain U-ViT of 1B parameters and can run on a GPU with at least 10 GB memory. They can be downloaded from Hugging Face:

  • UniDiffuser-v0: This version is trained on LAION-5B at 512x512 resolution, which contains noisy webdata of text-image pairs.
  • UniDiffuser-v1: This version is resumed from UniDiffuser-v0, and is further trained with a set of less noisy internal text-image pairs. It uses a flag as its input to distinguish webdata and internal data during training.

Both links contain three files:

  • autoencoder_kl.pth is the weight of the image autoencoder converted from Stable Diffusion.
  • caption_decoder.pth is the weight of the finetuned GPT-2 text decoder.
  • uvit_v0.pth or uvit_v1.pth is the weight of U-ViT for UniDiffuser-v0 or UniDiffuser-v1.

Note that UniDiffuser-v0 and UniDiffuser-v1 share the same autoencoder_kl.pth and caption_decoder.pth. You only need to download them once. As for other components, they will be automatically downloaded.

After downloading, create a new folder named models and put all pretrained models into this folder as follows:

├── models
│   └── autoencoder_kl.pth
│   └── caption_decoder.pth
│   └── uvit_v0.pth or uvit_v1.pth

Inference

We suggest to use UniDiffuser-v1 for a better performance. Results are put into out directory by default.

  • text-to-image generation
python sample_multi_v1.py --mode=t2i --prompt="an elephant under the sea"
  • image-to-text generation
python sample_multi_v1.py --mode=i2t --img=assets/space.jpg
  • joint generation
python sample_multi_v1.py --mode=joint
  • image generation
python sample_multi_v1.py --mode=i
  • text generation
python sample_multi_v1.py --mode=t
  • image variation
python sample_multi_v1.py --mode=i2t2i --img=assets/space.jpg
  • text variation
python sample_multi_v1.py --mode=t2i2t --prompt="an elephant under the sea"

We provide all supported arguments below

…

The inference command of UniDiffuser-v0 is basically the same as UniDiffuser-v1, only need to change sample_multi_v1.py to sample_multi_v0.py. For example:

python sample_multi_v0.py --mode=t2i --prompt="an elephant under the sea"

Integration with diffusers

UniDiffuser is also available in diffusers. It is available in six different modes. Here is how one can use the UniDiffuserPipeline to generate images from text:

import torch
from diffusers import UniDiffuserPipeline

device = "cuda"
model_id_or_path = "thu-ml/unidiffuser-v1"
pipe = UniDiffuserPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe.to(device)

# Text-to-image generation
prompt = "an elephant under the sea"

sample = pipe(prompt=prompt, num_inference_steps=20, guidance_scale=8.0)
t2i_image = sample.images[0]
t2i_image.save("unidiffuser_text2img_sample_image.png")

To learn more details, check out the official UniDiffuser documentation.

References

If you find the code useful for your research, please consider citing

@article{bao2022one,
  title={One Transformer Fits All Distributions in Multi-Modal Diffusion at Scale},
  author={Bao, Fan and Nie, Shen and Xue, Kaiwen and Li, Chongxuan and Pu, Shi and Wang, Yaole and Yue, Gang and Cao, Yue and Su, Hang and Zhu, Jun},
  year={2023}
}

@inproceedings{bao2022all,
  title={All are Worth Words: A ViT Backbone for Diffusion Models},
  author={Bao, Fan and Nie, Shen and Xue, Kaiwen and Cao, Yue and Li, Chongxuan and Su, Hang and Zhu, Jun},
  booktitle = {CVPR},
  year={2023}
}

This implementation is heavily based on the U-ViT code.

GitHub Issues· 24 open

View all on GitHub
  • #4

    About training codes

    Updated Nov 6, 2024
  • #32

    Does unidiffuser support using the peft library for lora fine-tuning?

    Updated Mar 7, 2024
  • #38

    Training uvit

    Updated Jan 19, 2024
  • #21

    can' t load tokenizer for 'gpt2', why?

    Updated Dec 12, 2023
  • #37

    May I ask what validation loss you used during training?

    Updated Nov 7, 2023
  • #36

    Unidiffuser can not be loaded in the latest version of diffusers

    Updated Nov 1, 2023
  • #29

    expected str, bytes or os.PathLike object, not NoneType

    Updated Oct 7, 2023
  • #35

    question about training

    Updated Oct 4, 2023
  • #31

    How to reproduce CLIP score

    Updated Jul 12, 2023
  • #30

    requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

    Updated Jun 27, 2023

Highlights

  • •We highly suggest install xformers, which would greatly speed up the attention computation for *both training and inference*.
  • •UniDiffuser-v0: This version is trained on LAION-5B at 512x512 resolution, which contains noisy webdata of text-image pairs.
  • •autoencoder_kl.pth is the weight of the image autoencoder converted from Stable Diffusion.
  • •caption_decoder.pth is the weight of the finetuned GPT-2 text decoder.
  • •uvit_v0.pth or uvit_v1.pth is the weight of U-ViT for UniDiffuser-v0 or UniDiffuser-v1.
  • •text-to-image generation
  • •image-to-text generation
  • •joint generation
  • •image generation
  • •text generation

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言