Skip to content

[feat] Add Ring Attention support - #1694

Draft
klhhhhh wants to merge 49 commits into
hao-ai-lab:mainfrom
klhhhhh:ring-atten
Draft

[feat] Add Ring Attention support#1694
klhhhhh wants to merge 49 commits into
hao-ai-lab:mainfrom
klhhhhh:ring-atten

Conversation

@klhhhhh

@klhhhhh klhhhhh commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Motivation

Video diffusion models process long spatiotemporal token sequences, making attention increasingly memory- and communication-intensive as video resolution and duration grow. Existing Ulysses sequence parallelism distributes sequence tokens across GPUs by exchanging sequence and attention-head dimensions, but its parallel degree is constrained by the number of attention heads.

This PR adds Ring Attention as an additional sequence-parallel strategy in FastVideo. Ring Attention keeps queries local while circulating sharded keys and values across a Ring process group, allowing every query shard to attend to the complete global sequence without materializing the full sequence on each GPU.

The PR supports both:

  • Pure Ring Attention, where the entire sequence-parallel group forms one Ring group (ring_size == sp_size).

  • Unified Sequence Parallelism (USP), which combines Ulysses and Ring Attention in a two-dimensional process mesh:

    sp_size = ulysses_size × ring_size
    

USP enables more flexible sequence-parallel configurations and avoids requiring the full sequence-parallel degree to be provided exclusively by either Ulysses or Ring Attention.

The implementation integrates Ring Attention with FastVideo's distributed process-group management, attention layer, and local RoPE handling. It also adds tests covering configuration validation, RoPE slicing, Ring subgroup and Ulysses subgroup construction, the single-GPU Ring fallback, blockwise log-sum-exp merging, and two-GPU pure-Ring numerical parity against full FlashAttention.

The current numerical parity test validates the pure-Ring kernel. The hybrid USP path is currently covered by process-group layout tests and multi-GPU integration runs.

@mergify mergify Bot added type: feat New feature or capability scope: inference Inference pipeline, serving, CLI scope: attention Attention backends (VSA, STA, Flash, etc.) scope: infra CI, tests, Docker, build scope: distributed SP, FSDP, USP, multi-node labels Aug 7, 2026
@mergify

mergify Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 1 protections blocking · waiting on 👀 reviews and 🤖 CI

Protection Waiting on
🔴 PR merge requirements 👀 reviews and 🤖 CI

🔴 PR merge requirements

Waiting for

  • #approved-reviews-by>=1
  • check-success=fastcheck-passed
  • check-success=full-suite-passed
  • check-success~=pre-commit
This rule is failing.
  • #approved-reviews-by>=1
  • check-success=fastcheck-passed
  • check-success=full-suite-passed
  • check-success~=pre-commit
  • title~=(?i)^\[(feat|feature|bugfix|fix|refactor|perf|ci|doc|docs|misc|chore|kernel|new.?model|skill|skills|infra)\]

@mergify

mergify Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

1 similar comment
@mergify

mergify Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@mergify

mergify Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase:

git fetch origin main
git rebase origin/main
# Resolve any conflicts, then:
git push --force-with-lease

@mergify mergify Bot added the needs-rebase PR has merge conflicts label Aug 10, 2026
@mergify

mergify Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@mergify mergify Bot removed the needs-rebase PR has merge conflicts label Aug 14, 2026
@mergify

mergify Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

1 similar comment
@mergify

mergify Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@klhhhhh klhhhhh changed the title [feat] Add initial pure Ring Attention support [feat] Add Ring Attention support Aug 20, 2026
@mergify

mergify Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@mergify

mergify Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@klhhhhh

klhhhhh commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

Run ring-atten and usp algorithm successfully.

4-GPU Ring Attention Inference Test

This PR was manually validated with four GPUs using the following sequence-parallel configuration:

  • num_gpus=4
  • sp_size=4
  • ring_size=2
  • Attention backend: FLASH_ATTN
  • FSDP inference: disabled
  • DiT/VAE CPU offloading: disabled
  • Text encoder CPU offloading: enabled

Because ring_size=2 is smaller than sp_size=4, this configuration exercises the hybrid USP/Ring Attention path rather than pure Ring Attention.

Inference example

examples/inference/basic/basic.py was configured as follows:

from fastvideo import VideoGenerator

# from fastvideo.api.sampling_param import SamplingParam

OUTPUT_PATH = "video_samples"


def main():
    # FastVideo will automatically use the optimal default arguments for the
    # model.
    # If a local path is provided, FastVideo will make a best-effort attempt
    # to identify the optimal arguments.
    generator = VideoGenerator.from_pretrained(
        "Wan-AI/Wan2.1-T2V-1.3B-Diffusers",
        # FastVideo will automatically handle distributed setup.
        num_gpus=4,
        sp_size=4,
        ring_size=2,
        use_fsdp_inference=False,
        dit_cpu_offload=False,
        vae_cpu_offload=False,
        text_encoder_cpu_offload=True,
        # Set to False when CPU RAM is limited or an obscure
        # "CUDA error: Invalid argument" is encountered.
        pin_cpu_memory=True,
        # image_encoder_cpu_offload=False,
    )

    # sampling_param = SamplingParam.from_pretrained(
    #     "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
    # )
    # sampling_param.num_frames = 45
    # sampling_param.image_path = (
    #     "https://huggingface.co/datasets/huggingface/"
    #     "documentation-images/resolve/main/diffusers/astronaut.jpg"
    # )

    # Generate a video using the same API regardless of the GPU count.
    prompt = (
        "A curious raccoon peers through a vibrant field of yellow sunflowers, "
        "its eyes wide with interest. The playful yet serene atmosphere is "
        "complemented by soft natural light filtering through the petals. "
        "Mid-shot, warm and cheerful tones."
    )
    generator.generate_video(
        prompt,
        output_path=OUTPUT_PATH,
        save_video=True,
    )

    # Generate another video without reloading the model.
    prompt2 = (
        "A majestic lion strides across the golden savanna, its powerful frame "
        "glistening under the warm afternoon sun. The tall grass ripples gently "
        "in the breeze, enhancing the lion's commanding presence. The tone is "
        "vibrant, embodying the raw energy of the wild. Low angle, steady "
        "tracking shot, cinematic."
    )
    generator.generate_video(
        prompt2,
        output_path=OUTPUT_PATH,
        save_video=True,
    )


if __name__ == "__main__":
    main()

Running script

The following script was used to run the four-GPU inference test:

#!/usr/bin/env bash
set -euo pipefail

cd /FastVideo

export CUDA_VISIBLE_DEVICES=0,1,2,3
export FASTVIDEO_ATTENTION_BACKEND=FLASH_ATTN
export NCCL_DEBUG=INFO
export PYTHONUNBUFFERED=1
export NCCL_CUMEM_ENABLE=0
export NCCL_IB_DISABLE=1
export NCCL_P2P_DISABLE=1
export TORCH_NCCL_ASYNC_ERROR_HANDLING=1

echo "===== GPU information ====="
nvidia-smi \
    --query-gpu=index,name,memory.total \
    --format=csv

echo "===== FastVideo source ====="
python -c 'import fastvideo; print(fastvideo.__file__)'

echo "===== FlashAttention ====="
python -c 'import flash_attn; print("flash_attn:", flash_attn.__version__)'

echo "===== Start 4-GPU USP/Ring Attention inference ====="
echo "num_gpus=4, sp_size=4, ring_size=2"

mkdir -p video_samples logs

python examples/inference/basic/basic.py \
    2>&1 | tee logs/ring_4gpu_pipeline.log

echo "===== Finished ====="
echo "Videos: /FastVideo/video_samples"
echo "Log: /FastVideo/logs/ring_4gpu_pipeline.log"

Run command

chmod +x run_ring_4gpu.sh
./run_ring_4gpu.sh

Outputs

Generated videos:

/FastVideo/video_samples

Execution log:

/FastVideo/logs/ring_4gpu_pipeline.log

@klhhhhh

klhhhhh commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

And the running log is here.

ring_4gpu_pipeline.log

@mergify

mergify Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@mergify

mergify Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase:

git fetch origin main
git rebase origin/main
# Resolve any conflicts, then:
git push --force-with-lease

@mergify mergify Bot added the needs-rebase PR has merge conflicts label Aug 22, 2026
@mergify

mergify Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Pre-commit checks failed

Hi @klhhhhh, the pre-commit checks have failed. To fix them locally:

# Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install

# Run all checks and auto-fix what's possible
pre-commit run --all-files

Common fixes:

  • yapf: yapf -i <file> (formatting)
  • ruff: ruff check --fix <file> (linting)
  • codespell: codespell --write-changes <file> (spelling)

After fixing, commit and push the changes. The checks will re-run automatically.

For future commits, pre-commit will run automatically on changed files before each commit.

@mergify mergify Bot removed the needs-rebase PR has merge conflicts label Aug 23, 2026
@mergify

mergify Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase:

git fetch origin main
git rebase origin/main
# Resolve any conflicts, then:
git push --force-with-lease

@mergify mergify Bot added the needs-rebase PR has merge conflicts label Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: attention Attention backends (VSA, STA, Flash, etc.) scope: distributed SP, FSDP, USP, multi-node scope: inference Inference pipeline, serving, CLI scope: infra CI, tests, Docker, build type: feat New feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants