866 lines
35 KiB
Markdown
Executable File
866 lines
35 KiB
Markdown
Executable File
# Wan2.2 Full Parameter Training Guide
|
||
|
||
This document provides a complete workflow for full parameter training of Wan2.2 Diffusion Transformer, including environment setup, data preparation, distributed training, and inference testing.
|
||
|
||
> **Note**: Wan2.2 is a video generation model that supports Text-to-Video (T2V), Image-to-Video (I2V), and Text-Image-to-Video (TI2V). Wan2.2 adopts a dual-Transformer architecture (high-noise/low-noise models) for higher quality video generation. This document covers the training workflow for general video generation tasks.
|
||
|
||
---
|
||
|
||
## Table of Contents
|
||
- [1. Environment Setup](#1-environment-setup)
|
||
- [2. Data Preparation](#2-data-preparation)
|
||
- [2.1 Quick Test Dataset](#21-quick-test-dataset)
|
||
- [2.2 Dataset Structure](#22-dataset-structure)
|
||
- [2.3 metadata.json Format](#23-metadatajson-format)
|
||
- [2.4 Relative vs Absolute Path Usage](#24-relative-vs-absolute-path-usage)
|
||
- [3. Full Parameter Training](#3-full-parameter-training)
|
||
- [3.1 Download Pretrained Model](#31-download-pretrained-model)
|
||
- [3.2 Quick Start (DeepSpeed-Zero-2)](#32-quick-start-deepspeed-zero-2)
|
||
- [3.3 Training Parameter Reference](#33-training-parameter-reference)
|
||
- [3.4 Training Validation](#34-training-validation)
|
||
- [3.5 Training with FSDP](#35-training-with-fsdp)
|
||
- [3.6 Other Backends](#36-other-backends)
|
||
- [3.7 Multi-Node Distributed Training](#37-multi-node-distributed-training)
|
||
- [4. Inference Testing](#4-inference-testing)
|
||
- [4.1 Inference Parameter Reference](#41-inference-parameter-reference)
|
||
- [4.2 Text-to-Video (T2V) Inference](#42-text-to-video-t2v-inference)
|
||
- [4.3 Image-to-Video (I2V) Inference](#43-image-to-video-i2v-inference)
|
||
- [4.4 Multi-GPU Parallel Inference](#44-multi-gpu-parallel-inference)
|
||
- [5. Additional Resources](#5-additional-resources)
|
||
|
||
---
|
||
|
||
## 1. Environment Setup
|
||
|
||
**Option 1: Using requirements.txt**
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
**Option 2: Manual Installation**
|
||
|
||
```bash
|
||
pip install Pillow einops safetensors timm tomesd librosa "torch>=2.1.2" torchdiffeq torchsde decord datasets numpy scikit-image
|
||
pip install omegaconf SentencePiece imageio[ffmpeg] imageio[pyav] tensorboard beautifulsoup4 ftfy func_timeout onnxruntime
|
||
pip install "peft>=0.17.0" "accelerate>=0.25.0" "gradio>=3.41.2" "diffusers>=0.30.1" "transformers>=4.46.2"
|
||
pip install yunchang xfuser modelscope openpyxl
|
||
pip uninstall opencv-python opencv-contrib-python opencv-python-headless -y
|
||
pip install opencv-python-headless
|
||
pip install deepspeed==0.17.0 numpy==1.26.4
|
||
```
|
||
|
||
**Option 3: Using Docker**
|
||
|
||
When using Docker, ensure that the GPU driver and CUDA environment are properly installed on your machine, then execute the following commands:
|
||
|
||
```bash
|
||
# pull image
|
||
docker pull mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/easycv/torch_cuda:cogvideox_fun
|
||
|
||
# enter image
|
||
docker run -it -p 7860:7860 --network host --gpus all --security-opt seccomp:unconfined --shm-size 200g mybigpai-public-registry.cn-beijing.cr.aliyuncs.com/easycv/torch_cuda:cogvideox_fun
|
||
```
|
||
|
||
---
|
||
|
||
## 2. Data Preparation
|
||
|
||
### 2.1 Quick Test Dataset
|
||
|
||
We provide a test dataset containing several training samples.
|
||
|
||
```bash
|
||
# Download official demo dataset
|
||
modelscope download --dataset PAI/X-Fun-Videos-Demo --local_dir ./datasets/X-Fun-Videos-Demo
|
||
```
|
||
|
||
### 2.2 Dataset Structure
|
||
|
||
```
|
||
📦 datasets/
|
||
├── 📂 my_dataset/
|
||
│ ├── 📂 train/
|
||
│ │ ├── 📄 video001.mp4
|
||
│ │ ├── 📄 video002.mp4
|
||
│ │ └── 📄 ...
|
||
│ └── 📄 metadata.json
|
||
```
|
||
|
||
### 2.3 metadata.json Format
|
||
|
||
**Relative Path Format** (example format):
|
||
```json
|
||
[
|
||
{
|
||
"file_path": "train/video001.mp4",
|
||
"text": "A beautiful sunset over the ocean, golden hour lighting",
|
||
"type": "video",
|
||
"width": 1024,
|
||
"height": 1024
|
||
},
|
||
{
|
||
"file_path": "train/video002.mp4",
|
||
"text": "A person walking through a forest, cinematic view",
|
||
"type": "video",
|
||
"width": 1328,
|
||
"height": 1328
|
||
}
|
||
]
|
||
```
|
||
|
||
**Absolute Path Format**:
|
||
```json
|
||
[
|
||
{
|
||
"file_path": "/mnt/data/videos/sunset.mp4",
|
||
"text": "A beautiful sunset over the ocean",
|
||
"type": "video",
|
||
"width": 1024,
|
||
"height": 1024
|
||
}
|
||
]
|
||
```
|
||
|
||
**Key Field Descriptions**:
|
||
- `file_path`: Video path (relative or absolute path)
|
||
- `text`: Video description (English prompt)
|
||
- `type`: Data type, should be `"video"`
|
||
- `width` / `height`: Video width and height (**recommended to provide**, used for bucket training. If not provided, they will be automatically read during training, which may affect training speed when data is stored on slow storage systems like OSS).
|
||
- You can use `scripts/process_json_add_width_and_height.py` to extract width and height from JSON files without these fields. It supports processing both images and videos.
|
||
- Usage: `python scripts/process_json_add_width_and_height.py --input_file datasets/X-Fun-Videos-Demo/metadata.json --output_file datasets/X-Fun-Videos-Demo/metadata_add_width_height.json`.
|
||
|
||
### 2.4 Relative vs Absolute Path Usage
|
||
|
||
**Relative Path**:
|
||
|
||
If your data uses relative paths, configure in the training script:
|
||
|
||
```bash
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
```
|
||
|
||
**Absolute Path**:
|
||
|
||
If your data uses absolute paths, configure in the training script:
|
||
|
||
```bash
|
||
export DATASET_NAME=""
|
||
export DATASET_META_NAME="/mnt/data/metadata_add_width_height.json"
|
||
```
|
||
|
||
> 💡 **Recommendation**: If the dataset is small and stored locally, use relative paths. If the dataset is stored on external storage (e.g., NAS, OSS) or shared across multiple machines, use absolute paths.
|
||
|
||
---
|
||
|
||
## 3. Full Parameter Training
|
||
|
||
### 3.1 Download Pretrained Model
|
||
|
||
```bash
|
||
# Create model directory
|
||
mkdir -p models/Diffusion_Transformer
|
||
|
||
# Download official Wan2.2 weights
|
||
# T2V model (Text-to-Video)
|
||
modelscope download --model Wan-AI/Wan2.2-T2V-A14B --local_dir models/Diffusion_Transformer/Wan2.2-T2V-A14B
|
||
# Or I2V model (Image-to-Video)
|
||
# modelscope download --model Wan-AI/Wan2.2-I2V-A14B --local_dir models/Diffusion_Transformer/Wan2.2-I2V-A14B
|
||
# Or TI2V model (Text-Image-to-Video)
|
||
# modelscope download --model Wan-AI/Wan2.2-TI2V-5B --local_dir models/Diffusion_Transformer/Wan2.2-TI2V-5B
|
||
```
|
||
|
||
### 3.2 Quick Start (DeepSpeed-Zero-2)
|
||
|
||
After downloading the dataset as in **2.1 Quick Test Dataset** and the pretrained model as in **3.1 Download Pretrained Model**, you can directly copy and run the quick start command.
|
||
|
||
We recommend using DeepSpeed-Zero-2 or FSDP for training. Here we use DeepSpeed-Zero-2 as an example.
|
||
|
||
The difference between DeepSpeed-Zero-2 and FSDP in this repository is whether the model weights are sharded. **If GPU memory is insufficient when using multiple GPUs with DeepSpeed-Zero-2**, you can switch to FSDP for training.
|
||
|
||
**Wan2.2 T2V Training Example**:
|
||
|
||
```bash
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --use_deepspeed --deepspeed_config_file config/zero_stage2_config.json --deepspeed_multinode_launcher standard scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_t2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="normal" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
**Wan2.2 I2V Training Example**:
|
||
|
||
```bash
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-I2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --use_deepspeed --deepspeed_config_file config/zero_stage2_config.json --deepspeed_multinode_launcher standard scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_i2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="i2v" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
**Wan2.2 TI2V Training Example**:
|
||
|
||
```bash
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-TI2V-5B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --use_deepspeed --deepspeed_config_file config/zero_stage2_config.json --deepspeed_multinode_launcher standard scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_5b.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="full" \
|
||
--train_mode="ti2v" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
### 3.3 Training Parameter Reference
|
||
|
||
**Wan2.2 Dual-Transformer Architecture Explanation**:
|
||
|
||
Wan2.2 adopts an innovative dual-Transformer architecture:
|
||
- **Low Noise Model**: Handles the low-noise stage (closer to final output)
|
||
- **High Noise Model**: Handles the high-noise stage (initial generation stage)
|
||
- **Boundary Type (boundary_type)**:
|
||
- `low`: Train low-noise model, high-noise model uses pretrained weights (recommended for T2V/I2V fine-tuning)
|
||
- `high`: Train high-noise model, low-noise model uses pretrained weights
|
||
- `full`: Single model training (for single-Transformer models like TI2V-5B)
|
||
|
||
**Key Parameter Descriptions**:
|
||
|
||
| Parameter | Description | Example Value |
|
||
|-----------|-------------|---------------|
|
||
| `--pretrained_model_name_or_path` | Pretrained model path | `models/Diffusion_Transformer/Wan2.2-T2V-A14B` |
|
||
| `--train_data_dir` | Training data directory | `datasets/X-Fun-Videos-Demo/` |
|
||
| `--train_data_meta` | Training data metadata file | `datasets/X-Fun-Videos-Demo/metadata_add_width_height.json` |
|
||
| `--train_batch_size` | Batch size | 1 |
|
||
| `--image_sample_size` | Maximum training resolution for images | 640 |
|
||
| `--video_sample_size` | Maximum training resolution for videos | 640 |
|
||
| `--token_sample_size` | Token sample size | 640 |
|
||
| `--video_sample_stride` | Video sample stride | 2 |
|
||
| `--video_sample_n_frames` | Number of video frames to sample | 81 |
|
||
| `--gradient_accumulation_steps` | Gradient accumulation steps (effectively increases batch size) | 1 |
|
||
| `--dataloader_num_workers` | Number of DataLoader workers | 8 |
|
||
| `--num_train_epochs` | Number of training epochs | 100 |
|
||
| `--checkpointing_steps` | Save checkpoint every N steps | 50 |
|
||
| `--learning_rate` | Initial learning rate | 2e-05 |
|
||
| `--lr_scheduler` | Learning rate scheduler | `constant_with_warmup` |
|
||
| `--lr_warmup_steps` | Learning rate warmup steps | 100 |
|
||
| `--seed` | Random seed | 42 |
|
||
| `--output_dir` | Output directory | `output_dir_wan2.2` |
|
||
| `--gradient_checkpointing` | Gradient checkpointing | - |
|
||
| `--mixed_precision` | Mixed precision: `fp16/bf16` | `bf16` |
|
||
| `--adam_weight_decay` | AdamW weight decay | 3e-2 |
|
||
| `--adam_epsilon` | AdamW epsilon value | 1e-10 |
|
||
| `--vae_mini_batch` | Mini-batch size for VAE encoding | 1 |
|
||
| `--max_grad_norm` | Maximum gradient norm for clipping | 0.05 |
|
||
| `--enable_bucket` | Enable bucket training without cropping, groups by resolution | - |
|
||
| `--random_hw_adapt` | Randomly scale images/videos to `[min_size, max_size]` | - |
|
||
| `--training_with_video_token_length` | Train based on token length, supports arbitrary resolutions | - |
|
||
| `--uniform_sampling` | Uniform timestep sampling | - |
|
||
| `--low_vram` | Low VRAM mode | - |
|
||
| `--boundary_type` | Wan2.2 dual-Transformer boundary type: `low` (train low-noise), `high` (train high-noise), `full` (single model like TI2V-5B) | `low` |
|
||
| `--train_mode` | Training mode: `normal` (T2V), `i2v` (Image-to-Video), or `ti2v` (Text-Image-to-Video) | `normal` |
|
||
| `--resume_from_checkpoint` | Resume training from checkpoint, use `"latest"` to auto-select | None |
|
||
| `--validation_steps` | Run validation every N steps | 100 |
|
||
| `--validation_epochs` | Run validation every N epochs | 500 |
|
||
| `--validation_prompts` | Prompts for validation video generation | `"A brown dog shaking its head..."` |
|
||
| `--validation_paths` | Reference image paths for I2V validation (i2v mode only) | `"asset/1.png"` |
|
||
| `--trainable_modules` | Trainable modules (`"."` means all modules) | `"."` |
|
||
|
||
**Sample Size Configuration Guide**:
|
||
- `video_sample_size` represents the resolution size for videos; when `random_hw_adapt` is True, it represents the minimum value for both video and image resolutions.
|
||
- `image_sample_size` represents the resolution size for images; when `random_hw_adapt` is True, it represents the maximum value for both video and image resolutions.
|
||
- `token_sample_size` represents the resolution corresponding to the maximum token length when `training_with_video_token_length` is True.
|
||
- Due to potential confusion in configuration, **if you don't require arbitrary resolution for finetuning**, it is recommended to set `video_sample_size`, `image_sample_size`, and `token_sample_size` to the same fixed value, such as **(320, 480, 512, 640, 960)**.
|
||
- **All set to 320** represents **240P**.
|
||
- **All set to 480** represents **320P**.
|
||
- **All set to 640** represents **480P**.
|
||
- **All set to 960** represents **720P**.
|
||
|
||
**Wan2.2 Training Strategy Recommendations**:
|
||
- **T2V Model (Dual-Transformer)**: Use `boundary_type="low"` and `train_mode="normal"` to train the low-noise model. This maintains the generality of the high-noise part while fine-tuning the low-noise part for your data.
|
||
- **I2V Model (Dual-Transformer)**: Use `boundary_type="low"` and `train_mode="i2v"` to train the low-noise model. The dataset needs to include reference images.
|
||
- **TI2V Model (Single-Transformer)**: Use `boundary_type="full"` and `train_mode="ti2v"` for full training. The dataset needs to include reference images.
|
||
- **Memory Optimization**: Wan2.2 models are large (14B/5B parameters). It is strongly recommended to use `--low_vram` and `--gradient_checkpointing`.
|
||
- **Multi-GPU Training**: For 14B models, it is recommended to use FSDP or DeepSpeed-Zero-2/3 for multi-GPU training.
|
||
|
||
**Token Length Training Explanation**:
|
||
- When `training_with_video_token_length` is enabled, the model trains based on token length.
|
||
- For example: a video with 512x512 resolution and 49 frames has a token length of 13,312, requiring `token_sample_size = 512`.
|
||
- At 512x512 resolution, the number of video frames is 49 (~= 512 * 512 * 49 / 512 / 512).
|
||
- At 768x768 resolution, the number of video frames is 21 (~= 512 * 512 * 49 / 768 / 768).
|
||
- At 1024x1024 resolution, the number of video frames is 9 (~= 512 * 512 * 49 / 1024 / 1024).
|
||
- These resolutions combined with their corresponding frame counts allow the model to generate videos of different sizes.
|
||
|
||
### 3.4 Training Validation
|
||
|
||
You can configure validation parameters to periodically generate test videos during training, allowing you to monitor training progress and model quality.
|
||
|
||
**Validation Parameter Descriptions**:
|
||
|
||
| Parameter | Description | Recommended Value |
|
||
|-----------|-------------|-------------------|
|
||
| `--validation_steps` | Run validation every N steps | 100 |
|
||
| `--validation_epochs` | Run validation every N epochs | 500 |
|
||
| `--validation_prompts` | Prompts for validation video generation | English prompts |
|
||
| `--validation_paths` | Reference image paths for I2V validation (i2v mode only) | `"asset/1.png"` |
|
||
|
||
**Normal Mode Example** (T2V validation):
|
||
|
||
```bash
|
||
--validation_steps=100 \
|
||
--validation_epochs=500 \
|
||
--validation_prompts="A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
```
|
||
|
||
**ti2v Mode Example** (TI2V validation):
|
||
|
||
```bash
|
||
--validation_paths "asset/1.png" \
|
||
--validation_steps=100 \
|
||
--validation_epochs=500 \
|
||
--validation_prompts="A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
```
|
||
|
||
**i2v Mode Example** (I2V validation):
|
||
|
||
```bash
|
||
--validation_paths "asset/1.png" \
|
||
--validation_steps=100 \
|
||
--validation_epochs=500 \
|
||
--validation_prompts="A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
```
|
||
|
||
**Notes**:
|
||
- Validation videos will be saved to the `output_dir` directory
|
||
- Multiple prompts format: `--validation_prompts "prompt1" "prompt2" "prompt3"`
|
||
- `i2v` or `ti2v` mode requires the `--validation_paths` parameter
|
||
- Wan2.2 validation automatically selects single or dual-Transformer based on `boundary_type`
|
||
|
||
### 3.5 Training with FSDP
|
||
|
||
**If GPU memory is insufficient when using multiple GPUs with DeepSpeed-Zero-2**, you can switch to FSDP for training.
|
||
|
||
```sh
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --mixed_precision="bf16" --use_fsdp --fsdp_auto_wrap_policy TRANSFORMER_BASED_WRAP --fsdp_transformer_layer_cls_to_wrap=WanAttentionBlock --fsdp_sharding_strategy "FULL_SHARD" --fsdp_state_dict_type=SHARDED_STATE_DICT --fsdp_backward_prefetch "BACKWARD_PRE" --fsdp_cpu_ram_efficient_loading False scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_t2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="normal" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
### 3.6 Other Backends
|
||
|
||
#### 3.6.1 Training with DeepSpeed-Zero-3
|
||
|
||
DeepSpeed Zero-3 is not highly recommended at the moment. In this repository, using FSDP has fewer errors and is more stable.
|
||
|
||
DeepSpeed Zero-3 is suitable for 14B Wan at high resolutions. After training, you can use the following command to get the final model:
|
||
```bash
|
||
python scripts/zero_to_bf16.py output_dir/checkpoint-{our-num-steps} output_dir/checkpoint-{your-num-steps}-outputs --max_shard_size 80GB --safe_serialization
|
||
```
|
||
|
||
Training shell command is as follows:
|
||
```sh
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --zero_stage 3 --zero3_save_16bit_model true --zero3_init_flag true --use_deepspeed --deepspeed_config_file config/zero_stage3_config.json --deepspeed_multinode_launcher standard scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_t2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="normal" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
#### 3.6.2 Training Without DeepSpeed and FSDP
|
||
|
||
**This approach is not recommended as it lacks memory-saving backends and may easily cause out-of-memory errors**. Provided here for reference only.
|
||
|
||
```sh
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --mixed_precision="bf16" scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_t2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="normal" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
### 3.7 Multi-Node Distributed Training
|
||
|
||
**Suitable for**: Ultra-large-scale datasets, faster training speed
|
||
|
||
#### 3.7.1 Environment Configuration
|
||
|
||
Assuming 2 machines with 8 GPUs each:
|
||
|
||
**Machine 0 (Master)**:
|
||
```bash
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
export MASTER_ADDR="192.168.1.100" # Master machine IP
|
||
export MASTER_PORT=10086
|
||
export WORLD_SIZE=2 # Total number of machines
|
||
export NUM_PROCESS=16 # Total processes = machines × 8
|
||
export RANK=0 # Current machine rank (0 or 1)
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
accelerate launch --mixed_precision="bf16" --main_process_ip=$MASTER_ADDR --main_process_port=$MASTER_PORT --num_machines=$WORLD_SIZE --num_processes=$NUM_PROCESS --machine_rank=$RANK --use_deepspeed --deepspeed_config_file config/zero_stage2_config.json --deepspeed_multinode_launcher standard scripts/wan2.2/train.py \
|
||
--config_path="config/wan2.2/wan_civitai_t2v.yaml" \
|
||
--pretrained_model_name_or_path=$MODEL_NAME \
|
||
--train_data_dir=$DATASET_NAME \
|
||
--train_data_meta=$DATASET_META_NAME \
|
||
--image_sample_size=640 \
|
||
--video_sample_size=640 \
|
||
--token_sample_size=640 \
|
||
--video_sample_stride=2 \
|
||
--video_sample_n_frames=81 \
|
||
--train_batch_size=1 \
|
||
--video_repeat=1 \
|
||
--gradient_accumulation_steps=1 \
|
||
--dataloader_num_workers=8 \
|
||
--num_train_epochs=100 \
|
||
--checkpointing_steps=50 \
|
||
--learning_rate=2e-05 \
|
||
--lr_scheduler="constant_with_warmup" \
|
||
--lr_warmup_steps=100 \
|
||
--seed=42 \
|
||
--output_dir="output_dir_wan2.2" \
|
||
--gradient_checkpointing \
|
||
--mixed_precision="bf16" \
|
||
--adam_weight_decay=3e-2 \
|
||
--adam_epsilon=1e-10 \
|
||
--vae_mini_batch=1 \
|
||
--max_grad_norm=0.05 \
|
||
--random_hw_adapt \
|
||
--training_with_video_token_length \
|
||
--enable_bucket \
|
||
--uniform_sampling \
|
||
--low_vram \
|
||
--boundary_type="low" \
|
||
--train_mode="normal" \
|
||
--trainable_modules "."
|
||
```
|
||
|
||
**Machine 1 (Worker)**:
|
||
```bash
|
||
export MODEL_NAME="models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
export DATASET_NAME="datasets/X-Fun-Videos-Demo/"
|
||
export DATASET_META_NAME="datasets/X-Fun-Videos-Demo/metadata_add_width_height.json"
|
||
export MASTER_ADDR="192.168.1.100" # Same as Master
|
||
export MASTER_PORT=10086
|
||
export WORLD_SIZE=2
|
||
export NUM_PROCESS=16
|
||
export RANK=1 # Note: this is 1
|
||
# NCCL_IB_DISABLE=1 and NCCL_P2P_DISABLE=1 are used in multi nodes without RDMA.
|
||
# export NCCL_IB_DISABLE=1
|
||
# export NCCL_P2P_DISABLE=1
|
||
NCCL_DEBUG=INFO
|
||
|
||
# Use the same accelerate launch command as Machine 0
|
||
```
|
||
|
||
#### 3.7.2 Multi-Node Training Notes
|
||
|
||
- **Network Requirements**:
|
||
- Recommended: RDMA/InfiniBand (high performance)
|
||
- Without RDMA, add environment variables:
|
||
```bash
|
||
export NCCL_IB_DISABLE=1
|
||
export NCCL_P2P_DISABLE=1
|
||
```
|
||
|
||
- **Data Synchronization**: All machines must have access to the same data paths (NFS/shared storage)
|
||
|
||
---
|
||
|
||
## 4. Inference Testing
|
||
|
||
### 4.1 Inference Parameter Reference
|
||
|
||
**Key Parameter Descriptions**:
|
||
|
||
| Parameter | Description | Example Value |
|
||
|-----------|-------------|---------------|
|
||
| `GPU_memory_mode` | GPU memory management mode, see table below | `model_group_offload` |
|
||
| `ulysses_degree` | Head dimension parallelism, 1 for single GPU | 1 |
|
||
| `ring_degree` | Sequence dimension parallelism, 1 for single GPU | 1 |
|
||
| `fsdp_dit` | Use FSDP for Transformer during multi-GPU inference | `False` |
|
||
| `fsdp_text_encoder` | Use FSDP for text encoder during multi-GPU inference | `True` |
|
||
| `compile_dit` | Compile Transformer for faster inference (effective for fixed resolution) | `False` |
|
||
| `model_name` | Model path | `models/Diffusion_Transformer/Wan2.2-T2V-A14B` |
|
||
| `sampler_name` | Sampler type: `Flow`, `Flow_Unipc`, `Flow_DPM++` | `Flow_Unipc` |
|
||
| `transformer_path` | Path to trained low-noise Transformer weights | `None` |
|
||
| `transformer_high_path` | Path to trained high-noise Transformer weights (dual-Transformer models only) | `None` |
|
||
| `vae_path` | Path to trained VAE weights | `None` |
|
||
| `lora_path` | Low-noise model LoRA weights path | `None` |
|
||
| `lora_high_path` | High-noise model LoRA weights path (dual-Transformer models only) | `None` |
|
||
| `sample_size` | Generated video resolution `[height, width]` | `[480, 832]` or `[832, 480]` |
|
||
| `video_length` | Number of video frames | `81` |
|
||
| `fps` | Frames per second | `16` |
|
||
| `weight_dtype` | Model weight precision, use `torch.float16` for GPUs not supporting bf16 | `torch.bfloat16` |
|
||
| `validation_image_start` | Reference image path for I2V/TI2V mode | `"asset/1.png"` |
|
||
| `prompt` | Positive prompt describing content | `"A brown dog shaking its head..."` |
|
||
| `negative_prompt` | Negative prompt to avoid certain content | `"low resolution, low quality..."` |
|
||
| `guidance_scale` | Guidance strength | 6.0 |
|
||
| `seed` | Random seed for reproducibility | 43 |
|
||
| `num_inference_steps` | Number of inference steps | 50 |
|
||
| `lora_weight` | Low-noise model LoRA weight strength | 0.55 |
|
||
| `lora_high_weight` | High-noise model LoRA weight strength (dual-Transformer models only) | 0.55 |
|
||
| `save_path` | Path to save generated video | `samples/wan-videos-i2v` or `samples/wan-videos-t2v` |
|
||
|
||
**GPU Memory Management Modes**:
|
||
|
||
| Mode | Description | Memory Usage |
|
||
|------|-------------|--------------|
|
||
| `model_full_load` | Full model loaded to GPU | Highest |
|
||
| `model_full_load_and_qfloat8` | Full load + FP8 quantization | High |
|
||
| `model_cpu_offload` | Offload model to CPU after use | Medium |
|
||
| `model_cpu_offload_and_qfloat8` | CPU offload + FP8 quantization | Medium-Low |
|
||
| `model_group_offload` | Layer groups switch between CPU/CUDA | Low |
|
||
| `sequential_cpu_offload` | Layer-by-layer offload (slowest) | Lowest |
|
||
|
||
### 4.2 Text-to-Video (T2V) Inference
|
||
|
||
Run the following command for single GPU inference:
|
||
|
||
```bash
|
||
python examples/wan2.2/predict_t2v.py
|
||
```
|
||
|
||
Edit `examples/wan2.2/predict_t2v.py` according to your needs. For initial inference, focus on the following parameters. If you're interested in other parameters, refer to the inference parameter reference above.
|
||
|
||
```python
|
||
# Choose based on GPU memory
|
||
GPU_memory_mode = "sequential_cpu_offload"
|
||
# Based on actual model path
|
||
model_name = "models/Diffusion_Transformer/Wan2.2-T2V-A14B"
|
||
# Path to trained low-noise weights, e.g., "output_dir_wan2.2/checkpoint-xxx/diffusion_pytorch_model.safetensors"
|
||
transformer_path = None
|
||
# Path to trained high-noise weights (if trained dual-Transformer)
|
||
transformer_high_path = None
|
||
# Write based on the content you want to generate
|
||
prompt = "A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
# ...
|
||
```
|
||
|
||
### 4.3 Image-to-Video (I2V) Inference
|
||
|
||
Run the following command for single GPU inference:
|
||
|
||
```bash
|
||
python examples/wan2.2/predict_i2v.py
|
||
```
|
||
|
||
Edit `examples/wan2.2/predict_i2v.py` according to your needs. For initial inference, focus on the following parameters. If you're interested in other parameters, refer to the inference parameter reference above.
|
||
|
||
```python
|
||
# Choose based on GPU memory
|
||
GPU_memory_mode = "sequential_cpu_offload"
|
||
# Based on actual model path
|
||
model_name = "models/Diffusion_Transformer/Wan2.2-I2V-A14B"
|
||
# Path to trained low-noise weights, e.g., "output_dir_wan2.2/checkpoint-xxx/diffusion_pytorch_model.safetensors"
|
||
transformer_path = None
|
||
# Path to trained high-noise weights
|
||
transformer_high_path = None
|
||
# Starting image for image-to-video
|
||
validation_image_start = "asset/1.png"
|
||
# Write based on the content you want to generate
|
||
prompt = "A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
# ...
|
||
```
|
||
|
||
### 4.3.1 Text-Image-to-Video (TI2V) Inference
|
||
|
||
Run the following command for single GPU inference:
|
||
|
||
```bash
|
||
python examples/wan2.2/predict_ti2v.py
|
||
```
|
||
|
||
Edit `examples/wan2.2/predict_ti2v.py` according to your needs. For initial inference, focus on the following parameters. If you're interested in other parameters, refer to the inference parameter reference above.
|
||
|
||
```python
|
||
# Choose based on GPU memory
|
||
GPU_memory_mode = "sequential_cpu_offload"
|
||
# Based on actual model path (TI2V single model)
|
||
model_name = "models/Diffusion_Transformer/Wan2.2-TI2V-5B"
|
||
# Path to trained weights
|
||
transformer_path = None
|
||
# TI2V has only one model, transformer_high_path is not used
|
||
transformer_high_path = None
|
||
# Starting image for image-to-video
|
||
validation_image_start = "asset/1.png"
|
||
# Write based on the content you want to generate
|
||
prompt = "A brown dog shaking its head, sitting on a light-colored sofa in a cozy room. Behind the dog, there's a framed painting on a shelf, surrounded by pink flowers. The soft, warm lighting in the room creates a comfortable atmosphere."
|
||
# ...
|
||
```
|
||
|
||
### 4.4 Multi-GPU Parallel Inference
|
||
|
||
**Suitable for**: High-resolution generation, accelerated inference
|
||
|
||
#### Install Parallel Inference Dependencies
|
||
|
||
```bash
|
||
pip install xfuser==0.4.2 yunchang==0.6.2
|
||
```
|
||
|
||
#### Configure Parallel Strategy
|
||
|
||
Edit `examples/wan2.2/predict_t2v.py`, `examples/wan2.2/predict_i2v.py`, or `examples/wan2.2/predict_ti2v.py`:
|
||
|
||
```python
|
||
# Ensure ulysses_degree × ring_degree = number of GPUs used
|
||
# For example, using 2 GPUs:
|
||
ulysses_degree = 2 # Head dimension parallelism
|
||
ring_degree = 1 # Sequence dimension parallelism
|
||
```
|
||
|
||
**Configuration Principles**:
|
||
- `ulysses_degree` must be divisible by the model's number of heads
|
||
- `ring_degree` splits along the sequence dimension and affects communication overhead; avoid using it when heads can be evenly divided
|
||
|
||
**Configuration Examples**:
|
||
|
||
| GPU Count | ulysses_degree | ring_degree | Description |
|
||
|-----------|---------------|-------------|-------------|
|
||
| 1 | 1 | 1 | Single GPU |
|
||
| 4 | 4 | 1 | Head parallelism |
|
||
| 8 | 8 | 1 | Head parallelism |
|
||
| 8 | 4 | 2 | Hybrid parallelism |
|
||
|
||
#### Run Multi-GPU Inference
|
||
|
||
```bash
|
||
torchrun --nproc-per-node=2 examples/wan2.2/predict_t2v.py
|
||
```
|
||
|
||
---
|
||
|
||
## 5. Additional Resources
|
||
|
||
- **Official GitHub**: https://github.com/aigc-apps/VideoX-Fun |