6 examples of 'pytorch check if gpu is available' in Python

Every line of 'pytorch check if gpu is available' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
204def is_gpu_available():
205 from tensorflow.python.client import device_lib
206 local_device_protos = device_lib.list_local_devices()
207 gpu_list = [x.name for x in local_device_protos if x.device_type == 'GPU']
208 if len(gpu_list) > 0:
209 print("Tensorflow GPU:", gpu_list)
210 return True
211 else:
212 return False
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

152def gpu_available_in_session():
153 sess = tfv1.get_default_session()
154 for dev in sess.list_devices():
155 if dev.device_type.lower() == 'gpu':
156 return True
157 return False
29def linux_with_gpu():
30 """Returns if machine is running an Linux OS and has a GPU"""
31 has_gpu = is_available()
32 return is_linux() and has_gpu
36def check_cuda_support():
37 """ Check if tensorflow was build with CUDA """
38 return tf.test.is_built_with_cuda()
41def is_supported() -> bool:
42 return nvgpu.is_supported()
251def gpu_no_of_var(var):
252 """
253 Function that returns the GPU number or whether the tensor is on GPU or not
254
255 Args:
256 var: torch tensor
257
258 Returns:
259 The CUDA device that the torch tensor is on, or whether the tensor is on GPU
260
261 """
262
263 try:
264 is_cuda = next(var.parameters()).is_cuda
265 except:
266 is_cuda = var.is_cuda
267
268 if is_cuda:
269 try:
270 return next(var.parameters()).get_device()
271 except:
272 return var.get_device()
273 else:
274 return False

Related snippets