-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.ps1
341 lines (262 loc) · 10.7 KB
/
server.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#Requires -Version 5.0
<#
.SYNOPSIS
Automatically starts the llama.cpp server with optimal settings.
.DESCRIPTION
This script automatically starts the llama.cpp server with optimal settings.
.PARAMETER model
Specifies the path to the GGUF model file.
.PARAMETER chatTemplate
Specifies the chat template (options: chatml, command-r, deepseek, gemma, llama2, llama3, monarch, openchat, orion, vicuna, vicuna-orca, zephyr).
.PARAMETER parallel
Specifies the number of slots for process requests (default: 1).
.PARAMETER contextSize
Specifies the prompt context size in tokens.
.PARAMETER numberOfGPULayers
Specifies the number of layers offloaded into the GPU.
.PARAMETER modelContextLength
Specifies the models context length it was trained on.
.PARAMETER kvCacheDataType
Specifies the KV cache data type (options: f32, f16, q8_0, q4_0).
.PARAMETER verbose
Increases the verbosity of the llama.cpp server.
.PARAMETER help
Shows the manual on how to use this script.
.EXAMPLE
.\server.ps1 -model "..\vendor\llama.cpp\models\gemma-2-9b-it-IQ4_XS.gguf"
.EXAMPLE
.\server.ps1 -model "C:\models\gemma-2-9b-it-IQ4_XS.gguf" -chatTemplate "llama3" -parallel 4
.EXAMPLE
.\server.ps1 -model "C:\models\gemma-2-9b-it-IQ4_XS.gguf" -contextSize 4096 -numberOfGPULayers 10
.EXAMPLE
.\server.ps1 -model "C:\models\gemma-2-9b-it-IQ4_XS.gguf" -port 8081 -kvCacheDataType q8_0
.EXAMPLE
.\server.ps1 -model "..\vendor\llama.cpp\models\gemma-2-9b-it-IQ4_XS.gguf" -verbose
#>
Param (
[Parameter(
HelpMessage="The path to the GGUF model file."
)]
[String]
$model,
[Parameter(
HelpMessage="The chat template."
)]
[ValidateSetAttribute(
"chatml",
"command-r",
"deepseek",
"gemma",
"llama2",
"llama3",
"monarch",
"openchat",
"orion",
"vicuna",
"vicuna-orca",
"zephyr"
)]
[String]
$chatTemplate,
[Parameter(
HelpMessage="The number of slots for process requests."
)]
[ValidateRange(1,256)]
[Int]
$parallel=1,
[Parameter(
HelpMessage="The prompt context size in tokens."
)]
[Int]
$contextSize,
[Parameter(
HelpMessage="The number of layers offloaded into the GPU."
)]
[Int]
$numberOfGPULayers=-1,
[Parameter(
HelpMessage="The server port."
)]
[Int]
$port=8080,
[Parameter(
HelpMessage="Specifies the models context length it was trained on."
)]
[Int]
$modelContextLength=-1,
[Parameter(
HelpMessage="Specifies the KV cache data type."
)]
[ValidateSetAttribute("f32", "f16", "q8_0", "q4_0")]
[String]
$kvCacheDataType="f16",
[switch]
$help
)
if ($help) {
Get-Help -Detailed $PSCommandPath
exit
}
# The -verbose option is a default PowerShell parameter.
$verbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent -eq $true
# We are resolving the absolute path to the llama.cpp project directory.
$llamaCppPath = Resolve-Path -Path "${PSScriptRoot}\..\vendor\llama.cpp"
function Convert-FileSize($length) {
$suffix = "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
$index = 0
while ($length -gt 1kb) {
$length = $length / 1kb
$index++
}
"{0:N1} {1}" -f $length, $suffix[$index]
}
# We are listing possible models to choose from.
if (!$model) {
Write-Host "Please add the -model option with one of the following paths: " -ForegroundColor "DarkYellow"
Get-ChildItem -Path "${llamaCppPath}\models\" -Filter '*.gguf' -Exclude 'ggml-vocab-*' -Recurse | `
ForEach-Object {
New-Object PSObject -Property @{
FullName = Resolve-Path -Relative $_.FullName
FileSize = Convert-FileSize $_.Length
}
}
exit
}
# We are using the filename of the model as an alias for
# the API responses to not leak the directory structure.
$alias = (Get-ChildItem $model).Name
$numberOfPhysicalCores = Get-CimInstance -ClassName 'Win32_Processor' | Select -ExpandProperty "NumberOfCores"
conda activate llama.cpp
$modelFileSize = (Get-Item -Path "${model}").Length
$modelDataIsAvailable = $false
Write-Host "Extracting model details..." -ForegroundColor "Yellow"
try {
# We are trying to extract model details from the GGUF file.
# https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#llm
# TODO: Find a robust way to resolve this values.
$modelData = Invoke-Expression "python ${llamaCppPath}\gguf-py\scripts\gguf_dump.py --no-tensors `"${model}`""
$modelContextLength = [Int]($modelData | Select-String -Pattern '\bcontext_length = (\d+)\b').Matches.Groups[1].Value
$modelHeadCount = [Int]($modelData | Select-String -Pattern '\bhead_count = (\d+)\b').Matches.Groups[1].Value
$modelBlockCount = [Int]($modelData | Select-String -Pattern '\bblock_count = (\d+)\b').Matches.Groups[1].Value
$modelEmbeddingLength = [Int]($modelData | Select-String -Pattern '\bembedding_length = (\d+)\b').Matches.Groups[1].Value
# We are adding a fallback to the head_count value if the
# head_count_kv value is not set. A missing head_count_kv
# means that the model does not use Grouped-Query-Attention.
$matchHeadCountKV = $modelData | Select-String -Pattern '\bhead_count_kv = (\d+)\b'
if ($matchHeadCountKV){
$modelHeadCountKV = [Int]($matchHeadCountKV.Matches.Groups[1].Value)
} else {
$modelHeadCountKV = $modelHeadCount
}
$modelDataIsAvailable = $true
}
catch {
Write-host $_.Exception.Message -ForegroundColor "Red"
Write-Host $_.ScriptStackTrace -Foreground "DarkGray"
if ($modelContextLength -lt 0) {
throw "Failed to extract model details, please provide the -modelContextLength value to use this model."
}
Write-Host "Failed to extract model details, proceeding without automated GPU offloading..." -ForegroundColor "Yellow"
}
if (!$contextSize) {
# We are defaulting the optimal model context size
# for each independent sequence slot. For details see:
# https://github.com/ggerganov/llama.cpp/discussions/4130
$contextSize = $modelContextLength * $parallel
}
if ($modelDataIsAvailable) {
# The Key (K) and Value (V) states of the model are cached in a FP16 format.
# The allocated size of the KV Cache is based on specific model details.
# https://github.com/ggerganov/llama.cpp/discussions/3485
# https://github.com/ollama/ollama/blob/v0.1.25/llm/llm.go#L51
$kvSize = 2 * 2 * $contextSize * $modelBlockCount * $modelEmbeddingLength * $modelHeadCountKV / $modelHeadCount
# The compute graph size is the amount of overhead and tensors
# llama.cpp needs to allocate. This is an estimated value.
# https://github.com/ollama/ollama/blob/v0.1.25/llm/llm.go#L56
$graphSize = ($modelHeadCount / $modelHeadCountKV) * $kvSize / 6
# The maximum number of layers are the model blocks plus one input layer.
# https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#llm
$maximumNumberOfLayers = $modelBlockCount + 1
$averageLayerSize = $modelFileSize / $maximumNumberOfLayers
$freeMemory = ([Int](
Get-CIMInstance Win32_OperatingSystem | Select -ExpandProperty FreePhysicalMemory
) * 1024)
$freeGPUMemory = 0
$enableFlashAttention = $false
# We are using the presence of NVIDIA System Management Interface
# (nvidia-smi) and NVIDIA CUDA Compiler Driver (nvcc) to infer
# the availability of a CUDA-compatible GPU.
if ((Get-Command "nvidia-smi" -ErrorAction SilentlyContinue) -and
(Get-Command "nvcc" -ErrorAction SilentlyContinue)) {
$freeGPUMemory = ([Int](
Invoke-Expression "nvidia-smi --query-gpu=memory.free --format=csv,noheader" | `
Select-String -Pattern '\b(\d+) MiB\b'
).Matches.Groups[1].Value * 1024 * 1024)
# The CUDA Flash Attention implementation of llama.cpp requires
# the NVIDIA GPU to have a Compute Capability of >= 6.0.
# https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications
# https://github.com/ggerganov/llama.cpp/issues/7055
$enableFlashAttention = ([Double](
Invoke-Expression "nvidia-smi --query-gpu=compute_cap --format=csv,noheader"
) -ge 6.0)
# The automatic calculating the optimal number of GPU layers can
# always be "overruled" by using the -numberOfGPULayers option.
if ($numberOfGPULayers -lt 0) {
$numberOfGPULayers = [Math]::Floor(($freeGPUMemory - $kvSize - $graphSize) / $averageLayerSize)
if ($numberOfGPULayers -gt $maximumNumberOfLayers) {
$numberOfGPULayers = $maximumNumberOfLayers
}
if ($numberOfGPULayers -lt 1) {
$numberOfGPULayers = 0
}
}
}
Write-Host "Listing calculated memory details..." -ForegroundColor "Yellow"
[PSCustomObject]@{
"Model Size" = "$([Math]::Ceiling($modelFileSize / 1MB)) MiB"
"KV Cache Size" = "$([Math]::Ceiling($kvSize / 1MB)) MiB"
"Graph Size" = "$([Math]::Ceiling($graphSize / 1MB)) MiB"
"Average Layer Size" = "$([Math]::Ceiling(($averageLayerSize) / 1MB)) MiB"
"Minimum Required VRAM" = "$([Math]::Ceiling(($averageLayerSize + $graphSize + $kvSize) / 1MB)) MiB"
"Total Required Memory" = "$([Math]::Ceiling(($modelFileSize + $graphSize + $kvSize) / 1MB)) MiB"
"Free GPU Memory (VRAM)" = "$([Math]::Ceiling($freeGPUMemory / 1MB)) MiB"
"Free System Memory (RAM)" = "$([Math]::Ceiling($freeMemory / 1MB)) MiB"
} | Format-List | Out-String | ForEach-Object { $_.Trim("`r","`n") }
}
# The global fallback is using only the CPU.
if ($numberOfGPULayers -lt 0) {
$numberOfGPULayers = 0
}
Write-Host "Starting llama.cpp server with custom options at http://127.0.0.1:${port}..." -ForegroundColor "Yellow"
$commandBinary = "${llamaCppPath}\build\bin\Release\llama-server"
$commandArguments = @(
"--n-predict 1024",
"--port '${port}'",
"--model '${model}'",
"--alias '${alias}'",
"--ctx-size '${contextSize}'",
"--threads '${numberOfPhysicalCores}'",
"--n-gpu-layers '${numberOfGPULayers}'",
"--parallel '${parallel}'",
"--cache-type-k '${kvCacheDataType}'",
"--cache-type-v '${kvCacheDataType}'"
)
if ($chatTemplate) {
$commandArguments += "--chat-template '${chatTemplate}'"
}
if ($enableFlashAttention) {
$commandArguments += "--flash-attn"
}
if ($verbose) {
$commandArguments += "--verbose"
}
$commandArguments = $commandArguments | ForEach-Object {
if ($commandArguments.IndexOf($_) -ne $commandArguments.Count - 1) {
" $_ ```n"
} else {
" $_ `n"
}
}
$command = $commandBinary + " ```n " + $commandArguments
Write-Host $command -ForegroundColor "Green"
Invoke-Expression $command