add available memory check to accelerators #4508
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are many scenarios where we need to get an accurate estimate of the available memory on a device. Currently we rely on the torch memory allocator stats to give us this information, however there are several cases where memory may be allocated outside the view of torch. This means that
torch.cuda.get_device_properties(device_index).total_memory - torch.cuda.memory_allocated(device_index)
is not accurate. This is usually less of a problem on data center GPUs but quite common on consumer grade GPUs that are often shared between torch and the operating system.This PR introduces
available_memory
to the abstract accelerator interface. On CUDA devices we can rely onpynvml
to get the ground truth w.r.t. available memory.This also introduces a hard dependency on
pynvml
. I have tested on non-GPU systems and this package seems to install successfully but fails at runtime at thenvmlInit()
call. We fall back to using torch stats for memory in cases where pynvml is not functional.