Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Save optimizer.state in cpu by default. #966

Merged
merged 5 commits into from Apr 26, 2023

Conversation

HAOCHENYE
Copy link
Collaborator

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Runner will save optimizer.state in GPU by default if cuda is available. If the user's device does not have a GPU, an error will be raised like this:

CUDA_VISIBLE_DEVICES= python -c "import torch;torch.load('work_dir/epoch_1.pth')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 712, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 1049, in _load
    result = unpickler.load()
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 1019, in persistent_load
    load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 1001, in load_tensor
    wrap_storage=restore_location(storage, location),
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 175, in default_restore_location
    result = fn(storage, location)
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 152, in _cuda_deserialize
    device = validate_cuda_device(location)
  File "/home/yehaochen/anaconda3/envs/pytorch112python38/lib/python3.8/site-packages/torch/serialization.py", line 136, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

This PR makes Runner save optimizer.state to CPU by default.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@codecov
Copy link

codecov bot commented Feb 27, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@8beacd3). Click here to learn what that means.
Patch has no changes to coverable lines.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #966   +/-   ##
=======================================
  Coverage        ?   77.91%           
=======================================
  Files           ?      139           
  Lines           ?    11311           
  Branches        ?     2288           
=======================================
  Hits            ?     8813           
  Misses          ?     2103           
  Partials        ?      395           
Flag Coverage Δ
unittests 77.91% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

mmengine/utils/misc.py Outdated Show resolved Hide resolved
mmengine/utils/misc.py Outdated Show resolved Hide resolved
mmengine/utils/misc.py Outdated Show resolved Hide resolved
mmengine/utils/misc.py Outdated Show resolved Hide resolved
mmengine/runner/runner.py Outdated Show resolved Hide resolved
@HAOCHENYE HAOCHENYE requested a review from C1rN09 as a code owner April 6, 2023 13:26
@zhouzaida zhouzaida added this to the 0.7.3 milestone Apr 21, 2023
@zhouzaida zhouzaida merged commit 6ba667c into open-mmlab:main Apr 26, 2023
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants