__init__.py 1.2 KB

123456789101112131415161718192021222324252627
  1. from warnings import warn
  2. import torch
  3. def print_device_info(device=None):
  4. # prints device stats. Code from https://stackoverflow.com/a/53374933/12891528
  5. device = torch.device(device or ('cuda' if torch.cuda.is_available() else 'cpu'))
  6. print('Using device:', device)
  7. # Additional Info when using cuda
  8. if device.type == 'cuda':
  9. print(torch.cuda.get_device_name(0))
  10. print('Memory Usage:')
  11. print('Allocated:', round(torch.cuda.memory_allocated(0) / 1024 ** 3, 1), 'GB')
  12. print('Cached: ', round(torch.cuda.memory_cached(0) / 1024 ** 3, 1), 'GB')
  13. def increase_file_limit(new_soft=2 ** 15, new_hard=2 ** 15):
  14. """ Increase the maximum number of open files. On Linux, this allows spawning more processes/threads. """
  15. try:
  16. import resource # note: local import to avoid ImportError for those who don't have it
  17. soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
  18. print(f"Increasing file limit - soft {soft}=>{new_soft}, hard {hard}=>{new_hard}")
  19. return resource.setrlimit(resource.RLIMIT_NOFILE, (max(soft, new_soft), max(hard, new_hard)))
  20. except Exception as e:
  21. warn(f"Failed to increase file limit: {e}")