utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Utils."""
  2. from os import path
  3. import enum
  4. from platform import uname
  5. import uuid
  6. import psutil
  7. import torch
  8. from aphrodite import cuda_utils
  9. class Device(enum.Enum):
  10. GPU = enum.auto()
  11. CPU = enum.auto()
  12. class Counter:
  13. def __init__(self, start: int = 0) -> None:
  14. self.counter = start
  15. def __next__(self) -> int:
  16. i = self.counter
  17. self.counter += 1
  18. return i
  19. def reset(self) -> None:
  20. self.counter = 0
  21. def get_max_shared_memory_bytes(gpu: int = 0) -> int:
  22. """Returns the maximum shared memory per thread block in bytes."""
  23. # https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html
  24. cudaDevAttrMaxSharedMemoryPerBlockOptin = 97 # pylint: disable=invalid-name
  25. max_shared_mem = cuda_utils.get_device_attribute(
  26. cudaDevAttrMaxSharedMemoryPerBlockOptin, gpu)
  27. return int(max_shared_mem)
  28. def get_gpu_memory(gpu: int = 0) -> int:
  29. """Returns the total memory of the GPU in bytes."""
  30. return torch.cuda.get_device_properties(gpu).total_memory
  31. def get_cpu_memory() -> int:
  32. """Returns the total CPU memory of the node or container in bytes."""
  33. memory_limit = psutil.virtual_memory().total
  34. for limit_file in [
  35. "/sys/fs/cgroup/memory/memory.limit_in_bytes", # v1
  36. "/sys/fs/cgroup/memory.max" # v2
  37. ]:
  38. if path.exists(limit_file):
  39. with open(limit_file) as f:
  40. content = f.read().strip()
  41. if content.isnumeric(): # v2 can have "max" as limit
  42. memory_limit = min(memory_limit, int(content))
  43. return memory_limit
  44. def random_uuid() -> str:
  45. return str(uuid.uuid4().hex)
  46. def in_wsl() -> bool:
  47. # Reference: https://github.com/microsoft/WSL/issues/4071
  48. return "microsoft" in " ".join(uname()).lower()