KohakuBlueleaf / HyperKohaku

A diffusers based implementation of HyperDreamBooth
Apache License 2.0
125 stars 7 forks source link

Multi GPU parallelization #7

Closed sasso-effe closed 1 year ago

sasso-effe commented 1 year ago

Hi! Thank you for the amazing work. Training on a single GPU is quite slow, and since the project uses accelerate, I was expecting it to run also on multiple GPUs. However, after some small tweaks (removing the --put_in_cpu flag when training the preoptimized loras, and substituting hypernetwork.train_params() with hypernetwork.module.train_params()) I am stuck with this error:

Traceback (most recent call last):                                                                                                                            
  File "/home/hyper_dreambooth/./train_hyperdreambooth.py", line 1323, in <module>                                              
    main(args)                                                                                                                                                
  File "/home/hyper_dreambooth/./train_hyperdreambooth.py", line 1117, in main                                                  
    pred_weights = hypernetwork(pixel_values)                                                                                                                 
  File "/home/mambaforge/envs/hyper/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl                                  
    return forward_call(*args, **kwargs)                                                                                                                      
  File "/home/mambaforge/envs/hyper/lib/python3.9/site-packages/torch/nn/parallel/distributed.py", line 1139, in forward                               
    if torch.is_grad_enabled() and self.reducer._rebuild_buckets():                                                                                           
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that 
were not used in producing loss. You can enable unused parameter detection by passing the keyword argument `find_unused_parameters=True` to `torch.nn.parallel
.DistributedDataParallel`, and by                                                                                                                             
making sure all `forward` function outputs participate in calculating loss.                                                                                   
If you already have done the above, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's `f
orward` function. Please include the loss function and the structure of the return value of `forward` of your module when reporting this issue (e.g. list, dic
t, iterable).                                                                                                                                                 
Parameter indices which did not receive grad for rank 3: 1                                                                                                    
 In addition, you can set the environment variable TORCH_DISTRIBUTED_DEBUG to either INFO or DETAIL to print out information about which particular parameters
 did not receive gradient on this rank as part of this error  

Am I doing something wrong, or is the code intended to run only on a single GPU?

sasso-effe commented 1 year ago

The solution was super dumb. It's sufficient to follow what stated in the error:

    ddp_kwargs = DistributedDataParallelKwargs(find_unused_parameters=True)
    accelerator = Accelerator(
        gradient_accumulation_steps=args.gradient_accumulation_steps,
        mixed_precision=args.mixed_precision,
        log_with=args.report_to,
        project_config=accelerator_project_config,
        kwargs_handlers=[ddp_kwargs]
    )
YoucanBaby commented 1 year ago

Hello~! Thx for your code. I change code according to your way. But get this error:

Traceback (most recent call last):
  File "/home/xyf/Personalization/HyperDreamBooth/./train_preoptimized_liloras.py", line 1225, in <module>
    main(args)
  File "/home/xyf/Personalization/HyperDreamBooth/./train_preoptimized_liloras.py", line 1111, in main
    accelerator.backward(loss)
  File "/home/xyf/miniconda3/envs/py310/lib/python3.10/site-packages/accelerate/accelerator.py", line 1853, in backward
    loss.backward(**kwargs)
  File "/home/xyf/miniconda3/envs/py310/lib/python3.10/site-packages/torch/_tensor.py", line 487, in backward
    torch.autograd.backward(
  File "/home/xyf/miniconda3/envs/py310/lib/python3.10/site-packages/torch/autograd/__init__.py", line 200, in backward
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass

RuntimeError: Expected to mark a variable ready only once. This error is caused by one of the following reasons: 
1) Use of a module parameter outside the `forward` function. Please make sure model parameters are not shared across 
multiple concurrent forward-backward passes. or try to use _set_static_graph() as a workaround if this module graph does not
 change during training loop.

2) Reused parameters in multiple reentrant backward passes. For example, if you use multiple `checkpoint` functions to wrap 
the same part of your model, it would result in the same set of parameters been used by different reentrant backward passes 
multiple times, and hence marking a variable ready multiple times. 
DDP does not support such use cases in default. You can try to use _set_static_graph() as a workaround if your module graph 
does not change over iterations.

Parameter at index 14999 has been marked as ready twice. This means that multiple autograd engine  hooks have fired for this 
particular parameter during this iteration. You can set the environment variable TORCH_DISTRIBUTED_DEBUG to either INFO or
 DETAIL to print parameter names for further debugging.

The error suggests that certain parameters are being used to compute the loss twice. Have you come across this issue before?

sasso-effe commented 1 year ago

Nope, I didn't have this issue. Are you using the .sh file to launch the script? What version of torch are you using?

YoucanBaby commented 1 year ago

Nope, I didn't have this issue. Are you using the .sh file to launch the script? What version of torch are you using?

The code for DDP works good in 3090, but it fails on the A100.