|
@echo off |
|
setlocal |
|
powershell -NoProfile -ExecutionPolicy Bypass -Command "$p='%~f0'; $c=Get-Content -Raw -LiteralPath $p; $m='### POWERSHELL ###'; $i=$c.LastIndexOf($m); if ($i -lt 0) { throw 'PowerShell payload marker was not found.' }; Invoke-Expression $c.Substring($i + $m.Length)" |
|
if errorlevel 1 ( |
|
echo Update failed. |
|
exit /b 1 |
|
) |
|
exit /b 0 |
|
|
|
### POWERSHELL ### |
|
$ErrorActionPreference = 'Stop' |
|
$ProgressPreference = 'SilentlyContinue' |
|
|
|
$root = (Get-Location).Path |
|
$filesDir = Join-Path $root 'files' |
|
$versionFile = Join-Path $root 'version.txt' |
|
$cudaShaFile = Join-Path $root 'cuda-dll.sha256' |
|
$repo = 'ggml-org/llama.cpp' |
|
$headers = @{ 'User-Agent' = 'llama-cpp-windows-cuda-updater' } |
|
|
|
function Get-AssetDigest { |
|
param([object] $Asset) |
|
|
|
if ($Asset.PSObject.Properties.Name -contains 'digest' -and $Asset.digest) { |
|
return [string] $Asset.digest |
|
} |
|
|
|
return '' |
|
} |
|
|
|
function Find-OneAsset { |
|
param( |
|
[object[]] $Assets, |
|
[string] $Kind, |
|
[string] $Pattern |
|
) |
|
|
|
$matches = @( |
|
$Assets | |
|
Where-Object { $_.name -like $Pattern } | |
|
Sort-Object name -Descending |
|
) |
|
|
|
if ($matches.Count -eq 0) { |
|
throw ('Could not find {0} asset matching {1}' -f $Kind, $Pattern) |
|
} |
|
|
|
if ($matches.Count -gt 1) { |
|
Write-Host ('Found several {0} assets; using {1}' -f $Kind, $matches[0].name) |
|
} |
|
|
|
return $matches[0] |
|
} |
|
|
|
if (-not (Test-Path -LiteralPath $filesDir)) { |
|
New-Item -ItemType Directory -Path $filesDir | Out-Null |
|
} |
|
|
|
$currentVersion = '' |
|
if (Test-Path -LiteralPath $versionFile) { |
|
$currentVersion = (Get-Content -Raw -LiteralPath $versionFile).Trim() |
|
} |
|
|
|
$currentCudaDigest = '' |
|
if (Test-Path -LiteralPath $cudaShaFile) { |
|
$currentCudaDigest = (Get-Content -Raw -LiteralPath $cudaShaFile).Trim() |
|
} |
|
|
|
Write-Host ('Checking latest release for {0}...' -f $repo) |
|
$release = Invoke-RestMethod ` |
|
-Headers $headers ` |
|
-Uri ('https://api.github.com/repos/{0}/releases/latest' -f $repo) |
|
|
|
$latestVersion = [string] $release.tag_name |
|
if ([string]::IsNullOrWhiteSpace($latestVersion)) { |
|
throw 'GitHub response did not contain tag_name' |
|
} |
|
|
|
$assets = @($release.assets) |
|
$llamaAsset = Find-OneAsset ` |
|
-Assets $assets ` |
|
-Kind 'llama.cpp CUDA 13 Windows x64' ` |
|
-Pattern ('llama-{0}-bin-win-cuda-13.*-x64.zip' -f $latestVersion) |
|
$cudaAsset = Find-OneAsset ` |
|
-Assets $assets ` |
|
-Kind 'CUDA 13 DLL' ` |
|
-Pattern 'cudart-llama-bin-win-cuda-13.*-x64.zip' |
|
|
|
$cudaDigest = Get-AssetDigest $cudaAsset |
|
$versionChanged = ($latestVersion -ne $currentVersion) |
|
$cudaChanged = $true |
|
|
|
if ($cudaDigest -and $currentCudaDigest -and ($cudaDigest -eq $currentCudaDigest)) { |
|
$cudaChanged = $false |
|
} |
|
|
|
if (-not $versionChanged -and -not $cudaChanged) { |
|
Write-Host ('Already up to date: {0}' -f $latestVersion) |
|
exit 0 |
|
} |
|
|
|
if (-not $versionChanged -and $cudaDigest -and -not $currentCudaDigest) { |
|
Set-Content -Encoding ASCII -NoNewline -Path $cudaShaFile -Value $cudaDigest |
|
Write-Host ('Version is current ({0}); recorded CUDA DLL digest.' -f $latestVersion) |
|
exit 0 |
|
} |
|
|
|
$tmp = Join-Path $env:TEMP ('llama-cpp-update-' + [guid]::NewGuid().ToString('N')) |
|
New-Item -ItemType Directory -Path $tmp | Out-Null |
|
|
|
try { |
|
if ($versionChanged) { |
|
$llamaZip = Join-Path $tmp $llamaAsset.name |
|
Write-Host ('Downloading {0}...' -f $llamaAsset.name) |
|
Invoke-WebRequest ` |
|
-Headers $headers ` |
|
-Uri $llamaAsset.browser_download_url ` |
|
-OutFile $llamaZip |
|
|
|
Write-Host 'Extracting llama.cpp binaries...' |
|
Expand-Archive -Force -Path $llamaZip -DestinationPath $tmp |
|
Copy-Item ` |
|
-Force ` |
|
-Recurse ` |
|
-Path (Join-Path $tmp '*') ` |
|
-Destination $filesDir ` |
|
-Exclude '*.zip' |
|
} |
|
|
|
if ($cudaChanged) { |
|
if (-not $cudaDigest) { |
|
Write-Host 'GitHub did not provide a digest for the CUDA DLL asset; downloading DLL archive.' |
|
} |
|
|
|
$cudaZip = Join-Path $tmp $cudaAsset.name |
|
Write-Host ('Downloading {0}...' -f $cudaAsset.name) |
|
Invoke-WebRequest ` |
|
-Headers $headers ` |
|
-Uri $cudaAsset.browser_download_url ` |
|
-OutFile $cudaZip |
|
|
|
Write-Host 'Extracting CUDA DLLs...' |
|
Expand-Archive -Force -Path $cudaZip -DestinationPath $tmp |
|
Copy-Item ` |
|
-Force ` |
|
-Recurse ` |
|
-Path (Join-Path $tmp '*') ` |
|
-Destination $filesDir ` |
|
-Exclude '*.zip' |
|
} |
|
|
|
Set-Content -Encoding ASCII -NoNewline -Path $versionFile -Value $latestVersion |
|
if ($cudaDigest) { |
|
Set-Content -Encoding ASCII -NoNewline -Path $cudaShaFile -Value $cudaDigest |
|
} |
|
|
|
Write-Host ('Updated to {0}' -f $latestVersion) |
|
} |
|
finally { |
|
if (Test-Path -LiteralPath $tmp) { |
|
Remove-Item -Recurse -Force -LiteralPath $tmp |
|
} |
|
} |