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

Commit 75aa183

Browse files
committed
update
Signed-off-by: linnan wang <wangnan318@gmail.com>
1 parent 2eb57c2 commit 75aa183

1 file changed

Lines changed: 258 additions & 0 deletions

File tree

docs/automodel_training_doc.md

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# Diffusion Model Fine-tuning with Automodel Backend
2+
3+
Train diffusion models with distributed training support using NeMo Automodel and flow matching.
4+
5+
**Currently Supported:** Wan 2.1 Text-to-Video (1.3B and 14B models)
6+
7+
---
8+
9+
## Quick Start
10+
11+
### 1. Docker Setup
12+
13+
```bash
14+
# Build image
15+
docker build -f docker/Dockerfile.ci -t dfm-training .
16+
17+
# Run container
18+
docker run --gpus all -it \
19+
-v $(pwd):/workspace \
20+
-v /path/to/data:/data \
21+
--ipc=host \
22+
--ulimit memlock=-1 \
23+
--ulimit stack=67108864 \
24+
dfm-training bash
25+
26+
# Inside container: Initialize submodules
27+
export UV_PROJECT_ENVIRONMENT=
28+
git submodule update --init --recursive 3rdparty/
29+
```
30+
31+
### 2. Prepare Data
32+
33+
**Create video dataset:**
34+
```
35+
<your_video_folder>/
36+
├── video1.mp4
37+
├── video2.mp4
38+
└── meta.json
39+
```
40+
41+
**meta.json format:**
42+
```json
43+
[
44+
{
45+
"file_name": "video1.mp4",
46+
"width": 1280,
47+
"height": 720,
48+
"start_frame": 0,
49+
"end_frame": 121,
50+
"vila_caption": "A detailed description of the video content..."
51+
}
52+
]
53+
```
54+
55+
**Preprocess videos to .meta files:**
56+
57+
There are two preprocessing modes:
58+
59+
**Mode 1: Full video (recommended for training)**
60+
```bash
61+
python dfm/src/automodel/utils/data/preprocess_resize.py \
62+
--mode video \
63+
--video_folder <your_video_folder> \
64+
--output_folder ./processed_meta \
65+
--model Wan-AI/Wan2.1-T2V-1.3B-Diffusers \
66+
--height 480 \
67+
--width 720 \
68+
--center-crop
69+
```
70+
71+
**Mode 2: Extract frames (for frame-based training)**
72+
```bash
73+
python dfm/src/automodel/utils/data/preprocess_resize.py \
74+
--mode frames \
75+
--num-frames 40 \
76+
--video_folder <your_video_folder> \
77+
--output_folder ./processed_frames \
78+
--model Wan-AI/Wan2.1-T2V-1.3B-Diffusers \
79+
--height 240 \
80+
--width 416 \
81+
--center-crop
82+
```
83+
84+
**Key arguments:**
85+
- `--mode`: `video` (full video) or `frames` (extract evenly-spaced frames)
86+
- `--num-frames`: Number of frames to extract (only for `frames` mode)
87+
- `--height/--width`: Target resolution
88+
- `--center-crop`: Crop to exact size after aspect-preserving resize
89+
90+
**Preprocessing modes:**
91+
- **`video` mode**: Processes entire video sequence, creates one `.meta` file per video
92+
- **`frames` mode**: Extracts N evenly-spaced frames, creates one `.meta` file per frame (treated as 1-frame videos)
93+
94+
**Output:** Creates `.meta` files containing:
95+
- Encoded video latents (normalized)
96+
- Text embeddings (from UMT5)
97+
- First frame as JPEG (video mode only)
98+
- Metadata
99+
100+
### 3. Train
101+
102+
**Single-node (8 GPUs):**
103+
```bash
104+
export UV_PROJECT_ENVIRONMENT=
105+
106+
uv run --group automodel --with . \
107+
torchrun --nproc-per-node=8 \
108+
examples/automodel/finetune/finetune.py \
109+
-c examples/automodel/finetune/wan2_1_t2v_flow.yaml
110+
```
111+
112+
**Multi-node with SLURM:**
113+
```bash
114+
#!/bin/bash
115+
#SBATCH -N 2
116+
#SBATCH --ntasks-per-node 1
117+
#SBATCH --gpus-per-node=8
118+
#SBATCH --exclusive
119+
120+
export MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1)
121+
export MASTER_PORT=29500
122+
export NUM_GPUS=8
123+
124+
# Per-rank UV cache to avoid conflicts
125+
unset UV_PROJECT_ENVIRONMENT
126+
mkdir -p /opt/uv_cache/${SLURM_JOB_ID}_${SLURM_PROCID}
127+
export UV_CACHE_DIR=/opt/uv_cache/${SLURM_JOB_ID}_${SLURM_PROCID}
128+
129+
uv run --group automodel --with . \
130+
torchrun \
131+
--nnodes=$SLURM_NNODES \
132+
--nproc-per-node=$NUM_GPUS \
133+
--rdzv_backend=c10d \
134+
--rdzv_endpoint=$MASTER_ADDR:$MASTER_PORT \
135+
examples/automodel/finetune/finetune.py \
136+
-c examples/automodel/finetune/wan2_1_t2v_flow_multinode.yaml
137+
```
138+
139+
### 4. Validate
140+
141+
```bash
142+
uv run --group automodel --with . \
143+
python examples/automodel/generate/wan_validate.py \
144+
--meta_folder <your_meta_folder> \
145+
--guidance_scale 5 \
146+
--checkpoint ./checkpoints/step_1000 \
147+
--num_samples 10
148+
```
149+
150+
**Note:** You can use `--checkpoint ./checkpoints/LATEST` to automatically use the most recent checkpoint.
151+
152+
---
153+
154+
## Configuration
155+
156+
### Fine-tuning Config (`wan2_1_t2v_flow.yaml`)
157+
158+
```yaml
159+
model:
160+
pretrained_model_name_or_path: Wan-AI/Wan2.1-T2V-1.3B-Diffusers
161+
162+
step_scheduler:
163+
global_batch_size: 8
164+
local_batch_size: 1
165+
num_epochs: 10
166+
ckpt_every_steps: 100
167+
168+
data:
169+
dataloader:
170+
meta_folder: "<your_processed_meta_folder>"
171+
num_workers: 2
172+
173+
optim:
174+
learning_rate: 5e-6
175+
176+
flow_matching:
177+
timestep_sampling: "uniform"
178+
flow_shift: 3.0
179+
180+
fsdp:
181+
dp_size: 8 # Single node: 8 GPUs
182+
183+
checkpoint:
184+
enabled: true
185+
checkpoint_dir: "./checkpoints"
186+
```
187+
188+
### Multi-node Config Differences
189+
190+
```yaml
191+
fsdp:
192+
dp_size: 16 # 2 nodes × 8 GPUs
193+
dp_replicate_size: 2 # Replicate across 2 nodes
194+
```
195+
196+
### Pretraining vs Fine-tuning
197+
198+
| Setting | Fine-tuning | Pretraining |
199+
|---------|-------------|-------------|
200+
| `learning_rate` | 5e-6 | 5e-5 |
201+
| `weight_decay` | 0.01 | 0.1 |
202+
| `flow_shift` | 3.0 | 2.5 |
203+
| `logit_std` | 1.0 | 1.5 |
204+
| Dataset size | 100s-1000s | 10K+ |
205+
206+
---
207+
208+
## Hardware Requirements
209+
210+
| Component | Minimum | Recommended |
211+
|-----------|---------|-------------|
212+
| GPU | A100 40GB | A100 80GB / H100 |
213+
| GPUs | 4 | 8+ |
214+
| RAM | 128 GB | 256 GB+ |
215+
| Storage | 500 GB SSD | 2 TB NVMe |
216+
217+
---
218+
219+
## Features
220+
221+
- ✅ **Flow Matching**: Pure flow matching training
222+
- ✅ **Distributed**: FSDP2 + Tensor Parallelism
223+
- ✅ **Mixed Precision**: BF16 by default
224+
- ✅ **WandB**: Automatic logging
225+
- ✅ **Checkpointing**: consolidated, and sharded formats
226+
- ✅ **Multi-node**: SLURM and torchrun support
227+
228+
---
229+
230+
## Supported Models
231+
232+
| Model | Parameters | Parallelization | Status |
233+
|-------|------------|-----------------|--------|
234+
| Wan 2.1 T2V 1.3B | 1.3B | FSDP2 via Automodel + DDP | ✅ |
235+
| Wan 2.1 T2V 14B | 14B | FSDP2 via Automodel + DDP | ✅ |
236+
| FLUX | TBD | TBD | 🔄 In Progress |
237+
238+
---
239+
240+
## Advanced
241+
242+
**Custom parallelization:**
243+
```yaml
244+
fsdp:
245+
tp_size: 2 # Tensor parallel
246+
dp_size: 4 # Data parallel
247+
```
248+
249+
**Checkpoint cleanup:**
250+
```python
251+
from pathlib import Path
252+
import shutil
253+
254+
def cleanup_old_checkpoints(checkpoint_dir, keep_last_n=3):
255+
checkpoints = sorted(Path(checkpoint_dir).glob("step_*"))
256+
for old_ckpt in checkpoints[:-keep_last_n]:
257+
shutil.rmtree(old_ckpt)
258+
```

0 commit comments

Comments
 (0)