haosulab / ManiSkill2-Learn

Apache License 2.0
80 stars 16 forks source link

Multi-GPU training results in RuntimeError #17

Closed ErikKrauter closed 1 year ago

ErikKrauter commented 1 year ago

I am trying to run the example training script run_ppo.sh in order to train a ppo agent on mulitple GPUs. However, I am getting a RuntimeError in line 288, in update_actor_critic loss.backward():

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. arameter at index 14 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 code is executed on three GeForce GTX TITAN X GPUs. I have run the training script with --gpu-ids 0 1 2 I am very new to CUDA and multiprocessing in pytorch. I would appreciate assistance in debugging this issue. Let me know if you need further information.

xuanlinli17 commented 1 year ago

This is caused by the find_unused_parameters=True setting in DDP. For PPO, the actor network's gaussian head's std parameter is unused during agent forward (and thus automatically marked as ready for DDP reduce purposes since it's unused and we have find_unused_parameters=True), but it actually has gradients when performing agent updates, which DDP is not aware of, causing DDP errors. For now you can remove the find_unused_parameters=True in L302 of maniskill2_learn/utils/torch/module_utils.py to train PPO (though, find_unused_parameters=True is necessary when training SAC).

xuanlinli17 commented 1 year ago

Now we pushed a fix, should work for PPO w/o modifying source code

ErikKrauter commented 1 year ago

Thank you. With find_unused_parameters=False it works. I have another question concerning that flag. If I use find_unused_parameters=False with SAC, will it actually change the outcome of the training, meaning the success rate and other KPIs, or will that only affect training performance in terms of training time? I would like to keep the flag set to false for all cases, just to not mess with the inner workings of the library all the time and potentially altering performance without deliberately wanting to.

xuanlinli17 commented 1 year ago

find_unused_parameters does not affect performance. However torch will report an error when find_unused_parameters=False for SAC, since in SAC, some parameters are not used when updating some parts of the network, so find_unused_parameters is necessary.

You can pull the latest code. We fixed the issue by forcibly setting find_unused_parameters=False for PPO and True for other algorithms.