aisop/tools/Install-GpuRuntime.ps1

77 lines
4.3 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$Destination = (Join-Path $PSScriptRoot '..\libs\cuda'),
[string]$CudaRuntimeSource = ''
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$runtimeDirectory = [IO.Path]::GetFullPath($Destination)
New-Item -ItemType Directory -Path $runtimeDirectory -Force | Out-Null
function Install-NvidiaArchive($Name, $BaseUri, $Package) {
if (!$Package.relative_path -or !$Package.sha256) { throw "NVIDIA 清单缺少 Windows 运行库:$Name" }
$archivePath = Join-Path $runtimeDirectory "$Name-runtime.zip"
if (!(Test-Path -LiteralPath $archivePath)) {
Write-Host "Downloading $Name..."
Invoke-WebRequest -Uri ($BaseUri + $Package.relative_path) -OutFile $archivePath
}
if ((Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash -ne $Package.sha256) {
throw "$Name SHA256 校验失败,请移除下载文件后重试。"
}
$archive = [IO.Compression.ZipFile]::OpenRead($archivePath)
try {
foreach ($entry in $archive.Entries) {
$fileName = [IO.Path]::GetFileName($entry.Name)
if ($entry.FullName -match '/bin/(?:x64/)?[^/]+\.dll$') {
$target = Join-Path $runtimeDirectory $fileName
} elseif ($fileName -match '^LICENSE|^EULA') {
$target = Join-Path $runtimeDirectory "LICENSE-$Name-$fileName"
} else { continue }
[IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $target, $true)
Write-Host ('Extracted ' + $fileName)
}
}
finally { $archive.Dispose() }
Remove-Item -LiteralPath $archivePath
}
# ORT 1.27+ 的默认 Windows GPU 包使用 CUDA 13;仅放入应用目录,不修改系统 CUDA 或 PATH。
$cudaDlls = @('cublasLt64_13.dll','cublas64_13.dll','cudart64_13.dll')
if ($CudaRuntimeSource) {
$source = [IO.Path]::GetFullPath($CudaRuntimeSource)
foreach ($name in $cudaDlls) {
if (!(Test-Path -LiteralPath (Join-Path $source $name))) { throw "CUDA 13 来源目录缺少 $name" }
}
foreach ($name in $cudaDlls) {
$target = Join-Path $runtimeDirectory $name
if ([IO.Path]::GetFullPath((Join-Path $source $name)) -ne $target) {
Copy-Item -LiteralPath (Join-Path $source $name) -Destination $target -Force
}
}
} elseif (@($cudaDlls | Where-Object { !(Test-Path -LiteralPath (Join-Path $runtimeDirectory $_)) }).Count) {
$cuda = Invoke-RestMethod 'https://developer.download.nvidia.com/compute/cuda/redist/redistrib_13.0.2.json'
foreach ($name in @('libcublas','cuda_cudart')) {
Install-NvidiaArchive $name 'https://developer.download.nvidia.com/compute/cuda/redist/' $cuda.$name.'windows-x86_64'
}
}
$nvrtcDlls = @('nvrtc-builtins64_130.dll','nvrtc64_130_0.dll')
if (@($nvrtcDlls | Where-Object { !(Test-Path -LiteralPath (Join-Path $runtimeDirectory $_)) }).Count) {
$cuda = Invoke-RestMethod 'https://developer.download.nvidia.com/compute/cuda/redist/redistrib_13.0.2.json'
Install-NvidiaArchive 'cuda_nvrtc' 'https://developer.download.nvidia.com/compute/cuda/redist/' $cuda.cuda_nvrtc.'windows-x86_64'
}
$cudnnDlls = @('cudnn_graph64_9.dll','cudnn_ops64_9.dll','cudnn_cnn64_9.dll','cudnn_adv64_9.dll',
'cudnn_engines_precompiled64_9.dll','cudnn_engines_runtime_compiled64_9.dll','cudnn_heuristic64_9.dll','cudnn64_9.dll')
# CUDA 12 与 CUDA 13 的 cuDNN 文件名相同,必须记录已安装的发行包,不能只判断 DLL 是否存在。
$cudnnStamp = Join-Path $runtimeDirectory '.cudnn-cuda13-version'
if (!(Test-Path -LiteralPath $cudnnStamp) -or
(Get-Content -LiteralPath $cudnnStamp -Raw).Trim() -ne '9.13.0' -or
@($cudnnDlls | Where-Object { !(Test-Path -LiteralPath (Join-Path $runtimeDirectory $_)) }).Count) {
$cudnn = Invoke-RestMethod 'https://developer.download.nvidia.com/compute/cudnn/redist/redistrib_9.13.0.json'
Install-NvidiaArchive 'cudnn' 'https://developer.download.nvidia.com/compute/cudnn/redist/' $cudnn.cudnn.'windows-x86_64'.cuda13
Set-Content -LiteralPath $cudnnStamp -Value '9.13.0'
}
foreach ($name in $cudaDlls + $nvrtcDlls + $cudnnDlls) {
if (!(Test-Path -LiteralPath (Join-Path $runtimeDirectory $name))) { throw "GPU 运行库不完整:$name" }
}
Write-Host 'CUDA 13 / cuDNN 9 ready. Rebuild and restart the application to use GPU inference.'