Runs garbage collection. Then clears the cache of the available accelerator.
()
| 403 | |
| 404 | |
| 405 | def free_memory(): |
| 406 | """ |
| 407 | Runs garbage collection. Then clears the cache of the available accelerator. |
| 408 | """ |
| 409 | gc.collect() |
| 410 | |
| 411 | if torch.cuda.is_available(): |
| 412 | torch.cuda.empty_cache() |
| 413 | elif torch.backends.mps.is_available(): |
| 414 | torch.mps.empty_cache() |
| 415 | elif is_torch_npu_available(): |
| 416 | torch_npu.npu.empty_cache() |
| 417 | elif hasattr(torch, "xpu") and torch.xpu.is_available(): |
| 418 | torch.xpu.empty_cache() |
| 419 | |
| 420 | |
| 421 | @contextmanager |
searching dependent graphs…