Skip to content

Context Parallelism

Sequence/Context Parallelism

cache-dit is compatible with context parallelism. Currently, we support the use of Hybrid Cache + Context Parallelism scheme (via CACHE_DIT parallelism backend) in cache-dit. Users can use Context Parallelism to further accelerate the speed of inference. Legacy configs that still use NATIVE_DIFFUSER are still accepted and will be automatically converted to CACHE_DIT. For more details, please refer to 📚examples. Currently, cache-dit supported context parallelism for FLUX.1, 🔥FLUX.2, Qwen-Image, 🔥Qwen-Image 2.1, Qwen-Image-Lightning, LTXVideo, Wan 2.1, Wan 2.2, 🔥Wan-Animate-2, HunyuanImage-2.1, HunyuanVideo, CogVideoX 1.0, CogVideoX 1.5, CogView 3/4 and VisualCloze, etc. cache-dit will support more models in the future.

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter, 
  cache_config=DBCacheConfig(...),
  # Set ulysses_size > 1 to enable ulysses style context parallelism.
  parallelism_config=ParallelismConfig(ulysses_size=2),
)

For Sol-Attn, use attention_backend="sol_attn" only with pure Ulysses context parallelism. Ulysses CP2/CP4 is supported because every rank sees the full sequence for its local heads. Ring and USP with Ring are explicitly rejected: rank-local thresholds and the missing LSE contract would not reproduce full-sequence Sol-Attn. Use another backend for Ring workloads.

L20x1 Ulysses-2 Ulysses-4 + compile
FLUX, 23.56s 13.80s 8.28s 7.27s

UAA: Ulysses Anything Attention

Any Sequence Length: We have implemented the UAA: Ulysses Anything Attention: An Ulysses Attention that supports arbitrary sequence length with zero padding and nearly zero theoretical communication overhead. The default Ulysses Attention requires that the sequence len of hidden states must be divisible by the number of devices. This imposes significant limitations on the practical application of Ulysses.

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter, 
  cache_config=DBCacheConfig(...),
  # Set `ulysses_anything` as True to enable UAA
  parallelism_config=ParallelismConfig(
    ulysses_size=2,
    ulysses_anything=True,
  ),
)

For example, in the T2I and I2V tasks, the length of prompts input by users is often variable, and it is difficult to ensure that this length is divisible by the number of devices. To address this issue, we have developed a padding-free Ulysses Attention (UAA) for arbitrary sequence length, which enhances the versatility of Ulysses.

dist.init_process_group(backend="cpu:gloo,cuda:nccl")
Compared to Ulysses Attention, in UAA, we have only added an extra all-gather op for scalar types to gather the seq_len value of each rank. To avoid multiple forced CUDA sync caused by H2D and D2H transfers, please add the gloo backend in init_process_group. This will significantly reduce communication latency.

Any Sequence Length
U*: Ulysses Attention, UAA: Ulysses Anything Attenton, UAA*: UAA + Gloo, Device: NVIDIA L20
FLUX.1-Dev w/o CPU Offload, 28 steps; Qwen-Image w/ CPU Offload, 50 steps; Gloo: Extra All Gather w/ Gloo

CP2 w/ U* CP2 w/ UAA* CP2 w/ UAA L20x1 CP2 w/ UAA* CP2 w/ U* L20x1 CP2 w/ UAA*
FLUX, 13.87s 🎉13.88s 14.75s 23.25s 🎉13.75s Qwen, 132s 181s 🎉133s
1024x1024 1024x1024 1024x1024 1008x1008 1008x1008 1312x1312 1328x1328 1328x1328
✔️U* ✔️UAA ✔️U* ✔️UAA ✔️U* ✔️UAA NO CP ❌U* ✔️UAA ✔️U* ✔️UAA NO CP ❌U* ✔️UAA

Any Head Num: By the way, Ulysses Attention and UAA in cache-dit now support arbitrary numbers of heads via additional padding and unpadding operations implemented before and after all-to-all. The overhead incurred by these extra padding and unpadding steps can be partially hidden through asynchronous communication. This support for arbitrary head counts is automatically activated whenever the number of heads is not divisible by the world size. For Example:

Any Head Num
Ulysses: Ulysses Attention, FP8 Ulysses: Ulysses w/ FP8 All2All, Device: NVIDIA L20
🔥Z-Image (Head=30, ❌CAN NOT divisible by 4), 1024x1024, 9 steps.

Ulysses 2, L20 Ulysses 4 FP8 Ulysses 4 + Cache + FP8 DQ
1024x1024, 3.19s 1024x1024, 1.98s 1024x1024, 1.89s 1024x1024, 1.63s 1024x1024, 1.23s
zimage C1_Q0_NONE_Ulysses2_sdpa_cudnn zimage C1_Q0_NONE_Ulysses4_sdpa_cudnn zimage C1_Q0_NONE_Ulysses4_ulysses_float8_sdpa_cudnn zimage C1_Q0_DBCache_F1B0_W4I1M0MC0_R0 6_SCM111110101_dynamic_CFG0_T0O0_Ulysses4_S2_ulysses_float8_sdpa_cudnn zimage C1_Q1_float8_DBCache_F1B0_W4I1M0MC0_R0 6_SCM111110101_dynamic_CFG0_T0O0_Ulysses4_S2_ulysses_float8_sdpa_cudnn

We have also implemented a padding-free version that support any head num. Please be informed that this solution cannot be used when seq len is not divisible by world size. Users can enable this feature through environment variables:

export CACHE_DIT_UNEVEN_HEADS_COMM_NO_PAD=1 # NOT WORK if seq len is also not divisible by world size

Important: Please note that Ulysses Anything Attention (UAA) is currently an experimental feature. It has not undergone large-scale testing, and may introduce a slight performance degradation while the cpu:gloo commucation backend is not available.

Async Ulysses QKV Projection

async_ulysses

Inspired by ByteDance-Seed/VeOmni: Async Ulysses CP, we have also added support for Async Ulysses QKV Projection for certain models in cache-dit. This enables partial overlap of communication and computation, which can further enhance the performance of Ulysses style Context Parallelism. Currently, only the 🔥FLUX.1, 🔥Qwen-Image, 🔥Z-Image, 🔥Ovis-Image, 🔥FLUX.2 and 🔥LongCat-Image models are supported, and more models will be added in the future—stay tuned!

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter, 
  cache_config=DBCacheConfig(...),
  # Set `ulysses_async` as True to enable Async Ulysses QKV Projection.
  parallelism_config=ParallelismConfig(
    ulysses_size=2,
    ulysses_async=True,
  ),
)

Ulysses: Standard Ulysses Attention, Async Ulysses: Ulysses Attention with Async QKV Projection

L20x2 w/ Ulysses w/ Async Ulysses w/ Ulysses + compile w/ Async Ulysses + compile
FLUX.1, 13.87s 🎉13.20s 12.21s 🎉11.97s

Async FP4/FP6/FP8 Ulysses Attention

async_ulysses_fp8

cache-dit supports Async FP4/FP6/FP8 Ulysses Attention for supported DiTs. Low-precision communication is configured only by quant_communicate_kwargs; an empty dictionary keeps the default BF16 path. FP4 E2M1, FP6 E2M3, and FP8 E4M3 all default to per_group_16. FP6/FP8 also accept explicit per_token and per_group_32; FP4 accepts only per_group_16. When communication quantization is enabled, Q/K/V/O use the quantized path by default. Set quantize_k=False to keep K in BF16 and protect the numerically sensitive Softmax(Q@K^T) computation; LSE always remains BF16. The fused O path requires the global head count and global sequence length to be divisible by ulysses_size; disable communication quantization for unsupported shapes.

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter,
  cache_config=DBCacheConfig(...),
  parallelism_config=ParallelismConfig(
    ulysses_size=2,
    quant_communicate_kwargs={
      "bits": 8,                       # 8=FP8, 6=packed FP6 E2M3, 4=packed FP4 E2M1
      "quant_type": "per_group_16",   # per_token / per_group_16 / per_group_32 (FP4: G16 only)
      "quantize_k": False,             # Optional: keep K in BF16 (defaults to True).
    },
    # FP6 with the unified default group-16 granularity can be written simply as:
    # quant_communicate_kwargs={"bits": 6},
    # FP4 with its default (and only) group-16 granularity:
    # quant_communicate_kwargs={"bits": 4},
  ),
)

The CLI uses the same keys in one comma-separated option, for example --quant-communicate-config 'bits=8,quant_type=per_group_16', --quant-communicate-config 'bits=6', or --quant-communicate-config 'bits=4'. The old --ulysses-float8* options are removed. Communication quantization requires Ulysses parallelism with ulysses_size > 1.

FP8 rows contain D payload bytes followed by ceil(D/G) scales. FP6 uses native E2M3 conversion and packs four 6-bit codes into three bytes inside a uint8 transport tensor; its payload and total row width are aligned to four bytes. FP4 uses native E2M1 conversion and packs two 4-bit codes per byte (even element in the low nibble), with ceil(D/2) payload bytes and one scale per 16 elements. All formats zero alignment padding, support a final partial group, and choose the smallest row width satisfying the 16-byte alignment rule for every per-rank chunk. QKV retains FP32 scales and epsilon 1e-6; O and the generic path retain BF16 scales and epsilon 1e-4. FP4/FP6 require sm100+ native conversion support and have been validated on sm120a. Lower bit widths reduce payload traffic but add pack/unpack work and quantization error, so evaluate both end-to-end latency and accuracy on the target model.

On PCIe-only multi-socket machines, cache-dit emits a one-time warning when a CP group spans NUMA nodes through SYS links and no NVLink is detected. This is a diagnostic only: process groups and rank placement are not changed. Topology discovery is fail-open when nvidia-smi is unavailable or its output cannot be parsed.

L20x2 w/ Ulysses w/ Ulysses FP8 w/ Ulysses + compile w/ Ulysses FP8 + compile
FLUX.1, 13.87s 🎉13.36s 12.21s 🎉11.54s

Ring Attention with Batched P2P

Currently, cache-dit support 2 ring_rotate_method, namely, allgather and p2p. allgather: Use allgather to gather the key and value tensors. p2p: Use batch_isend_irecv ops to rotate the key and value tensors (default). This method is more efficient due to th better overlap of communication and computation.

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter, 
  cache_config=DBCacheConfig(...),
  # Set `ring_rotate_method` as 'p2p' to enable the faster ring attention implementation
  parallelism_config=ParallelismConfig(
    ring_size=2,
    ring_rotate_method='p2p', # default is 'p2p'
  ),
)

The FLUX.1-dev benchmark for Ring w/ allgather (AG), Ring w/ batched p2p (p2p) and Ulysses listed as following:

Ring-2 w/ AG Ring-2 w/ p2p Ulysses-2 Ring-4 w/ AG Ring-4 w/ p2p Ulysses-4
14.01s 13.43s 13.87s 9.82s 9.16s 8.22s

USP: Unified Sequence Parallelism

Unified Sequence Parallelism combines Ring Attention and Ulysses Attention into a single approach for efficient long-sequence processing. It applies Ulysses’s all-to-all communication first to redistribute heads and sequence tokens, then uses Ring Attention to process the redistributed data, and finally reverses the all-to-all to restore the original layout.

Ulysses Attention efficiently parallelizes across attention heads. Ring Attention handles very long sequences with minimal memory overhead. Together, they enable 2D parallelization across both heads and sequence dimensions

from cache_dit import ParallelismConfig

cache_dit.enable_cache(
  pipe_or_adapter, 
  cache_config=DBCacheConfig(...),
  # Set both `ulysses_size` and `ring_size` greater than 1
  parallelism_config=ParallelismConfig(
    ulysses_size=2, ring_size=2,
  ),
)

From the below table (FLUX.1-dev), it’s clear that Ulysses provides better throughput, but the number of devices it can use remains limited to the number of attention heads, a limitation that is solved by unified attention.

Ring-4 w/ allgather Ring-4 w/ batched p2p Ulysses-4 USP-2-2
9.82s 9.16s 8.22s 8.55s