facebookresearch / adaptive_teacher

This repo provides the source code for "Cross-Domain Adaptive Teacher for Object Detection".
Other
183 stars 37 forks source link

Distributed training failure #5

Closed litingfeng closed 2 years ago

litingfeng commented 2 years ago

Hi,

When running the training code, I encountered the following issue.

Exception during training: Traceback (most recent call last): File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 402, in train_loop self.run_step_full_semisup() File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 597, in run_step_full_semisup all_label_data, branch="supervised" File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/nn/parallel/distributed.py", line 787, 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 forward 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, dict, iterable). Parameter indices which did not receive grad for rank 1: 66 67 68 69 70 71 72 73 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

Then I added find_unused_parameters=True to DistributedDataParallel() function. And the problem has been solved.

But now I have another issue.

Exception during training: Traceback (most recent call last): File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 403, in train_loop self.run_step_full_semisup() File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 657, in run_step_full_semisup losses.backward() File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/_tensor.py", line 255, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs) File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/autograd/init.py", line 149, in backward allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag 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 65 with name roi_heads.box_predictor.bbox_pred.bias has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration.

The answer from online suggests setting find_unused_parameters=False. But this will cause the previous error.

I was wondering if you have a better solution.

My environment: detectron v0.5 pytorch1.9.0 cuda 11.1

Thanks

yujheli commented 2 years ago

Sorry, I found this is probably a bug using distributed training in the local machine. I was running the code on the internal fblearner cluster (Facebook internal machine) and it worked fine for me. I will try to fix the bug as soon as possible. BTW, if you only use one GPU, I think the code can be running on the local machine for the current version.

michaelku1 commented 2 years ago

Hi,

When running the training code, I encountered the following issue.

Exception during training: Traceback (most recent call last): File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 402, in train_loop self.run_step_full_semisup() File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 597, in run_step_full_semisup all_label_data, branch="supervised" File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/nn/parallel/distributed.py", line 787, 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 forward 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, dict, iterable). Parameter indices which did not receive grad for rank 1: 66 67 68 69 70 71 72 73 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

Then I added find_unused_parameters=True to DistributedDataParallel() function. And the problem has been solved.

But now I have another issue.

Exception during training: Traceback (most recent call last): File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 403, in train_loop self.run_step_full_semisup() File "/research/cbim/vast/tl601/projects/adaptive_teacher/adapteacher/engine/trainer.py", line 657, in run_step_full_semisup losses.backward() File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/_tensor.py", line 255, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs) File "/research/cbim/vast/tl601/anaconda3/envs/adapteacher/lib/python3.7/site-packages/torch/autograd/init.py", line 149, in backward allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag 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 65 with name roi_heads.box_predictor.bbox_pred.bias has been marked as ready twice. This means that multiple autograd engine hooks have fired for this particular parameter during this iteration.

The answer from online suggests setting find_unused_parameters=False. But this will cause the previous error.

I was wondering if you have a better solution.

My environment: detectron v0.5 pytorch1.9.0 cuda 11.1

Thanks

Did you manage to solve the issue? I'm facing the same problem am currently looking into it.

yujheli commented 2 years ago

I kind of fixed it by adding redundant discriminator loss with a very small weight. I will add a pull request soon and the code will be updated.

michaelku1 commented 2 years ago

I kind of fixed it by adding redundant discriminator loss with a very small weight. I will add a pull request soon and the code will be updated.

I think I know what you mean, so the problem is because student model is also initialized with the domain discriminator head, for burn up you are kind of forced to compute the domain loss. I guess in that case multiplying domain loss by 0 should also work.

yujheli commented 2 years ago

I kind of fixed it by adding redundant discriminator loss with a very small weight. I will add a pull request soon and the code will be updated.

I think I know what you mean, so the problem is because student model is also initialized with the domain discriminator head, for burn up you are kind of forced to compute the domain loss. I guess in that case multiplying domain loss by 0 should also work.

I think the mutual learning stage also requires a small loss for the supervised branches on the source domain. The code is updated and please give it another try.

litingfeng commented 2 years ago

It runs well now. But is it normal that the eta is "eta: 5 days, 6:38:33" if I set IMG_PER_BATCH_LABEL and IMG_PER_BATCH_UNLABEL to 16 while 2 days if set to 4?

michaelku1 commented 2 years ago

It runs well now. But is it normal that the eta is "eta: 5 days, 6:38:33" if I set IMG_PER_BATCH_LABEL and IMG_PER_BATCH_UNLABEL to 16 while 2 days if set to 4?

Number of iterations is fixed, batch size is increased, you train more data, and that takes longer time.

michaelku1 commented 2 years ago

I kind of fixed it by adding redundant discriminator loss with a very small weight. I will add a pull request soon and the code will be updated.

I think I know what you mean, so the problem is because student model is also initialized with the domain discriminator head, for burn up you are kind of forced to compute the domain loss. I guess in that case multiplying domain loss by 0 should also work.

I think the mutual learning stage also requires a small loss for the supervised branches on the source domain. The code is updated and please give it another try.

Thanks for the quick fix yujhe! Training seems to be going well!

litingfeng commented 2 years ago

It runs well now. But is it normal that the eta is "eta: 5 days, 6:38:33" if I set IMG_PER_BATCH_LABEL and IMG_PER_BATCH_UNLABEL to 16 while 2 days if set to 4?

Number of iterations is fixed, batch size is increased, you train more data, and that takes longer time.

True. I wasn't aware that it's iterations, not epochs.