mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-08-31 12:21:29 +08:00
* auto batch finder * fix styling * add description * add different modes * fix copy paste error * better organised code * fix styling * add tests * fix * fix * add some documentation * added CHANGELOG.md * some documentation * update based on review * Update trainer.py * Update docs/source/training_tricks.rst Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com> * Update tests/trainer/test_trainer_tricks.py Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> * Update tests/trainer/test_trainer_tricks.py Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com> * use EvalModelTemplate * param tests * rename * wrap params * rename function * rename * rename param * fix * abs * rename * refactor code * add docs * try * arg * loop * exept * loop * drop bool * docs * docs * added check and test for passing dataloader to fit * styling fix * update based on review Co-authored-by: Nicki Skafte <nugginea@gmail.com> Co-authored-by: William Falcon <waf2107@columbia.edu> Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com> Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> Co-authored-by: Jirka <jirka.borovec@seznam.cz>
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import gc
|
|
import torch
|
|
|
|
|
|
def recursive_detach(in_dict: dict) -> dict:
|
|
"""Detach all tensors in `in_dict`.
|
|
|
|
May operate recursively if some of the values in `in_dict` are dictionaries
|
|
which contain instances of `torch.Tensor`. Other types in `in_dict` are
|
|
not affected by this utility function.
|
|
|
|
Args:
|
|
in_dict:
|
|
|
|
Return:
|
|
out_dict:
|
|
"""
|
|
out_dict = {}
|
|
for k, v in in_dict.items():
|
|
if isinstance(v, dict):
|
|
out_dict.update({k: recursive_detach(v)})
|
|
elif callable(getattr(v, 'detach', None)):
|
|
out_dict.update({k: v.detach()})
|
|
else:
|
|
out_dict.update({k: v})
|
|
return out_dict
|
|
|
|
|
|
def is_oom_error(exception):
|
|
return is_cuda_out_of_memory(exception) \
|
|
or is_cudnn_snafu(exception) \
|
|
or is_out_of_cpu_memory(exception)
|
|
|
|
|
|
def is_cuda_out_of_memory(exception):
|
|
return isinstance(exception, RuntimeError) \
|
|
and len(exception.args) == 1 \
|
|
and "CUDA out of memory." in exception.args[0]
|
|
|
|
|
|
def is_cudnn_snafu(exception):
|
|
return isinstance(exception, RuntimeError) \
|
|
and len(exception.args) == 1 \
|
|
and "cuDNN error: CUDNN_STATUS_NOT_SUPPORTED." in exception.args[0]
|
|
|
|
|
|
def is_out_of_cpu_memory(exception):
|
|
return isinstance(exception, RuntimeError) \
|
|
and len(exception.args) == 1 \
|
|
and "DefaultCPUAllocator: can't allocate memory" in exception.args[0]
|
|
|
|
|
|
def garbage_collection_cuda():
|
|
"""Garbage collection Torch (CUDA) memory."""
|
|
gc.collect()
|
|
if torch.cuda.is_available():
|
|
torch.cuda.empty_cache()
|