-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[feat] add VACE sequence parallel #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from .xdit_context_parallel import usp_attn_forward, usp_dit_forward, get_sequence_parallel_world_size, initialize_usp | ||
| from .xdit_context_parallel import usp_attn_forward, usp_dit_forward, usp_vace_forward, get_sequence_parallel_world_size, initialize_usp | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,39 @@ def usp_dit_forward(self, | |
| return x | ||
|
|
||
|
|
||
| def usp_vace_forward( | ||
| self, x, vace_context, context, t_mod, freqs, | ||
| use_gradient_checkpointing: bool = False, | ||
| use_gradient_checkpointing_offload: bool = False, | ||
| ): | ||
| # Compute full sequence length from the sharded x | ||
| full_seq_len = x.shape[1] * get_sequence_parallel_world_size() | ||
|
|
||
| # Embed vace_context via patch embedding | ||
| c = [self.vace_patch_embedding(u.unsqueeze(0)) for u in vace_context] | ||
| c = [u.flatten(2).transpose(1, 2) for u in c] | ||
| c = torch.cat([ | ||
| torch.cat([u, u.new_zeros(1, full_seq_len - u.size(1), u.size(2))], | ||
| dim=1) for u in c | ||
| ]) | ||
|
|
||
| # Chunk VACE context along sequence dim BEFORE processing through blocks | ||
| c = torch.chunk(c, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()] | ||
|
|
||
| # Process through vace_blocks (self_attn already monkey-patched to usp_attn_forward) | ||
| for block in self.vace_blocks: | ||
| c = gradient_checkpoint_forward( | ||
| block, | ||
| use_gradient_checkpointing, | ||
| use_gradient_checkpointing_offload, | ||
| c, x, context, t_mod, freqs | ||
| ) | ||
|
|
||
| # Hints are already sharded per-rank | ||
| hints = torch.unbind(c)[:-1] | ||
| return hints | ||
|
|
||
|
Comment on lines
+120
to
+151
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of def usp_vace_forward(
self, x, vace_context, context, t_mod, freqs,
use_gradient_checkpointing: bool = False,
use_gradient_checkpointing_offload: bool = False,
):
# Compute full sequence length from the sharded x
full_seq_len = x.shape[1] * get_sequence_parallel_world_size()
# Embed vace_context via patch embedding and flatten
c = self.vace_patch_embedding(vace_context)
c = c.flatten(2).transpose(1, 2)
# Pad or truncate to full sequence length
seq_len_diff = full_seq_len - c.size(1)
if seq_len_diff > 0:
padding = c.new_zeros(c.size(0), seq_len_diff, c.size(2))
c = torch.cat([c, padding], dim=1)
elif seq_len_diff < 0:
c = c[:, :full_seq_len]
# Chunk VACE context along sequence dim BEFORE processing through blocks
c = torch.chunk(c, get_sequence_parallel_world_size(), dim=1)[get_sequence_parallel_rank()]
# Process through vace_blocks (self_attn already monkey-patched to usp_attn_forward)
for block in self.vace_blocks:
c = gradient_checkpoint_forward(
block,
use_gradient_checkpointing,
use_gradient_checkpointing_offload,
c, x, context, t_mod, freqs
)
# Hints are already sharded per-rank
hints = torch.unbind(c)[:-1]
return hints |
||
|
|
||
| def usp_attn_forward(self, x, freqs): | ||
| q = self.norm_q(self.q(x)) | ||
| k = self.norm_k(self.k(x)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's duplicated logic for patching
self.vaceandself.vace2. This can be refactored to improve maintainability and reduce redundancy. You can use a loop to apply the same patching logic to both models.