(已解决)如何使用 PowerShell 确定具有最高 VRAM 容量的活动显卡?

问题描述 投票:0回答:1

好吧,我不是一个寻求帮助的人,但我已经为一项想要在私人 OpenAI Whisper 项目中实现的功能而苦苦挣扎了好几天。简而言之,我正在做一个项目来自动化一切,包括基于计算机性能的人工智能模型选择。我仅使用 powershell 语言执行此操作。

目标是在计算机上获得最好的显卡,获得 GB 的 VRAM 并告诉我的脚本可以,采用这个模型。

我遇到的问题是我的脚本确实检测到了所选显卡的名称,但在测试更改显卡后,它保留了在注册表编辑器中找到的最佳值...等等...我不是代码专家,所以如果它确实找到了解决方案,那将是一个简单的解决方案,也可能不是。

我想指出的是,如果没有的话,脚本还必须包括处理器是否扮演显卡的角色。

我尝试通过查看

Win32_VideoController
来获取计算机上的活动图形卡,然后,我的印象是您无法使用
Get-CimInstance
找到 VRAM 值,所以我想我应该去注册表编辑器获取
HardwareInformation.qwMemorySize
键,然后进行 GB 转换。然后,为了查明该卡是否处于活动状态并避免它从我这里获取值,我想对
Win32_VideoController
的“名称”值与
DriverDesc
寄存器中的数据进行比较,但显然数据不匹配,这给了我一个错误并且没有找到任何东西。

这是我当前的代码;

# 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"

也许还有其他解决方案吗?使用单独的模块或者可能是我将放入最终安装包中的程序?无论如何,预先感谢您能给我的任何帮助。

powershell graphics gpu openai-whisper vram
1个回答
0
投票

好吧,经过大量研究,我终于找到了解决方案。在查看如何用其他语言执行此操作时,我遇到了

nvidia-smi
命令,它为我们提供了活动显卡(如果有)的数组,包括 VRAM 总数。

然后,我在 AI 的帮助下完全修改了代码,如果它没有检测到 GPU,它会自动采用“小”模型。

这是功能代码,有兴趣的朋友可以看看;

# 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"

© www.soinside.com 2019 - 2024. All rights reserved.