Created
July 15, 2022 07:24
-
-
Save gatheluck/7bcf5bf5dcffd0f218b243d05e313e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import dataclasses | |
def cuda_memory_info(device = torch.device("cuda")): | |
@dataclasses.dataclass | |
class CudaMemoryInfo: | |
device: torch.device | |
total: int | |
reserved: int | |
allocated: int | |
reserved_percent: int = dataclasses.field(init=False) | |
allocated_percent: int = dataclasses.field(init=False) | |
def __post_init__(self): | |
self.reserved_percent = int(self.reserved * 100.0 // self.total) | |
self.allocated_percent = int(self.allocated * 100.0 // self.total) | |
return CudaMemoryInfo( | |
device=device, | |
total=torch.cuda.get_device_properties(device=device).total_memory, | |
reserved=torch.cuda.memory_reserved(device=device), | |
allocated=torch.cuda.memory_allocated(device=device), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment