|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Mock dataloader for automodel WAN training tests. |
| 16 | +
|
| 17 | +This module provides a mock dataset and dataloader that generates random |
| 18 | +tensors with the correct shapes for WAN 2.1 training, allowing functional |
| 19 | +tests to run without requiring real data. |
| 20 | +""" |
| 21 | + |
| 22 | +from typing import Dict, Optional, Tuple |
| 23 | + |
| 24 | +import torch |
| 25 | +from torch.utils.data import DataLoader, Dataset, DistributedSampler |
| 26 | + |
| 27 | + |
| 28 | +class MockWanDataset(Dataset): |
| 29 | + """Mock dataset that generates random data matching WAN 2.1 expected format. |
| 30 | +
|
| 31 | + Args: |
| 32 | + length: Number of samples in the dataset. |
| 33 | + num_channels: Number of latent channels (default: 16 for WAN). |
| 34 | + num_frame_latents: Number of temporal latent frames. |
| 35 | + spatial_h: Height of spatial latents. |
| 36 | + spatial_w: Width of spatial latents. |
| 37 | + text_seq_len: Length of text sequence. |
| 38 | + text_embed_dim: Dimension of text embeddings (default: 4096 for UMT5). |
| 39 | + device: Device to place tensors on. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + length: int = 1024, |
| 45 | + num_channels: int = 16, |
| 46 | + num_frame_latents: int = 16, |
| 47 | + spatial_h: int = 30, |
| 48 | + spatial_w: int = 52, |
| 49 | + text_seq_len: int = 77, |
| 50 | + text_embed_dim: int = 4096, |
| 51 | + device: str = "cpu", |
| 52 | + ) -> None: |
| 53 | + self.length = max(int(length), 1) |
| 54 | + self.num_channels = num_channels |
| 55 | + self.num_frame_latents = num_frame_latents |
| 56 | + self.spatial_h = spatial_h |
| 57 | + self.spatial_w = spatial_w |
| 58 | + self.text_seq_len = text_seq_len |
| 59 | + self.text_embed_dim = text_embed_dim |
| 60 | + self.device = device |
| 61 | + |
| 62 | + def __len__(self) -> int: |
| 63 | + return self.length |
| 64 | + |
| 65 | + def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]: |
| 66 | + """Generate a mock sample with random data. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Dict containing: |
| 70 | + - text_embeddings: [1, text_seq_len, text_embed_dim] |
| 71 | + - video_latents: [1, num_channels, num_frame_latents, spatial_h, spatial_w] |
| 72 | + - metadata: empty dict |
| 73 | + - file_info: mock file info |
| 74 | + """ |
| 75 | + # Generate random video latents: (1, C, T, H, W) |
| 76 | + video_latents = torch.randn( |
| 77 | + 1, |
| 78 | + self.num_channels, |
| 79 | + self.num_frame_latents, |
| 80 | + self.spatial_h, |
| 81 | + self.spatial_w, |
| 82 | + dtype=torch.float32, |
| 83 | + device=self.device, |
| 84 | + ) |
| 85 | + |
| 86 | + # Generate random text embeddings: (1, seq_len, embed_dim) |
| 87 | + text_embeddings = torch.randn( |
| 88 | + 1, |
| 89 | + self.text_seq_len, |
| 90 | + self.text_embed_dim, |
| 91 | + dtype=torch.float32, |
| 92 | + device=self.device, |
| 93 | + ) |
| 94 | + |
| 95 | + return { |
| 96 | + "text_embeddings": text_embeddings, |
| 97 | + "video_latents": video_latents, |
| 98 | + "metadata": {}, |
| 99 | + "file_info": { |
| 100 | + "meta_filename": f"mock_sample_{idx}.meta", |
| 101 | + "original_filename": f"mock_video_{idx}.mp4", |
| 102 | + "original_video_path": f"/mock/path/video_{idx}.mp4", |
| 103 | + "deterministic_latents": True, |
| 104 | + "memory_optimization": False, |
| 105 | + "num_frames": self.num_frame_latents * 4, # Approximate original frames |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | +def mock_collate_fn(batch): |
| 111 | + """Collate function for mock dataset, matching the real collate_fn behavior.""" |
| 112 | + text_embeddings = torch.cat([item["text_embeddings"] for item in batch], dim=0) |
| 113 | + video_latents = torch.cat([item["video_latents"] for item in batch], dim=0) |
| 114 | + |
| 115 | + return { |
| 116 | + "text_embeddings": text_embeddings, |
| 117 | + "video_latents": video_latents, |
| 118 | + "metadata": [item["metadata"] for item in batch], |
| 119 | + "file_info": [item["file_info"] for item in batch], |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +def build_mock_dataloader( |
| 124 | + *, |
| 125 | + dp_rank: int = 0, |
| 126 | + dp_world_size: int = 1, |
| 127 | + batch_size: int = 1, |
| 128 | + num_workers: int = 0, |
| 129 | + device: str = "cpu", |
| 130 | + length: int = 1024, |
| 131 | + num_channels: int = 16, |
| 132 | + num_frame_latents: int = 16, |
| 133 | + spatial_h: int = 30, |
| 134 | + spatial_w: int = 52, |
| 135 | + text_seq_len: int = 77, |
| 136 | + text_embed_dim: int = 4096, |
| 137 | + shuffle: bool = True, |
| 138 | +) -> Tuple[DataLoader, Optional[DistributedSampler]]: |
| 139 | + """Build a mock dataloader for WAN training tests. |
| 140 | +
|
| 141 | + This function follows the same interface as build_dataloader but generates |
| 142 | + random data instead of loading from .meta files. |
| 143 | +
|
| 144 | + Args: |
| 145 | + dp_rank: Data parallel rank. |
| 146 | + dp_world_size: Data parallel world size. |
| 147 | + batch_size: Batch size per GPU. |
| 148 | + num_workers: Number of dataloader workers. |
| 149 | + device: Device to place tensors on. |
| 150 | + length: Number of samples in mock dataset. |
| 151 | + num_channels: Number of latent channels (default: 16). |
| 152 | + num_frame_latents: Number of temporal latent frames. |
| 153 | + spatial_h: Height of spatial latents. |
| 154 | + spatial_w: Width of spatial latents. |
| 155 | + text_seq_len: Length of text sequence. |
| 156 | + text_embed_dim: Dimension of text embeddings. |
| 157 | + shuffle: Whether to shuffle data. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Tuple of (DataLoader, DistributedSampler or None). |
| 161 | + """ |
| 162 | + dataset = MockWanDataset( |
| 163 | + length=length, |
| 164 | + num_channels=num_channels, |
| 165 | + num_frame_latents=num_frame_latents, |
| 166 | + spatial_h=spatial_h, |
| 167 | + spatial_w=spatial_w, |
| 168 | + text_seq_len=text_seq_len, |
| 169 | + text_embed_dim=text_embed_dim, |
| 170 | + device=device, |
| 171 | + ) |
| 172 | + |
| 173 | + sampler = None |
| 174 | + if dp_world_size > 1: |
| 175 | + sampler = DistributedSampler( |
| 176 | + dataset, |
| 177 | + num_replicas=dp_world_size, |
| 178 | + rank=dp_rank, |
| 179 | + shuffle=shuffle, |
| 180 | + drop_last=False, |
| 181 | + ) |
| 182 | + |
| 183 | + use_pin_memory = device == "cpu" |
| 184 | + dataloader = DataLoader( |
| 185 | + dataset, |
| 186 | + batch_size=batch_size, |
| 187 | + shuffle=(sampler is None and shuffle), |
| 188 | + sampler=sampler, |
| 189 | + num_workers=num_workers, |
| 190 | + collate_fn=mock_collate_fn, |
| 191 | + pin_memory=use_pin_memory, |
| 192 | + ) |
| 193 | + |
| 194 | + return dataloader, sampler |
0 commit comments