Skip to content

Instantly share code, notes, and snippets.

@ZekunZh
Last active January 25, 2022 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZekunZh/a9333a4a463c4f030d2848723c656e77 to your computer and use it in GitHub Desktop.
Save ZekunZh/a9333a4a463c4f030d2848723c656e77 to your computer and use it in GitHub Desktop.
import os
import platform
import subprocess
import warnings
from distutils import spawn


def get_gpu_model_name() -> str:
    if platform.system() == "Windows":
        # If the platform is Windows and nvidia-smi
        # could not be found from the environment path,
        # try to find it from system drive with default installation path
        nvidia_smi = spawn.find_executable("nvidia-smi")
        if nvidia_smi is None:
            nvidia_smi = (
                    "%s\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi.exe"
                    % os.environ["systemdrive"]
            )
    else:
        nvidia_smi = "nvidia-smi"

    try:
        p = subprocess.Popen(
            [nvidia_smi, "--query-gpu=name", "--format=csv,noheader"],
            stdout=subprocess.PIPE,
        )
        stdout, stderror = p.communicate()
        output = stdout.decode("UTF-8")
        lines = output.split(os.linesep)
        if len(lines) > 1:
            warnings.warn("Multiple GPUs found, can't determine benchmark target")
        gpu_name = lines[0]
    except OSError:
        gpu_name = "None"
    return gpu_name


if __name__ == '__main__':
    print(get_gpu_model_name())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment