Skip to content

Commit 3b68f8d

Browse files
authored
[core] support cp in h3. (#14407)
* support cp in h3. * empty
1 parent 9c6a68c commit 3b68f8d

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/diffusers/models/transformers/transformer_minimax_h3.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ...configuration_utils import ConfigMixin, register_to_config
2222
from ...loaders import PeftAdapterMixin
2323
from ...utils import BaseOutput, apply_lora_scale, logging
24+
from .._modeling_parallel import ContextParallelInput, ContextParallelOutput
2425
from ..attention import AttentionMixin, AttentionModuleMixin, FeedForward
2526
from ..attention_dispatch import dispatch_attention_fn
2627
from ..cache_utils import CacheMixin
@@ -446,6 +447,36 @@ class MiniMaxH3Transformer3DModel(ModelMixin, ConfigMixin, AttentionMixin, PeftA
446447
"audio_proj_out",
447448
"rope",
448449
]
450+
# Context parallelism shards the packed sequence, so the split cannot happen on the inputs of `forward`: the rows
451+
# of the three modalities are scattered into the packed buffer with sequence-wide indices, which only address the
452+
# full sequence. The split therefore happens once the buffer is built, at the first block, and everything that is
453+
# indexed per row of the packed sequence is split alongside it: `adaln_indices` for the block stack,
454+
# `timestep_indices` for `norm_out`, and the two `rope` outputs, which carry one row of angles each. The two output
455+
# heads gather the sequence back, because `forward` then selects the video and audio rows with sequence-wide
456+
# indices as well.
457+
#
458+
# The packed sequence carries no padding, so its length is whatever the caller packed. Splitting it across the
459+
# context parallel region requires that length to be divisible by the region size — use `ulysses_anything` or
460+
# `ring_anything` for a layout that is not.
461+
_cp_plan = {
462+
"rope": {
463+
0: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True),
464+
1: ContextParallelInput(split_dim=0, expected_dims=2, split_output=True),
465+
},
466+
"transformer_blocks.0": {
467+
"hidden_states": ContextParallelInput(split_dim=1, expected_dims=3, split_output=False),
468+
},
469+
# `hidden_states` is split once and then flows from block to block already sharded, but every block reads
470+
# `adaln_indices` from `forward`'s unsharded tensor, so each of them splits its own copy.
471+
"transformer_blocks.*": {
472+
"adaln_indices": ContextParallelInput(split_dim=0, expected_dims=1, split_output=False),
473+
},
474+
"norm_out": {
475+
"timestep_indices": ContextParallelInput(split_dim=0, expected_dims=1, split_output=False),
476+
},
477+
"proj_out": ContextParallelOutput(gather_dim=1, expected_dims=3),
478+
"audio_proj_out": ContextParallelOutput(gather_dim=1, expected_dims=3),
479+
}
449480

450481
@register_to_config
451482
def __init__(

tests/models/transformers/test_models_transformer_minimax_h3.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from ..testing_utils import (
2424
AttentionTesterMixin,
2525
BaseModelTesterConfig,
26+
ContextParallelTesterMixin,
2627
MemoryTesterMixin,
2728
ModelTesterMixin,
2829
TorchCompileTesterMixin,
@@ -121,12 +122,11 @@ def get_packed_layout(self, num_video_tokens: int = NUM_VIDEO_TOKENS) -> dict:
121122
"text_indices": text_indices,
122123
}
123124

124-
def get_dummy_inputs(self, num_video_tokens: int = NUM_VIDEO_TOKENS) -> dict:
125+
def get_dummy_inputs(self, num_video_tokens: int = NUM_VIDEO_TOKENS, batch_size: int = 2) -> dict:
125126
generator = self.generator
126127
init_dict = self.get_init_dict()
127128
patch_size = init_dict["patch_size"]
128129
video_patch_dim = init_dict["in_channels"] * patch_size[0] * patch_size[1] * patch_size[2]
129-
batch_size = 2
130130

131131
return {
132132
"hidden_states": randn_tensor(
@@ -180,3 +180,7 @@ class TestMiniMaxH3TransformerAttention(MiniMaxH3TransformerTesterConfig, Attent
180180

181181
class TestMiniMaxH3TransformerTorchCompile(MiniMaxH3TransformerTesterConfig, TorchCompileTesterMixin):
182182
"""Torch compile tests for the MiniMax-H3 transformer."""
183+
184+
185+
class TestMiniMaxH3TransformerContextParallel(MiniMaxH3TransformerTesterConfig, ContextParallelTesterMixin):
186+
"""Context parallel inference tests for the MiniMax-H3 transformer."""

0 commit comments

Comments
 (0)