-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgpu.py
More file actions
33 lines (28 loc) · 1.27 KB
/
gpu.py
File metadata and controls
33 lines (28 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tensorflow as tf
import subprocess
import re
# 1. Check for GPU availability using TensorFlow
gpus = tf.config.list_physical_devices('GPU')
if gpus:
print("Num GPUs Available: ", len(gpus))
print("Detected GPU(s) by TensorFlow:")
for i, gpu in enumerate(gpus):
print(f" GPU {i}: {gpu.name} (Type: {gpu.device_type})")
# 2. Try to get the human-readable GPU name using nvidia-smi
try:
# Run nvidia-smi command to get GPU names
# 'vc_redist.x86.exe' from the response history should resolve the error.
smi_output = subprocess.check_output(['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'], encoding='utf-8')
gpu_names = [name.strip() for name in smi_output.strip().split('\n')]
if gpu_names:
print("\nHuman-readable GPU Name(s) from nvidia-smi:")
for i, name in enumerate(gpu_names):
print(f" GPU {i}: {name}")
else:
print("\nCould not retrieve human-readable GPU name from nvidia-smi.")
except FileNotFoundError:
print("\nnvidia-smi not found. Please ensure NVIDIA drivers are installed and in PATH.")
except Exception as e:
print(f"\nAn error occurred while running nvidia-smi: {e}")
else:
print("No GPUs Available.")