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

Commit 4727447

Browse files
author
root
committed
refactor inference_configs + mockdatamodule
1 parent f3828b0 commit 4727447

10 files changed

Lines changed: 77 additions & 214 deletions

File tree

dfm/src/megatron/data/wan/wan_taskencoder.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(
7373
self.patch_temporal = patch_temporal
7474
self.seq_length = seq_length
7575

76-
## actual encode_sample() for production
76+
7777
def encode_sample(self, sample: dict) -> dict:
7878
video_latent = sample["pth"]
7979
context_embeddings = sample["pickle"]
@@ -103,24 +103,6 @@ def encode_sample(self, sample: dict) -> dict:
103103
video_metadata=video_metadata,
104104
)
105105

106-
## mock encode_sample() for debugging
107-
# def encode_sample(self, sample: dict) -> dict:
108-
109-
# # mock encode sample
110-
# F_latents = 24
111-
# H_latents = 104
112-
# W_latents = 60
113-
# video_latent = torch.tensor(torch.randn(16, F_latents, H_latents, W_latents), dtype=torch.float32)
114-
# grid_size = torch.tensor([video_latent.shape[1] // self.patch_temporal, video_latent.shape[2] // self.patch_spatial, video_latent.shape[3] // self.patch_spatial], dtype=torch.int32)
115-
# context_embeddings = torch.tensor(torch.randn(512, 4096), dtype=torch.float32)
116-
# video_metadata = {}
117-
118-
# return dict(
119-
# video_latent=video_latent,
120-
# grid_size=grid_size,
121-
# context_embeddings=context_embeddings,
122-
# video_metadata=video_metadata,
123-
# )
124106

125107
def batch(self, samples: list[dict]) -> dict:
126108
# process video latents

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _encode_text(
7171
class FlowInferencePipeline:
7272
def __init__(
7373
self,
74-
config,
74+
inference_cfg,
7575
model_id="Wan-AI/Wan2.1-T2V-14B-Diffusers",
7676
checkpoint_dir=None,
7777
checkpoint_step=None,
@@ -90,8 +90,8 @@ def __init__(
9090
Initializes the FlowInferencePipeline with the given parameters.
9191
9292
Args:
93-
config (EasyDict):
94-
Object containing model parameters initialized from config.py
93+
inference_cfg (dict):
94+
Object containing inference configuration.
9595
checkpoint_dir (`str`):
9696
Path to directory containing model checkpoints
9797
t5_checkpoint_dir (`str`, *optional*, defaults to None):
@@ -106,7 +106,7 @@ def __init__(
106106
Whether to place T5 model on CPU. Only works without t5_fsdp.
107107
"""
108108
self.device = torch.device(f"cuda:{device_id}")
109-
self.config = config
109+
self.inference_cfg = inference_cfg
110110
self.model_id = model_id
111111
self.rank = rank
112112
self.t5_cpu = t5_cpu
@@ -115,25 +115,26 @@ def __init__(
115115
self.pipeline_parallel_size = pipeline_parallel_size
116116
self.sequence_parallel = sequence_parallel
117117
self.pipeline_dtype = pipeline_dtype
118-
self.num_train_timesteps = config.num_train_timesteps
119-
self.param_dtype = config.param_dtype
118+
self.num_train_timesteps = inference_cfg.num_train_timesteps
119+
self.param_dtype = inference_cfg.param_dtype
120+
self.text_len = inference_cfg.text_len
120121

121122
self.text_encoder = UMT5EncoderModel.from_pretrained(
122123
model_id,
123124
subfolder="text_encoder",
124-
torch_dtype=config.t5_dtype,
125+
torch_dtype=inference_cfg.t5_dtype,
125126
)
126127
self.tokenizer = AutoTokenizer.from_pretrained(
127128
model_id,
128129
subfolder="tokenizer",
129130
)
130131

131-
self.vae_stride = config.vae_stride
132-
self.patch_size = config.patch_size
132+
self.vae_stride = inference_cfg.vae_stride
133+
self.patch_size = inference_cfg.patch_size
133134
self.vae = AutoencoderKLWan.from_pretrained(
134135
model_id,
135136
subfolder="vae",
136-
torch_dtype=config.param_dtype,
137+
torch_dtype=inference_cfg.param_dtype,
137138
)
138139
self.vae.to(self.device)
139140

@@ -150,7 +151,7 @@ def __init__(
150151
dist.barrier()
151152
self.model.to(self.device)
152153

153-
self.sample_neg_prompt = config.sample_neg_prompt
154+
self.sample_neg_prompt = inference_cfg.sample_neg_prompt
154155

155156
def setup_model_from_checkpoint(self, checkpoint_dir):
156157
provider = WanModelProvider()
@@ -362,7 +363,7 @@ def generate(
362363
# we implement similar to Wan's diffuser setup
363364
# (https://github.com/huggingface/diffusers/blob/0f252be0ed42006c125ef4429156cb13ae6c1d60/src/diffusers/pipelines/wan/pipeline_wan.py#L157)
364365
# in which we pad the text to 512, pass through text encoder, and truncate to the actual tokens, then pad with 0s to 512.
365-
context_max_len = 512
366+
context_max_len = self.text_len
366367
context_lens = []
367368
contexts = []
368369
contexts_null = []

dfm/src/megatron/model/wan/inference/configs/__init__.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

dfm/src/megatron/model/wan/inference/configs/shared_config.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

dfm/src/megatron/model/wan/inference/configs/wan_t2v_14B.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

dfm/src/megatron/model/wan/inference/configs/wan_t2v_1_3B.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from megatron.core.transformer.transformer_block import TransformerBlock
2929
from megatron.core.transformer.transformer_config import TransformerConfig
3030
from megatron.core.utils import make_sharded_tensor_for_checkpoint
31-
from nemo.collections.diffusion.models.dit.dit_embeddings import ParallelTimestepEmbedding
31+
from dfm.src.megatron.model.common.dit_embeddings import ParallelTimestepEmbedding
3232
from torch import Tensor
3333

3434
from dfm.src.megatron.model.wan.wan_layer_spec import (

dfm/src/megatron/recipes/wan/wan.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from megatron.core.distributed import DistributedDataParallelConfig
3232

3333
from dfm.src.megatron.data.wan.wan_energon_datamodule import WanDataModuleConfig
34+
from dfm.src.megatron.data.wan.wan_mock_energon_datamodule import WanMockDataModuleConfig
3435
from dfm.src.megatron.model.wan.wan_provider import WanModelProvider
3536

3637

@@ -158,6 +159,28 @@ def pretrain_config(
158159

159160
precision_config.grad_reduce_in_fp32 = False
160161

162+
if mock:
163+
dataset = WanMockDataModuleConfig(
164+
path=None,
165+
seq_length=1024, # we don't need to use this value, just add because Bridge training requires for LLMs
166+
F_latents=3,
167+
H_latents=104,
168+
W_latents=60,
169+
context_seq_len=512,
170+
context_embeddings_dim=4096,
171+
micro_batch_size=micro_batch_size,
172+
global_batch_size=global_batch_size,
173+
num_workers=10,
174+
)
175+
else:
176+
dataset = WanDataModuleConfig(
177+
path=None,
178+
seq_length=1024, # we don't need to use this value, just add because Bridge training requires for LLMs
179+
micro_batch_size=micro_batch_size,
180+
global_batch_size=global_batch_size,
181+
num_workers=10,
182+
)
183+
161184
# Config Container
162185
cfg = ConfigContainer(
163186
model=model_cfg,
@@ -182,13 +205,7 @@ def pretrain_config(
182205
use_distributed_optimizer=True,
183206
use_megatron_fsdp=use_megatron_fsdp, # need use_distributed_optimizer=True
184207
),
185-
dataset=WanDataModuleConfig(
186-
path=None,
187-
seq_length=1024, # we don't need to use this value, just add because Bridge training requires for LLMs
188-
micro_batch_size=micro_batch_size,
189-
global_batch_size=global_batch_size,
190-
num_workers=10,
191-
),
208+
dataset=dataset,
192209
logger=LoggerConfig(
193210
log_interval=10,
194211
tensorboard_dir=tensorboard_dir,

0 commit comments

Comments
 (0)