-
Couldn't load subscription status.
- Fork 6.5k
[WIP][Training] Flux Control LoRA training script #10130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 97 commits
Commits
Show all changes
102 commits
Select commit
Hold shift + click to select a range
2829679
update
a-r-r-o-w be67dbd
Merge branch 'main' into flux-new
a-r-r-o-w f56ffb1
add
yiyixuxu 7e4df06
update
a-r-r-o-w 9ea52da
Merge remote-tracking branch 'origin/flux-fill-yiyi' into flux-new
a-r-r-o-w 217e90c
add control-lora conversion script; make flux loader handle norms; fi…
a-r-r-o-w b4f1cbf
control lora updates
a-r-r-o-w 414b30b
remove copied-from
a-r-r-o-w 6b02ac2
create separate pipelines for flux control
a-r-r-o-w 3169bf5
make fix-copies
a-r-r-o-w f7f006d
update docs
a-r-r-o-w 8bb940e
add tests
a-r-r-o-w 9e615fd
fix
a-r-r-o-w 6d168db
Merge branch 'main' into flux-new
a-r-r-o-w 89fd970
Apply suggestions from code review
a-r-r-o-w 73cfc51
remove control lora changes
a-r-r-o-w c94966f
apply suggestions from review
a-r-r-o-w cfe13e7
Revert "remove control lora changes"
a-r-r-o-w 0c959a7
update
a-r-r-o-w 6ef2c8b
update
a-r-r-o-w 42970ee
improve log messages
a-r-r-o-w 2ec93ba
Merge branch 'main' into flux-control-lora
a-r-r-o-w 993f3d3
Merge branch 'main' into flux-control-lora
sayakpaul 6523fa6
updates.
sayakpaul 81ab40b
updates
sayakpaul 4432e73
Merge branch 'main' into flux-control-lora
sayakpaul 0f747c0
Merge branch 'main' into flux-control-lora
sayakpaul 6d0c6dc
Merge branch 'flux-control-lora' into sayak-flux-control-lora
sayakpaul 1633619
support register_config.
sayakpaul b9039b1
fix
sayakpaul 5f94d74
fix
sayakpaul bd31651
fix
sayakpaul e18b7ad
Merge branch 'main' into flux-control-lora
a-r-r-o-w f54ec56
updates
sayakpaul 8032405
updates
sayakpaul 6b70bf7
updates
sayakpaul 3726e2d
fix-copies
sayakpaul b6ca9d9
Merge branch 'main' into flux-control-lora
sayakpaul 908d151
fix
sayakpaul 6af2097
Merge branch 'main' into flux-control-lora
sayakpaul 07d44e7
apply suggestions from review
a-r-r-o-w b66e691
add tests
a-r-r-o-w 66d7466
remove conversion script; enable on-the-fly conversion
a-r-r-o-w d827d1e
Merge branch 'main' into flux-control-lora
a-r-r-o-w 64c821b
bias -> lora_bias.
sayakpaul 30a89a6
fix-copies
sayakpaul bca1eaa
peft.py
sayakpaul 6ce181b
Merge branch 'main' into flux-control-lora
sayakpaul e7df197
fix lora conversion
a-r-r-o-w 5fd9fda
changes
sayakpaul a8c50ba
fix-copies
sayakpaul b12f797
updates for tests
sayakpaul f9bd3eb
fix
sayakpaul 6b35c92
Merge branch 'main' into flux-control-lora
sayakpaul 84c168c
alpha_pattern.
sayakpaul 118ed9b
Merge branch 'main' into flux-control-lora
sayakpaul be1d788
add a test for varied lora ranks and alphas.
sayakpaul 5b1bcd8
revert changes in num_channels_latents = self.transformer.config.in_c…
sayakpaul cde01e3
revert moe
sayakpaul 79af91d
Merge branch 'main' into flux-control-lora
sayakpaul 4b3efcc
Merge branch 'main' into flux-control-lora
sayakpaul f688ecf
add a sanity check on unexpected keys when loading norm layers.
sayakpaul eac6fd1
contro lora.
sayakpaul a6158d7
fixes
sayakpaul 90708fa
fixes
sayakpaul 8765e1b
Merge branch 'flux-control-lora' into flux-control-lora-training-script
sayakpaul d6518b7
Merge branch 'main' into flux-control-lora
sayakpaul 9a83eff
Merge branch 'main' into flux-control-lora-training-script
sayakpaul ecbc4cb
fixes
sayakpaul 55058e2
tests
sayakpaul a8bd03b
reviewer feedback
sayakpaul 49c0242
fix
sayakpaul 8b050ea
proper peft version for lora_bias
sayakpaul 3204627
fix-copies
sayakpaul 6ce2307
Merge branch 'flux-control-lora' into flux-control-lora-training-script
sayakpaul 1330d17
updates
sayakpaul 9007de0
updates
sayakpaul 67bc7e4
Merge branch 'main' into flux-control-lora-training-script
sayakpaul 7521fec
updates
sayakpaul 2b9bfa3
Merge branch 'main' into flux-control-lora
a-r-r-o-w 130e592
remove debug code
a-r-r-o-w b20ec7d
update docs
a-r-r-o-w 79d023a
Merge branch 'main' into flux-control-lora
sayakpaul d1715d3
integration tests
sayakpaul eb862de
Merge branch 'flux-control-lora' into flux-control-lora-training-script
sayakpaul cbad4b3
nis
sayakpaul cd7c155
fuse and unload.
sayakpaul 25616e2
fix
sayakpaul 0b83deb
add slices.
sayakpaul 4d10b33
Merge branch 'flux-control-lora' into flux-control-lora-training-script
sayakpaul b06376f
more updates.
sayakpaul 7d63a2a
button up readme
sayakpaul 9d90c51
train()
sayakpaul f46330b
add full fine-tuning version.
sayakpaul f188e80
fixes
sayakpaul 6c1a60f
Merge branch 'main' into flux-control-lora-training-script
sayakpaul 2610e6a
Merge branch 'main' into flux-control-lora-training-script
sayakpaul ecc16ed
Merge branch 'main' into flux-control-lora-training-script
sayakpaul 7dfe378
Apply suggestions from code review
sayakpaul f1d9550
set_grads_to_none remove.
sayakpaul 02016ca
readme
sayakpaul 64814c9
Merge branch 'main' into flux-control-lora-training-script
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Training Control LoRA with Flux | ||
|
|
||
| This (experimental) example shows how train Control LoRA with [Flux](https://huggingface.co/black-forest-labs/FLUX.1-dev) to condition it with additional structural controls (like depth maps, poses, etc.). | ||
|
|
||
| We simply expand the input channels of Flux.1 Dev from 64 to 128 to allow for additional inputs and then train a regular LoRA on top of it. To account for the newly added input channels, we additional append a LoRA on the underlying layer (`x_embedder`). Inference, however, is performed with the `FluxControlPipeline`. | ||
sayakpaul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| > [!NOTE] | ||
| > **Gated model** | ||
| > | ||
| > As the model is gated, before using it with diffusers you first need to go to the [FLUX.1 [dev] Hugging Face page](https://huggingface.co/black-forest-labs/FLUX.1-dev), fill in the form and accept the gate. Once you are in, you need to log in so that your system knows you’ve accepted the gate. Use the command below to log in: | ||
|
|
||
| ```bash | ||
| huggingface-cli login | ||
| ``` | ||
|
|
||
| Example command: | ||
|
|
||
| ```bash | ||
| accelerate launch train_control_lora_flux.py \ | ||
| --pretrained_model_name_or_path="black-forest-labs/FLUX.1-dev" \ | ||
| --dataset_name="raulc0399/open_pose_controlnet" \ | ||
| --output_dir="pose-control-lora" \ | ||
| --mixed_precision="bf16" \ | ||
| --train_batch_size=1 \ | ||
| --rank=64 \ | ||
| --gradient_accumulation_steps=4 \ | ||
| --gradient_checkpointing \ | ||
| --use_8bit_adam \ | ||
| --learning_rate=1e-4 \ | ||
| --report_to="wandb" \ | ||
| --lr_scheduler="constant" \ | ||
| --lr_warmup_steps=0 \ | ||
| --max_train_steps=5000 \ | ||
| --validation_image="openpose.png" \ | ||
| --validation_prompt="A couple, 4k photo, highly detailed" \ | ||
| --seed="0" \ | ||
| --push_to_hub | ||
| ``` | ||
|
|
||
| `openpose.png` comes from [here](https://huggingface.co/Adapter/t2iadapter/resolve/main/openpose.png). | ||
|
|
||
| You need to install `diffusers` from the branch of [this PR](https://github.com/huggingface/diffusers/pull/9999). When it's merged, you should install `diffusers` from the `main`. | ||
|
|
||
| The training script exposes additional CLI args that might be useful to experiment with: | ||
|
|
||
| * `use_lora_bias`: When set, additionally trains the biases of the `lora_B` layer. | ||
| * `train_norm_layers`: When set, additionally trains the normalization scales. Takes care of saving and loading. | ||
| * `lora_layers`: Specify the layers you want to apply LoRA to. If you specify "all-linear", all the linear layers will be LoRA-attached. | ||
|
|
||
| ## Training with DeepSpeed | ||
|
|
||
| It's possible to train with [DeepSpeed](https://github.com/microsoft/DeepSpeed), specifically leveraging the Zero2 system optimization. To use it, save the following config to an YAML file (feel free to modify as needed): | ||
|
|
||
| ```yaml | ||
| compute_environment: LOCAL_MACHINE | ||
| debug: false | ||
| deepspeed_config: | ||
| gradient_accumulation_steps: 1 | ||
| gradient_clipping: 1.0 | ||
| offload_optimizer_device: cpu | ||
| offload_param_device: cpu | ||
| zero3_init_flag: false | ||
| zero_stage: 2 | ||
| distributed_type: DEEPSPEED | ||
| downcast_bf16: 'no' | ||
| enable_cpu_affinity: false | ||
| machine_rank: 0 | ||
| main_training_function: main | ||
| mixed_precision: bf16 | ||
| num_machines: 1 | ||
| num_processes: 1 | ||
| rdzv_backend: static | ||
| same_network: true | ||
| tpu_env: [] | ||
| tpu_use_cluster: false | ||
| tpu_use_sudo: false | ||
| use_cpu: false | ||
| ``` | ||
|
|
||
| And then while launching training, pass the config file: | ||
|
|
||
| ```bash | ||
| accelerate launch --config_file=CONFIG_FILE.yaml ... | ||
| ``` | ||
|
|
||
| ## Full fine-tuning | ||
|
|
||
| We provide a non-LoRA version of the training script `train_control_flux.py`. Here is an example command: | ||
|
|
||
| ```bash | ||
| accelerate launch --config_file=accelerate_ds2.yaml train_control_flux.py \ | ||
| --pretrained_model_name_or_path="black-forest-labs/FLUX.1-dev" \ | ||
| --dataset_name="raulc0399/open_pose_controlnet" \ | ||
| --output_dir="pose-control" \ | ||
| --mixed_precision="bf16" \ | ||
| --train_batch_size=2 \ | ||
| --dataloader_num_workers=4 \ | ||
| --gradient_accumulation_steps=4 \ | ||
| --gradient_checkpointing \ | ||
| --use_8bit_adam \ | ||
| --proportion_empty_prompts=0.2 \ | ||
| --learning_rate=5e-5 \ | ||
| --adam_weight_decay=1e-4 \ | ||
| --set_grads_to_none \ | ||
| --report_to="wandb" \ | ||
| --lr_scheduler="cosine" \ | ||
| --lr_warmup_steps=1000 \ | ||
| --checkpointing_steps=1000 \ | ||
| --max_train_steps=10000 \ | ||
| --validation_steps=200 \ | ||
| --validation_image "2_pose_1024.jpg" "3_pose_1024.jpg" \ | ||
| --validation_prompt "two friends sitting by each other enjoying a day at the park, full hd, cinematic" "person enjoying a day at the park, full hd, cinematic" \ | ||
| --seed="0" \ | ||
| --push_to_hub | ||
| ``` | ||
|
|
||
| Change the `validation_image` and `validation_prompt` as needed. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| transformers==4.47.0 | ||
| wandb | ||
| torch | ||
| torchvision | ||
| accelerate==1.2.0 | ||
| peft>=0.14.0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.