diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

FLUX.1-dev dreambooth train on multigpu

Open MehmetcanTozlu opened this issue 2 years ago • 48 comments

Discussed in https://github.com/huggingface/diffusers/discussions/9277

Originally posted by MehmetcanTozlu August 26, 2024 I have 4 A6000 Ada Generation cards. but I get an error when training Flux.1-dev with dreambooth(train_dreambooth_flux.py). can anyone help?

pip list: absl-py 2.1.0 accelerate 0.33.0 bitsandbytes 0.43.3 certifi 2024.7.4 charset-normalizer 3.3.2 click 8.1.7 cmake 3.25.0 diffusers 0.31.0.dev0 docker-pycreds 0.4.0 filelock 3.15.4 fsspec 2024.6.1 ftfy 6.2.3 gitdb 4.0.11 GitPython 3.1.43 grpcio 1.66.0 huggingface-hub 0.24.6 idna 3.8 importlib_metadata 8.4.0 Jinja2 3.1.4 lit 15.0.7 Markdown 3.7 MarkupSafe 2.1.5 mpmath 1.3.0 networkx 3.3 numpy 1.26.4 nvidia-cublas-cu11 11.11.3.6 nvidia-cublas-cu12 12.1.3.1 nvidia-cuda-cupti-cu11 11.8.87 nvidia-cuda-cupti-cu12 12.1.105 nvidia-cuda-nvrtc-cu11 11.8.89 nvidia-cuda-nvrtc-cu12 12.1.105 nvidia-cuda-runtime-cu11 11.8.89 nvidia-cuda-runtime-cu12 12.1.105 nvidia-cudnn-cu11 8.7.0.84 nvidia-cudnn-cu12 9.1.0.70 nvidia-cufft-cu11 10.9.0.58 nvidia-cufft-cu12 11.0.2.54 nvidia-curand-cu11 10.3.0.86 nvidia-curand-cu12 10.3.2.106 nvidia-cusolver-cu11 11.4.1.48 nvidia-cusolver-cu12 11.4.5.107 nvidia-cusparse-cu11 11.7.5.86 nvidia-cusparse-cu12 12.1.0.106 nvidia-nccl-cu11 2.19.3 nvidia-nccl-cu12 2.20.5 nvidia-nvjitlink-cu12 12.6.20 nvidia-nvtx-cu11 11.8.86 nvidia-nvtx-cu12 12.1.105 packaging 24.1 peft 0.12.0 pillow 10.4.0 pip 22.0.2 platformdirs 4.2.2 protobuf 5.27.3 psutil 6.0.0 PyYAML 6.0.2 regex 2024.7.24 requests 2.32.3 safetensors 0.4.4 sentencepiece 0.2.0 sentry-sdk 2.13.0 setproctitle 1.3.3 setuptools 59.6.0 six 1.16.0 smmap 5.0.1 sympy 1.13.2 tensorboard 2.17.1 tensorboard-data-server 0.7.2 tokenizers 0.19.1 torch 2.2.1+cu118 torchaudio 2.2.1+cu118 torchvision 0.17.1+cu118 tqdm 4.66.5 transformers 4.44.2 triton 2.2.0 typing_extensions 4.12.2 urllib3 2.2.2 wandb 0.17.7 wcwidth 0.2.13 Werkzeug 3.0.4 zipp 3.20.0

nvidia-smi: +---------------------------------------------------------------------------------------+ | NVIDIA-SMI 535.161.08 Driver Version: 535.161.08 CUDA Version: 12.2 | |-----------------------------------------+----------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+======================+======================| | 0 NVIDIA RTX 6000 Ada Gene... On | 00000000:00:10.0 Off | Off | | 30% 32C P8 28W / 300W | 1MiB / 49140MiB | 0% Default | | | | N/A | +-----------------------------------------+----------------------+----------------------+ | 1 NVIDIA RTX 6000 Ada Gene... On | 00000000:00:11.0 Off | Off | | 30% 32C P8 24W / 300W | 1MiB / 49140MiB | 0% Default | | | | N/A | +-----------------------------------------+----------------------+----------------------+ | 2 NVIDIA RTX 6000 Ada Gene... On | 00000000:00:1B.0 Off | Off | | 30% 30C P8 22W / 300W | 1MiB / 49140MiB | 0% Default | | | | N/A | +-----------------------------------------+----------------------+----------------------+ | 3 NVIDIA RTX 6000 Ada Gene... On | 00000000:00:1C.0 Off | Off | | 30% 28C P8 24W / 300W | 1MiB / 49140MiB | 0% Default | | | | N/A | +-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=======================================================================================| | No running processes found | +---------------------------------------------------------------------------------------+

run command: accelerate launch --num_processes=4 train_dreambooth_flux.py --pretrained_model_name_or_path="/mnt/s1/flux_dev/FLUX.1-dev" --instance_data_dir="/home/wrusr/flux_train_env/flux_dreambooth_train/dreambooth-datasets_/style_spiderverse" --caption_column="/home/wrusr/flux_train_env/flux_dreambooth_train/dreambooth-datasets_/atext" --instance_prompt="A photo of spdrvrs" --output_dir="/home/wrusr/flux_train_env/flux_dreambooth_train" --resolution=024 --train_batch_size=1 --mixed_precision="fp16" --max_train_steps=500 --revision="fp16" --gradient_accumulation_steps=4 --learning_rate=1e-4 --report_to="wandb" --lr_scheduler="constant" --lr_warmup_steps=0 --seed="0" --push_to_hub

error: 08/26/2024 11:46:54 - INFO - main - Distributed environment: MULTI_GPU Backend: nccl Num processes: 4 Process index: 0 Local process index: 0 Device: cuda:0

Mixed precision type: fp16

08/26/2024 11:46:54 - INFO - main - Distributed environment: MULTI_GPU Backend: nccl Num processes: 4 Process index: 3 Local process index: 3 Device: cuda:3

Mixed precision type: fp16

08/26/2024 11:46:54 - INFO - main - Distributed environment: MULTI_GPU Backend: nccl Num processes: 4 Process index: 2 Local process index: 2 Device: cuda:2

Mixed precision type: fp16

08/26/2024 11:46:54 - INFO - main - Distributed environment: MULTI_GPU Backend: nccl Num processes: 4 Process index: 1 Local process index: 1 Device: cuda:1

Mixed precision type: fp16

You set add_prefix_space. The tokenizer needs to be converted from the slow tokenizers You are using a model of type clip_text_model to instantiate a model of type . This is not supported for all configurations of models and can yield errors. You are using a model of type t5 to instantiate a model of type . This is not supported for all configurations of models and can yield errors. Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:25<00:00, 12.75s/it] Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:25<00:00, 12.91s/it] Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:25<00:00, 12.92s/it] Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:25<00:00, 12.92s/it] {'axes_dims_rope'} was not found in config. Values will be initialized to default values. [2024-08-26 11:48:25,610] torch.distributed.elastic.multiprocessing.api: [WARNING] Sending process 32699 closing signal SIGTERM [2024-08-26 11:48:25,733] torch.distributed.elastic.multiprocessing.api: [WARNING] Sending process 32701 closing signal SIGTERM [2024-08-26 11:48:25,733] torch.distributed.elastic.multiprocessing.api: [WARNING] Sending process 32702 closing signal SIGTERM [2024-08-26 11:48:28,939] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: -9) local_rank: 1 (pid: 32700) of binary: /home/wrusr/flux_train_env/bin/python3.10 Traceback (most recent call last): File "/home/wrusr/flux_train_env/bin/accelerate", line 8, in sys.exit(main()) File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/accelerate/commands/accelerate_cli.py", line 48, in main args.func(args) File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/accelerate/commands/launch.py", line 1097, in launch_command multi_gpu_launcher(args) File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/accelerate/commands/launch.py", line 734, in multi_gpu_launcher distrib_run.run(args) File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/torch/distributed/run.py", line 803, in run elastic_launch( File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 135, in call return launch_agent(self._config, self._entrypoint, list(args)) File "/home/wrusr/flux_train_env/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 268, in launch_agent raise ChildFailedError( torch.distributed.elastic.multiprocessing.errors.ChildFailedError:

train_dreambooth_flux.py FAILED

Failures: <NO_OTHER_FAILURES>

Root Cause (first observed failure): [0]: time : 2024-08-26_11:48:25 host : wr-dev-train-1 rank : 1 (local_rank: 1) exitcode : -9 (pid: 32700) error_file: <N/A> traceback : Signal 9 (SIGKILL) received by PID 32700 ======================================================

MehmetcanTozlu avatar Aug 26 '24 11:08 MehmetcanTozlu

Nothing special @kopyl

compute_environment: LOCAL_MACHINE
debug: false
deepspeed_config:
  gradient_accumulation_steps: 4
  gradient_clipping: 1.0
  offload_optimizer_device: none
  offload_param_device: none
  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: fp16
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

icsl-Jeon avatar Sep 28 '24 14:09 icsl-Jeon

@icsl-Jeon interesting... I'm getting CUDA OOM errors with this config :( I have an H100 with 95 GB of memory...

kopyl avatar Oct 02 '24 20:10 kopyl

Did you grad checkpointing?

icsl-Jeon avatar Oct 05 '24 02:10 icsl-Jeon

Hi, I was using the latest version of diffusers to train lora on flux dev and I was still getting the error about

transformer.config.guidance_embeds

Once I manually edited the source and replaced it with True,

                if True:
                    guidance = torch.tensor([args.guidance_scale], device=accelerator.device)
                    guidance = guidance.expand(model_input.shape[0])
                else:
                    guidance = None

I was able to train with the accelerator config:

compute_environment: LOCAL_MACHINE
debug: false
deepspeed_config:
  gradient_accumulation_steps: 1
  offload_optimizer_device: none
  offload_param_device: none
  zero3_init_flag: false
  zero_stage: 2
distributed_type: DEEPSPEED
downcast_bf16: 'no'
dynamo_config:
  dynamo_backend: INDUCTOR
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

But I keep getting the warning:

Passing `txt_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch Tensor

aravindhv10 avatar Oct 06 '24 07:10 aravindhv10

I guess it is okay to ignore it. I successfully trained dreambooth.

icsl-Jeon avatar Oct 09 '24 11:10 icsl-Jeon

I guess it is okay to ignore it. I successfully trained dreambooth.

what did you change to make it work?

Adoni avatar Oct 09 '24 11:10 Adoni

Hi, I was using the latest version of diffusers to train lora on flux dev and I was still getting the error about

transformer.config.guidance_embeds

Once I manually edited the source and replaced it with True,

                if True:
                    guidance = torch.tensor([args.guidance_scale], device=accelerator.device)
                    guidance = guidance.expand(model_input.shape[0])
                else:
                    guidance = None

I was able to train with the accelerator config:

compute_environment: LOCAL_MACHINE
debug: false
deepspeed_config:
  gradient_accumulation_steps: 1
  offload_optimizer_device: none
  offload_param_device: none
  zero3_init_flag: false
  zero_stage: 2
distributed_type: DEEPSPEED
downcast_bf16: 'no'
dynamo_config:
  dynamo_backend: INDUCTOR
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

But I keep getting the warning:

Passing `txt_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch Tensor

I copied your config, but I still got OOM in H100 (80GB)

Adoni avatar Oct 09 '24 11:10 Adoni

gradient_checkpointing seems must.

icsl-Jeon avatar Oct 10 '24 10:10 icsl-Jeon

I switched to kohya_ss (sd-scripts). Works much better.

kopyl avatar Oct 10 '24 11:10 kopyl

gradient_checkpointing seems must.

Thanks! But actually, I've already turned it on and it still OOM

Adoni avatar Oct 10 '24 11:10 Adoni

gradient_checkpointing seems must.

Thanks! But actually, I've already turned it on and it still OOM

Did you use the same config which icsl-Jeon has?

kopyl avatar Oct 10 '24 12:10 kopyl

gradient_checkpointing seems must.

Thanks! But actually, I've already turned it on and it still OOM

Did you use the same config which icsl-Jeon has?

Yes. I used the same deepspeed config. It helped. Before I using deepspeed, it crashed on the forward stage; and after deepspeed, it crashed ont the backward stage. But it still crashed.

Adoni avatar Oct 10 '24 12:10 Adoni

@Adoni i tried to set it up for days. Nothing helped, so I just switched to training with kohya_ss (Flux branch)

kopyl avatar Oct 11 '24 10:10 kopyl

did you face the same issue with the Dreambooth LoRA script?https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora_flux.py @SunMarc could you maybe also take a look in case it's accelerate related?

linoytsaban avatar Oct 11 '24 11:10 linoytsaban

well deepspeed doesn't work with PEFT LoRAs in Diffusers

bghira avatar Oct 11 '24 12:10 bghira

After trying all of the suggestions on this thread to no avail (even with a H100 80GB 😅) I eventually was able to get this to work by switching from "train_dreambooth_flux.py" to "train_dreambooth_lora_flux.py" as @linoytsaban suggested.

I was failing to appreciate the difference between training Dreambooth (which fine-tunes the entire ~24GB Flux model) and training a LoRA (which trains a much smaller ~10MB subnet). I can't speak to the difference in results without a functional version of the full Dreambooth, but the LoRA version looks okay.

timlenardo avatar Oct 12 '24 17:10 timlenardo

well deepspeed doesn't work with PEFT LoRAs in Diffusers

What is the source of this statement? https://github.com/a-r-r-o-w/cogvideox-factory uses DeepSpeed with LoRA and is perfectly loadable, etc.

sayakpaul avatar Oct 14 '24 03:10 sayakpaul

bugs w/ PEFT+Accelerate that we've run into when finetuning Flux with it across multiple H100 nodes

bghira avatar Oct 14 '24 04:10 bghira

Hmm seems to be related https://github.com/huggingface/diffusers/issues/9665.

sayakpaul avatar Oct 14 '24 04:10 sayakpaul

Hey all,

I investigated this further and I have some good news.

My DeepSpeed config for accelerate is this:

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'
gpu_ids: all
enable_cpu_affinity: false
machine_rank: 0
main_training_function: main
mixed_precision: bf16
num_machines: 1
num_processes: 2
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: false

And then I am launching with the following command:

#!/bin/bash

export CUDA_VISIBLE_DEVICES=1,2
export ACCELERATE_CONFIG_FILE="deepspeed.yaml"

accelerate launch --config_file $ACCELERATE_CONFIG_FILE train_dreambooth_flux.py \
  --pretrained_model_name_or_path="black-forest-labs/FLUX.1-dev" \
  --dataset_name="Norod78/Yarn-art-style"\
  --instance_prompt="a puppy, yarn art style"\
  --output_dir="yarn_art_lora_flux" \
  --mixed_precision="bf16" \
  --use_8bit_adam \
  --weighting_scheme="none"\
  --resolution=1024 \
  --train_batch_size=4 \
  --repeats=1 \
  --learning_rate=1e-4 \
  --guidance_scale=1 \
  --report_to="wandb" \
  --gradient_accumulation_steps=4 \
  --gradient_checkpointing \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --guidance_scale=1\
  --max_train_steps=100 \
  --seed="0"\
  --push_to_hub

Also, note the following patch I had to apply to make it work:

diff --git a/examples/dreambooth/train_dreambooth_flux.py b/examples/dreambooth/train_dreambooth_flux.py
index 8e0f4e09a..a237bea3c 100644
--- a/examples/dreambooth/train_dreambooth_flux.py
+++ b/examples/dreambooth/train_dreambooth_flux.py
@@ -32,7 +32,8 @@ import torch.utils.checkpoint
 import transformers
 from accelerate import Accelerator
 from accelerate.logging import get_logger
-from accelerate.utils import DistributedDataParallelKwargs, ProjectConfiguration, set_seed
+from accelerate.state import AcceleratorState
+from accelerate.utils import DistributedDataParallelKwargs, ProjectConfiguration, set_seed, DistributedType
 from huggingface_hub import create_repo, upload_folder
 from huggingface_hub.utils import insecure_hashlib
 from PIL import Image
@@ -1000,6 +1001,8 @@ def main(args):
         project_config=accelerator_project_config,
         kwargs_handlers=[kwargs],
     )
+    if accelerator.distributed_type == DistributedType.DEEPSPEED:
+        AcceleratorState().deepspeed_plugin.deepspeed_config["overlap_comm"] = False
 
     # Disable AMP for MPS.
     if torch.backends.mps.is_available():
@@ -1120,6 +1123,7 @@ def main(args):
     transformer = FluxTransformer2DModel.from_pretrained(
         args.pretrained_model_name_or_path, subfolder="transformer", revision=args.revision, variant=args.variant
     )
+    use_guidance = True if transformer.config.guidance_embeds else False
 
     transformer.requires_grad_(True)
     vae.requires_grad_(False)
@@ -1580,7 +1584,7 @@ def main(args):
                 )
 
                 # handle guidance
-                if transformer.config.guidance_embeds:
+                if use_guidance:
                     guidance = torch.tensor([args.guidance_scale], device=accelerator.device)
                     guidance = guidance.expand(model_input.shape[0])
                 else:

Alternatively, this works too: https://github.com/huggingface/diffusers/issues/9665#issuecomment-2410391725

I am using two A100s and I am using all the latest versions of the libraries involved (deepspeed, accelerate, peft, transformers, etc.).

Could anyone give this a try and report back?

sayakpaul avatar Oct 14 '24 06:10 sayakpaul