Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,28 @@ We added ring attention support for Wan models. Below are the stats for one `720

(* There are some known stability issues for ring attention on 16 TPUs, please use `tokamax_flash` attention instead.)

### FlyWheel Attention
`attention=flywheel` runs Wan self-attention on the [FlyWheel](https://github.com/heavyball-research/flywheel-tpu) kernel instead of Splash, with the same sharding as `attention=flash`. `attention=ulysses_flywheel` runs the same kernel with the head sharding of `attention=ulysses` (requires `ici_context_parallelism` > 1 and heads divisible by it). Cross-attention, RoPE and QK-norm are unchanged and stay on Splash/XLA.

FlyWheel supports TPU v6e and tpu7x, and it is inference-only: the kernel has no backward pass, so do not use it for training. Inputs it does not support (attention masks, causal attention, GQA, non-bfloat16 q/k/v) raise an error instead of falling back to another kernel.

```bash
pip install flywheel-tpu # or: pip install -e ".[flywheel]"
python src/maxdiffusion/generate_wan.py \
src/maxdiffusion/configs/base_wan_27b.yml \
attention="flywheel" \
...
```

Wan 2.1 T2V 1.3B, `480x832`, 81 frames, 30 inference steps, CFG, seed 0, one v6e chip. Splash uses the faster of two v6e tilings we tried (`block_q`/`block_kv`/`block_kv_compute` 2048/4096/1024; 3024/2048/1024 took 65.1 s). Times are the average of two runs, which differed by less than 0.2%.

| Accelerator | Model | Attention Type | Denoise | e2e Generation Time |
| -- | -- | -- | -- | -- |
| v6e-1 | WAN 2.1 1.3B | Splash (`flash`) | 58.8 s | 63.3 s |
| v6e-1 | WAN 2.1 1.3B | FlyWheel (`flywheel`) | **49.5 s** | **54.0 s (1.17x)** |

The FlyWheel video differs from the Splash video by 25.0 dB PSNR, about the same as changing only the Splash tiling (25.1 dB between the 512 and 2048/4096/1024 tilings).

### Automatic Tile-Size Search

The optimal attention tile sizes (`block_q` / `block_kv`) depend on the sequence length, VMEM, sharding, and accelerator, and the feasibility edge is a VMEM OOM with no clean closed form — so we tune them empirically. Passing `enable_tile_search=true` to `generate_wan.py` runs a fast one-DiT-block grid search before inference and injects the winning block sizes into `flash_block_sizes` (`tile_search_mode=smart` by default; the search is opt-in and off by default). The core is model-agnostic (`utils/tile_size_grid_search.py`) with a per-model plug (`utils/wan_block_benchmark.py`), which also runs standalone via `python -m maxdiffusion.utils.wan_block_benchmark ... --smart-search`.
Expand Down
2 changes: 2 additions & 0 deletions end_to_end/tpu/run_wan_stack_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ TESTS=(
"$T/cross_attention_pallas_test.py"
# Lane sequence padding (feat/wan-fast-inference-optimizations)
"$T/wan_seq_pad_test.py"
# FlyWheel self-attention backend (needs flywheel-tpu on v6e / tpu7x for the numerical tests)
"$T/flywheel_attention_test.py"
)
PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}" exec "${PYTHON:-python3}" -m pytest -q -rs "${TESTS[@]}" "$@"
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ classifiers = [
quality = ["urllib3", "black", "isort", "ruff", "hf-doc-builder"]
docs = ["hf-doc-builder"]
training = ["accelerate", "datasets", "protobuf", "tensorboard", "Jinja2"]
# attention=flywheel / ulysses_flywheel (Wan inference on TPU v6e and tpu7x).
flywheel = ["flywheel-tpu>=0.4.1"]
test = [
"compel",
"datasets",
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_14b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ svg_low_noise_density: -1.0
# compute blocks and the sparse kernel for small ones. Must stay a dict so the
# command line can override it with JSON.
svg_flash_block_sizes: {}
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir

use_base2_exp: True
use_experimental_scheduler: True
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_1_3b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ svg_low_noise_density: -1.0
# compute blocks and the sparse kernel for small ones. Must stay a dict so the
# command line can override it with JSON.
svg_flash_block_sizes: {}
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring

use_base2_exp: True
use_experimental_scheduler: True
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_27b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ svg_low_noise_density: -1.0
# {"block_q":3328,"block_kv":2816,"block_kv_compute":256,
# "block_kv_compute_in":256,"heads_per_tile":1,"vmem_limit_bytes":67108864}.
svg_flash_block_sizes: {}
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir


#
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_animate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jit_initializers: True
# Set true to load weights from pytorch
from_pt: True
split_head_dim: False
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
use_base2_exp: True
use_experimental_scheduler: True
# use_k_centering: "auto" turns K-centering on for the non-ring
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_i2v_14b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jit_initializers: True
# Set true to load weights from pytorch
from_pt: True
split_head_dim: False
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
use_base2_exp: True
use_experimental_scheduler: True
# use_k_centering: "auto" turns K-centering on for the non-ring
Expand Down
2 changes: 1 addition & 1 deletion src/maxdiffusion/configs/base_wan_i2v_27b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jit_initializers: True
# Set true to load weights from pytorch
from_pt: True
split_head_dim: False
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
attention: 'flash' # Supported attention: dot_product, flash, flywheel, ulysses_flywheel, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block
use_base2_exp: True
use_experimental_scheduler: True
# use_k_centering: "auto" turns K-centering on for the non-ring
Expand Down
162 changes: 160 additions & 2 deletions src/maxdiffusion/models/attention_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,118 @@ def ring_scan_body(carry, _):
return x


# ---------------------------------------------------------------------------
# FlyWheel attention (https://github.com/heavyball-research/flywheel-tpu)
# ---------------------------------------------------------------------------

FLYWHEEL_KERNELS = ("flywheel", "ulysses_flywheel")


def _flywheel_attention(
query: jax.Array,
key: jax.Array,
value: jax.Array,
heads: int,
mesh: Mesh,
axis_names_q: AxisNames,
axis_names_kv: AxisNames,
scale: float,
use_ulysses: bool = False,
attention_mask: jax.Array = None,
is_causal: bool = False,
kv_heads: Optional[int] = None,
ulysses_shards: int = -1,
ulysses_attention_chunks: int = 1,
) -> jax.Array:
"""FlyWheel `flash_attn_varlen_func` self-attention.

`use_ulysses=False` keeps the `flash` sharding (sequence-sharded q, full K/V);
`use_ulysses=True` keeps the `ulysses` sharding (all-to-all around the kernel).
Each device makes one varlen call with cu_seqlens = [0, length], so K/V padding
is never attended. Forward-only and bfloat16-only; unsupported inputs raise.
"""
kernel_name = "ulysses_flywheel" if use_ulysses else "flywheel"
if attention_mask is not None:
raise NotImplementedError(f"{kernel_name} does not support attention_mask.")
if is_causal:
raise NotImplementedError(f"{kernel_name} supports full (non-causal) attention only.")
if kv_heads is not None and kv_heads != heads:
raise NotImplementedError(f"{kernel_name} does not support GQA (got heads={heads}, kv_heads={kv_heads}).")
for name, tensor in (("query", query), ("key", key), ("value", value)):
if tensor.dtype != jnp.bfloat16:
raise TypeError(f"{kernel_name} requires bfloat16 inputs, got {name}.dtype={tensor.dtype}.")

num_shards = mesh.shape[CONTEXT] if CONTEXT in mesh.shape else 1
q_axis_names = nn.logical_to_mesh_axes(axis_names_q)
kv_axis_names = nn.logical_to_mesh_axes(axis_names_kv)
if use_ulysses:
_validate_implicit_ulysses_degree(ulysses_shards, num_shards, kernel_name)
if heads % num_shards != 0:
raise ValueError(
f"{kernel_name} requires the number of heads to be divisible by the context shard count, "
f"got heads={heads} and context_shards={num_shards}."
)
elif num_shards > 1 and _mesh_axis_in_spec(kv_axis_names[2], CONTEXT):
raise ValueError(
f"{kernel_name} needs the full K/V sequence on every device, but the K/V sequence axis is "
f"sharded over '{CONTEXT}'. Use ulysses_flywheel for sequence-sharded K/V."
)

query, orig_q_seq_len = _reshape_data_for_flash(query, heads, num_shards)

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.

When use_ulysses=False and attention_sharding_uniform=False (base_wan_*.yml maps activation_self_attn_heads to ('context', 'tensor') and leaves SELF_ATTN_Q_LENGTH unsharded unless pyconfig.py adds SEQUENCE_PARALLEL_AXIS_RULES), q_axis_names[2] is None while q_axis_names[1] shards heads across context. Passing num_shards unconditionally here pads an unsharded sequence axis on query, and jax.shard_map below fails with an opaque axis-divisibility ValueError whenever heads % context != 0 (e.g., Wan 2.1 1.3B heads=12 on context=8).

Please check _mesh_axis_in_spec(q_axis_names[1], CONTEXT) up front on the use_ulysses=False path (raising a clear ValueError pointing to attention_sharding_uniform=True or ulysses_flywheel), or gate query sequence padding on _mesh_axis_in_spec(q_axis_names[2], CONTEXT).

num_shards_kv = num_shards if use_ulysses else 1
key, orig_kv_seq_len = _reshape_data_for_flash(key, heads, num_shards_kv)
value, _ = _reshape_data_for_flash(value, heads, num_shards_kv)

try:
# Optional dependency, imported only when the kernel is selected.
from flywheel_tpu import flash_attn_varlen_func # pylint: disable=import-outside-toplevel
except ImportError as e:
raise ImportError(f"{kernel_name} requires the flywheel-tpu package (pip install flywheel-tpu).") from e

def local_attention(q, k, v):
if use_ulysses:
# [b, h, S/N, d] -> [b, h/N, S, d]: give up heads, gather the sequence.
q = jax.lax.all_to_all(q, axis_name=CONTEXT, split_axis=1, concat_axis=2, tiled=True)

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.

Perf note for v6e-4 (DP=1, CP=4) with CFG (B=2): running the 4D all_to_all on [b, h, S/N, d] with b=2 before folding b * h on line 1337 causes XLA on TPU to place the size-2 batch dimension in tile sublanes (T(2, 128) instead of T(8, 128)), adding expensive relayouts around the collectives inside the scanned layers.

See _ulysses_attention on current main (fold_batch when batch > 1, devices_in_batch_sharding == 1, and (batch * heads) % num_shards == 0), which reshapes [B, H, S, D] -> [1, B*H, S, D] outside shard_map before the Ulysses all_to_all.

k = jax.lax.all_to_all(k, axis_name=CONTEXT, split_axis=1, concat_axis=2, tiled=True)
v = jax.lax.all_to_all(v, axis_name=CONTEXT, split_axis=1, concat_axis=2, tiled=True)
q_len = orig_q_seq_len
else:
# Local query shard; its padding rows are computed and trimmed below.
q_len = q.shape[2]
b, h, s_q, d = q.shape
s_kv = k.shape[2]
out = flash_attn_varlen_func(
q.reshape(b * h, s_q, d),
k.reshape(b * h, s_kv, d),
v.reshape(b * h, s_kv, v.shape[-1]),
jnp.array([0, q_len], dtype=jnp.int32),
jnp.array([0, orig_kv_seq_len], dtype=jnp.int32),
max_seqlen_q=s_q,
max_seqlen_k=s_kv,
softmax_scale=float(scale),
causal=False,
)
out = out.reshape(b, h, s_q, v.shape[-1])
if use_ulysses:
# [b, h/N, S, d] -> [b, h, S/N, d]
out = jax.lax.all_to_all(out, axis_name=CONTEXT, split_axis=2, concat_axis=1, tiled=True)
return out

sharded_attention = jax.shard_map(
local_attention,
mesh=mesh,
in_specs=(q_axis_names, kv_axis_names, kv_axis_names),
out_specs=q_axis_names,
check_vma=False,
)
if use_ulysses:
x = _run_chunked_ulysses_attention(query, key, value, heads, num_shards, ulysses_attention_chunks, sharded_attention)
else:
x = sharded_attention(query, key, value)
x = x[:, :, :orig_q_seq_len, :]
return _reshape_heads_to_head_dim(x)


# ---------------------------------------------------------------------------
# Ulysses sequence-parallel attention
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3154,6 +3266,44 @@ def flash_kernel(q, k, v, context):
)


@register_kernel("flywheel")
def flywheel_kernel(q, k, v, context):

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.

NNXAttentionOp.apply_attention passes token_padding in context (from wan_seq_pad='lane' on main), and every registered kernel outside TOKEN_PADDING_KERNELS forwards token_padding=context.get('token_padding') and calls _reject_token_padding(...) (matching flash_kernel, tokamax_flash_kernel, and wan_seq_pad_test.py).

Right now both flywheel_kernel and ulysses_flywheel_kernel drop context.get('token_padding'), so a direct kernel call with active TokenPadding passes the padded orig_kv_seq_len into cu_seqlens_k and silently attends to tail pad tokens. Please thread token_padding=context.get('token_padding') into both wrappers and either reject it via _reject_token_padding or validate _resolve_token_padding(token_padding, orig_kv_seq_len, num_segments=1, q_seq_len=orig_q_seq_len) and use token_padding.real_len for cu_seqlens_k.

return _flywheel_attention(
q,
k,
v,
context["heads"],
context["mesh"],
context["axis_names_q"],
context["axis_names_kv"],
# K may arrive already multiplied by the softmax scale (as attention="flash" accepts).
scale=1.0 if context.get("k_prescaled", False) else context["scale"],
attention_mask=context["attention_mask"],
is_causal=context.get("is_causal", False),
kv_heads=context.get("kv_heads", None),
)


@register_kernel("ulysses_flywheel")
def ulysses_flywheel_kernel(q, k, v, context):
return _flywheel_attention(
q,
k,
v,
context["heads"],
context["mesh"],
context["axis_names_q"],
context["axis_names_kv"],
scale=1.0 if context.get("k_prescaled", False) else context["scale"],
use_ulysses=True,
attention_mask=context["attention_mask"],
is_causal=context.get("is_causal", False),
kv_heads=context.get("kv_heads", None),
ulysses_shards=context.get("ulysses_shards", -1),
ulysses_attention_chunks=context.get("ulysses_attention_chunks", 1),
)


@register_kernel("tokamax_flash")
def tokamax_flash_kernel(q, k, v, context):
return _tpu_flash_attention(
Expand Down Expand Up @@ -3259,6 +3409,8 @@ def cudnn_flash_te_kernel(q, k, v, context):
_FLASH_MIN_SEQ_LENGTH_KERNELS = frozenset({
"flash",
"tokamax_flash",
"flywheel",
"ulysses_flywheel",
"ulysses",
"ulysses_custom",
"ulysses_custom_fixed_m",
Expand Down Expand Up @@ -4185,7 +4337,7 @@ def _wan_opt(name):
self.wan_cross_attn_kernel = _wan_opt("wan_cross_attn_kernel")
self.wan_cross_attn_cpu_interpret = bool(attention_config["wan_cross_attn_cpu_interpret"])

if attention_kernel in {"flash", "cudnn_flash_te"} and mesh is None:
if attention_kernel in {"flash", "cudnn_flash_te", *FLYWHEEL_KERNELS} and mesh is None:
raise ValueError(f"The flash attention kernel requires a value for mesh, but mesh is {self.mesh}")
self.dim_head = dim_head
self.heads = heads
Expand All @@ -4212,7 +4364,8 @@ def _wan_opt(name):
"ulysses_custom_fixed_m_per_q_block",
)
cross_attention_uses_local_kv = not is_self_attention and (
cross_attention_remapped_to_flash or attention_kernel in ("flash", "tokamax_flash", "cudnn_flash_te")
cross_attention_remapped_to_flash
or attention_kernel in ("flash", "tokamax_flash", "cudnn_flash_te", *FLYWHEEL_KERNELS)
)
if is_self_attention:
axis_names_q = (BATCH, SELF_ATTN_HEAD, SELF_ATTN_Q_LENGTH, D_KV)
Expand All @@ -4227,6 +4380,11 @@ def _wan_opt(name):
)
if cross_attention_remapped_to_flash:
attention_kernel = "tokamax_flash"
elif not is_self_attention and attention_kernel in FLYWHEEL_KERNELS:
# FlyWheel replaces self-attention only. Cross-attention (video queries x
# text keys, and the I2V image mask) takes the flash path, which
# `wan_cross_attn_kernel` may still swap for its Pallas kernel.
attention_kernel = "flash"
if not is_self_attention and self.wan_cross_attn_kernel == "pallas":
is_tpu = mesh is not None and all(d.platform == "tpu" for d in mesh.devices.flat)
is_cpu_interpret = bool(self.wan_cross_attn_cpu_interpret)
Expand Down
Loading