Spaces:
Running
Running
Commit
·
56d5816
1
Parent(s):
14ba40f
add docs for image conditioning
Browse files- docs/finetrainers/documentation_README.md +129 -0
- docs/finetrainers/documentation_args.md +316 -0
- docs/finetrainers/documentation_models_README.md +11 -4
- docs/finetrainers/documentation_models_wan.md +65 -3
- docs/finetrainers/documentation_trainers_control_trainer.md +3 -0
- docs/finetrainers/documentation_trainers_sft_trainer.md +3 -0
- docs/finetrainers/examples_training_wan_image_conditioning__train.sh +175 -0
- docs/finetrainers/examples_training_wan_image_conditioning__ttraining.json +26 -0
- docs/finetrainers/examples_training_wan_image_conditioning__tvalidation.json +44 -0
- requirements.txt +3 -2
- requirements_without_flash_attention.txt +3 -2
- train.py +9 -3
docs/finetrainers/documentation_README.md
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# finetrainers 🧪
|
2 |
+
|
3 |
+
Finetrainers is a work-in-progress library to support (accessible) training of diffusion models and various commonly used training algorithms.
|
4 |
+
|
5 |
+
<table align="center">
|
6 |
+
<tr>
|
7 |
+
<td align="center"><video src="https://github.com/user-attachments/assets/aad07161-87cb-4784-9e6b-16d06581e3e5">Your browser does not support the video tag.</video></td>
|
8 |
+
<td align="center"><video src="https://github.com/user-attachments/assets/c23d53e2-b422-4084-9156-3fce9fd01dad">Your browser does not support the video tag.</video></td>
|
9 |
+
</tr>
|
10 |
+
<tr>
|
11 |
+
<th align="center">CogVideoX LoRA training as the first iteration of this project</th>
|
12 |
+
<th align="center">Replication of PikaEffects</th>
|
13 |
+
</tr>
|
14 |
+
</table>
|
15 |
+
|
16 |
+
## Table of Contents
|
17 |
+
|
18 |
+
- [Quickstart](#quickstart)
|
19 |
+
- [Features](#features)
|
20 |
+
- [News](#news)
|
21 |
+
- [Support Matrix](#support-matrix)
|
22 |
+
- [Featured Projects](#featured-projects-)
|
23 |
+
- [Acknowledgements](#acknowledgements)
|
24 |
+
|
25 |
+
## Quickstart
|
26 |
+
|
27 |
+
Clone the repository and make sure the requirements are installed: `pip install -r requirements.txt` and install `diffusers` from source by `pip install git+https://github.com/huggingface/diffusers`. The requirements specify `diffusers>=0.32.1`, but it is always recommended to use the `main` branch of Diffusers for the latest features and bugfixes. Note that the `main` branch for `finetrainers` is also the development branch, and stable support should be expected from the release tags.
|
28 |
+
|
29 |
+
Checkout to the latest stable release tag:
|
30 |
+
|
31 |
+
```bash
|
32 |
+
git fetch --all --tags
|
33 |
+
git checkout tags/v0.1.0
|
34 |
+
```
|
35 |
+
|
36 |
+
Follow the instructions mentioned in the [README](https://github.com/a-r-r-o-w/finetrainers/tree/v0.1.0) for the latest stable release.
|
37 |
+
|
38 |
+
#### Using the main branch
|
39 |
+
|
40 |
+
To get started quickly with example training scripts on the main development branch, refer to the following:
|
41 |
+
- [LTX-Video Pika Effects Crush](./examples/training/sft/ltx_video/crush_smol_lora/)
|
42 |
+
- [CogVideoX Pika Effects Crush](./examples/training/sft/cogvideox/crush_smol_lora/)
|
43 |
+
- [Wan T2V Pika Effects Crush](./examples/training/sft/wan/crush_smol_lora/)
|
44 |
+
|
45 |
+
The following are some simple datasets/HF orgs with good datasets to test training with quickly:
|
46 |
+
- [Disney Video Generation Dataset](https://huggingface.co/datasets/Wild-Heart/Disney-VideoGeneration-Dataset)
|
47 |
+
- [bigdatapw Video Dataset Collection](https://huggingface.co/bigdata-pw)
|
48 |
+
- [Finetrainers HF Dataset Collection](https://huggingface.co/finetrainers)
|
49 |
+
|
50 |
+
Please checkout [`docs/models`](./docs/models/) and [`examples/training`](./examples/training/) to learn more about supported models for training & example reproducible training launch scripts. For a full list of arguments that can be set for training, refer to [`docs/args`](./docs/args.md).
|
51 |
+
|
52 |
+
> [!IMPORTANT]
|
53 |
+
> It is recommended to use Pytorch 2.5.1 or above for training. Previous versions can lead to completely black videos, OOM errors, or other issues and are not tested. For fully reproducible training, please use the same environment as mentioned in [environment.md](./docs/environment.md).
|
54 |
+
|
55 |
+
## Features
|
56 |
+
|
57 |
+
- DDP, FSDP-2 & HSDP support for all models
|
58 |
+
- LoRA and full-rank finetuning; Conditional Control training
|
59 |
+
- Memory-efficient single-GPU training
|
60 |
+
- Auto-detection of commonly used dataset formats
|
61 |
+
- Combined image/video datasets, multiple chainable local/remote datasets, multi-resolution bucketing & more
|
62 |
+
- Memory-efficient precomputation support with/without on-the-fly precomputation for large scale datasets
|
63 |
+
- Standardized model specification format for training arbitrary models
|
64 |
+
- Fake FP8 training (QAT upcoming!)
|
65 |
+
|
66 |
+
## News
|
67 |
+
|
68 |
+
- 🔥 **2025-04-12**: Channel-concatenated control conditioning support added for CogView4 and Wan!
|
69 |
+
- 🔥 **2025-04-08**: `torch.compile` support added!
|
70 |
+
- 🔥 **2025-04-06**: Flux support added!
|
71 |
+
- 🔥 **2025-03-07**: CogView4 support added!
|
72 |
+
- 🔥 **2025-03-03**: Wan T2V support added!
|
73 |
+
- 🔥 **2025-03-03**: We have shipped a complete refactor to support multi-backend distributed training, better precomputation handling for big datasets, model specification format (externally usable for training custom models), FSDP & more.
|
74 |
+
- 🔥 **2025-02-12**: We have shipped a set of tooling to curate small and high-quality video datasets for fine-tuning. See [video-dataset-scripts](https://github.com/huggingface/video-dataset-scripts) documentation page for details!
|
75 |
+
- 🔥 **2025-02-12**: Check out [eisneim/ltx_lora_training_i2v_t2v](https://github.com/eisneim/ltx_lora_training_i2v_t2v/)! It builds off of `finetrainers` to support image to video training for LTX-Video and STG guidance for inference.
|
76 |
+
- 🔥 **2025-01-15**: Support for naive FP8 weight-casting training added! This allows training HunyuanVideo in under 24 GB upto specific resolutions.
|
77 |
+
- 🔥 **2025-01-13**: Support for T2V full-finetuning added! Thanks to [@ArEnSc](https://github.com/ArEnSc) for taking up the initiative!
|
78 |
+
- 🔥 **2025-01-03**: Support for T2V LoRA finetuning of [CogVideoX](https://huggingface.co/docs/diffusers/main/api/pipelines/cogvideox) added!
|
79 |
+
- 🔥 **2024-12-20**: Support for T2V LoRA finetuning of [Hunyuan Video](https://huggingface.co/docs/diffusers/main/api/pipelines/hunyuan_video) added! We would like to thank @SHYuanBest for his work on a training script [here](https://github.com/huggingface/diffusers/pull/10254).
|
80 |
+
- 🔥 **2024-12-18**: Support for T2V LoRA finetuning of [LTX Video](https://huggingface.co/docs/diffusers/main/api/pipelines/ltx_video) added!
|
81 |
+
|
82 |
+
## Support Matrix
|
83 |
+
|
84 |
+
The following trainers are currently supported:
|
85 |
+
|
86 |
+
- [SFT Trainer](./docs/trainer/sft_trainer.md)
|
87 |
+
- [Control Trainer](./docs/trainer/control_trainer.md)
|
88 |
+
|
89 |
+
> [!NOTE]
|
90 |
+
> The following numbers were obtained from the [release branch](https://github.com/a-r-r-o-w/finetrainers/tree/v0.0.1). The `main` branch is unstable at the moment and may use higher memory.
|
91 |
+
|
92 |
+
<div align="center">
|
93 |
+
|
94 |
+
| **Model Name** | **Tasks** | **Min. LoRA VRAM<sup>*</sup>** | **Min. Full Finetuning VRAM<sup>^</sup>** |
|
95 |
+
|:----------------------------------------------:|:-------------:|:----------------------------------:|:---------------------------------------------:|
|
96 |
+
| [LTX-Video](./docs/models/ltx_video.md) | Text-to-Video | 5 GB | 21 GB |
|
97 |
+
| [HunyuanVideo](./docs/models/hunyuan_video.md) | Text-to-Video | 32 GB | OOM |
|
98 |
+
| [CogVideoX-5b](./docs/models/cogvideox.md) | Text-to-Video | 18 GB | 53 GB |
|
99 |
+
| [Wan](./docs/models/wan.md) | Text-to-Video | TODO | TODO |
|
100 |
+
| [CogView4](./docs/models/cogview4.md) | Text-to-Image | TODO | TODO |
|
101 |
+
| [Flux](./docs/models/flux.md) | Text-to-Image | TODO | TODO |
|
102 |
+
|
103 |
+
</div>
|
104 |
+
|
105 |
+
<sub><sup>*</sup>Noted for training-only, no validation, at resolution `49x512x768`, rank 128, with pre-computation, using **FP8** weights & gradient checkpointing. Pre-computation of conditions and latents may require higher limits (but typically under 16 GB).</sub><br/>
|
106 |
+
<sub><sup>^</sup>Noted for training-only, no validation, at resolution `49x512x768`, with pre-computation, using **BF16** weights & gradient checkpointing.</sub>
|
107 |
+
|
108 |
+
If you would like to use a custom dataset, refer to the dataset preparation guide [here](./docs/dataset/README.md).
|
109 |
+
|
110 |
+
## Featured Projects 🔥
|
111 |
+
|
112 |
+
Checkout some amazing projects citing `finetrainers`:
|
113 |
+
- [Diffusion as Shader](https://github.com/IGL-HKUST/DiffusionAsShader)
|
114 |
+
- [SkyworkAI's SkyReels-A1](https://github.com/SkyworkAI/SkyReels-A1) & [SkyReels-A2](https://github.com/SkyworkAI/SkyReels-A2)
|
115 |
+
- [Aether](https://github.com/OpenRobotLab/Aether)
|
116 |
+
- [MagicMotion](https://github.com/quanhaol/MagicMotion)
|
117 |
+
- [eisneim's LTX Image-to-Video](https://github.com/eisneim/ltx_lora_training_i2v_t2v/)
|
118 |
+
- [wileewang's TransPixar](https://github.com/wileewang/TransPixar)
|
119 |
+
- [Feizc's Video-In-Context](https://github.com/feizc/Video-In-Context)
|
120 |
+
|
121 |
+
Checkout the following UIs built for `finetrainers`:
|
122 |
+
- [jbilcke's VideoModelStudio](https://github.com/jbilcke-hf/VideoModelStudio)
|
123 |
+
- [neph1's finetrainers-ui](https://github.com/neph1/finetrainers-ui)
|
124 |
+
|
125 |
+
## Acknowledgements
|
126 |
+
|
127 |
+
* `finetrainers` builds on top of & takes inspiration from great open-source libraries - `transformers`, `accelerate`, `torchtune`, `torchtitan`, `peft`, `diffusers`, `bitsandbytes`, `torchao` and `deepspeed` - to name a few.
|
128 |
+
* Some of the design choices of `finetrainers` were inspired by [`SimpleTuner`](https://github.com/bghira/SimpleTuner).
|
129 |
+
`
|
docs/finetrainers/documentation_args.md
ADDED
@@ -0,0 +1,316 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Arguments
|
2 |
+
|
3 |
+
This document lists all the arguments that can be passed to the `train.py` script. For more information, please take a look at the `finetrainers/args.py` file.
|
4 |
+
|
5 |
+
## Table of contents
|
6 |
+
|
7 |
+
- [General arguments](#general)
|
8 |
+
- [SFT training arguments](#sft-training)
|
9 |
+
- [Control training arguments](#control-training)
|
10 |
+
|
11 |
+
## General
|
12 |
+
|
13 |
+
<!-- TODO(aryan): write a github workflow that automatically updates this page -->
|
14 |
+
|
15 |
+
```
|
16 |
+
PARALLEL ARGUMENTS
|
17 |
+
------------------
|
18 |
+
parallel_backend (`str`, defaults to `accelerate`):
|
19 |
+
The parallel backend to use for training. Choose between ['accelerate', 'ptd'].
|
20 |
+
pp_degree (`int`, defaults to `1`):
|
21 |
+
The degree of pipeline parallelism.
|
22 |
+
dp_degree (`int`, defaults to `1`):
|
23 |
+
The degree of data parallelism (number of model replicas).
|
24 |
+
dp_shards (`int`, defaults to `-1`):
|
25 |
+
The number of data parallel shards (number of model partitions).
|
26 |
+
cp_degree (`int`, defaults to `1`):
|
27 |
+
The degree of context parallelism.
|
28 |
+
|
29 |
+
MODEL ARGUMENTS
|
30 |
+
---------------
|
31 |
+
model_name (`str`):
|
32 |
+
Name of model to train. To get a list of models, run `python train.py --list_models`.
|
33 |
+
pretrained_model_name_or_path (`str`):
|
34 |
+
Path to pretrained model or model identifier from https://huggingface.co/models. The model should be
|
35 |
+
loadable based on specified `model_name`.
|
36 |
+
revision (`str`, defaults to `None`):
|
37 |
+
If provided, the model will be loaded from a specific branch of the model repository.
|
38 |
+
variant (`str`, defaults to `None`):
|
39 |
+
Variant of model weights to use. Some models provide weight variants, such as `fp16`, to reduce disk
|
40 |
+
storage requirements.
|
41 |
+
cache_dir (`str`, defaults to `None`):
|
42 |
+
The directory where the downloaded models and datasets will be stored, or loaded from.
|
43 |
+
tokenizer_id (`str`, defaults to `None`):
|
44 |
+
Identifier for the tokenizer model. This is useful when using a different tokenizer than the default from `pretrained_model_name_or_path`.
|
45 |
+
tokenizer_2_id (`str`, defaults to `None`):
|
46 |
+
Identifier for the second tokenizer model. This is useful when using a different tokenizer than the default from `pretrained_model_name_or_path`.
|
47 |
+
tokenizer_3_id (`str`, defaults to `None`):
|
48 |
+
Identifier for the third tokenizer model. This is useful when using a different tokenizer than the default from `pretrained_model_name_or_path`.
|
49 |
+
text_encoder_id (`str`, defaults to `None`):
|
50 |
+
Identifier for the text encoder model. This is useful when using a different text encoder than the default from `pretrained_model_name_or_path`.
|
51 |
+
text_encoder_2_id (`str`, defaults to `None`):
|
52 |
+
Identifier for the second text encoder model. This is useful when using a different text encoder than the default from `pretrained_model_name_or_path`.
|
53 |
+
text_encoder_3_id (`str`, defaults to `None`):
|
54 |
+
Identifier for the third text encoder model. This is useful when using a different text encoder than the default from `pretrained_model_name_or_path`.
|
55 |
+
transformer_id (`str`, defaults to `None`):
|
56 |
+
Identifier for the transformer model. This is useful when using a different transformer model than the default from `pretrained_model_name_or_path`.
|
57 |
+
vae_id (`str`, defaults to `None`):
|
58 |
+
Identifier for the VAE model. This is useful when using a different VAE model than the default from `pretrained_model_name_or_path`.
|
59 |
+
text_encoder_dtype (`torch.dtype`, defaults to `torch.bfloat16`):
|
60 |
+
Data type for the text encoder when generating text embeddings.
|
61 |
+
text_encoder_2_dtype (`torch.dtype`, defaults to `torch.bfloat16`):
|
62 |
+
Data type for the text encoder 2 when generating text embeddings.
|
63 |
+
text_encoder_3_dtype (`torch.dtype`, defaults to `torch.bfloat16`):
|
64 |
+
Data type for the text encoder 3 when generating text embeddings.
|
65 |
+
transformer_dtype (`torch.dtype`, defaults to `torch.bfloat16`):
|
66 |
+
Data type for the transformer model.
|
67 |
+
vae_dtype (`torch.dtype`, defaults to `torch.bfloat16`):
|
68 |
+
Data type for the VAE model.
|
69 |
+
layerwise_upcasting_modules (`List[str]`, defaults to `[]`):
|
70 |
+
Modules that should have fp8 storage weights but higher precision computation. Choose between ['transformer'].
|
71 |
+
layerwise_upcasting_storage_dtype (`torch.dtype`, defaults to `float8_e4m3fn`):
|
72 |
+
Data type for the layerwise upcasting storage. Choose between ['float8_e4m3fn', 'float8_e5m2'].
|
73 |
+
layerwise_upcasting_skip_modules_pattern (`List[str]`, defaults to `["patch_embed", "pos_embed", "x_embedder", "context_embedder", "^proj_in$", "^proj_out$", "norm"]`):
|
74 |
+
Modules to skip for layerwise upcasting. Layers such as normalization and modulation, when casted to fp8 precision
|
75 |
+
naively (as done in layerwise upcasting), can lead to poorer training and inference quality. We skip these layers
|
76 |
+
by default, and recommend adding more layers to the default list based on the model architecture.
|
77 |
+
compile_modules (`List[str]`, defaults to `[]`):
|
78 |
+
Modules that should be regionally compiled with `torch.compile`. Choose one or more from ['transformer'].
|
79 |
+
|
80 |
+
DATASET ARGUMENTS
|
81 |
+
-----------------
|
82 |
+
dataset_config (`str`):
|
83 |
+
File to a dataset file containing information about training data. This file can contain information about one or
|
84 |
+
more datasets in JSON format. The file must have a key called "datasets", which is a list of dictionaries. Each
|
85 |
+
dictionary must contain the following keys:
|
86 |
+
- "data_root": (`str`)
|
87 |
+
The root directory containing the dataset. This parameter must be provided if `dataset_file` is not provided.
|
88 |
+
- "dataset_file": (`str`)
|
89 |
+
Path to a CSV/JSON/JSONL/PARQUET/ARROW/HF_HUB_DATASET file containing metadata for training. This parameter
|
90 |
+
must be provided if `data_root` is not provided.
|
91 |
+
- "dataset_type": (`str`)
|
92 |
+
Type of dataset. Choose between ['image', 'video'].
|
93 |
+
- "id_token": (`str`)
|
94 |
+
Identifier token appended to the start of each prompt if provided. This is useful for LoRA-type training
|
95 |
+
for single subject/concept/style training, but is not necessary.
|
96 |
+
- "image_resolution_buckets": (`List[Tuple[int, int]]`)
|
97 |
+
Resolution buckets for image. This should be a list of tuples containing 2 values, where each tuple
|
98 |
+
represents the resolution (height, width). All images will be resized to the nearest bucket resolution.
|
99 |
+
This parameter must be provided if `dataset_type` is 'image'.
|
100 |
+
- "video_resolution_buckets": (`List[Tuple[int, int, int]]`)
|
101 |
+
Resolution buckets for video. This should be a list of tuples containing 3 values, where each tuple
|
102 |
+
represents the resolution (num_frames, height, width). All videos will be resized to the nearest bucket
|
103 |
+
resolution. This parameter must be provided if `dataset_type` is 'video'.
|
104 |
+
- "reshape_mode": (`str`)
|
105 |
+
All input images/videos are reshaped using this mode. Choose between the following:
|
106 |
+
["center_crop", "random_crop", "bicubic"].
|
107 |
+
- "remove_common_llm_caption_prefixes": (`boolean`)
|
108 |
+
Whether or not to remove common LLM caption prefixes. See `~constants.py` for the list of common prefixes.
|
109 |
+
dataset_shuffle_buffer_size (`int`, defaults to `1`):
|
110 |
+
The buffer size for shuffling the dataset. This is useful for shuffling the dataset before training. The default
|
111 |
+
value of `1` means that the dataset will not be shuffled.
|
112 |
+
precomputation_items (`int`, defaults to `512`):
|
113 |
+
Number of data samples to precompute at once for memory-efficient training. The higher this value,
|
114 |
+
the more disk memory will be used to save the precomputed samples (conditions and latents).
|
115 |
+
precomputation_dir (`str`, defaults to `None`):
|
116 |
+
The directory where the precomputed samples will be stored. If not provided, the precomputed samples
|
117 |
+
will be stored in a temporary directory of the output directory.
|
118 |
+
precomputation_once (`bool`, defaults to `False`):
|
119 |
+
Precompute embeddings from all datasets at once before training. This is useful to save time during training
|
120 |
+
with smaller datasets. If set to `False`, will save disk space by precomputing embeddings on-the-fly during
|
121 |
+
training when required. Make sure to set `precomputation_items` to a reasonable value in line with the size
|
122 |
+
of your dataset(s).
|
123 |
+
|
124 |
+
DATALOADER_ARGUMENTS
|
125 |
+
--------------------
|
126 |
+
See https://pytorch.org/docs/stable/data.html for more information.
|
127 |
+
|
128 |
+
dataloader_num_workers (`int`, defaults to `0`):
|
129 |
+
Number of subprocesses to use for data loading. `0` means that the data will be loaded in a blocking manner
|
130 |
+
on the main process.
|
131 |
+
pin_memory (`bool`, defaults to `False`):
|
132 |
+
Whether or not to use the pinned memory setting in PyTorch dataloader. This is useful for faster data loading.
|
133 |
+
|
134 |
+
DIFFUSION ARGUMENTS
|
135 |
+
-------------------
|
136 |
+
flow_resolution_shifting (`bool`, defaults to `False`):
|
137 |
+
Resolution-dependent shifting of timestep schedules.
|
138 |
+
[Scaling Rectified Flow Transformers for High-Resolution Image Synthesis](https://arxiv.org/abs/2403.03206).
|
139 |
+
TODO(aryan): We don't support this yet.
|
140 |
+
flow_base_seq_len (`int`, defaults to `256`):
|
141 |
+
Base number of tokens for images/video when applying resolution-dependent shifting.
|
142 |
+
flow_max_seq_len (`int`, defaults to `4096`):
|
143 |
+
Maximum number of tokens for images/video when applying resolution-dependent shifting.
|
144 |
+
flow_base_shift (`float`, defaults to `0.5`):
|
145 |
+
Base shift for timestep schedules when applying resolution-dependent shifting.
|
146 |
+
flow_max_shift (`float`, defaults to `1.15`):
|
147 |
+
Maximum shift for timestep schedules when applying resolution-dependent shifting.
|
148 |
+
flow_shift (`float`, defaults to `1.0`):
|
149 |
+
Instead of training with uniform/logit-normal sigmas, shift them as (shift * sigma) / (1 + (shift - 1) * sigma).
|
150 |
+
Setting it higher is helpful when trying to train models for high-resolution generation or to produce better
|
151 |
+
samples in lower number of inference steps.
|
152 |
+
flow_weighting_scheme (`str`, defaults to `none`):
|
153 |
+
We default to the "none" weighting scheme for uniform sampling and uniform loss.
|
154 |
+
Choose between ['sigma_sqrt', 'logit_normal', 'mode', 'cosmap', 'none'].
|
155 |
+
flow_logit_mean (`float`, defaults to `0.0`):
|
156 |
+
Mean to use when using the `'logit_normal'` weighting scheme.
|
157 |
+
flow_logit_std (`float`, defaults to `1.0`):
|
158 |
+
Standard deviation to use when using the `'logit_normal'` weighting scheme.
|
159 |
+
flow_mode_scale (`float`, defaults to `1.29`):
|
160 |
+
Scale of mode weighting scheme. Only effective when using the `'mode'` as the `weighting_scheme`.
|
161 |
+
|
162 |
+
TRAINING ARGUMENTS
|
163 |
+
------------------
|
164 |
+
training_type (`str`, defaults to `None`):
|
165 |
+
Type of training to perform. Choose between ['lora'].
|
166 |
+
seed (`int`, defaults to `42`):
|
167 |
+
A seed for reproducible training.
|
168 |
+
batch_size (`int`, defaults to `1`):
|
169 |
+
Per-device batch size.
|
170 |
+
train_steps (`int`, defaults to `1000`):
|
171 |
+
Total number of training steps to perform.
|
172 |
+
max_data_samples (`int`, defaults to `2**64`):
|
173 |
+
Maximum number of data samples observed during training training. If lesser than that required by `train_steps`,
|
174 |
+
the training will stop early.
|
175 |
+
gradient_accumulation_steps (`int`, defaults to `1`):
|
176 |
+
Number of gradients steps to accumulate before performing an optimizer step.
|
177 |
+
gradient_checkpointing (`bool`, defaults to `False`):
|
178 |
+
Whether or not to use gradient/activation checkpointing to save memory at the expense of slower
|
179 |
+
backward pass.
|
180 |
+
checkpointing_steps (`int`, defaults to `500`):
|
181 |
+
Save a checkpoint of the training state every X training steps. These checkpoints can be used both
|
182 |
+
as final checkpoints in case they are better than the last checkpoint, and are also suitable for
|
183 |
+
resuming training using `resume_from_checkpoint`.
|
184 |
+
checkpointing_limit (`int`, defaults to `None`):
|
185 |
+
Max number of checkpoints to store.
|
186 |
+
resume_from_checkpoint (`str`, defaults to `None`):
|
187 |
+
Whether training should be resumed from a previous checkpoint. Use a path saved by `checkpointing_steps`,
|
188 |
+
or `"latest"` to automatically select the last available checkpoint.
|
189 |
+
|
190 |
+
OPTIMIZER ARGUMENTS
|
191 |
+
-------------------
|
192 |
+
optimizer (`str`, defaults to `adamw`):
|
193 |
+
The optimizer type to use. Choose between the following:
|
194 |
+
- Torch optimizers: ["adam", "adamw"]
|
195 |
+
- Bitsandbytes optimizers: ["adam-bnb", "adamw-bnb", "adam-bnb-8bit", "adamw-bnb-8bit"]
|
196 |
+
lr (`float`, defaults to `1e-4`):
|
197 |
+
Initial learning rate (after the potential warmup period) to use.
|
198 |
+
lr_scheduler (`str`, defaults to `cosine_with_restarts`):
|
199 |
+
The scheduler type to use. Choose between ['linear', 'cosine', 'cosine_with_restarts', 'polynomial',
|
200 |
+
'constant', 'constant_with_warmup'].
|
201 |
+
lr_warmup_steps (`int`, defaults to `500`):
|
202 |
+
Number of steps for the warmup in the lr scheduler.
|
203 |
+
lr_num_cycles (`int`, defaults to `1`):
|
204 |
+
Number of hard resets of the lr in cosine_with_restarts scheduler.
|
205 |
+
lr_power (`float`, defaults to `1.0`):
|
206 |
+
Power factor of the polynomial scheduler.
|
207 |
+
beta1 (`float`, defaults to `0.9`):
|
208 |
+
beta2 (`float`, defaults to `0.95`):
|
209 |
+
beta3 (`float`, defaults to `0.999`):
|
210 |
+
weight_decay (`float`, defaults to `0.0001`):
|
211 |
+
Penalty for large weights in the model.
|
212 |
+
epsilon (`float`, defaults to `1e-8`):
|
213 |
+
Small value to avoid division by zero in the optimizer.
|
214 |
+
max_grad_norm (`float`, defaults to `1.0`):
|
215 |
+
Maximum gradient norm to clip the gradients.
|
216 |
+
|
217 |
+
VALIDATION ARGUMENTS
|
218 |
+
--------------------
|
219 |
+
validation_dataset_file (`str`, defaults to `None`):
|
220 |
+
Path to a CSV/JSON/PARQUET/ARROW file containing information for validation. The file must contain atleast the
|
221 |
+
"caption" column. Other columns such as "image_path" and "video_path" can be provided too. If provided, "image_path"
|
222 |
+
will be used to load a PIL.Image.Image and set the "image" key in the sample dictionary. Similarly, "video_path"
|
223 |
+
will be used to load a List[PIL.Image.Image] and set the "video" key in the sample dictionary.
|
224 |
+
The validation dataset file may contain other attributes specific to inference/validation such as:
|
225 |
+
- "height" and "width" and "num_frames": Resolution
|
226 |
+
- "num_inference_steps": Number of inference steps
|
227 |
+
- "guidance_scale": Classifier-free Guidance Scale
|
228 |
+
- ... (any number of additional attributes can be provided. The ModelSpecification::validate method will be
|
229 |
+
invoked with the sample dictionary to validate the sample.)
|
230 |
+
validation_steps (`int`, defaults to `500`):
|
231 |
+
Number of training steps after which a validation step is performed.
|
232 |
+
enable_model_cpu_offload (`bool`, defaults to `False`):
|
233 |
+
Whether or not to offload different modeling components to CPU during validation.
|
234 |
+
|
235 |
+
MISCELLANEOUS ARGUMENTS
|
236 |
+
-----------------------
|
237 |
+
tracker_name (`str`, defaults to `finetrainers`):
|
238 |
+
Name of the tracker/project to use for logging training metrics.
|
239 |
+
push_to_hub (`bool`, defaults to `False`):
|
240 |
+
Whether or not to push the model to the Hugging Face Hub.
|
241 |
+
hub_token (`str`, defaults to `None`):
|
242 |
+
The API token to use for pushing the model to the Hugging Face Hub.
|
243 |
+
hub_model_id (`str`, defaults to `None`):
|
244 |
+
The model identifier to use for pushing the model to the Hugging Face Hub.
|
245 |
+
output_dir (`str`, defaults to `None`):
|
246 |
+
The directory where the model checkpoints and logs will be stored.
|
247 |
+
logging_dir (`str`, defaults to `logs`):
|
248 |
+
The directory where the logs will be stored.
|
249 |
+
logging_steps (`int`, defaults to `1`):
|
250 |
+
Training logs will be tracked every `logging_steps` steps.
|
251 |
+
allow_tf32 (`bool`, defaults to `False`):
|
252 |
+
Whether or not to allow the use of TF32 matmul on compatible hardware.
|
253 |
+
nccl_timeout (`int`, defaults to `1800`):
|
254 |
+
Timeout for the NCCL communication.
|
255 |
+
report_to (`str`, defaults to `wandb`):
|
256 |
+
The name of the logger to use for logging training metrics. Choose between ['wandb'].
|
257 |
+
verbose (`int`, defaults to `1`):
|
258 |
+
Whether or not to print verbose logs.
|
259 |
+
- 0: Diffusers/Transformers warning logging on local main process only
|
260 |
+
- 1: Diffusers/Transformers info logging on local main process only
|
261 |
+
- 2: Diffusers/Transformers debug logging on local main process only
|
262 |
+
- 3: Diffusers/Transformers debug logging on all processes
|
263 |
+
```
|
264 |
+
|
265 |
+
## SFT training
|
266 |
+
|
267 |
+
If using `--training_type lora`, these arguments can be specified.
|
268 |
+
|
269 |
+
```
|
270 |
+
rank (int):
|
271 |
+
Rank of the low rank approximation.
|
272 |
+
lora_alpha (int):
|
273 |
+
The lora_alpha parameter to compute scaling factor (lora_alpha / rank) for low-rank matrices.
|
274 |
+
target_modules (`str` or `List[str]`):
|
275 |
+
Target modules for the low rank approximation. Can be a regex string or a list of regex strings.
|
276 |
+
```
|
277 |
+
|
278 |
+
No additional arguments are required for `--training_type full-finetune`.
|
279 |
+
|
280 |
+
## Control training
|
281 |
+
|
282 |
+
If using `--training_type control-lora`, these arguments can be specified.
|
283 |
+
|
284 |
+
```
|
285 |
+
control_type (`str`, defaults to `"canny"`):
|
286 |
+
Control type for the low rank approximation matrices. Can be "canny", "custom".
|
287 |
+
rank (int, defaults to `64`):
|
288 |
+
Rank of the low rank approximation matrix.
|
289 |
+
lora_alpha (int, defaults to `64`):
|
290 |
+
The lora_alpha parameter to compute scaling factor (lora_alpha / rank) for low-rank matrices.
|
291 |
+
target_modules (`str` or `List[str]`, defaults to `"(transformer_blocks|single_transformer_blocks).*(to_q|to_k|to_v|to_out.0|ff.net.0.proj|ff.net.2)"`):
|
292 |
+
Target modules for the low rank approximation matrices. Can be a regex string or a list of regex strings.
|
293 |
+
train_qk_norm (`bool`, defaults to `False`):
|
294 |
+
Whether to train the QK normalization layers.
|
295 |
+
frame_conditioning_type (`str`, defaults to `"full"`):
|
296 |
+
Type of frame conditioning. Can be "index", "prefix", "random", "first_and_last", or "full".
|
297 |
+
frame_conditioning_index (int, defaults to `0`):
|
298 |
+
Index of the frame conditioning. Only used if `frame_conditioning_type` is "index".
|
299 |
+
frame_conditioning_concatenate_mask (`bool`, defaults to `False`):
|
300 |
+
Whether to concatenate the frame mask with the latents across channel dim.
|
301 |
+
```
|
302 |
+
|
303 |
+
If using `--training_type control-full-finetune`, these arguments can be specified.
|
304 |
+
|
305 |
+
```
|
306 |
+
control_type (`str`, defaults to `"canny"`):
|
307 |
+
Control type for the low rank approximation matrices. Can be "canny", "custom".
|
308 |
+
train_qk_norm (`bool`, defaults to `False`):
|
309 |
+
Whether to train the QK normalization layers.
|
310 |
+
frame_conditioning_type (`str`, defaults to `"index"`):
|
311 |
+
Type of frame conditioning. Can be "index", "prefix", "random", "first_and_last", or "full".
|
312 |
+
frame_conditioning_index (int, defaults to `0`):
|
313 |
+
Index of the frame conditioning. Only used if `frame_conditioning_type` is "index".
|
314 |
+
frame_conditioning_concatenate_mask (`bool`, defaults to `False`):
|
315 |
+
Whether to concatenate the frame mask with the latents across channel dim.
|
316 |
+
```
|
docs/finetrainers/documentation_models_README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
#
|
2 |
|
3 |
This directory contains the training-related specifications for all the models we support in `finetrainers`. Each model page has:
|
4 |
- an example training command
|
@@ -20,14 +20,21 @@ The following table shows the algorithms supported for training and the models t
|
|
20 |
|
21 |
| Model | SFT | Control | ControlNet | Distillation |
|
22 |
|:-----------------------------------------:|:---:|:-------:|:----------:|:------------:|
|
23 |
-
| [CogVideoX](./cogvideox.md)
|
24 |
-
| [
|
25 |
-
| [
|
|
|
|
|
|
|
26 |
|
27 |
For launching SFT Training:
|
28 |
- `--training_type lora`: Trains a new set of low-rank weights of the model, yielding a smaller adapter model. Currently, only LoRA is supported from [🤗 PEFT](https://github.com/huggingface/peft)
|
29 |
- `--training_type full-finetune`: Trains the full-rank weights of the model, yielding a full-parameter trained model.
|
30 |
|
|
|
|
|
|
|
|
|
31 |
Any model architecture loadable in diffusers/transformers for above models can be used for training. For example, [SkyReels-T2V](https://huggingface.co/Skywork/SkyReels-V1-Hunyuan-T2V) is a finetune of HunyuanVideo, which is compatible for continual training out-of-the-box. Custom models can be loaded either by writing your own [ModelSpecification](TODO(aryan): add link) or by using the following set of arguments:
|
32 |
- `--tokenizer_id`, `--tokenizer_2_id`, `--tokenizer_3_id`: The tokenizers to use for training in conjunction with text encoder conditioning models.
|
33 |
- `--text_encoder_id`, `--text_encoder_2_id`, `--text_encoder_3_id`: The text encoder conditioning models.
|
|
|
1 |
+
# Finetrainers training documentation
|
2 |
|
3 |
This directory contains the training-related specifications for all the models we support in `finetrainers`. Each model page has:
|
4 |
- an example training command
|
|
|
20 |
|
21 |
| Model | SFT | Control | ControlNet | Distillation |
|
22 |
|:-----------------------------------------:|:---:|:-------:|:----------:|:------------:|
|
23 |
+
| [CogVideoX](./cogvideox.md) | 🤗 | 😡 | 😡 | 😡 |
|
24 |
+
| [CogView4](./cogview4.md) | 🤗 | 🤗 | 😡 | 😡 |
|
25 |
+
| [Flux](./flux.md) | 🤗 | 😡 | 😡 | 😡 |
|
26 |
+
| [HunyuanVideo](./hunyuan_video.md) | 🤗 | 😡 | 😡 | 😡 |
|
27 |
+
| [LTX-Video](./ltx_video.md) | 🤗 | 😡 | 😡 | 😡 |
|
28 |
+
| [Wan](./wan.md) | 🤗 | 🤗 | 😡 | 😡 |
|
29 |
|
30 |
For launching SFT Training:
|
31 |
- `--training_type lora`: Trains a new set of low-rank weights of the model, yielding a smaller adapter model. Currently, only LoRA is supported from [🤗 PEFT](https://github.com/huggingface/peft)
|
32 |
- `--training_type full-finetune`: Trains the full-rank weights of the model, yielding a full-parameter trained model.
|
33 |
|
34 |
+
For launching Control Training:
|
35 |
+
- `--training_type control-lora`: Trains lora-rank weights for additional channel-wise concatenated control condition.
|
36 |
+
- `--training_type control-full-finetune`: Trains the full-rank control conditioned model.
|
37 |
+
|
38 |
Any model architecture loadable in diffusers/transformers for above models can be used for training. For example, [SkyReels-T2V](https://huggingface.co/Skywork/SkyReels-V1-Hunyuan-T2V) is a finetune of HunyuanVideo, which is compatible for continual training out-of-the-box. Custom models can be loaded either by writing your own [ModelSpecification](TODO(aryan): add link) or by using the following set of arguments:
|
39 |
- `--tokenizer_id`, `--tokenizer_2_id`, `--tokenizer_3_id`: The tokenizers to use for training in conjunction with text encoder conditioning models.
|
40 |
- `--text_encoder_id`, `--text_encoder_2_id`, `--text_encoder_3_id`: The text encoder conditioning models.
|
docs/finetrainers/documentation_models_wan.md
CHANGED
@@ -7,6 +7,7 @@ For LoRA training, specify `--training_type lora`. For full finetuning, specify
|
|
7 |
Examples available:
|
8 |
- [PIKA crush effect](../../examples/training/sft/wan/crush_smol_lora/)
|
9 |
- [3DGS dissolve](../../examples/training/sft/wan/3dgs_dissolve/)
|
|
|
10 |
|
11 |
To run an example, run the following from the root directory of the repository (assuming you have installed the requirements and are using Linux/WSL):
|
12 |
|
@@ -36,8 +37,69 @@ video = pipe("<my-awesome-prompt>").frames[0]
|
|
36 |
export_to_video(video, "output.mp4", fps=8)
|
37 |
```
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
You can refer to the following guides to know more about the model pipeline and performing LoRA inference in `diffusers`:
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
7 |
Examples available:
|
8 |
- [PIKA crush effect](../../examples/training/sft/wan/crush_smol_lora/)
|
9 |
- [3DGS dissolve](../../examples/training/sft/wan/3dgs_dissolve/)
|
10 |
+
- [I2V conditioning](../../examples/training/control/wan/image_condition/)
|
11 |
|
12 |
To run an example, run the following from the root directory of the repository (assuming you have installed the requirements and are using Linux/WSL):
|
13 |
|
|
|
37 |
export_to_video(video, "output.mp4", fps=8)
|
38 |
```
|
39 |
|
40 |
+
To use trained Control LoRAs, the following can be used for inference (ideally, you should raise a support request in Diffusers):
|
41 |
+
|
42 |
+
<details>
|
43 |
+
<summary> Control Lora inference </summary>
|
44 |
+
|
45 |
+
```python
|
46 |
+
import numpy as np
|
47 |
+
import torch
|
48 |
+
from diffusers import WanPipeline
|
49 |
+
from diffusers.utils import export_to_video, load_video
|
50 |
+
from finetrainers.trainer.control_trainer.data import apply_frame_conditioning_on_latents
|
51 |
+
from finetrainers.models.utils import _expand_conv3d_with_zeroed_weights
|
52 |
+
from finetrainers.patches import load_lora_weights
|
53 |
+
from finetrainers.patches.dependencies.diffusers.control import control_channel_concat
|
54 |
+
|
55 |
+
dtype = torch.bfloat16
|
56 |
+
device = torch.device("cuda")
|
57 |
+
generator = torch.Generator().manual_seed(0)
|
58 |
+
|
59 |
+
pipe = WanPipeline.from_pretrained("Wan-AI/Wan2.1-T2V-1.3B-Diffusers", torch_dtype=dtype).to(device)
|
60 |
+
|
61 |
+
in_channels = pipe.transformer.config.in_channels
|
62 |
+
patch_channels = pipe.transformer.patch_embedding.in_channels
|
63 |
+
pipe.transformer.patch_embedding = _expand_conv3d_with_zeroed_weights(pipe.transformer.patch_embedding, new_in_channels=2 * patch_channels)
|
64 |
+
|
65 |
+
load_lora_weights(pipe, "/raid/aryan/wan-control-image-condition", "wan-lora")
|
66 |
+
pipe.to(device)
|
67 |
+
|
68 |
+
prompt = "The video shows a vibrant green Mustang GT parked in a parking lot. The car is positioned at an angle, showcasing its sleek design and black rims. The car's hood is black, contrasting with the green body. The Mustang GT logo is visible on the side of the car. The parking lot appears to be empty, with the car being the main focus of the video. The car's position and the absence of other vehicles suggest that the video might be a promotional or showcase video for the Mustang GT. The overall style of the video is simple and straightforward, focusing on the car and its design."
|
69 |
+
control_video = load_video("examples/training/control/wan/image_condition/validation_dataset/0.mp4")
|
70 |
+
height, width, num_frames = 480, 704, 49
|
71 |
+
|
72 |
+
# Take evenly space `num_frames` frames from the control video
|
73 |
+
indices = np.linspace(0, len(control_video) - 1, num_frames).astype(int)
|
74 |
+
control_video = [control_video[i] for i in indices]
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
latents = pipe.prepare_latents(1, in_channels, height, width, num_frames, dtype, device, generator)
|
78 |
+
latents_mean = torch.tensor(pipe.vae.config.latents_mean).view(1, -1, 1, 1, 1).to(latents)
|
79 |
+
latents_std = 1.0 / torch.tensor(pipe.vae.config.latents_std).view(1, -1, 1, 1, 1).to(latents)
|
80 |
+
control_video = pipe.video_processor.preprocess_video(control_video, height=height, width=width)
|
81 |
+
control_video = control_video.to(device=device, dtype=dtype)
|
82 |
+
control_latents = pipe.vae.encode(control_video).latent_dist.sample(generator=generator)
|
83 |
+
control_latents = ((control_latents.float() - latents_mean) * latents_std).to(dtype)
|
84 |
+
control_latents = apply_frame_conditioning_on_latents(
|
85 |
+
control_latents,
|
86 |
+
expected_num_frames=latents.size(2),
|
87 |
+
channel_dim=1,
|
88 |
+
frame_dim=2,
|
89 |
+
frame_conditioning_type="index",
|
90 |
+
frame_conditioning_index=0,
|
91 |
+
concatenate_mask=False,
|
92 |
+
)
|
93 |
+
|
94 |
+
with control_channel_concat(pipe.transformer, ["hidden_states"], [control_latents], dims=[1]):
|
95 |
+
video = pipe(prompt, latents=latents, num_inference_steps=30, generator=generator).frames[0]
|
96 |
+
|
97 |
+
export_to_video(video, "output.mp4", fps=16)
|
98 |
+
```
|
99 |
+
</details>
|
100 |
+
|
101 |
You can refer to the following guides to know more about the model pipeline and performing LoRA inference in `diffusers`:
|
102 |
|
103 |
+
- [Wan in Diffusers](https://huggingface.co/docs/diffusers/main/en/api/pipelines/wan)
|
104 |
+
- [Load LoRAs for inference](https://huggingface.co/docs/diffusers/main/en/tutorials/using_peft_for_inference)
|
105 |
+
- [Merge LoRAs](https://huggingface.co/docs/diffusers/main/en/using-diffusers/merge_loras)
|
docs/finetrainers/documentation_trainers_control_trainer.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Control Trainer
|
2 |
+
|
3 |
+
The Control trainer supports channel-concatenated control conditioning for models either using low-rank adapters or full-rank training. It involves adding extra input channels to the patch embedding layer (referred to as the "control injection" layer in finetrainers), to mix conditioning features into the latent stream. This architecture choice is very common and has been seen before in many models - CogVideoX-I2V, HunyuanVideo-I2V, Alibaba's Fun Control models, etc.
|
docs/finetrainers/documentation_trainers_sft_trainer.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# SFT Trainer
|
2 |
+
|
3 |
+
The SFT trainer supports low-rank and full-rank finetuning of models.
|
docs/finetrainers/examples_training_wan_image_conditioning__train.sh
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
set -e -x
|
4 |
+
|
5 |
+
# export TORCH_LOGS="+dynamo,recompiles,graph_breaks"
|
6 |
+
# export TORCHDYNAMO_VERBOSE=1
|
7 |
+
export WANDB_MODE="offline"
|
8 |
+
export NCCL_P2P_DISABLE=1
|
9 |
+
export NCCL_IB_DISABLE=1
|
10 |
+
export TORCH_NCCL_ENABLE_MONITORING=0
|
11 |
+
export FINETRAINERS_LOG_LEVEL="INFO"
|
12 |
+
|
13 |
+
# Download the validation dataset
|
14 |
+
if [ ! -d "examples/training/control/wan/image_condition/validation_dataset" ]; then
|
15 |
+
echo "Downloading validation dataset..."
|
16 |
+
huggingface-cli download --repo-type dataset finetrainers/OpenVid-1k-split-validation --local-dir examples/training/control/wan/image_condition/validation_dataset
|
17 |
+
else
|
18 |
+
echo "Validation dataset already exists. Skipping download."
|
19 |
+
fi
|
20 |
+
|
21 |
+
# Finetrainers supports multiple backends for distributed training. Select your favourite and benchmark the differences!
|
22 |
+
# BACKEND="accelerate"
|
23 |
+
BACKEND="ptd"
|
24 |
+
|
25 |
+
# In this setting, I'm using 1 GPU on 4-GPU node for training
|
26 |
+
NUM_GPUS=1
|
27 |
+
CUDA_VISIBLE_DEVICES="3"
|
28 |
+
|
29 |
+
# Check the JSON files for the expected JSON format
|
30 |
+
TRAINING_DATASET_CONFIG="examples/training/control/wan/image_condition/training.json"
|
31 |
+
VALIDATION_DATASET_FILE="examples/training/control/wan/image_condition/validation.json"
|
32 |
+
|
33 |
+
# Depending on how many GPUs you have available, choose your degree of parallelism and technique!
|
34 |
+
DDP_1="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 1 --cp_degree 1 --tp_degree 1"
|
35 |
+
DDP_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 2 --dp_shards 1 --cp_degree 1 --tp_degree 1"
|
36 |
+
DDP_4="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 4 --dp_shards 1 --cp_degree 1 --tp_degree 1"
|
37 |
+
DDP_8="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 8 --dp_shards 1 --cp_degree 1 --tp_degree 1"
|
38 |
+
FSDP_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 2 --cp_degree 1 --tp_degree 1"
|
39 |
+
FSDP_4="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 1 --dp_shards 4 --cp_degree 1 --tp_degree 1"
|
40 |
+
HSDP_2_2="--parallel_backend $BACKEND --pp_degree 1 --dp_degree 2 --dp_shards 2 --cp_degree 1 --tp_degree 1"
|
41 |
+
|
42 |
+
# Parallel arguments
|
43 |
+
parallel_cmd=(
|
44 |
+
$DDP_1
|
45 |
+
)
|
46 |
+
|
47 |
+
# Model arguments
|
48 |
+
model_cmd=(
|
49 |
+
--model_name "wan"
|
50 |
+
--pretrained_model_name_or_path "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
51 |
+
--compile_modules transformer
|
52 |
+
)
|
53 |
+
|
54 |
+
# Control arguments
|
55 |
+
control_cmd=(
|
56 |
+
--control_type none
|
57 |
+
--rank 128
|
58 |
+
--lora_alpha 128
|
59 |
+
--target_modules "blocks.*(to_q|to_k|to_v|to_out.0|ff.net.0.proj|ff.net.2)"
|
60 |
+
--frame_conditioning_type index
|
61 |
+
--frame_conditioning_index 0
|
62 |
+
)
|
63 |
+
|
64 |
+
# Dataset arguments
|
65 |
+
dataset_cmd=(
|
66 |
+
--dataset_config $TRAINING_DATASET_CONFIG
|
67 |
+
--dataset_shuffle_buffer_size 32
|
68 |
+
)
|
69 |
+
|
70 |
+
# Dataloader arguments
|
71 |
+
dataloader_cmd=(
|
72 |
+
--dataloader_num_workers 0
|
73 |
+
)
|
74 |
+
|
75 |
+
# Diffusion arguments
|
76 |
+
diffusion_cmd=(
|
77 |
+
--flow_weighting_scheme "logit_normal"
|
78 |
+
)
|
79 |
+
|
80 |
+
# Training arguments
|
81 |
+
# We target just the attention projections layers for LoRA training here.
|
82 |
+
# You can modify as you please and target any layer (regex is supported)
|
83 |
+
training_cmd=(
|
84 |
+
--training_type control-lora
|
85 |
+
--seed 42
|
86 |
+
--batch_size 1
|
87 |
+
--train_steps 10000
|
88 |
+
--gradient_accumulation_steps 1
|
89 |
+
--gradient_checkpointing
|
90 |
+
--checkpointing_steps 1000
|
91 |
+
--checkpointing_limit 2
|
92 |
+
# --resume_from_checkpoint 3000
|
93 |
+
--enable_slicing
|
94 |
+
--enable_tiling
|
95 |
+
)
|
96 |
+
|
97 |
+
# Optimizer arguments
|
98 |
+
optimizer_cmd=(
|
99 |
+
--optimizer "adamw"
|
100 |
+
--lr 2e-5
|
101 |
+
--lr_scheduler "constant_with_warmup"
|
102 |
+
--lr_warmup_steps 1000
|
103 |
+
--lr_num_cycles 1
|
104 |
+
--beta1 0.9
|
105 |
+
--beta2 0.99
|
106 |
+
--weight_decay 1e-4
|
107 |
+
--epsilon 1e-8
|
108 |
+
--max_grad_norm 1.0
|
109 |
+
)
|
110 |
+
|
111 |
+
# Validation arguments
|
112 |
+
validation_cmd=(
|
113 |
+
--validation_dataset_file "$VALIDATION_DATASET_FILE"
|
114 |
+
--validation_steps 501
|
115 |
+
)
|
116 |
+
|
117 |
+
# Miscellaneous arguments
|
118 |
+
miscellaneous_cmd=(
|
119 |
+
--tracker_name "finetrainers-wan-control"
|
120 |
+
--output_dir "/raid/aryan/wan-control-image-condition"
|
121 |
+
--init_timeout 600
|
122 |
+
--nccl_timeout 600
|
123 |
+
--report_to "wandb"
|
124 |
+
)
|
125 |
+
|
126 |
+
# Execute the training script
|
127 |
+
if [ "$BACKEND" == "accelerate" ]; then
|
128 |
+
|
129 |
+
ACCELERATE_CONFIG_FILE=""
|
130 |
+
if [ "$NUM_GPUS" == 1 ]; then
|
131 |
+
ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_1.yaml"
|
132 |
+
elif [ "$NUM_GPUS" == 2 ]; then
|
133 |
+
ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_2.yaml"
|
134 |
+
elif [ "$NUM_GPUS" == 4 ]; then
|
135 |
+
ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_4.yaml"
|
136 |
+
elif [ "$NUM_GPUS" == 8 ]; then
|
137 |
+
ACCELERATE_CONFIG_FILE="accelerate_configs/uncompiled_8.yaml"
|
138 |
+
fi
|
139 |
+
|
140 |
+
accelerate launch --config_file "$ACCELERATE_CONFIG_FILE" --gpu_ids $CUDA_VISIBLE_DEVICES train.py \
|
141 |
+
"${parallel_cmd[@]}" \
|
142 |
+
"${model_cmd[@]}" \
|
143 |
+
"${control_cmd[@]}" \
|
144 |
+
"${dataset_cmd[@]}" \
|
145 |
+
"${dataloader_cmd[@]}" \
|
146 |
+
"${diffusion_cmd[@]}" \
|
147 |
+
"${training_cmd[@]}" \
|
148 |
+
"${optimizer_cmd[@]}" \
|
149 |
+
"${validation_cmd[@]}" \
|
150 |
+
"${miscellaneous_cmd[@]}"
|
151 |
+
|
152 |
+
elif [ "$BACKEND" == "ptd" ]; then
|
153 |
+
|
154 |
+
export CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES
|
155 |
+
|
156 |
+
torchrun \
|
157 |
+
--standalone \
|
158 |
+
--nnodes=1 \
|
159 |
+
--nproc_per_node=$NUM_GPUS \
|
160 |
+
--rdzv_backend c10d \
|
161 |
+
--rdzv_endpoint="localhost:19242" \
|
162 |
+
train.py \
|
163 |
+
"${parallel_cmd[@]}" \
|
164 |
+
"${model_cmd[@]}" \
|
165 |
+
"${control_cmd[@]}" \
|
166 |
+
"${dataset_cmd[@]}" \
|
167 |
+
"${dataloader_cmd[@]}" \
|
168 |
+
"${diffusion_cmd[@]}" \
|
169 |
+
"${training_cmd[@]}" \
|
170 |
+
"${optimizer_cmd[@]}" \
|
171 |
+
"${validation_cmd[@]}" \
|
172 |
+
"${miscellaneous_cmd[@]}"
|
173 |
+
fi
|
174 |
+
|
175 |
+
echo -ne "-------------------- Finished executing script --------------------\n\n"
|
docs/finetrainers/examples_training_wan_image_conditioning__ttraining.json
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"datasets": [
|
3 |
+
{
|
4 |
+
"data_root": "recoilme/aesthetic_photos_xs",
|
5 |
+
"dataset_type": "image",
|
6 |
+
"image_resolution_buckets": [
|
7 |
+
[1024, 1024]
|
8 |
+
],
|
9 |
+
"reshape_mode": "bicubic",
|
10 |
+
"remove_common_llm_caption_prefixes": true
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"data_root": "finetrainers/OpenVid-1k-split",
|
14 |
+
"dataset_type": "video",
|
15 |
+
"video_resolution_buckets": [
|
16 |
+
[49, 512, 512],
|
17 |
+
[49, 768, 768],
|
18 |
+
[49, 1024, 1024],
|
19 |
+
[49, 480, 704],
|
20 |
+
[49, 704, 480]
|
21 |
+
],
|
22 |
+
"reshape_mode": "bicubic",
|
23 |
+
"remove_common_llm_caption_prefixes": true
|
24 |
+
}
|
25 |
+
]
|
26 |
+
}
|
docs/finetrainers/examples_training_wan_image_conditioning__tvalidation.json
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"data": [
|
3 |
+
{
|
4 |
+
"caption": "A vibrant green Mustang GT parked in a parking lot. The car is positioned at an angle, showcasing its sleek design and black rims. The car's hood is black, contrasting with the green body. The Mustang GT logo is visible on the side of the car. The parking lot appears to be empty, with the car being the main focus of the video. The car's position and the absence of other vehicles suggest that the video might be a promotional or showcase video for the Mustang GT. The overall style of the video is simple and straightforward, focusing on the car and its design.",
|
5 |
+
"video_path": "examples/training/control/wan/image_condition/validation_dataset/0.mp4",
|
6 |
+
"num_inference_steps": 30,
|
7 |
+
"num_frames": 49,
|
8 |
+
"height": 480,
|
9 |
+
"width": 704,
|
10 |
+
"frame_conditioning_type": "index",
|
11 |
+
"frame_conditioning_index": 0
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"caption": "A cooking tutorial featuring a man in a kitchen. He is wearing a white t-shirt and a black apron. In the first frame, he is holding a bowl and appears to be explaining something. In the second frame, he is mixing ingredients in the bowl. In the third frame, he is pouring the mixture into another bowl. The kitchen is well-equipped with various appliances and utensils. There are also other people in the background, possibly assisting with the cooking process. The style of the video is informative and instructional, with a focus on the cooking process.",
|
15 |
+
"video_path": "examples/training/control/wan/image_condition/validation_dataset/1.mp4",
|
16 |
+
"num_inference_steps": 30,
|
17 |
+
"num_frames": 49,
|
18 |
+
"height": 480,
|
19 |
+
"width": 704,
|
20 |
+
"frame_conditioning_type": "index",
|
21 |
+
"frame_conditioning_index": 0
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"caption": "A man in a suit and tie, standing against a blue background with a digital pattern. He appears to be speaking or presenting, as suggested by his open mouth and focused expression. The style of the video is likely a news segment or an interview, given the professional attire of the man and the formal setting. The background suggests a modern, technology-focused theme, which could be related to the content of the man's speech or the context of the interview. The overall impression is one of professionalism and seriousness.",
|
25 |
+
"video_path": "examples/training/control/wan/image_condition/validation_dataset/2.mp4",
|
26 |
+
"num_inference_steps": 30,
|
27 |
+
"num_frames": 49,
|
28 |
+
"height": 480,
|
29 |
+
"width": 704,
|
30 |
+
"frame_conditioning_type": "index",
|
31 |
+
"frame_conditioning_index": 0
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"caption": "A man in a workshop, dressed in a black shirt and a beige hat, with a beard and glasses. He is holding a hammer and a metal object, possibly a piece of iron or a tool. The workshop is filled with various tools and equipment, including a workbench, a vice, and a shelf with various items. The man appears to be engaged in a craft or a repair project. The style of the video is realistic and documentary, capturing the man's actions and the environment of the workshop in detail. The focus is on the man and his work, with no additional text or graphics. The lighting is natural, suggesting that the video was taken during the day. The overall impression is of a skilled craftsman at work in his workshop.",
|
35 |
+
"video_path": "examples/training/control/wan/image_condition/validation_dataset/3.mp4",
|
36 |
+
"num_inference_steps": 30,
|
37 |
+
"num_frames": 49,
|
38 |
+
"height": 480,
|
39 |
+
"width": 704,
|
40 |
+
"frame_conditioning_type": "index",
|
41 |
+
"frame_conditioning_index": 0
|
42 |
+
}
|
43 |
+
]
|
44 |
+
}
|
requirements.txt
CHANGED
@@ -3,8 +3,9 @@
|
|
3 |
# For GPU monitoring of NVIDIA chipsets
|
4 |
pynvml
|
5 |
|
6 |
-
|
7 |
-
#finetrainers
|
|
|
8 |
# temporary fix for pip install bug:
|
9 |
#finetrainers @ git+https://github.com/jbilcke-hf/finetrainers-patches.git@fix_missing_sft_trainer_files
|
10 |
|
|
|
3 |
# For GPU monitoring of NVIDIA chipsets
|
4 |
pynvml
|
5 |
|
6 |
+
# we are waiting for the next PyPI release
|
7 |
+
#finetrainers==0.1.0
|
8 |
+
finetrainers @ git+https://github.com/a-r-r-o-w/finetrainers.git@main
|
9 |
# temporary fix for pip install bug:
|
10 |
#finetrainers @ git+https://github.com/jbilcke-hf/finetrainers-patches.git@fix_missing_sft_trainer_files
|
11 |
|
requirements_without_flash_attention.txt
CHANGED
@@ -7,8 +7,9 @@ pynvml
|
|
7 |
#eva-decord==0.6.1
|
8 |
#decord
|
9 |
|
10 |
-
|
11 |
-
#finetrainers
|
|
|
12 |
# temporary fix for pip install bug:
|
13 |
#finetrainers @ git+https://github.com/jbilcke-hf/finetrainers-patches.git@fix_missing_sft_trainer_files
|
14 |
|
|
|
7 |
#eva-decord==0.6.1
|
8 |
#decord
|
9 |
|
10 |
+
# we are waiting for the next PyPI release
|
11 |
+
#finetrainers==0.1.0
|
12 |
+
finetrainers @ git+https://github.com/a-r-r-o-w/finetrainers.git@main
|
13 |
# temporary fix for pip install bug:
|
14 |
#finetrainers @ git+https://github.com/jbilcke-hf/finetrainers-patches.git@fix_missing_sft_trainer_files
|
15 |
|
train.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import sys
|
2 |
import traceback
|
3 |
|
4 |
-
from finetrainers import BaseArgs, SFTTrainer, TrainingType, get_logger
|
5 |
from finetrainers.config import _get_model_specifiction_cls
|
|
|
6 |
from finetrainers.trainer.sft_trainer.config import SFTFullRankConfig, SFTLowRankConfig
|
7 |
|
8 |
|
@@ -35,11 +36,14 @@ def main():
|
|
35 |
training_cls = SFTLowRankConfig
|
36 |
elif training_type == TrainingType.FULL_FINETUNE:
|
37 |
training_cls = SFTFullRankConfig
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
raise ValueError(f"Training type {training_type} not supported.")
|
40 |
|
41 |
-
|
42 |
-
args.extend_args(training_config.add_args, training_config.map_args, training_config.validate_args)
|
43 |
args = args.parse_args()
|
44 |
|
45 |
model_specification_cls = _get_model_specifiction_cls(args.model_name, args.training_type)
|
@@ -64,6 +68,8 @@ def main():
|
|
64 |
|
65 |
if args.training_type in [TrainingType.LORA, TrainingType.FULL_FINETUNE]:
|
66 |
trainer = SFTTrainer(args, model_specification)
|
|
|
|
|
67 |
else:
|
68 |
raise ValueError(f"Training type {args.training_type} not supported.")
|
69 |
|
|
|
1 |
import sys
|
2 |
import traceback
|
3 |
|
4 |
+
from finetrainers import BaseArgs, ControlTrainer, SFTTrainer, TrainingType, get_logger
|
5 |
from finetrainers.config import _get_model_specifiction_cls
|
6 |
+
from finetrainers.trainer.control_trainer.config import ControlFullRankConfig, ControlLowRankConfig
|
7 |
from finetrainers.trainer.sft_trainer.config import SFTFullRankConfig, SFTLowRankConfig
|
8 |
|
9 |
|
|
|
36 |
training_cls = SFTLowRankConfig
|
37 |
elif training_type == TrainingType.FULL_FINETUNE:
|
38 |
training_cls = SFTFullRankConfig
|
39 |
+
elif training_type == TrainingType.CONTROL_LORA:
|
40 |
+
training_cls = ControlLowRankConfig
|
41 |
+
elif training_type == TrainingType.CONTROL_FULL_FINETUNE:
|
42 |
+
training_cls = ControlFullRankConfig
|
43 |
else:
|
44 |
raise ValueError(f"Training type {training_type} not supported.")
|
45 |
|
46 |
+
args.register_args(training_cls())
|
|
|
47 |
args = args.parse_args()
|
48 |
|
49 |
model_specification_cls = _get_model_specifiction_cls(args.model_name, args.training_type)
|
|
|
68 |
|
69 |
if args.training_type in [TrainingType.LORA, TrainingType.FULL_FINETUNE]:
|
70 |
trainer = SFTTrainer(args, model_specification)
|
71 |
+
elif args.training_type in [TrainingType.CONTROL_LORA, TrainingType.CONTROL_FULL_FINETUNE]:
|
72 |
+
trainer = ControlTrainer(args, model_specification)
|
73 |
else:
|
74 |
raise ValueError(f"Training type {args.training_type} not supported.")
|
75 |
|