[mmdetection] RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. 해결방법
공부하는 피카츄
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one
mmdetection에서 single gpu 에서 실험을 돌릴때는 괜찮은데 multiple gpu를 사용하니까 갑자기 에러가 발생했다.
찾아보니 DDP (Distributed Data Parallel) 관련 에러다.
빠른 해결방법
빠르게 해결하고 싶은 분을 위한 방법이다.
mmdetection/mmdet/apis/train.py 에서
# put model on gpus
if distributed:
find_unused_parameters = cfg.get('find_unused_parameters', False)
# Sets the `find_unused_parameters` parameter in
# torch.nn.parallel.DistributedDataParallel
부분을 다음과 같이 바꾸면 된다.
# put model on gpus
if distributed:
find_unused_parameters = cfg.get('find_unused_parameters', True)
# Sets the `find_unused_parameters` parameter in
# torch.nn.parallel.DistributedDataParallel
정확한 코드라인은 다음 링크를 들어가면 알수 있다.
GitHub - open-mmlab/mmdetection: OpenMMLab Detection Toolbox and Benchmark
OpenMMLab Detection Toolbox and Benchmark. Contribute to open-mmlab/mmdetection development by creating an account on GitHub.
github.com
코드 자체를 바꾸고 싶지 않은 분들은 config 파일에서 find_unused_parameters를 True 로 하게끔 설정하면 될것 같다.
왜 에러가 뜰까?
나의 경우 Faster-RCNN의 backbone을 resnet 에서 mobilenet_v2 로 바꾸니까 에러가 떴다. 신기하게 default setting인 resnet50을 사용하는 경우 에러가 발생하지 않았다. 조사해보니 loss를 계산할때 쓰이지 않는 parameter가 존재하는 경우 해당 에러가 발생한다고 한다. 즉 mobilenet_v2를 사용했을때 loss를 계산할 때 사용되지 않는 parameter나 output값이 존재한다는 것이다.
mmdetection 뿐만 아니라 pytorch DDP 자체에서 많이 발생하는 에러인것 같다.
https://github.com/pytorch/pytorch/issues/43259
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. · Issue #43259 · pytorch/p
🐛 Bug To Reproduce Epoch: 1, iter 0: loss = 10.099 0%| | 1/144967 [00:02<116:54:31, 2.90s/it] Traceback (most recent call last): File "train.py", line 99, in solver.train() File "...
github.com
해당 링크에서도 사람들이 해결방법을 서로 공유하고 있다.
매번 느끼지만 DDP를 제대로 사용하는 것은 어려운 것 같다.
그래도 GPU를 제대로 활용하기 위해서 DDP는 필수다!