Last active
May 25, 2026 19:55
-
-
Save andiradulescu/6e4bd1f6e83ecdb56ad0fce88d5df93d to your computer and use it in GitHub Desktop.
gpu-diag.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # GPU cloud diagnostic script | |
| # Run as root. Identifies bottlenecks on rented GPU instances. | |
| # curl -sSL https://gist.githubusercontent.com/andiradulescu/6e4bd1f6e83ecdb56ad0fce88d5df93d/raw/799f1474ac7d822ebe83ff1919048aabbd8403bf/gpu-diag.sh | bash | |
| set -u | |
| LOG="/tmp/gpu-diag-$(date +%Y%m%d-%H%M%S).log" | |
| exec > >(tee -a "$LOG") 2>&1 | |
| section() { | |
| echo | |
| echo "============================================================" | |
| echo "== $1" | |
| echo "============================================================" | |
| } | |
| run() { | |
| echo | |
| echo "\$ $*" | |
| eval "$@" 2>&1 || echo "[exit $?]" | |
| } | |
| section "0. Environment & install dependencies" | |
| run "uname -a" | |
| run "lsb_release -a 2>/dev/null || cat /etc/os-release" | |
| run "date -u" | |
| run "apt-get update -qq" | |
| run "DEBIAN_FRONTEND=noninteractive apt-get install -y -qq \ | |
| sysbench fio pciutils numactl hwloc lshw util-linux \ | |
| bc curl" | |
| section "1. CPU topology & oversubscription check" | |
| run "lscpu" | |
| run "cat /proc/cpuinfo | grep 'model name' | head -1" | |
| run "nproc" | |
| run "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null || echo 'no cpufreq'" | |
| run "lscpu | grep -E 'Thread|Core|Socket|NUMA|MHz'" | |
| section "2. NUMA topology" | |
| run "numactl --hardware" | |
| run "timeout 10 lstopo-no-graphics --of txt 2>/dev/null | head -60 || echo '[lstopo skipped/timed out]'" | |
| section "3. Memory layout" | |
| run "free -h" | |
| run "cat /proc/meminfo | head -10" | |
| run "dmidecode -t memory 2>/dev/null | grep -E 'Size|Speed|Type:|Configured' | head -40 || echo 'no dmidecode access'" | |
| section "4. GPU enumeration & details" | |
| run "nvidia-smi -L" | |
| run "nvidia-smi" | |
| run "nvidia-smi -q | grep -E 'Product Name|GPU UUID|Bus Id|Power Limit|Persistence|Compute Mode|Bus Type|Link Width|Link Gen'" | |
| run "nvidia-smi --query-gpu=name,uuid,memory.total,memory.free,compute_mode,pstate,clocks.gr,clocks.mem --format=csv" | |
| section "5. MIG / vGPU check" | |
| run "nvidia-smi mig -lgi 2>&1 | head -20" | |
| run "nvidia-smi mig -lci 2>&1 | head -20" | |
| run "nvidia-smi vgpu 2>&1 | head -20" | |
| run "ls /proc/driver/nvidia/gpus/ 2>/dev/null" | |
| section "6. PCIe link state (CRITICAL - Gen5 x16 expected)" | |
| run "nvidia-smi --query-gpu=pcie.link.gen.current,pcie.link.gen.max,pcie.link.width.current,pcie.link.width.max --format=csv" | |
| run "lspci -nn | grep -i nvidia" | |
| run "for bdf in \$(lspci -nn | grep -i nvidia | awk '{print \$1}'); do \ | |
| echo '--- '\$bdf' ---'; \ | |
| lspci -vvv -s \$bdf 2>/dev/null | grep -E 'LnkSta:|LnkCap:|Speed|Width'; \ | |
| done" | |
| section "7. GPU-CPU topology" | |
| run "nvidia-smi topo -m" | |
| run "nvidia-smi topo -p2p w" | |
| section "8. Memory bandwidth (sysbench, all threads)" | |
| THREADS=$(nproc) | |
| run "sysbench memory --memory-block-size=1M --memory-total-size=50G \ | |
| --memory-oper=read --threads=$THREADS run | grep -E 'transferred|per second|total time'" | |
| run "sysbench memory --memory-block-size=1M --memory-total-size=50G \ | |
| --memory-oper=write --threads=$THREADS run | grep -E 'transferred|per second|total time'" | |
| section "9. CPU compute (sysbench)" | |
| run "sysbench cpu --cpu-max-prime=20000 --threads=$THREADS --time=10 run | \ | |
| grep -E 'events per second|total time|min:|avg:|max:|95th'" | |
| section "10. Storage random IOPS" | |
| TESTDIR="${TESTDIR:-/tmp}" | |
| echo "Testing in $TESTDIR; df -h:" | |
| run "df -h $TESTDIR" | |
| run "mount | grep \"\$(df $TESTDIR | tail -1 | awk '{print \$1}')\"" | |
| run "fio --name=randread4k --rw=randread --bs=4k --size=4G --numjobs=8 \ | |
| --runtime=20 --direct=1 --ioengine=libaio --group_reporting \ | |
| --directory=$TESTDIR --filename_format=fio.\$jobnum --unlink=1 \ | |
| | grep -E 'IOPS|bw=|lat \\(|read:'" | |
| section "11. Storage sequential throughput" | |
| run "fio --name=seqread1M --rw=read --bs=1M --size=4G --numjobs=4 \ | |
| --runtime=20 --direct=1 --ioengine=libaio --group_reporting \ | |
| --directory=$TESTDIR --filename_format=fio.\$jobnum --unlink=1 \ | |
| | grep -E 'IOPS|bw=|read:'" | |
| run "fio --name=seqwrite1M --rw=write --bs=1M --size=4G --numjobs=4 \ | |
| --runtime=20 --direct=1 --ioengine=libaio --group_reporting \ | |
| --directory=$TESTDIR --filename_format=fio.\$jobnum --unlink=1 \ | |
| | grep -E 'IOPS|bw=|write:'" | |
| section "12. GPU host-to-device bandwidth (PCIe real-world test)" | |
| if command -v python3 >/dev/null; then | |
| cat > /tmp/h2d_bench.py <<'PYEOF' | |
| import time | |
| try: | |
| import torch | |
| except ImportError: | |
| import subprocess | |
| subprocess.check_call(['pip', 'install', '-q', 'torch', '--index-url', 'https://download.pytorch.org/whl/cu121']) | |
| import torch | |
| if not torch.cuda.is_available(): | |
| print("CUDA not available via torch") | |
| raise SystemExit(0) | |
| for dev in range(torch.cuda.device_count()): | |
| torch.cuda.set_device(dev) | |
| name = torch.cuda.get_device_name(dev) | |
| sizes_mb = [64, 256, 1024, 4096] | |
| print(f"\n=== GPU {dev}: {name} ===") | |
| for mb in sizes_mb: | |
| n = mb * 1024 * 1024 // 4 | |
| cpu = torch.empty(n, dtype=torch.float32, pin_memory=True) | |
| gpu = torch.empty(n, dtype=torch.float32, device=f'cuda:{dev}') | |
| # warmup | |
| gpu.copy_(cpu, non_blocking=True); torch.cuda.synchronize() | |
| # H2D | |
| t0 = time.perf_counter() | |
| for _ in range(5): | |
| gpu.copy_(cpu, non_blocking=True) | |
| torch.cuda.synchronize() | |
| h2d = (mb * 5) / (time.perf_counter() - t0) / 1024 # GB/s | |
| # D2H | |
| t0 = time.perf_counter() | |
| for _ in range(5): | |
| cpu.copy_(gpu, non_blocking=True) | |
| torch.cuda.synchronize() | |
| d2h = (mb * 5) / (time.perf_counter() - t0) / 1024 | |
| print(f" {mb:>5} MB | H2D: {h2d:6.2f} GB/s | D2H: {d2h:6.2f} GB/s") | |
| PYEOF | |
| run "python3 /tmp/h2d_bench.py" | |
| else | |
| echo "python3 not found; skipping H2D test" | |
| fi | |
| section "13. GPU compute sanity (matmul TFLOPS)" | |
| if command -v python3 >/dev/null; then | |
| cat > /tmp/matmul_bench.py <<'PYEOF' | |
| import time | |
| try: | |
| import torch | |
| except ImportError: | |
| raise SystemExit(0) | |
| if not torch.cuda.is_available(): raise SystemExit(0) | |
| for dev in range(torch.cuda.device_count()): | |
| torch.cuda.set_device(dev) | |
| name = torch.cuda.get_device_name(dev) | |
| print(f"\n=== GPU {dev}: {name} ===") | |
| for dtype, label in [(torch.float32, "FP32"), (torch.float16, "FP16"), (torch.bfloat16, "BF16")]: | |
| n = 8192 | |
| a = torch.randn(n, n, device='cuda', dtype=dtype) | |
| b = torch.randn(n, n, device='cuda', dtype=dtype) | |
| for _ in range(3): torch.matmul(a, b) | |
| torch.cuda.synchronize() | |
| iters = 20 | |
| t0 = time.perf_counter() | |
| for _ in range(iters): c = torch.matmul(a, b) | |
| torch.cuda.synchronize() | |
| dt = time.perf_counter() - t0 | |
| tflops = (2 * n**3 * iters) / dt / 1e12 | |
| print(f" {label} {n}x{n} matmul: {tflops:7.1f} TFLOPS ({dt/iters*1000:.2f} ms/iter)") | |
| PYEOF | |
| run "python3 /tmp/matmul_bench.py" | |
| fi | |
| section "14. Container / virt detection" | |
| run "systemd-detect-virt 2>/dev/null || echo 'no systemd-detect-virt'" | |
| run "cat /proc/1/cgroup | head -5" | |
| run "ls -la /.dockerenv 2>/dev/null && echo 'in docker'" | |
| run "grep -E 'hypervisor|vmx|svm' /proc/cpuinfo | head -1" | |
| section "15. Network (in case of network-attached storage)" | |
| run "ip -br addr" | |
| run "ip route | head -5" | |
| section "DONE" | |
| echo | |
| echo "Full log saved to: $LOG" | |
| echo | |
| echo "Key things to check in output:" | |
| echo " - Section 5: any MIG/vGPU partition? (you may not have full 96GB)" | |
| echo " - Section 6: PCIe link should be Gen5 x16. If Gen4 or x8, that's bottleneck." | |
| echo " - Section 8: read bandwidth <100 GB/s on cloud is typical, baremetal Turin = 500+" | |
| echo " - Section 10: cloud shared storage often <100k IOPS vs local NVMe 1M+" | |
| echo " - Section 12: H2D should be ~50-55 GB/s on PCIe Gen5 x16, ~25 GB/s on Gen4 x16" | |
| echo " - Section 13: RTX PRO 6000 Blackwell BF16 should hit ~600-800 TFLOPS dense" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment