MCG-NJU / MeMOTR

[ICCV 2023] MeMOTR: Long-Term Memory-Augmented Transformer for Multi-Object Tracking
https://arxiv.org/abs/2307.15700
MIT License
140 stars 8 forks source link

question about --use-checkpoint #15

Closed mtmyyy closed 4 months ago

mtmyyy commented 5 months ago

当我不使用checkpoint,可以运行但是速度比较慢 当我使用checkpoint,并且CHECKPOINT_LEVEL=2时,会有以下错误 ValueError: Unexpected keyword arguments: use_reentrant features, pos = checkpoint(self.backbone, frame, use_reentrant=False) 点进去def checkpoint(function, *args, **kwargs):checkpoint函数没有use_reentrant这个参数 当我把use_reentrant=False删去或者使用CHECKPOINT_LEVEL=3时,又会有以下错误

  File "main.py", line 120, in <module>
    main(config=merged_config)
  File "main.py", line 103, in main
    train(config=config)
  File "/cver/tcying/ytc/MeMOTR/train_engine.py", line 126, in train
    train_one_epoch(
  File "/cver/tcying/ytc/MeMOTR/train_engine.py", line 238, in train_one_epoch
    loss.backward()
  File "/cver/tcying/lib/python3.8/site-packages/torch/_tensor.py", line 255, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/cver/tcying/lib/python3.8/site-packages/torch/autograd/__init__.py", line 147, in backward
    Variable._execution_engine.run_backward(
  File "/cver/tcying/lib/python3.8/site-packages/torch/autograd/function.py", line 87, in apply
    return self._forward_cls.backward(self, *args)  # type: ignore[attr-defined]
  File "/cver/tcying/lib/python3.8/site-packages/torch/utils/checkpoint.py", line 138, in backward
    torch.autograd.backward(outputs_with_grad, args_with_grad)
  File "/cver/tcying/lib/python3.8/site-packages/torch/autograd/__init__.py", line 147, in backward
    Variable._execution_engine.run_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.
Parameter at index 271 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.

请问该如何解决呢

HELLORPG commented 5 months ago

如果你本身不使用 --use-checkpoint 的情况下可以运行,那应该是没有动机需要使用 --use-checkpoint 的,因为这个参数的目的是压缩显存占用,会降低 ~30-50% 的运行速度。

根据你上一个 issue #13 ,感觉你现在很大概率使用的是 PyTorch 1.9.1,在这个版本中,checkpoint 函数和我们的版本中使用的是有区别的。如果要使用 checkpoint 技术的话,我还是建议你将 PyTorch 升级到 1.12 以及以上的版本,我和相关开发者的讨论在这里,因为我们并没有在 1.9.1 中测试过 checkpoint 功能,并且当初官方这个功能的发展并不完善,因此可能会带来很多额外的问题。

mtmyyy commented 5 months ago

感谢回复 主要是batchsize只能设置为1,大了就跑不了了,并且四张卡训练MOT17需要两三天,所以就很难受。 我没有跑过其他transformer-based tracker,不知道其他的速度怎么样,或者请问一下你跟MOTR的速度差的大吗?

HELLORPG commented 5 months ago

你用的是什么型号的 GPU? 如果你使用 Deformable DETR 架构的话,我们的训练速度和推理速度和 MOTR 都是差不多的。

mtmyyy commented 5 months ago

A5000,每张卡显存24G,batchsize为1的时候,训练的时候显示Max Memory=9657MB左右,但是实际占用每张卡会达到19809MiB左右,按道理来说Deformable DETR不应该这么大呀😂

HELLORPG commented 5 months ago

OKOK,我就是确认一下显存,因为模型在后期会由于帧数的增加会增加显存的占用。 这个实际占用问题不大,PyTorch 会尽力压榨 GPU 的显存空间,所以会出现在系统中看到的实际占用远高于 log 中输出的 mem 的情况。 但是四张卡由于 batch size 变小了可能会带来效果上的损失和不稳定,使用 checkpoint 的话确实可以缓解问题,但是会稍微慢一点并且可能你得升级一下版本,太老的 checkpoint 问题会比较多,尤其是对于我们这种 RNN-like 的结构。

HELLORPG commented 4 months ago

由于长时间没有收到回复,我暂时关闭了这个 issue。如果您后续有需要的话,随时重新打开即可。