Skip to content

Add Sparse VideoGen attention for Wan on TPUs - #480

Merged
copybara-service[bot] merged 1 commit into
AI-Hypercomputer:mainfrom
Ravi-VK:svg-head-local-pr-clean
Sep 24, 2026
Merged

copybara-service[bot] merged 1 commit into
AI-Hypercomputer:mainfrom
Ravi-VK:svg-head-local-pr-clean

Conversation

@Ravi-VK

@Ravi-VK Ravi-VK commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Video diffusion processes long sequences of video tokens across many denoising steps, making self-attention expensive. Sparse attention reduces this cost by skipping interactions that contribute little to the output.

This PR introduces an opt-in sparse attention implementation for MaxDiffusion: Sparse VideoGen (SVG) for Wan on TPUs. SVG samples a few queries to choose a spatial or temporal pattern for each head, then computes only the selected mask. Density and active steps/layers are configurable, and SVG is disabled by default.

See SVG.md for an illustrated explanation, configuration examples, and profiling instructions.

The implementation rounds sparse boundaries to hardware tiles, approximately preserving the attention-pair budget. Selected interior tiles share one mask-free kernel; only tiles touching sequence padding need validity masks.

The feature is organized into six commits:

  1. Kernel primitives: compute attention over selected ranges of tiles.
  2. Tile rounding and dispatch: select boundary tiles and combine main and padding-tail results.
  3. Routing and layouts: choose each head’s pattern and arrange tokens for efficient execution.
  4. Wan integration: connect SVG to inference with configuration controls and unsupported-mode checks.
  5. Tests: cover routing, layouts, configuration, sharding, and kernel correctness.
  6. Documentation: explain the method, usage, profiling, and results with illustrations.

A review follow-up adds configuration and shape guards, a static inactive-step shortcut, and regression tests.

Wan2.2 720p results on TPU v6e-8, using 81 frames and 40 denoising steps:

Policy Estimated transformer FLOPs saved Denoising speedup PSNR (dB)
Conservative ≈27.2% 1.13× 26.47
Moderate ≈32.2% 1.20× 26.14
Aggressive ≈37.3% 1.28× 24.80

Timings are medians of three warm runs against same-node optimized fixed-M dense controls. PSNR uses FFmpeg’s aggregate YUV metric against dense outputs.

In an earlier evaluation using the moderate SVG policy, Wan2.2 at 720p retained 98.1% of the dense baseline’s mean VBench dimension score across a 31-prompt, 16-dimension screening subset.

SVG supports inference through the custom Ulysses/ring backends; training, Animate, external masks, periodic support, chunked Ulysses, CFG cache, and MagCache are unsupported.

@Ravi-VK
Ravi-VK requested a review from entrpn as a code owner September 15, 2026 03:07

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements Sparse VideoGen (SVG) attention on TPUs for Wan in MaxDiffusion, introducing modules for routing, token placement, and kernel execution. The review feedback highlights several critical issues to address: potential TypeError and AttributeError crashes in the pipeline and attention configuration logic, possible NaN propagation bugs in the attention kernels when all keys are masked out, a missing divisibility check in the Pallas partial kernel, and opportunities to optimize compilation by statically checking step_index.

Comment thread src/maxdiffusion/models/attention_flax.py
Comment thread src/maxdiffusion/kernels/custom_svg_balanced_rounding_attention.py
Comment thread src/maxdiffusion/kernels/custom_svg_static_range_attention.py
Comment thread src/maxdiffusion/kernels/custom_svg_balanced_rounding_partial.py
Comment thread src/maxdiffusion/models/wan/transformers/svg_attention.py Outdated
Comment thread src/maxdiffusion/pipelines/wan/wan_pipeline_2_2.py Outdated
Comment thread src/maxdiffusion/pipelines/wan/wan_pipeline.py Outdated

@Perseus14 Perseus14 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some minor comments. PTAL

Comment thread src/maxdiffusion/kernels/custom_svg_balanced_rounding_attention.py
Comment thread src/maxdiffusion/pipelines/wan/wan_pipeline_2_2.py Outdated
Comment thread src/maxdiffusion/models/wan/transformers/svg_attention.py Outdated
Comment thread src/maxdiffusion/models/wan/transformers/svg_attention.py Outdated
Perseus14
Perseus14 previously approved these changes Sep 21, 2026
@Perseus14

Copy link
Copy Markdown
Collaborator

@Ravi-VK Please squash the commits

Comment thread docs/svg.md
@Ravi-VK
Ravi-VK force-pushed the svg-head-local-pr-clean branch from 446bd95 to 74f46de Compare September 22, 2026 11:39
@Ravi-VK
Ravi-VK force-pushed the svg-head-local-pr-clean branch from 74f46de to fa7c1ba Compare September 22, 2026 12:10
Implement opt-in SVG attention for Wan inference with per-head spatial and temporal routing, head-local token layouts, and TPU sparse-attention kernels. Round sparse support to hardware tiles while preserving the approximate pair budget, retain valid support for every query, and use a padding-only cleanup path for sequence tails.

Add configuration and scheduling controls, distributed inference integration, cache and unsupported-mode guards, AOT cache identity handling, documentation, and CPU/TPU regression coverage.
@Ravi-VK
Ravi-VK force-pushed the svg-head-local-pr-clean branch from fa7c1ba to db8c0cd Compare September 22, 2026 12:46
@copybara-service
copybara-service Bot merged commit 389fff5 into AI-Hypercomputer:main Sep 24, 2026
15 of 16 checks passed
jitendra-jalwaniya added a commit that referenced this pull request Oct 12, 2026
Add docs/images/svg/{head-patterns,attention-tiles,dense-vs-svg}.png, taken unchanged from PR #480 (db8c0cd). The #480 merge left out docs/svg.md and these images; this PR adds docs/svg.md, so its figures come with it.
jitendra-jalwaniya added a commit that referenced this pull request Oct 12, 2026
Expose Sparse VideoGen (SVG) attention for LTX-2 end to end. SVG is off
by default and only active when use_svg_attention is set and
svg_spatial_density < 1.0 (svg_attention_enabled()).

- ltx2_pipeline.py: pass the denoising step index (svg_step_index)
  through both the scanned and unscanned diffusion loops. Only add svg_*
  keys to the transformer attention_config when SVG is active; otherwise
  pass {"use_svg_attention": False}, so unused SVG settings never reach
  the model graph. Reject SVG combined with CFG cache or MagCache only
  when SVG is active. svg_num_layers defaults to 48.
- ltx2_video.yml, ltx2_3_video.yml: add all SVG options (disabled by
  default) and svg_flash_block_sizes for the sparse kernel tiling, and
  expose enable_vae_tiling / enable_vae_slicing (already read by
  generate_ltx2.py) for command-line override. The Wan-only
  svg_{high,low}_noise_density settings are not used by LTX-2.
- generate_ltx2.py: key the AOT cache on SVG settings, including those in
  the transformer attention_config, only when SVG is active.
- docs/svg.md: SVG-on-TPU documentation for Wan and LTX-2, with its
  figures (docs/images/svg/, taken unchanged from PR #480), a copyable
  LTX-2 example and a note on profiler_steps vs the SVG window.
- Tests: config propagation, density-1.0 fallback, svg_step_index
  forwarding, svg_attention_enabled(), AOT cache identity, and a
  real-model check that the AOT graph signature is identical across
  unused svg_* overrides and at svg_spatial_density=1.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants