tensorflow tf.is_gpu_available() (判断GPU是否可用)

@tf_export("test.is_gpu_available")
def is_gpu_available(cuda_only=False, min_cuda_compute_capability=None):
  """Returns whether TensorFlow can access a GPU.
  返回TensorFlow是否可以访问GPU。

  Args:
    cuda_only: limit the search to CUDA gpus. 将搜索限制在CUDA GPU中。
    min_cuda_compute_capability: a (major,minor) pair that indicates the minimum
      CUDA compute capability required, or None if no requirement.
      一个(主要,次要)对,指示所需的最低CUDA计算能力;如果没有要求,则为无。

  Returns:
    True iff a gpu device of the requested kind is available.
    如果请求的类型的gpu设备可用,则为true。
  """

  def compute_capability_from_device_desc(device_desc):
    # TODO(jingyue): The device description generator has to be in sync with
    # this file. Another option is to put compute capability in
    # DeviceAttributes, but I avoided that to keep DeviceAttributes
    # target-independent. Reconsider this option when we have more things like
    # this to keep in sync.
    # LINT.IfChange
    # TODO(jingyue):设备描述生成器必须与此文件同步。 
    # 另一个选择是将计算功能放到DeviceAttributes中,但是我避免了使DeviceAttributes与目标无关的问题。 
    # 当我们有更多类似的东西保持同步时,请重新考虑此选项。 LINT.IfChange
    match = re.search(r"compute capability: (\d+)\.(\d+)", device_desc)
    # LINT.ThenChange(//tensorflow/core/\
    #                 common_runtime/gpu/gpu_device.cc)
    if not match:
      return 0, 0
    return int(match.group(1)), int(match.group(2))

  try:
    for local_device in device_lib.list_local_devices():
      if local_device.device_type == "GPU":
        if (min_cuda_compute_capability is None or
            compute_capability_from_device_desc(
                local_device.physical_device_desc) >=
            min_cuda_compute_capability):
          return True
      if local_device.device_type == "SYCL" and not cuda_only:
        return True
    return False
  except errors_impl.NotFoundError as e:
    if not all([x in str(e) for x in ["CUDA", "not find"]]):
      raise e
    else:
      logging.error(str(e))
      return False

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值