def sg_gpus():
r""" Gets current available GPU nums
Returns:
A integer : total # of GPUs available
"""
global _gpus
if _gpus is None:
local_device_protos = device_lib.list_local_devices()
_gpus = len([x.name for x in local_device_protos if x.device_type == 'GPU'])
return max(_gpus, 1)
#
# context helpers
#
python类list_local_devices()的实例源码
def has_gpu() -> bool:
"""Check if TensorFlow can access GPU.
The test is based on
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/platform/test.py
...but we are interested only in CUDA GPU devices.
Returns:
True, if TF can access the GPU
"""
# pylint: disable=global-statement
global __HAS_GPU_RESULT
# pylint: enable=global-statement
if __HAS_GPU_RESULT is None:
__HAS_GPU_RESULT = any((x.device_type == 'GPU')
for x in _device_lib.list_local_devices())
return __HAS_GPU_RESULT
def has_gpu() -> bool:
"""Check if TensorFlow can access GPU.
The test is based on
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/platform/test.py
...but we are interested only in CUDA GPU devices.
Returns:
True, if TF can access the GPU
"""
# pylint: disable=global-statement
global __HAS_GPU_RESULT
# pylint: enable=global-statement
if __HAS_GPU_RESULT is None:
__HAS_GPU_RESULT = any((x.device_type == 'GPU')
for x in _device_lib.list_local_devices())
return __HAS_GPU_RESULT
def has_gpu() -> bool:
"""Check if TensorFlow can access GPU.
The test is based on
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/platform/test.py
...but we are interested only in CUDA GPU devices.
Returns:
True, if TF can access the GPU
"""
# pylint: disable=global-statement
global __HAS_GPU_RESULT
# pylint: enable=global-statement
if __HAS_GPU_RESULT is None:
__HAS_GPU_RESULT = any((x.device_type == 'GPU')
for x in _device_lib.list_local_devices())
return __HAS_GPU_RESULT
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
values = ""
counter = 0
length = len(local_device_protos)
for device in local_device_protos:
if device.device_type == "GPU":
description = "Found " + device.physical_device_desc
values += description
if counter < length - 1: values += "\n"
counter += 1
# return [x.name for x in local_device_protos if x.device_type == 'GPU']
return values
# this is taken from the C++ header file
# const int INFO = 0; // base_logging::INFO;
# const int WARNING = 1; // base_logging::WARNING;
# const int ERROR = 2; // base_logging::ERROR;
# const int FATAL = 3; // base_logging::FATAL;
# const int NUM_SEVERITIES = 4; // base_logging::NUM_SEVERITIES;
def is_gpu_available(cuda_only=True):
"""
code from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/platform/test.py
Returns whether TensorFlow can access a GPU.
Args:
cuda_only: limit the search to CUDA gpus.
Returns:
True iff a gpu device of the requested kind is available.
"""
from tensorflow.python.client import device_lib as _device_lib
if cuda_only:
return any((x.device_type == 'GPU')
for x in _device_lib.list_local_devices())
else:
return any((x.device_type == 'GPU' or x.device_type == 'SYCL')
for x in _device_lib.list_local_devices())
def get_available_gpus():
r"""
Returns the number of GPUs available on this system.
"""
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type=='GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type=='GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
"""
The function does what its name says. Simple as that.
"""
local_device_protos = device_lib.list_local_devices()
return len([x.name for x in local_device_protos if x.device_type == 'GPU'])
def get_available_gpus():
try:
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
except Exception as ex:
logging.error(
'Error while trying to list available GPUs: %s' % str(ex))
return list()
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
def get_tf_session():
""" Returning a session. Set options here (e.g. for GPUs) if desired. """
tf.reset_default_graph()
tf_config = tf.ConfigProto(inter_op_parallelism_threads=1,
intra_op_parallelism_threads=1)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
print("AVAILABLE GPUS: ", get_available_gpus())
return session
def get_tf_session():
""" Returning a session. Set options here if desired. """
tf.reset_default_graph()
tf_config = tf.ConfigProto(inter_op_parallelism_threads=1,
intra_op_parallelism_threads=1)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
print("AVAILABLE GPUS: ", get_available_gpus())
return session
def get_available_gpus():
"""
??GPU????nvidia-smi
?????????ps aux | grep PID
:return: GPU??
"""
local_device_protos = device_lib.list_local_devices()
print "all: %s" % [x.name for x in local_device_protos]
print "gpu: %s" % [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_devices(gpu_only=False):
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
if gpu_only: return [x.name for x in local_device_protos if x.device_type == 'GPU']
else: return [x.name for x in local_device_protos]
def get_available_gpus(ngpus=-1):
'''
:param int ngpus: GPUs max to use. Default -1 means all gpus.
:returns: List of gpu devices. Ex.: ['/gpu:0', '/gpu:1', ...]
'''
local_device_protos = device_lib.list_local_devices()
gpus_list = [x.name for x in local_device_protos if x.device_type == 'GPU']
return gpus_list[:ngpus] if ngpus > -1 else gpus_list
def __init__(self):
self.devices = device_lib.list_local_devices()
self.cpus = [x.name for x in self.devices if x.device_type == 'CPU']
self.gpus = [x.name for x in self.devices if x.device_type == 'GPU']
self.iterate_cpus = it.cycle(self.cpus)
self.iterate_gpus = it.cycle(self.gpus)
def get_num_gpu():
"""Get number of available GPUs
Returns:
a `int`, available GPUs in CUDA_VISIBLE_DEVICES, or in the system.
"""
env = os.environ.get('CUDA_VISIBLE_DEVICES', None)
if env is not None:
return len(env.split(','))
from tensorflow.python.client import device_lib
device_protos = device_lib.list_local_devices()
gpus = [x.name for x in device_protos if x.device_type == 'GPU']
return len(gpus)
def _get_available_gpus(self):
"""Get available GPUs."""
from tensorflow.python.client import device_lib
local_device_protos = device_lib.list_local_devices()
return [int(x.name[-1]) for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
def tf_is_gpu():
local_devices = device_lib.list_local_devices()
return len([x for x in local_devices if x.device_type == 'GPU']) > 0
def count_gpus():
from tensorflow.python.client import device_lib
count = 0
for device in device_lib.list_local_devices():
if device.device_type == "GPU":
count+=1
return count
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']