You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.6KB

  1. # -*- coding: utf-8 -*-
  2. from traceback import format_exc
  3. from GPUtil import getAvailable, getGPUs
  4. from loguru import logger
  5. from enums.ExceptionEnum import ExceptionType
  6. from exception.CustomerException import ServiceException
  7. # order- 确定返回可用 GPU 设备 ID 的顺序。order应指定为以下字符串之一:
  8. # 'first'- 按升序排列可用的 GPU 设备 ID(默认)
  9. # 'last'- 按 id 降序排列可用的 GPU 设备 id
  10. # 'random'- 随机订购可用的 GPU 设备 ID
  11. # 'load'- 按负载递增排序可用的 GPU 设备 ID
  12. # 'memory'- 通过升序内存使用来排序可用的 GPU 设备 ID
  13. # limit- 将返回的 GPU 设备 ID 数量限制为指定数量。必须是正整数。(默认 = 1)
  14. # maxLoad- 被认为可用的 GPU 的最大当前相对负载。负载大于 的 GPUmaxLoad不会返回。(默认 = 0.5)
  15. # maxMemory- 被视为可用的 GPU 的最大当前相对内存使用量。maxMemory不返回当前内存使用量大于的 GPU 。(默认 = 0.5)
  16. # includeNan- 真/假标志,指示是否包括负载或内存使用为 NaN 的 GPU(指示无法检索使用情况)。(默认 = 假)
  17. # excludeID- ID 列表,应从可用 GPU 列表中排除。见GPU类描述。(默认 = [])
  18. # excludeUUIDexcludeID-除了它使用 UUID 之外,其他相同。(默认 = [])
  19. # 输出
  20. # deviceIDs - 所有可用 GPU 设备 ID 的列表。如果当前负载和内存使用量分别小于maxLoad和maxMemory,则认为 GPU 可用。该列表是根据 排序的order。返回的设备 ID 的最大数量由 限制limit。
  21. def get_gpu_ids():
  22. try:
  23. return getAvailable(maxLoad=0.90, maxMemory=0.90)
  24. except Exception:
  25. logger.info("获取GPU异常: {}", format_exc())
  26. return []
  27. def get_all_gpu_ids():
  28. return getGPUs()
  29. def get_first_gpu_name():
  30. gps = get_all_gpu_ids()
  31. if len(gps) == 0:
  32. raise ServiceException(ExceptionType.NO_GPU_RESOURCES.value[0],
  33. ExceptionType.NO_GPU_RESOURCES.value[1])
  34. return gps[0].name
  35. def check_gpu_resource(requestId=None):
  36. gpu_ids = get_gpu_ids()
  37. if len(gpu_ids) == 0:
  38. print_gpu_status(requestId)
  39. raise ServiceException(ExceptionType.NO_GPU_RESOURCES.value[0],
  40. ExceptionType.NO_GPU_RESOURCES.value[1])
  41. return gpu_ids
  42. def print_gpu_ex_status(requestId=None):
  43. gpu_ids = get_gpu_ids()
  44. result = False
  45. if len(gpu_ids) == 0 or 0 not in gpu_ids:
  46. result = True
  47. print_gpu_status(requestId)
  48. return result
  49. def print_gpu_status(requestId=None):
  50. GPUs = get_all_gpu_ids()
  51. if len(GPUs) == 0:
  52. return
  53. for gpu in GPUs:
  54. if requestId:
  55. logger.info("""############################################################################################
  56. GPU ID:{}, GPU 名称:{}, 负载率:{}, 内存使用率:{}, 总内存:{}, 占用内存:{}, 空闲内存:{}, requestId:{}
  57. ############################################################################################""", gpu.id,
  58. gpu.name, gpu.load * 100, gpu.memoryUtil * 100, gpu.memoryTotal, gpu.memoryUsed, gpu.memoryFree,
  59. requestId)
  60. else:
  61. logger.info("""############################################################################################
  62. GPU ID:{}, GPU 名称:{}, 负载率:{}, 内存使用率:{}, 总内存:{}, 占用内存:{}, 空闲内存:{}
  63. ############################################################################################""", gpu.id,
  64. gpu.name, gpu.load * 100, gpu.memoryUtil * 100, gpu.memoryTotal, gpu.memoryUsed, gpu.memoryFree)