gen_windows.ps1 7.92 KB
Newer Older
1
2
3
4
5
#!powershell

$ErrorActionPreference = "Stop"

function init_vars {
6
    $script:SRC_DIR = $(resolve-path "..\..\")
7
    $script:llamacppDir = "../llama.cpp"
8
    $script:cmakeDefs = @("-DBUILD_SHARED_LIBS=on", "-DLLAMA_NATIVE=off",  "-A", "x64")
9
10
    $script:cmakeTargets = @("ext_server")
    $script:ARCH = "amd64" # arm not yet supported.
11
    if ($env:CGO_CFLAGS -contains "-g") {
12
        $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
13
        $script:config = "RelWithDebInfo"
14
    } else {
15
        $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
16
        $script:config = "Release"
17
    }
18
19
20
21
22
    # Try to find the CUDA dir
    if ($env:CUDA_LIB_DIR -eq $null) {
        $d=(get-command -ea 'silentlycontinue' nvcc).path
        if ($d -ne $null) {
            $script:CUDA_LIB_DIR=($d| split-path -parent)
23
            $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
24
25
26
27
        }
    } else {
        $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
    }
28
    $script:GZIP=(get-command -ea 'silentlycontinue' gzip).path
29
    $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
30
31
32
33
34
    if ($null -eq $env:CMAKE_CUDA_ARCHITECTURES) {
        $script:CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
    } else {
        $script:CMAKE_CUDA_ARCHITECTURES=$env:CMAKE_CUDA_ARCHITECTURES
    }
35
36
    # Note: 10 Windows Kit signtool crashes with GCP's plugin
    ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
37
38
39
    if ("${env:KEY_CONTAINER}") {
        ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
    }
40
41
42
43
44
}

function git_module_setup {
    # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
    & git submodule init
Daniel Hiltgen's avatar
Daniel Hiltgen committed
45
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
46
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
47
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
48
49
50
}

function apply_patches {
51
    # Wire up our CMakefile
52
53
    if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
        Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama'
54
    }
55
56
57
58
59
60
61
62
63
64
65

    # Apply temporary patches until fix is upstream
    $patches = Get-ChildItem "../patches/*.diff"
    foreach ($patch in $patches) {
        # Extract file paths from the patch file
        $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
            $parts = $_ -split ' '
            ($parts[1] -split '/', 2)[1]
        }

        # Checkout each file
66
        Set-Location -Path ${script:llamacppDir}
67
68
69
70
71
72
73
74
75
76
77
        foreach ($file in $filePaths) {
            git checkout $file
        }
    }

    # Apply each patch
    foreach ($patch in $patches) {
        Set-Location -Path ${script:llamacppDir}
        git apply $patch.FullName
    }

78
    # Avoid duplicate main symbols when we link into the cgo binary
79
    $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
80
    $content = $content -replace 'int main\(', 'int __main('
81
    Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
82
83
84
}

function build {
85
    write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
86
    & cmake --version
87
    & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
88
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
89
90
    write-host "building with: cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })"
    & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
Daniel Hiltgen's avatar
Daniel Hiltgen committed
91
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
92
93
}

94
95
96
function install {
    rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
    md "${script:buildDir}/lib" -ea 0 > $null
97
    cp "${script:buildDir}/bin/${script:config}/ext_server.dll" "${script:buildDir}/lib"
98
    cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
99
    # Display the dll dependencies in the build log
100
101
102
103
104
    if ($script:DUMPBIN -ne $null) {
        & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/${script:config}/ext_server.dll" | select-string ".dll"
    }
}

105
106
107
108
function sign {
    if ("${env:KEY_CONTAINER}") {
        write-host "Signing ${script:buildDir}/lib/*.dll"
        foreach ($file in (get-childitem "${script:buildDir}/lib/*.dll")){
109
            & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
110
111
112
113
114
115
                /csp "Google Cloud KMS Provider" /kc "${env:KEY_CONTAINER}" $file
            if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
        }
    }
}

116
function compress_libs {
117
118
    if ($script:GZIP -eq $null) {
        write-host "gzip not installed, not compressing files"
119
120
121
122
123
        return
    }
    write-host "Compressing dlls..."
    $libs = dir "${script:buildDir}/lib/*.dll"
    foreach ($file in $libs) {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
124
        & "$script:GZIP" --best -f $file
125
    }
126
127
}

128
function cleanup {
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    $patches = Get-ChildItem "../patches/*.diff"
    foreach ($patch in $patches) {
        # Extract file paths from the patch file
        $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
            $parts = $_ -split ' '
            ($parts[1] -split '/', 2)[1]
        }

        # Checkout each file
        Set-Location -Path ${script:llamacppDir}
        foreach ($file in $filePaths) {            
            git checkout $file
        }
    }
143
    Set-Location "${script:llamacppDir}/examples/server"
144
    git checkout CMakeLists.txt server.cpp
145

146
147
148
149
150
}

init_vars
git_module_setup
apply_patches
Daniel Hiltgen's avatar
Daniel Hiltgen committed
151

152
153
154
155
156
157
158
159
# -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
# -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
# -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
# -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver

$script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on", "-DLLAMA_NATIVE=off")

$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
160
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu"
161
162
163
write-host "Building LCD CPU"
build
install
164
sign
165
compress_libs
166
167

$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
168
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx"
169
170
171
write-host "Building AVX CPU"
build
install
172
sign
173
compress_libs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
174

175
$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
176
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx2"
177
write-host "Building AVX2 CPU"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
178
build
179
install
180
sign
181
compress_libs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
182

183
184
if ($null -ne $script:CUDA_LIB_DIR) {
    # Then build cuda as a dynamically loaded library
185
186
    $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
    $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
187
188
189
190
191
    if ($null -ne $script:CUDA_VERSION) {
        $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
    }
    init_vars
    $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
192
    $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
193
194
    build
    install
195
    sign
196
197
    compress_libs
}
198
# TODO - actually implement ROCm support on windows
199
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/rocm"
200

201
202
203
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
204
205

cleanup
206
write-host "`ngo generate completed"