Skip to content

Commit fce03c6

Browse files
committed
Add explicit Ulysses ring attention sharding with segment-id masking
1 parent 4691a2c commit fce03c6

14 files changed

Lines changed: 671 additions & 127 deletions

src/maxdiffusion/common_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@
9595
[CROSS_ATTN_Q_LENGTH, CONTEXT],
9696
[CROSS_ATTN_KV_LENGTH, CONTEXT],
9797
]
98+
99+
### Common axis rules for 2D Ulysses + ring attention ###
100+
# Public configs shard sequence on `context`; attention code privately reshapes
101+
# that axis into hidden ring and Ulysses axes for the hybrid kernel.
102+
ULYSSES_RING_ATTENTION_AXIS_RULES = [
103+
[SELF_ATTN_HEAD, None],
104+
[SELF_ATTN_Q_LENGTH, CONTEXT],
105+
[SELF_ATTN_KV_LENGTH, CONTEXT],
106+
[CROSS_ATTN_HEAD, None],
107+
[CROSS_ATTN_Q_LENGTH, CONTEXT],
108+
[CROSS_ATTN_KV_LENGTH, None],
109+
]

src/maxdiffusion/configs/base_wan_14b.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ jit_initializers: True
6969
# Set true to load weights from pytorch
7070
from_pt: True
7171
split_head_dim: True
72-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom
72+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
7373
use_base2_exp: True
7474
use_experimental_scheduler: True
75+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
76+
ulysses_shards: -1
7577
flash_min_seq_length: 4096
7678
dropout: 0.0
7779

src/maxdiffusion/configs/base_wan_1_3b.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ jit_initializers: True
6565
# Set true to load weights from pytorch
6666
from_pt: True
6767
split_head_dim: True
68-
attention: 'flash' # Supported attention: dot_product, flash, cudnn_flash_te, ring, ulysses
68+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
6969
use_base2_exp: True
7070
use_experimental_scheduler: True
71+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
72+
ulysses_shards: -1
7173
flash_min_seq_length: 0
7274

7375
# If mask_padding_tokens is True, we pass in segment ids to splash attention to avoid attending to padding tokens.

src/maxdiffusion/configs/base_wan_27b.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ jit_initializers: True
6969
# Set true to load weights from pytorch
7070
from_pt: True
7171
split_head_dim: True
72-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom
72+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
7373
use_base2_exp: True
7474
use_experimental_scheduler: True
75+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
76+
ulysses_shards: -1
7577
flash_min_seq_length: 4096
7678
dropout: 0.0
7779

src/maxdiffusion/configs/base_wan_animate.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ jit_initializers: True
6767
# Set true to load weights from pytorch
6868
from_pt: True
6969
split_head_dim: True
70-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom
70+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
7171
use_base2_exp: True
7272
use_experimental_scheduler: True
73+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
74+
ulysses_shards: -1
7375
flash_min_seq_length: 4096
7476
# If mask_padding_tokens is True, we pass in segment ids to splash attention to avoid attending to padding tokens.
7577
# Else we do not pass in segment ids and on vpu bound hardware like trillium this is faster.

src/maxdiffusion/configs/base_wan_i2v_14b.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ jit_initializers: True
6969
# Set true to load weights from pytorch
7070
from_pt: True
7171
split_head_dim: True
72-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom
72+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
7373
use_base2_exp: True
7474
use_experimental_scheduler: True
75+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
76+
ulysses_shards: -1
7577
flash_min_seq_length: 4096
7678
dropout: 0.0
7779

src/maxdiffusion/configs/base_wan_i2v_27b.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ jit_initializers: True
6969
# Set true to load weights from pytorch
7070
from_pt: True
7171
split_head_dim: True
72-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom
72+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, ulysses, ulysses_custom, ulysses_ring
7373
use_base2_exp: True
7474
use_experimental_scheduler: True
75+
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
76+
ulysses_shards: -1
7577
flash_min_seq_length: 4096
7678
dropout: 0.0
7779

src/maxdiffusion/max_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ def get_flash_block_sizes(config):
617617
"""Create custom flash attention BlockSizes."""
618618
flash_block_sizes = None
619619
if len(config.flash_block_sizes.keys()) > 0:
620-
attention_is_tokamax = "tokamax" in config.attention
620+
attention_is_tokamax = "tokamax" in config.attention or config.attention == "ulysses_ring"
621621
user_block_sizes: Dict[str, int] = config.flash_block_sizes
622622
if attention_is_tokamax:
623623
max_logging.log(

0 commit comments

Comments
 (0)