Clears the backend cache and collects garbage.
()
| 24 | |
| 25 | |
| 26 | def empty_cache(): |
| 27 | """Clears the backend cache and collects garbage.""" |
| 28 | |
| 29 | # Collecting garbage is not an idempotent operation, and to avoid OOM errors, |
| 30 | # gc.collect() has to be called both before and after emptying the backend cache. |
| 31 | # See https://github.com/p-e-w/heretic/pull/17 for details. |
| 32 | gc.collect() |
| 33 | |
| 34 | if torch.cuda.is_available(): |
| 35 | torch.cuda.empty_cache() |
| 36 | elif is_xpu_available(): |
| 37 | torch.xpu.empty_cache() |
| 38 | elif is_mlu_available(): |
| 39 | torch.mlu.empty_cache() # ty:ignore[unresolved-attribute] |
| 40 | elif is_sdaa_available(): |
| 41 | torch.sdaa.empty_cache() # ty:ignore[unresolved-attribute] |
| 42 | elif is_musa_available(): |
| 43 | torch.musa.empty_cache() # ty:ignore[unresolved-attribute] |
| 44 | elif torch.backends.mps.is_available(): |
| 45 | torch.mps.empty_cache() |
| 46 | |
| 47 | gc.collect() |
| 48 | |
| 49 | |
| 50 | def get_nvidia_driver_version() -> str | None: |
no outgoing calls
no test coverage detected