Home

Awesome

<div align="center"> <!-- <h1>KTransformers</h1> --> <p align="center"> <picture> <img alt="xDiT" src="https://raw.githubusercontent.com/xdit-project/xdit_assets/main/XDiTlogo.png" width="50%"> </p> <h3>A Scalable Inference Engine for Diffusion Transformers (DiTs) on Multiple Computing Devices</h3> <a href="#cite-us">πŸ“ Papers</a> | <a href="#QuickStart">πŸš€ Quick Start</a> | <a href="#support-dits">🎯 Supported DiTs</a> | <a href="#dev-guide">πŸ“š Dev Guide </a> | <a href="https://github.com/xdit-project/xDiT/discussions">πŸ“ˆ Discussion </a> | <a href="https://medium.com/@xditproject">πŸ“ Blogs</a></strong> <p></p>

</div> <h2 id="agenda">Table of Contents</h2> <h2 id="meet-xdit">πŸ”₯ Meet xDiT</h2>

Diffusion Transformers (DiTs) are driving advancements in high-quality image and video generation. With the escalating input context length in DiTs, the computational demand of the Attention mechanism grows quadratically! Consequently, multi-GPU and multi-machine deployments are essential to meet the real-time requirements in online services.

<h3 id="meet-xdit-parallel">Parallel Inference</h3>

To meet real-time demand for DiTs applications, parallel inference is a must. xDiT is an inference engine designed for the parallel deployment of DiTs on a large scale. xDiT provides a suite of efficient parallel approaches for Diffusion Models, as well as computation accelerations.

The overview of xDiT is shown as follows.

<picture> <img alt="xDiT" src="https://raw.githubusercontent.com/xdit-project/xdit_assets/main/methods/xdit_overview.png"> </picture>
  1. Sequence Parallelism, USP is a unified sequence parallel approach proposed by us combining DeepSpeed-Ulysses, Ring-Attention.

  2. PipeFusion, a sequence-level pipeline parallelism, similar to TeraPipe but takes advantage of the input temporal redundancy characteristics of diffusion models.

  3. Data Parallel: Processes multiple prompts or generates multiple images from a single prompt in parallel across images.

  4. CFG Parallel, also known as Split Batch: Activates when using classifier-free guidance (CFG) with a constant parallelism of 2.

The four parallel methods in xDiT can be configured in a hybrid manner, optimizing communication patterns to best suit the underlying network hardware.

As shown in the following picture, xDiT offers a set of APIs to adapt DiT models in huggingface/diffusers to hybrid parallel implementation through simple wrappers. If the model you require is not available in the model zoo, developing it by yourself is not so difficult; please refer to our Dev Guide.

We also have implemented the following parallel strategies for reference:

  1. Tensor Parallelism
  2. DistriFusion
<h3 id="meet-xdit-perf">Computing Acceleration</h3>

Optimization is orthogonal to parallel and focuses on accelerating performance on a single GPU.

First, xDiT employs a series of kernel acceleration methods. In addition to utilizing well-known Attention optimization libraries, we leverage compilation acceleration technologies such as torch.compile and onediff.

Furthermore, xDiT incorporates optimization techniques from DiTFastAttn, which exploits computational redundancies between different steps of the Diffusion Model to accelerate inference on a single GPU.

<h2 id="updates">πŸ“’ Updates</h2> <h2 id="support-dits">🎯 Supported DiTs</h2> <div align="center">
Model NameCFGSPPipeFusion
🎬 HunyuanVideoNAβœ”οΈβŽ
🎬 CogVideoX1.5βœ”οΈβœ”οΈβŽ
🎬 Mochi-1βœ”οΈβœ”οΈβŽ
🎬 CogVideoXβœ”οΈβœ”οΈβŽ
🎬 LatteβŽβœ”οΈβŽ
πŸ”΅ HunyuanDiT-v1.2-Diffusersβœ”οΈβœ”οΈβœ”οΈ
🟠 FluxNAβœ”οΈβœ”οΈ
πŸ”΄ PixArt-Sigmaβœ”οΈβœ”οΈβœ”οΈ
🟒 PixArt-alphaβœ”οΈβœ”οΈβœ”οΈ
🟠 Stable Diffusion 3βœ”οΈβœ”οΈβœ”οΈ
</div>

Supported by legacy version only, including DistriFusion and Tensor Parallel as the standalone parallel strategies:

<div align="center">

πŸ”΄ DiT-XL

</div> <h2 id="comfyui">πŸ–ΌοΈ TACO-DiT: ComfyUI with xDiT</h2>

ComfyUI, is the most popular web-based Diffusion Model interface optimized for workflow. It provides users with a UI platform for image generation, supporting plugins like LoRA, ControlNet, and IPAdaptor. Yet, its design for native single-GPU usage leaves it struggling with the demands of today’s large DiTs, resulting in unacceptably high latency for users like Flux.1.

Using our commercial project TACO-DiT, a SaaS build on xDiT, we’ve successfully implemented a multi-GPU parallel processing workflow within ComfyUI, effectively addressing Flux.1’s performance challenges. Below is the example of using TACO-DiT to accelerate a Flux workflow with LoRA:

ComfyUI xDiT Demo

By using TACO-DiT, you could significantly reduce your ComfyUI workflow inference latency, and boosting the throughput with Multi-GPUs. Now it is compatible with multiple Plug-ins, including Controlnet and loras.

More features and details can be found in our Intro Video:

The blog article is also available: Supercharge Your AIGC Experience: Leverage xDiT for Multiple GPU Parallel in ComfyUI Flux.1 Workflow.

Currently, if you need the parallel version of ComfyUI, please fill in this application form or contact xditproject@outlook.com.

<h2 id="perf">πŸ“ˆ Performance</h2> <h3 id="perf_hunyuanvideo">HunyuanVideo</h3>
  1. HunyuanVideo Performance Report
<h3 id="perf_cogvideox">Mochi1</h3>
  1. mochi1-xdit: Reducing the Inference Latency by 3.54x Compare to the Official Open Souce Implementation!
<h3 id="perf_cogvideox">CogVideo</h3>
  1. CogVideo Performance Report
<h3 id="perf_flux">Flux.1</h3>
  1. Flux Performance Report
<h3 id="perf_latte">Latte</h3>
  1. Latte Performance Report
<h3 id="perf_hunyuandit">HunyuanDiT</h3>
  1. HunyuanDiT Performance Report
<h3 id="perf_sd3">SD3</h3>
  1. Stable Diffusion 3 Performance Report
<h3 id="perf_pixart">Pixart</h3>
  1. Pixart-Alpha Performance Report (legacy)
<h2 id="QuickStart">πŸš€ QuickStart</h2>

1. Install from pip

We set diffusers as an optional installation requirement. First, if you only use the USP interface, you don't need to install diffusers. Second, different models have different requirements for diffusers - for example, the latest models may need to be installed from the diffusers main branch.

pip install xfuser
# Or optionally, with diffusers
pip install "xfuser[diffusers]"

2. Install from source

pip install -e .
# Or optionally, with diffusers
pip install -e ".[diffusers]"

Note that we use two self-maintained packages:

  1. yunchang
  2. DistVAE

The flash_attn used for yunchang should be >= 2.6.0

3. Docker

We provide a docker image for developers to develop with xDiT. The docker image is thufeifeibear/xdit-dev.

4. Usage

We provide examples demonstrating how to run models with xDiT in the ./examples/ directory. You can easily modify the model type, model directory, and parallel options in the examples/run.sh within the script to run some already supported DiT models.

bash examples/run.sh

Hybriding multiple parallelism techniques togather is essential for efficiently scaling. It's important that the product of all parallel degrees matches the number of devices. Note use_cfg_parallel means cfg_parallel=2. For instance, you can combine CFG, PipeFusion, and sequence parallelism with the command below to generate an image of a cute dog through hybrid parallelism. Here ulysses_degree * pipefusion_parallel_degree * cfg_degree(use_cfg_parallel) == number of devices == 8.

torchrun --nproc_per_node=8 \
examples/pixartalpha_example.py \
--model models/PixArt-XL-2-1024-MS \
--pipefusion_parallel_degree 2 \
--ulysses_degree 2 \
--num_inference_steps 20 \
--warmup_steps 0 \
--prompt "A cute dog" \
--use_cfg_parallel

⚠️ Applying PipeFusion requires setting warmup_steps, also required in DistriFusion, typically set to a small number compared with num_inference_steps. The warmup step impacts the efficiency of PipeFusion as it cannot be executed in parallel, thus degrading to a serial execution. We observed that a warmup of 0 had no effect on the PixArt model. Users can tune this value according to their specific tasks.

5. Launch an HTTP Service

You can also launch an HTTP service to generate images with xDiT.

Launching a Text-to-Image Http Service

<h2 id="dev-guide">πŸ“š Develop Guide</h2>

We provide different difficulty levels for adding new models, please refer to the following tutorial.

Manual for adding new models

A high-level design of xDiT framework is provided below, which may help you understand the xDiT framework.

The implement and design of xdit framework

<h2 id="secrets">✨ The xDiT's Arsenal</h2>

The remarkable performance of xDiT is attributed to two key facets. Firstly, it leverages parallelization techniques, pioneering innovations such as USP, PipeFusion, and hybrid parallelism, to scale DiTs inference to unprecedented scales.

Secondly, we employ compilation technologies to enhance execution on GPUs, integrating established solutions like torch.compile and onediff to optimize xDiT's performance.

<h3 id="parallel">1. Parallel Methods</h3>

As illustrated in the accompanying images, xDiTs offer a comprehensive set of parallelization techniques. For the DiT backbone, the foundational methodsβ€”Data, USP, PipeFusion, and CFG parallelβ€”operate in a hybrid fashion. Additionally, the distinct methods, Tensor and DistriFusion parallel, function independently. For the VAE module, xDiT offers a parallel implementation, DistVAE, designed to prevent out-of-memory (OOM) issues. The (<span style="color: red;">xDiT</span>) highlights the methods first proposed by use.

<div align="center"> <img src="https://raw.githubusercontent.com/xdit-project/xdit_assets/main/methods/xdit_method.png" alt="xdit methods"> </div>

The communication and memory costs associated with the aforementioned intra-image parallelism, except for the CFG and DP (they are inter-image parallel), in DiTs are detailed in the table below. (* denotes that communication can be overlapped with computation.)

As we can see, PipeFusion and Sequence Parallel achieve lowest communication cost on different scales and hardware configurations, making them suitable foundational components for a hybrid approach.

𝒑: Number of pixels;
𝒉𝒔: Model hidden size;
𝑳: Number of model layers;
𝑷: Total model parameters;
𝑡: Number of parallel devices;
𝑴: Number of patch splits;
𝑸𝑢: Query and Output parameter count;
𝑲𝑽: KV Activation parameter count;
𝑨 = 𝑸 = 𝑢 = 𝑲 = 𝑽: Equal parameters for Attention, Query, Output, Key, and Value;

attn-KVcommunication costparam memoryactivations memoryextra buff memory
Tensor Parallelfresh$4O(p \times hs)L$$\frac{1}{N}P$$\frac{2}{N}A = \frac{1}{N}QO$$\frac{2}{N}A = \frac{1}{N}KV$
DistriFusion*stale$2O(p \times hs)L$$P$$\frac{2}{N}A = \frac{1}{N}QO$$2AL = (KV)L$
Ring Sequence Parallel*fresh$2O(p \times hs)L$$P$$\frac{2}{N}A = \frac{1}{N}QO$$\frac{2}{N}A = \frac{1}{N}KV$
Ulysses Sequence Parallelfresh$\frac{4}{N}O(p \times hs)L$$P$$\frac{2}{N}A = \frac{1}{N}QO$$\frac{2}{N}A = \frac{1}{N}KV$
PipeFusion*stale-$2O(p \times hs)$$\frac{1}{N}P$$\frac{2}{M}A = \frac{1}{M}QO$$\frac{2L}{N}A = \frac{1}{N}(KV)L$
<h4 id="PipeFusion">1.1. PipeFusion</h4>

PipeFusion: Displaced Patch Pipeline Parallelism for Diffusion Models

<h4 id="USP">1.2. USP: Unified Sequence Parallelism</h4>

USP: A Unified Sequence Parallelism Approach for Long Context Generative AI

<h4 id="hybrid_parallel">1.3. Hybrid Parallel</h4>

Hybrid Parallelism

<h4 id="cfg_parallel">1.4. CFG Parallel</h4>

CFG Parallel

<h4 id="parallel_vae">1.5. Parallel VAE</h4>

Patch Parallel VAE

<h3 id="1gpuacc">Single GPU Acceleration</h3> <h4 id="compilation">Compilation Acceleration</h4>

We utilize two compilation acceleration techniques, torch.compile and onediff, to enhance runtime speed on GPUs. These compilation accelerations are used in conjunction with parallelization methods.

We employ the nexfort backend of onediff. Please install it before use:

pip install onediff
pip install -U nexfort

For usage instructions, refer to the example/run.sh. Simply append --use_torch_compile or --use_onediff to your command. Note that these options are mutually exclusive, and their performance varies across different scenarios.

<h4 id="dittfastattn">DiTFastAttn</h4>

xDiT also provides DiTFastAttn for single GPU acceleration. It can reduce computation cost of attention layer by leveraging redundancies between different steps of the Diffusion Model.

DiTFastAttn: Attention Compression for Diffusion Transformer Models

<h2 id="history">🚧 History and Looking for Contributions</h2>

We conducted a major upgrade of this project in August 2024, introducing a new set of APIs that are now the preferred choice for all users.

The legacy APIs are applied in early stage of xDiT to explore and compare different parallelization methods. They are located in the legacy branch, are now considered outdated and do not support hybrid parallelism. Despite this limitation, they offer a broader range of individual parallelization methods, including PipeFusion, Sequence Parallel, DistriFusion, and Tensor Parallel.

For users working with Pixart models, you can still run the examples in the scripts/ directory under the legacy branch. However, for all other models, we strongly recommend adopting the formal APIs to ensure optimal performance and compatibility.

We also warmly welcome developers to join us in enhancing the project. If you have ideas for new features or models, please share them in our issues. Your contributions are invaluable in driving the project forward and ensuring it meets the needs of the community.

<h2 id="cite-us">πŸ“ Cite Us</h2>

xDiT: an Inference Engine for Diffusion Transformers (DiTs) with Massive Parallelism

@article{fang2024xdit,
  title={xDiT: an Inference Engine for Diffusion Transformers (DiTs) with Massive Parallelism},
  author={Fang, Jiarui and Pan, Jinzhe and Sun, Xibo and Li, Aoyu and Wang, Jiannan},
  journal={arXiv preprint arXiv:2411.01738},
  year={2024}
}

PipeFusion: Patch-level Pipeline Parallelism for Diffusion Transformers Inference

@article{fang2024pipefusion,
  title={PipeFusion: Patch-level Pipeline Parallelism for Diffusion Transformers Inference},
  author={Jiarui Fang and Jinzhe Pan and Jiannan Wang and Aoyu Li and Xibo Sun},
  journal={arXiv preprint arXiv:2405.14430},
  year={2024}
}

USP: A Unified Sequence Parallelism Approach for Long Context Generative AI

@article{fang2024unified,
  title={A Unified Sequence Parallelism Approach for Long Context Generative AI},
  author={Fang, Jiarui and Zhao, Shangchun},
  journal={arXiv preprint arXiv:2405.07719},
  year={2024}
}

Unveiling Redundancy in Diffusion Transformers (DiTs): A Systematic Study

@article{sun2024unveiling,
  title={Unveiling Redundancy in Diffusion Transformers (DiTs): A Systematic Study},
  author={Sun, Xibo and Fang, Jiarui and Li, Aoyu and Pan, Jinzhe},
  journal={arXiv preprint arXiv:2411.13588},
  year={2024}
}