Skip to content
This repository was archived by the owner on May 20, 2026. It is now read-only.

Commit 2152abd

Browse files
author
Huy Vu2
committed
clean up, remove all CP for sbhd, CP now is only for thd
1 parent 7b834f0 commit 2152abd

4 files changed

Lines changed: 8 additions & 37 deletions

File tree

dfm/src/megatron/model/wan/flow_matching/flow_inference_pipeline.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from dfm.src.megatron.model.wan.utils.utils import grid_sizes_calculation, patchify
2828
from megatron.core import parallel_state
2929
from torch.nn import functional as F
30-
from dfm.src.megatron.model.wan.utils.utils import split_inputs_cp, cat_outputs_cp
3130

3231
import math
3332
from typing import Tuple, Union
@@ -99,10 +98,8 @@ def __init__(
9998
wan_checkpoint_dir = self._select_checkpoint_dir(checkpoint_dir, checkpoint_step)
10099
self.model = self.setup_model_from_checkpoint(wan_checkpoint_dir)
101100

102-
# DEBUGGING thd
103-
# set qkv_format to to "thd" for context parallelism
104-
# self.model.config.qkv_format = "sbhd"
105-
self.model.config.qkv_format = "thd"
101+
# if we use context parallelism, we need to set qkv_format to "thd" for context parallelism
102+
self.model.config.qkv_format = "thd" # "sbhd"
106103

107104
# set self.sp_size=1 for later use, just to respect the original Wan inference code
108105
self.sp_size = 1
@@ -487,31 +484,13 @@ def noop_no_sync():
487484
timestep = [t] * batch_size
488485
timestep = torch.stack(timestep)
489486

490-
# DEBUGGING thd
491-
# # run context parallelism slitting
492-
# if parallel_state.get_context_parallel_world_size() > 1:
493-
# latent_model_input = split_inputs_cp(latent_model_input, 0)
494-
# arg_c['context'] = split_inputs_cp(arg_c['context'], 0)
495-
# arg_null['context'] = split_inputs_cp(arg_null['context'], 0)
496-
497487
self.model.to(self.device)
498488
noise_pred_cond = self.forward_pp_step(
499489
latent_model_input, grid_sizes=grid_sizes, max_video_seq_len=max_video_seq_len, timestep=timestep, arg_c=arg_c)
500490

501491
noise_pred_uncond = self.forward_pp_step(
502492
latent_model_input, grid_sizes=grid_sizes, max_video_seq_len=max_video_seq_len, timestep=timestep, arg_c=arg_null)
503493

504-
# DEBUGGING thd
505-
# # run context parallelism gathering
506-
# if parallel_state.get_context_parallel_world_size() > 1:
507-
# arg_c['context'] = cat_outputs_cp(arg_c['context'], 0) # we need to cat the context back together for the next timestep
508-
# arg_null['context'] = cat_outputs_cp(arg_null['context'], 0) # we need to cat the context back together for the next timestep
509-
# # TODO: does this step slow down speed???
510-
# noise_pred_cond = noise_pred_cond.contiguous()
511-
# noise_pred_uncond = noise_pred_uncond.contiguous()
512-
# noise_pred_cond = cat_outputs_cp(noise_pred_cond, 0)
513-
# noise_pred_uncond = cat_outputs_cp(noise_pred_uncond, 0)
514-
515494
# run unpatchify
516495
unpatchified_noise_pred_cond = noise_pred_cond
517496
unpatchified_noise_pred_cond = unpatchified_noise_pred_cond.transpose(0, 1) # bring sbhd -> bshd

dfm/src/megatron/model/wan/flow_matching/flow_pipeline.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from torch import Tensor
2121
from diffusers import WanPipeline
2222
from dfm.src.megatron.model.wan.flow_matching.time_shift_utils import compute_density_for_timestep_sampling
23-
from dfm.src.megatron.model.wan.utils.utils import patchify, split_inputs_cp, thd_split_inputs_cp
23+
from dfm.src.megatron.model.wan.utils.utils import patchify, thd_split_inputs_cp
2424

2525
class FlowPipeline:
2626

@@ -150,12 +150,6 @@ def training_step(
150150
# ========================================================================
151151

152152
if parallel_state.get_context_parallel_world_size() > 1:
153-
# DEBUGGING thd
154-
# video_latents = split_inputs_cp(video_latents, 0)
155-
# noisy_latents = split_inputs_cp(noisy_latents, 0)
156-
# noise = split_inputs_cp(noise, 0)
157-
# context_embeddings = split_inputs_cp(context_embeddings, 0)
158-
# split_loss_mask = split_inputs_cp(loss_mask, 0)
159153
video_latents = thd_split_inputs_cp(video_latents, packed_seq_params['self_attention'].cu_seqlens_q, parallel_state.get_context_parallel_group())
160154
noisy_latents = thd_split_inputs_cp(noisy_latents, packed_seq_params['self_attention'].cu_seqlens_q, parallel_state.get_context_parallel_group())
161155
noise = thd_split_inputs_cp(noise, packed_seq_params['self_attention'].cu_seqlens_q, parallel_state.get_context_parallel_group())

dfm/src/megatron/model/wan/rope_utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import torch
22
from torch.cuda import amp
3-
from dfm.src.megatron.model.wan.utils.utils import split_inputs_cp
43
from megatron.core import parallel_state
54

65
class Wan3DRopeEmbeddings(torch.nn.Module):
@@ -57,10 +56,9 @@ def forward(self, n_head, dim_head, max_seq_len, grid_sizes, device):
5756
# We concatenate them along dim=1 to get (max_seq_len, batch_size, 1, dim_head)
5857
freqs_real = torch.cat(freqs_real, dim=1)
5958

60-
# DEBUGGING thd
61-
# # TODO: if run context/sequence related parallel, then we need to scatter
62-
# # the freqs_real to the context parallel region, using specific cp_rank split method
63-
# if parallel_state.get_context_parallel_world_size() > 1:
64-
# freqs_real = split_inputs_cp(freqs_real, 0)
59+
# Note:
60+
# when running context_parallel, which must use "thd" for qkv_format,
61+
# we don't need to scatter the freqs to the context parallel region,
62+
# because mcore rope_utils will automatically retrieve the correct freqs for each context parallel region
6563

6664
return freqs_real

dfm/src/megatron/model/wan/utils/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def cat_outputs_cp(x: torch.Tensor, seq_dim: int) -> torch.Tensor:
129129
else:
130130
return x
131131

132-
# DEBUGGING thd
132+
133133
def thd_split_inputs_cp(x: torch.Tensor,
134134
cu_seqlens_q_padded: torch.Tensor,
135135
cp_group: dist.ProcessGroup) -> torch.Tensor:

0 commit comments

Comments
 (0)