broadfield commited on
Commit
75c8fc0
·
verified ·
1 Parent(s): dfe7ddb

Add llamainstall.ps1 — PowerShell version manager for llama.cpp on Windows

Browse files
Files changed (1) hide show
  1. llamainstall.ps1 +277 -0
llamainstall.ps1 ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # llamainstall.ps1 — llama.cpp version manager & installer for Windows
3
+ # -----------------------------------------------------------------------------
4
+ # Installs llama.cpp (the single multi-call "llama.exe" binary) from the
5
+ # official ggml-org/install.sh bucket, auto-detecting the best backend
6
+ # (CUDA -> ROCm -> Vulkan -> CPU), and lets you install, switch, roll back
7
+ # and remove versions — like nvm-windows, but for llama.cpp.
8
+ #
9
+ # Usage (PowerShell >= 5.1):
10
+ # irm https://huggingface.co/datasets/broadfield/llamainstall-scripts/resolve/main/llamainstall.ps1 | iex
11
+ # (installs latest, adds llama.exe to your PATH via WindowsApps)
12
+ #
13
+ # llamainstall install [version] # install a version and activate it
14
+ # llamainstall use <version> # switch to an installed version
15
+ # llamainstall list # show installed versions + active
16
+ # llamainstall remove <version> # delete a version
17
+ # llamainstall doctor # detect CPU/GPU and recommend config
18
+ # llamainstall serve [model] # download a small model and run server
19
+ # llamainstall uninstall # remove everything
20
+ #
21
+ # Versions: latest | stable | b<build> | g<commit> | v<tag>
22
+ # Env: LLAMA_BUCKET, LLAMA_VERSION, HF_TOKEN, SKIP_CUDA/ROCM/VULKAN
23
+ # =============================================================================
24
+
25
+ $ErrorActionPreference = "Stop"
26
+
27
+ if (!$LLAMA_BUCKET) { $LLAMA_BUCKET = $env:LLAMA_BUCKET }
28
+ if (!$LLAMA_BUCKET) { $LLAMA_BUCKET = "ggml-org/install.sh" }
29
+ $REPO = "https://huggingface.co/buckets/$LLAMA_BUCKET/resolve"
30
+
31
+ $BASE = Join-Path $env:LOCALAPPDATA "llamainstall"
32
+ $VERSIONS_DIR = Join-Path $BASE "versions"
33
+ $ACTIVE_FILE = Join-Path $BASE "active"
34
+ $SHIM_DIR = Join-Path $env:LOCALAPPDATA "Microsoft\WindowsApps"
35
+ $SHIM = Join-Path $SHIM_DIR "llama.exe"
36
+
37
+ $WebParams = @{}
38
+ if ($env:HF_TOKEN) { $WebParams["Headers"] = @{ "Authorization" = "Bearer $env:HF_TOKEN" } }
39
+
40
+ function Info { param([string]$M) Write-Host $M }
41
+ function Die { param([string]$M) [Console]::Error.WriteLine("llamainstall: $M"); exit 111 }
42
+
43
+ function Resolve-Version {
44
+ param([string]$Req)
45
+ if (!$Req) { $Req = "latest" }
46
+ switch -Regex ($Req) {
47
+ "^(latest|nightly|b[0-9]+|g[0-9a-f]+|v[0-9.]+)$" { }
48
+ "stable" { $Req = "" }
49
+ default { Die "unknown version: $Req (use latest, stable, b<build>, g<commit>, v<tag>)" }
50
+ }
51
+ if (!$Req) {
52
+ try { $Req = (Invoke-RestMethod "$REPO/latest" @WebParams).Trim() }
53
+ catch { Die "could not resolve latest version from $REPO/latest" }
54
+ }
55
+ return $Req
56
+ }
57
+
58
+ function Download {
59
+ param($File, [string]$Path)
60
+ if (Test-Path "$TMP\$File") { return $true }
61
+ try {
62
+ if ($Path -like "*.zst") {
63
+ if (!(Get-Command zstd -ErrorAction SilentlyContinue)) {
64
+ if (!(Download "unzstd.exe" "$ARCH/windows/unzstd.exe")) { return $false }
65
+ $zstd = "$TMP\unzstd.exe"
66
+ } else {
67
+ $zstd = "zstd"
68
+ }
69
+ Invoke-RestMethod "$REPO/$VER/$Path" -OutFile "$TMP\tmp.zst" @WebParams
70
+ if ($zstd -eq "zstd") {
71
+ & $zstd -d -f "$TMP\tmp.zst" -o "$TMP\$File" | Out-Null
72
+ } else {
73
+ Start-Process -FilePath $zstd -RedirectStandardInput "$TMP\tmp.zst" `
74
+ -RedirectStandardOutput "$TMP\$File" -NoNewWindow -Wait
75
+ }
76
+ Remove-Item "$TMP\tmp.zst" -Force -ErrorAction SilentlyContinue
77
+ } else {
78
+ Invoke-RestMethod "$REPO/$VER/$Path" -OutFile "$TMP\$File" @WebParams
79
+ }
80
+ return (Test-Path "$TMP\$File")
81
+ } catch {
82
+ return $false
83
+ }
84
+ }
85
+
86
+ function Probe-Backend {
87
+ param([string]$Probe, [string]$Backend, [string]$CfgPath)
88
+ # $CfgPath is a .NET format string where {0} gets the probed config
89
+ if (!(Download "probe-$Probe.exe" "$ARCH/windows/$Backend/probe/probe.exe.zst")) { return "" }
90
+ $OUT = & "$TMP\probe-$Probe.exe" 2>$null
91
+ if ($LASTEXITCODE -eq 5) {
92
+ # GPU present but the toolkit is missing (same codes as the official installer)
93
+ if ($Backend -eq "cuda") { Info " NVIDIA GPU detected, but the CUDA Toolkit is not installed.`n Install it with: winget install Nvidia.CUDA" }
94
+ elseif ($Backend -eq "rocm") { Info " AMD GPU detected, but the HIP SDK is not installed.`n See: https://rocm.docs.amd.com/projects/install-on-windows" }
95
+ return ""
96
+ }
97
+ if ($LASTEXITCODE) { return "" }
98
+ $CONFIG = ($OUT -split " ")[0]
99
+ if (!$CONFIG) { return "" }
100
+ if ($Backend -in @("vulkan", "cpu")) {
101
+ if (!(Download "featcode.exe" "$ARCH/windows/featcode.exe")) { return "" }
102
+ $CONFIG = (& "$TMP\featcode.exe" 2>$null | Out-String).Trim()
103
+ # featcode with no args prints the config token; the features list is
104
+ # informational only (featcode <config>), matching the official installer
105
+ if ($LASTEXITCODE -or !$CONFIG) { return "" }
106
+ }
107
+ $path = $CfgPath -f $CONFIG
108
+ if (!(Download "llama.exe" $path)) { return "" }
109
+ return $CONFIG
110
+ }
111
+
112
+ function Install-Version {
113
+ param([string]$VER)
114
+ $installed = Join-Path $VERSIONS_DIR $VER "llama.exe"
115
+ if (Test-Path $installed) { Info "version $VER is already installed"; return }
116
+
117
+ $global:TMP = Join-Path $BASE "tmp"
118
+ Remove-Item $TMP -Recurse -Force -ErrorAction SilentlyContinue
119
+ New-Item -Path $TMP -ItemType Directory -Force | Out-Null
120
+ Push-Location $TMP
121
+
122
+ Info "detecting your hardware and picking the best backend..."
123
+ $cfg = ""
124
+ if (!$env:SKIP_CUDA) { $cfg = Probe-Backend "cuda" "cuda" "$ARCH/windows/cuda/{0}/llama-app.exe.zst" }
125
+ if (!(Test-Path "$TMP\llama.exe") -and !$env:SKIP_ROCM) { $cfg = Probe-Backend "rocm" "rocm" "$ARCH/windows/rocm/{0}/llama-app.exe.zst" }
126
+ if (!(Test-Path "$TMP\llama.exe") -and !$env:SKIP_VULKAN){ $cfg = Probe-Backend "vulkan" "vulkan" "$ARCH/windows/vulkan/{0}/llama-app.exe.zst" }
127
+ if (!(Test-Path "$TMP\llama.exe")) { $cfg = Probe-Backend "cpu" "cpu" "$ARCH/windows/cpu/{0}/llama-app.exe.zst" }
128
+
129
+ if (!(Test-Path "$TMP\llama.exe")) {
130
+ Pop-Location
131
+ Die "no prebuilt binary for $VER on Windows/$ARCH — build from source (see docs)"
132
+ }
133
+
134
+ # verify version stamp
135
+ $stamp = (& "$TMP\llama.exe" version | Out-String)
136
+ if ($LASTEXITCODE) { Pop-Location; Die "downloaded binary failed to run" }
137
+ $ok = ""
138
+ if ($VER -match "^b([0-9]+)$") { $ok = "build $($Matches[1])," }
139
+ elseif ($VER -match "^g([0-9a-f]+)$") { $ok = "commit $($Matches[1]))" }
140
+ elseif ($VER -match "^v(.+)$") { $ok = $Matches[1] }
141
+ if (!$ok) { Pop-Location; Die "invalid version: $VER" }
142
+ if (!$stamp.Contains($ok)) { Pop-Location; Die "version mismatch: expected '$ok', binary reports: $stamp" }
143
+
144
+ $target = Join-Path $VERSIONS_DIR $VER
145
+ New-Item -Path $target -ItemType Directory -Force | Out-Null
146
+ Move-Item "$TMP\llama.exe" (Join-Path $target "llama.exe") -Force
147
+ Pop-Location
148
+ Remove-Item $TMP -Recurse -Force -ErrorAction SilentlyContinue
149
+ Info "installed llama.cpp $VER"
150
+ }
151
+
152
+ function Activate {
153
+ param([string]$VER)
154
+ $bin = Join-Path $VERSIONS_DIR $VER "llama.exe"
155
+ if (!(Test-Path $bin)) { Die "$VER is not installed (run: llamainstall install $VER)" }
156
+ if (!(Test-Path $SHIM_DIR)) { New-Item -Path $SHIM_DIR -ItemType Directory -Force | Out-Null }
157
+ Copy-Item $bin $SHIM -Force
158
+ Set-Content -Path $ACTIVE_FILE -Value $VER
159
+ Info "llama.exe -> $VER ($SHIM [already on your PATH])"
160
+ }
161
+
162
+ # --- commands -----------------------------------------------------------------
163
+
164
+ function Cmd-Install { param([string]$Req)
165
+ $VER = Resolve-Version $Req
166
+ Info "llama.cpp version: $VER"
167
+ Install-Version $VER
168
+ Activate $VER
169
+ Info "done. try: llama.exe serve (or: llamainstall serve)"
170
+ }
171
+
172
+ function Cmd-Use { param([string]$Req)
173
+ if (!$Req) { Die "usage: llamainstall use <version>" }
174
+ Activate (Resolve-Version $Req)
175
+ }
176
+
177
+ function Cmd-List {
178
+ if (!(Test-Path $VERSIONS_DIR)) { Info "no versions installed — try: llamainstall install"; return }
179
+ $active = ""; if (Test-Path $ACTIVE_FILE) { $active = Get-Content $ACTIVE_FILE }
180
+ Get-ChildItem $VERSIONS_DIR -Directory | Sort-Object Name | ForEach-Object {
181
+ if ($_.Name -eq $active) { Info " * $($_.Name) (active)" }
182
+ else { Info " $($_.Name)" }
183
+ }
184
+ }
185
+
186
+ function Cmd-Remove { param([string]$Req)
187
+ if (!$Req) { Die "usage: llamainstall remove <version>" }
188
+ $VER = Resolve-Version $Req
189
+ $target = Join-Path $VERSIONS_DIR $VER
190
+ if (!(Test-Path $target)) { Die "$VER is not installed" }
191
+ Remove-Item $target -Recurse -Force
192
+ if ((Get-Content $ACTIVE_FILE -ErrorAction SilentlyContinue) -eq $VER) {
193
+ Remove-Item $ACTIVE_FILE, $SHIM -Force -ErrorAction SilentlyContinue
194
+ $next = Get-ChildItem $VERSIONS_DIR -Directory | Sort-Object Name | Select-Object -Last 1
195
+ if ($next) { Activate $next.Name }
196
+ }
197
+ Info "removed $VER"
198
+ }
199
+
200
+ function Cmd-Doctor {
201
+ Info "== llamainstall doctor =="
202
+ Info "OS: Windows $([System.Environment]::OSVersion.Version)"
203
+ Info "Arch: $env:PROCESSOR_ARCHITECTURE"
204
+ Info "RAM: $([math]::Round((Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory / 1GB, 1)) GB"
205
+ Info ""
206
+ Info "== GPU =="
207
+ Get-CimInstance Win32_VideoController | ForEach-Object {
208
+ $mem = if ($_.AdapterRAM -gt 0) { "$([math]::Round($_.AdapterRAM / 1GB, 1)) GB" } else { "?" }
209
+ Info " GPU: $($_.Name) (VRAM: $mem)"
210
+ }
211
+ Info ""
212
+ Info "== Notes =="
213
+ Info " * CUDA acceleration requires the NVIDIA CUDA Toolkit: winget install Nvidia.CUDA"
214
+ Info " * ROCm/HIP for AMD: see https://rocm.docs.amd.com/projects/install-on-windows"
215
+ Info " * Vulkan works on most GPUs (including iGPUs) without extra toolkits"
216
+ Info " * Run 'llamainstall install' to auto-select the best backend."
217
+ }
218
+
219
+ function Cmd-Models {
220
+ @"
221
+ Popular GGUF models (from the Hugging Face model hub):
222
+
223
+ Approx. Model HF repo id
224
+ ---------------------------------------------------------------
225
+ 0.6 GB Qwen3.5-0.8B-GGUF ggml-org/Qwen3.5-0.8B-GGUF
226
+ 0.9 GB Llama-3.2-1B-Instruct-GGUF ggml-org/Llama-3.2-1B-Instruct-GGUF
227
+ 2.0 GB Qwen3-4B-GGUF ggml-org/Qwen3-4B-GGUF
228
+ 2.2 GB Llama-3.2-3B-Instruct-GGUF ggml-org/Llama-3.2-3B-Instruct-GGUF
229
+ 4.9 GB Qwen3-8B-GGUF ggml-org/Qwen3-8B-GGUF
230
+ 5.0 GB DeepSeek-R1-Distill-Qwen-7B ggml-org/DeepSeek-R1-Distill-Qwen-7B-GGUF
231
+
232
+ Quick start: llamainstall serve
233
+ "@ -split "`n" | ForEach-Object { Info $_ }
234
+ }
235
+
236
+ function Cmd-Serve { param([string]$Model)
237
+ if (!(Test-Path $SHIM)) { Die "llama.exe is not installed yet — run: llamainstall install" }
238
+ if (!$Model) { $Model = "ggml-org/Qwen3.5-0.8B-GGUF" }
239
+ Info "starting llama server with $Model (model download happens on first launch)..."
240
+ & $SHIM serve -hf $Model
241
+ }
242
+
243
+ function Cmd-Uninstall {
244
+ Info "removing $BASE and $SHIM..."
245
+ Remove-Item $BASE, $SHIM -Recurse -Force -ErrorAction SilentlyContinue
246
+ Info "done. llama.cpp has been removed."
247
+ }
248
+
249
+ # --- entry --------------------------------------------------------------------
250
+
251
+ function Main {
252
+ param([string[]]$Args)
253
+ switch ($env:PROCESSOR_ARCHITECTURE) {
254
+ "ARM64" { $global:ARCH = "aarch64" }
255
+ "AMD64" { $global:ARCH = "x86_64" }
256
+ default { Die "unsupported architecture: $env:PROCESSOR_ARCHITECTURE" }
257
+ }
258
+ if (!$env:LLAMA_VERSION) { $global:VER = $null } else { $global:VER = $env:LLAMA_VERSION }
259
+
260
+ $cmd = if ($Args.Count -gt 0) { $Args[0] } else { "install" }
261
+ $arg = if ($Args.Count -gt 1) { $Args[1] } else { $null }
262
+ switch ($cmd) {
263
+ { $_ -in @("install", "default", "i") } { Cmd-Install $arg; break }
264
+ { $_ -in @("use", "switch") } { Cmd-Use $arg; break }
265
+ { $_ -in @("list", "ls") } { Cmd-List; break }
266
+ { $_ -in @("remove", "rm") } { Cmd-Remove $arg; break }
267
+ { $_ -in @("doctor", "dr") } { Cmd-Doctor; break }
268
+ "models" { Cmd-Models; break }
269
+ "serve" { Cmd-Serve $arg; break }
270
+ "uninstall" { Cmd-Uninstall; break }
271
+ { $_ -in @("--version", "-V") } { Info "llamainstall 1.0.0 (for llama.cpp v1.x)"; break }
272
+ { $_ -in @("-h", "--help", "help") } { Get-Content $MyInvocation.MyCommand.Path | Where-Object { $_ -like "#*" } | ForEach-Object { $_ -replace "^# ?", "" } ; break }
273
+ default { Die "unknown command: $cmd (see: llamainstall help)" }
274
+ }
275
+ }
276
+
277
+ Main @args