import torch import gc def torch_clear_memory(): try: if torch.cuda.is_available(): torch.cuda.empty_cache() except Exception: pass gc.collect()