-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrebuild_llama.cpp.ps1
More file actions
279 lines (214 loc) · 8.7 KB
/
rebuild_llama.cpp.ps1
File metadata and controls
279 lines (214 loc) · 8.7 KB
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
#Requires -Version 5.0
<#
.SYNOPSIS
Automatically rebuild llama.cpp for a Windows environment.
.DESCRIPTION
This script automatically rebuilds llama.cpp for a Windows environment.
.PARAMETER blasAccelerator
Specifies the BLAS accelerator, supported values are: "OpenBLAS", "CUDA", "OFF"
.PARAMETER version
Specifies a llama.cpp commit or tag to checkout a specific version.
.PARAMETER target
Specifies CMake build targets to compile a specific subset of the llama.cpp project.
.PARAMETER help
Shows the manual on how to use this script.
.EXAMPLE
.\rebuild_llama.cpp.ps1
.EXAMPLE
.\rebuild_llama.cpp.ps1 -blasAccelerator "OpenBLAS" -version "50e0535"
.EXAMPLE
.\rebuild_llama.cpp.ps1 -target "llama-server llama-cli"
#>
Param (
[ValidateSet("OpenBLAS", "CUDA", "OFF")]
[String]
$blasAccelerator,
[String]
$version,
[String]
$pullRequest,
[String]
$target,
[switch]
$help
)
if ($help) {
Get-Help -Detailed $PSCommandPath
exit
}
$stopwatch = [System.Diagnostics.Stopwatch]::startNew()
# We are defaulting the optional version to the tag of the
# "latest" release in GitHub to avoid unstable versions.
if (!$version) {
$path = [regex]::Match(
(git -C .\vendor\llama.cpp\ ls-remote --get-url),
'(?<=github\.com:).*?(?=\.git)'
).Value
$version = (
(Invoke-WebRequest -UseBasicParsing "https://api.github.com/repos/${path}/releases/latest") | `
ConvertFrom-Json
).tag_name
}
# We are automatically detecting the best BLAS accelerator setting
# on the given system if the user did not specify it manually.
if (!$blasAccelerator) {
# The fallback is using the OpenBLAS library.
$blasAccelerator = "OpenBLAS"
# 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)) {
$blasAccelerator = "CUDA"
}
}
if ($target) {
$buildTargetInformation = "${target}"
} else {
$buildTargetInformation = "(using project defaults)"
}
Write-Host "Building the llama.cpp project..." -ForegroundColor "Yellow"
if (!$pullRequest) {
Write-Host "Version: ${version}" -ForegroundColor "DarkYellow"
} else {
Write-Host "Pull Request: ${pullRequest}" -ForegroundColor "DarkYellow"
}
Write-Host "BLAS accelerator: ${blasAccelerator}" -ForegroundColor "DarkYellow"
Write-Host "Build target: ${buildTargetInformation}" -ForegroundColor "DarkYellow"
$openBLASVersion = "0.3.30"
if (-not(Test-Path -Path "./vendor/OpenBLAS/OpenBLAS-${openBLASVersion}-x64.zip")) {
Invoke-WebRequest `
-UseBasicParsing `
-Uri "https://github.com/xianyi/OpenBLAS/releases/download/v${openBLASVersion}/OpenBLAS-${openBLASVersion}-x64.zip" `
-OutFile "./vendor/OpenBLAS/OpenBLAS-${openBLASVersion}-x64.zip"
Expand-Archive `
-Path "./vendor/OpenBLAS/OpenBLAS-${openBLASVersion}-x64.zip" `
-DestinationPath "./vendor/OpenBLAS" `
-Force
}
if (-not(Test-Path -Path "./vendor/wikitext-2-raw-v1/wikitext-2-raw-v1.zip")) {
Invoke-WebRequest `
-UseBasicParsing `
-Uri "https://huggingface.co/datasets/ggml-org/ci/resolve/main/wikitext-2-raw-v1.zip" `
-OutFile "./vendor/wikitext-2-raw-v1/wikitext-2-raw-v1.zip"
Expand-Archive `
-Path "./vendor/wikitext-2-raw-v1/wikitext-2-raw-v1.zip" `
-DestinationPath "./vendor/wikitext-2-raw-v1" `
-Force
}
if (-not(Test-Path -Path "./vendor/bartowski1182/calibration_datav5.txt")) {
Invoke-WebRequest `
-UseBasicParsing `
-Uri "https://gist.github.com/bartowski1182/82ae9b520227f57d79ba04add13d0d0d/raw/ce111d8971a07caebd8234ef336b2102d6c5fb85/calibration_datav5.txt" `
-OutFile "./vendor/bartowski1182/calibration_datav5.txt"
}
function Resolve-UnixPath {
Param ([String] $path)
Write-Output ((Resolve-Path "$path").Path -replace '\\','/')
}
# TODO: This assumes that every default branch across all
# submodules is equal which might break in the future...
$defaultBranch = "origin/master"
# We are resetting every submodule to the head of their default
# branch prior to updating them to avoid any merge conflicts.
git submodule foreach --recursive git fetch origin
git submodule foreach --recursive git reset --hard $defaultBranch
git submodule update --remote --rebase --force
# Untracked files in the submodule survive `git reset --hard` and `git checkout`.
# A stale `vendor/llama.cpp/build-info.h` from late 2023 has been shadowing the
# new `common/build-info.h` because `tools/server/CMakeLists.txt` adds the repo
# root to the include path. Wipe untracked files (and dirs) so the tree matches
# the checked-out commit exactly.
git -C ./vendor/llama.cpp clean --force -d
if (!$pullRequest) {
# We are checking out a specific version (tag / commit)
# of the repository to enable quick debugging.
git -C ./vendor/llama.cpp checkout $version
} else {
# We are checking out a specific pull request
# of the repository to enable quick debugging.
git -C ./vendor/llama.cpp fetch origin pull/${pullRequest}/head:PR
git -C ./vendor/llama.cpp reset --hard PR
}
$lines = @(
"# This is a workaround for a CMake bug on Windows to build llama.cpp"
"# with OpenBLAS. The find_package(BLAS) call fails to find OpenBLAS,"
"# so we have to link the 'libopenblas.dll' shared library manually."
"# "
"# @see https://github.com/ggerganov/llama.cpp/issues/627"
"# @see https://discourse.cmake.org/t/8414"
"# "
"if (LLAMA_BLAS AND DEFINED LLAMA_BLAS_VENDOR)"
" if (`${LLAMA_BLAS_VENDOR} MATCHES `"OpenBLAS`")"
" set(LLAMA_EXTRA_INCLUDES `${LLAMA_EXTRA_INCLUDES} `"$(Resolve-UnixPath "./vendor/OpenBLAS/include")`")"
" set(LLAMA_EXTRA_LIBS `${LLAMA_EXTRA_LIBS} `"$(Resolve-UnixPath "./vendor/OpenBLAS/lib/libopenblas.dll.a")`")"
" add_compile_definitions(GGML_USE_OPENBLAS)"
" endif()"
"endif()"
""
)
if (!(Select-String -Path "./vendor/llama.cpp/CMakeLists.txt" -Pattern $lines[0] -SimpleMatch -Quiet)) {
$lines + (Get-Content "./vendor/llama.cpp/CMakeLists.txt") | `
Set-Content "./vendor/llama.cpp/CMakeLists.txt"
}
Remove-Item -Path "./vendor/llama.cpp/build" -Force -Recurse
New-Item -Path "./vendor/llama.cpp" -Name "build" -ItemType "directory"
Set-Location -Path "./vendor/llama.cpp/build"
Write-Host "[CMake] Configuring and generating project..." -ForegroundColor "Yellow"
# Upstream ggml/CMakeLists.txt sets `cmake_policy(SET CMP0194 NEW)` and then
# calls `project("ggml" C CXX ASM)`. On CMake 4.1+ that rejects cl.exe as
# an assembler for the generic ASM language, and the Visual Studio generator
# has no integration between generic ASM and MASM. Point CMake at MASM
# (ml64.exe) explicitly; it ships with MSVC but is not normally on PATH.
$ml64 = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" `
-latest -products * -find 'VC\Tools\MSVC\*\bin\Hostx64\x64\ml64.exe' |
Select-Object -First 1
if (-not $ml64) { throw "ml64.exe not found. Install the VS C++ workload." }
switch ($blasAccelerator) {
"OpenBLAS" {
cmake `
-DCMAKE_ASM_COMPILER="$ml64" `
-DGGML_BLAS=ON `
-DGGML_BLAS_VENDOR=OpenBLAS `
-DLLAMA_CURL=OFF `
..
}
"CUDA" {
cmake `
-DCMAKE_ASM_COMPILER="$ml64" `
-DGGML_CUDA=ON `
-DLLAMA_CURL=OFF `
..
}
default {
cmake `
-DCMAKE_ASM_COMPILER="$ml64" `
..
}
}
Write-Host "[CMake] Building project targets '${target}'..." -ForegroundColor "Yellow"
cmake `
--build . `
--config Release `
--parallel (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors `
$(if ($target) { "--target ${target}" })
Copy-Item -Path "../../OpenBLAS/bin/libopenblas.dll" -Destination "./bin/Release/libopenblas.dll"
Set-Location -Path "../../../"
Write-Host "[Python] Installing dependencies..." -ForegroundColor "Yellow"
conda activate llama.cpp
# We are installing the latest available version of all llama.cpp
# project dependencies.
pip install `
--upgrade `
--upgrade-strategy "eager" `
--requirement ./vendor/llama.cpp/requirements.txt
# We are overriding some package versions and installing
# additional packages that are missing from llama.cpp.
pip install `
--upgrade `
--upgrade-strategy "eager" `
--requirement ./requirements_override.txt
conda list
$stopwatch.Stop()
$durationInSeconds = [Math]::Floor([Decimal]($stopwatch.Elapsed.TotalSeconds))
Write-Host "Successfully finished the build in ${durationInSeconds} seconds." -ForegroundColor "Yellow"