powershellgraphicsgpuopenai-whispervram

How can I use PowerShell to determine the active graphics card with the highest VRAM capacity?


Okay, I'm not one to ask for help, but I've been struggling for days with a feature I want to implement in a private OpenAI Whisper project. To put it in context, I'm doing a project to automate everything, including AI model selection based on computer performance. I'm doing this only in a powershell language.

The aim is to get the best graphics card on the computer, get its VRAM in GB and tell my script ok, take this model.

The problem I'm having is that my script does detect the name of the selected graphics card, but it keeps the best value it finds in the registry editor after tests to change the graphics card... etc... I'm not a code expert, so if it does find a solution, it'll be a simple one or maybe not.

I'd like to point out that the script must also include whether or not the processor plays the role of the graphics card if there is none.

I tried to get the active grahpics cards on the computer by looking in Win32_VideoController then, I have the impression that you can't find the VRAM value with a Get-CimInstance so I thought I'd go to the registry editor to get the HardwareInformation.qwMemorySize key and then do a GB conversion. Then, to find out if the card is active and avoid it taking the value from me, I wanted to make a comparison between the "Name" value of the Win32_VideoController and the data in the DriverDesc register, except that apparently the data doesn't match, which gives me an error and it doesn't find anything.

Here's my current code ;

# Function to convert VRAM size to GB
function ConvertTo-GB {
    param (
        [double]$bytes
    )
    return [math]::round($bytes / 1GB)
}

# Recover all graphics cards except Microsoft Remote Display Adapter
$gpus = Get-CimInstance -ClassName Win32_VideoController | Where-Object {
    $_.Name -notmatch "Microsoft Remote Display Adapter" -and $_.Status -eq "OK"
}

# Initialize variables
$maxVRAM = 0
$selectedGPU = $null

# Retrieve HardwareInformation.qwMemorySize and DriverDesc values from the registry
$keyPath = "HKLM:\SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0*"
$registryValues = Get-ItemProperty -Path $keyPath -Name HardwareInformation.qwMemorySize,DriverDesc -ErrorAction SilentlyContinue

# Browse all graphics cards to find the one with the largest VRAM
foreach ($gpu in $gpus) {
    # Check that the DriverDesc property is set
    if ($gpu.DriverDesc -ne $null) {
        # Find the corresponding value of HardwareInformation.qwMemorySize
        $registryValue = $registryValues | Where-Object { $_.DriverDesc.Trim().ToLower() -eq $gpu.DriverDesc.Trim().ToLower() }
        if ($registryValue -ne $null) {
            $qwMemorySize = $registryValue.HardwareInformation.qwMemorySize[0]
            if ($qwMemorySize -ne $null) {
                $vram = ConvertTo-GB -bytes $qwMemorySize
                Write-Host "Found VRAM for $($gpu.Name): $vram GB"
            } else {
                Write-Warning "Unable to retrieve VRAM for $($gpu.Name) from registry key: $keyPath"
                $vram = 0
            }
        } else {
            Write-Warning "Unable to find registry value for $($gpu.Name) with DriverDesc: $($gpu.DriverDesc)"
            $vram = 0
        }
    } else {
        Write-Warning "DriverDesc is not defined for $($gpu.Name)"
        $vram = 0
    }

    # Check if this board has the largest VRAM found
    if ($vram -gt $maxVRAM) {
        $maxVRAM = $vram
        $selectedGPU = $gpu
    }
}

# Check if a graphics card has been selected
if ($selectedGPU -eq $null) {
    Write-Warning "No suitable GPU found."
    $model = "none"
    $selectedGPUName = "None"
} else {
    # Select the appropriate model according to VRAM quantity
    switch ($maxVRAM) {
        { $_ -lt 2 } { $model = "base" }
        { $_ -lt 5 } { $model = "small" }
        { $_ -lt 10 } { $model = "medium" }
        default { $model = "large-v3" }
    }
    $selectedGPUName = $selectedGPU.Caption
}

# Display selected model and VRAM quantity
$result = [PSCustomObject] @{
    Model = $selectedGPUName
    "VRAM, GB" = $maxVRAM
    "Selected Model" = $model
}

$result | Format-Table -AutoSize

# Use the selected template for your Whisper project
Write-Host "Model $model selected for your Whisper project with $maxVRAM Go of VRAM"

Is there perhaps another solution? Using a separate module or possibly a program that I'll put in my final installation package? Anyway, thanks in advance for any help you can give me.


Solution

  • Ok, I've finally found my solution after a lot of research. Looking at how to do this in other languages, I came across the nvidia-smi command, which gives us an array of the active graphics card (if any) including the total VRAM count.

    I then completely modified the code with the help of AI in this direction and made it so that IF it doesn't detect a GPU, it automatically takes the "small" model.

    Here is the functional code for those who are interested;

    # Initialize the model and VRAM size
    $model = "small"
    $vram = 0
    $gpuName = "Unknown"
    
    # Retrieve information about the active graphics card
    try {
        $nvidiaSmiOutput = nvidia-smi --query-gpu=memory.total --format=csv,noheader
        if ($nvidiaSmiOutput -ne $null) {
            # Retrieve the VRAM size of the active graphics card in megabytes (MB)
            $vram = [math]::round($nvidiaSmiOutput.Trim() -replace " MiB", "")
    
            # Select the appropriate model based on the VRAM size
            switch ($vram) {
                { $_ -lt 2048 } { $model = "base" }
                { $_ -lt 5120 } { $model = "small" }
                { $_ -lt 10240 } { $model = "medium" }
                default { $model = "large-v3" }
            }
    
            # Retrieve the name of the active graphics card
            $gpuName = (nvidia-smi --query-gpu=name --format=csv,noheader).Trim()
        }
    } catch {
        # Set the model to "small" by default if an error occurs
        Write-Warning "Failed to retrieve GPU information. Using default model 'small'."
    }
    
    # Display the selected model and VRAM size
    $result = [PSCustomObject] @{
        Model = $gpuName
        "VRAM, MiB" = $vram
        "Selected Model" = $model
    }
    
    $result | Format-Table -AutoSize
    
    # Use the selected model for your Whisper project
    Write-Host "Model $model selected for your Whisper project with $vram MiB of VRAM"