peft icon indicating copy to clipboard operation
peft copied to clipboard

Error occurs in distributed training with gradient checkpointing

Open hiyouga opened this issue 1 year ago • 0 comments

Dear @younesbelkada @pacman100,

I got a Runtime Error when training a model with accelerate when gradient checkpointing was enabled

accelerate launch main.py --args
model.enable_input_require_grads()
model.gradient_checkpointing_enable()

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 55 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. Parameter at index 55 with name base_model.model.transformer.layers.27.attention.query_key_value.lora_B.default.weight has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration.

But if I turn off gradient checkpointing, it does work.

[W reducer.cpp:1300] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration, which can adversely affect performance. If your model indeed never has any unused parameters in the forward pass, consider turning this flag off. Note that this warning may be a false positive if your model has flow control causing later iterations to have unused parameters. (function operator()) {'loss': 2.8811, 'learning_rate': 4.996842391515044e-05, 'epoch': 0.03}
{'loss': 2.7382, 'learning_rate': 4.977574773353732e-05, 'epoch': 0.05}
{'loss': 2.4719, 'learning_rate': 4.940928748292363e-05, 'epoch': 0.08}
{'loss': 2.3728, 'learning_rate': 4.887161361866608e-05, 'epoch': 0.11}
{'loss': 2.1218, 'learning_rate': 4.81664975366043e-05, 'epoch': 0.13}
{'loss': 2.1161, 'learning_rate': 4.7298885119428773e-05, 'epoch': 0.16}

However, I want to enable gradient checkpointing to save the memory. Is there a solution to it?

hiyouga avatar Apr 14 '23 18:04 hiyouga