utils.py 866 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Utils."""
  2. import enum
  3. from platform import uname
  4. import uuid
  5. import psutil
  6. import torch
  7. class Device(enum.Enum):
  8. GPU = enum.auto()
  9. CPU = enum.auto()
  10. class Counter:
  11. '''A basic counter.'''
  12. def __init__(self, start: int = 0) -> None:
  13. self.counter = start
  14. def __next__(self) -> int:
  15. id = self.counter
  16. self.counter += 1
  17. return id
  18. def reset(self) -> None:
  19. self.counter = 0
  20. def get_gpu_memory(gpu: int = 0) -> int:
  21. """Returns the total memory of the GPU in bytes."""
  22. return torch.cuda.get_device_properties(gpu).total_memory
  23. def get_cpu_memory() -> int:
  24. """Returns the total CPU memory of the node in bytes."""
  25. return psutil.virtual_memory().total
  26. def random_uuid() -> str:
  27. return str(uuid.uuid4().hex)
  28. def in_wsl() -> bool:
  29. return "microsoft" in " ".join(uname()).lower()