-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrebuild_llama.cpp.ps1
217 lines (162 loc) · 6.26 KB
/
rebuild_llama.cpp.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
#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]
$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 "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"
Write-Host "Version: ${version}" -ForegroundColor "DarkYellow"
Write-Host "BLAS accelerator: ${blasAccelerator}" -ForegroundColor "DarkYellow"
Write-Host "Build target: ${buildTargetInformation}" -ForegroundColor "DarkYellow"
$openBLASVersion = "0.3.27"
if (-not(Test-Path -Path "./vendor/OpenBLAS/OpenBLAS-${openBLASVersion}-x64.zip")) {
Invoke-WebRequest `
-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 `
-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
}
function Resolve-UnixPath {
Param ([String] $path)
Write-Output ((Resolve-Path "$path").Path -replace '\\','/')
}
# We are resetting every submodule to their head prior
# to updating them to avoid any merge conflicts.
git submodule foreach --recursive git reset --hard
git submodule update --remote --merge --force
# We are checking out a specific version (tag / commit)
# of the repository to enable quick debugging.
git -C ./vendor/llama.cpp checkout $version
$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"
switch ($blasAccelerator) {
"OpenBLAS" {
cmake `
-DGGML_BLAS=ON `
-DGGML_BLAS_VENDOR=OpenBLAS `
..
}
"CUDA" {
cmake `
-DGGML_CUDA=ON `
..
}
default {
cmake ..
}
}
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 and also overriding some package versions.
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"