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

$ErrorActionPreference = "Stop"

Daniel Hiltgen's avatar
Daniel Hiltgen committed
5
6
7
8
9
10
11
12
13
14
15
function amdGPUs {
    if ($env:AMDGPU_TARGETS) {
        return $env:AMDGPU_TARGETS
    }
    # TODO - load from some common data file for linux + windows build consistency
    $GPU_LIST = @(
        "gfx900"
        "gfx906:xnack-"
        "gfx908:xnack-"
        "gfx90a:xnack+"
        "gfx90a:xnack-"
16
17
18
        "gfx940"
        "gfx941"
        "gfx942"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
19
20
21
22
23
24
25
26
27
28
        "gfx1010"
        "gfx1012"
        "gfx1030"
        "gfx1100"
        "gfx1101"
        "gfx1102"
    )
    $GPU_LIST -join ';'
}

29
function init_vars {
30
    $script:SRC_DIR = $(resolve-path "..\..\")
31
    $script:llamacppDir = "../llama.cpp"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
32
33
34
35
    $script:cmakeDefs = @(
        "-DBUILD_SHARED_LIBS=on",
        "-DLLAMA_NATIVE=off"
        )
36
    $script:cmakeTargets = @("ollama_llama_server")
37
    $script:ARCH = "amd64" # arm not yet supported.
38
    $script:DIST_BASE = "${script:SRC_DIR}\dist\windows-${script:ARCH}\ollama_runners"
39
    if ($env:CGO_CFLAGS -contains "-g") {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
40
        $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on", "-DCMAKE_BUILD_TYPE=RelWithDebInfo")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
41
        $script:config = "RelWithDebInfo"
42
    } else {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
43
        $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off", "-DCMAKE_BUILD_TYPE=Release")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
44
        $script:config = "Release"
45
    }
Daniel Hiltgen's avatar
Daniel Hiltgen committed
46
47
48
    if ($null -ne $env:CMAKE_SYSTEM_VERSION) {
        $script:cmakeDefs += @("-DCMAKE_SYSTEM_VERSION=${env:CMAKE_SYSTEM_VERSION}")
    }
49
50
51
52
53
    # 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)
54
            $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
55
56
57
58
59
        }
    } else {
        $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
    }
    $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
60
61
62
63
64
    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
    }
65
66
67
68
69
70
    # Note: Windows Kits 10 signtool crashes with GCP's plugin
    if ($null -eq $env:SIGN_TOOL) {
        ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
    } else {
        ${script:SignTool}=${env:SIGN_TOOL}
    }
71
72
73
    if ("${env:KEY_CONTAINER}") {
        ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
    }
74
75
76
77
78
}

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
79
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
80
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
81
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
82
83
84
}

function apply_patches {
85
    # Wire up our CMakefile
86
87
    if (!(Select-String -Path "${script:llamacppDir}/CMakeLists.txt" -Pattern 'ollama')) {
        Add-Content -Path "${script:llamacppDir}/CMakeLists.txt" -Value 'add_subdirectory(../ext_server ext_server) # ollama'
88
    }
89
90
91
92
93
94
95
96
97
98
99
100

    # 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
        foreach ($file in $filePaths) {
101
            git -C "${script:llamacppDir}" checkout $file
102
103
104
105
106
        }
    }

    # Apply each patch
    foreach ($patch in $patches) {
107
        git -C "${script:llamacppDir}" apply $patch.FullName
108
    }
109
110
111
}

function build {
112
    write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
113
    & cmake --version
114
    & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
115
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
116
    write-host "building with: cmake --build $script:buildDir --config $script:config $($script:cmakeTargets | ForEach-Object { `"--target`", $_ })"
117
    & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
Daniel Hiltgen's avatar
Daniel Hiltgen committed
118
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
119
120
121
122
    # Rearrange output to be consistent between different generators
    if ($null -ne ${script:config} -And (test-path -path "${script:buildDir}/bin/${script:config}" ) ) {
        mv -force "${script:buildDir}/bin/${script:config}/*" "${script:buildDir}/bin/"
        remove-item "${script:buildDir}/bin/${script:config}"
123
124
125
    }
}

126
127
function sign {
    if ("${env:KEY_CONTAINER}") {
128
129
130
        write-host "Signing ${script:buildDir}/bin/*.exe  ${script:buildDir}/bin/*.dll"
        foreach ($file in @(get-childitem "${script:buildDir}/bin/*.exe") + @(get-childitem "${script:buildDir}/bin/*.dll")){
            & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
131
132
133
134
135
136
                /csp "Google Cloud KMS Provider" /kc "${env:KEY_CONTAINER}" $file
            if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
        }
    }
}

137
138
139
function install {
    write-host "Installing binaries to dist dir ${script:distDir}"
    mkdir ${script:distDir} -ErrorAction SilentlyContinue
140
141
    $binaries = dir "${script:buildDir}/bin/*.exe"
    foreach ($file in $binaries) {
142
        copy-item -Path $file -Destination ${script:distDir} -Force
143
144
    }

145
    write-host "Installing dlls to dist dir ${script:distDir}"
mofanke's avatar
mofanke committed
146
    $dlls = dir "${script:buildDir}/bin/*.dll"
147
    foreach ($file in $dlls) {
148
        copy-item -Path $file -Destination ${script:distDir} -Force
149
    }
150
151
}

152
function cleanup {
153
154
155
156
157
158
159
160
161
162
    $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
        foreach ($file in $filePaths) {            
163
            git -C "${script:llamacppDir}" checkout $file
164
        }
165
        git -C "${script:llamacppDir}" checkout CMakeLists.txt
166
    }
167
168
169
170
171
}

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

173
174
175
176
# -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
# -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
# -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver

177
$script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on")
178

179
if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
180

181
182
# GCC build for direct linking into the Go binary
init_vars
183
184
185
186
187
188
# cmake will silently fallback to msvc compilers if mingw isn't in the path, so detect and fail fast
# as we need this to be compiled by gcc for golang to be able to link with itx
write-host "Checking for MinGW..."
# error action ensures we exit on failure
get-command gcc
get-command mingw32-make
189
190
191
$script:cmakeTargets = @("llama", "ggml")
$script:cmakeDefs = @(
    "-G", "MinGW Makefiles"
192
193
    "-DCMAKE_C_COMPILER=gcc.exe",
    "-DCMAKE_CXX_COMPILER=g++.exe",
194
195
196
197
198
199
200
201
202
203
204
205
    "-DBUILD_SHARED_LIBS=off",
    "-DLLAMA_NATIVE=off",
    "-DLLAMA_AVX=off",
    "-DLLAMA_AVX2=off",
    "-DLLAMA_AVX512=off",
    "-DLLAMA_F16C=off",
    "-DLLAMA_FMA=off")
$script:buildDir="../build/windows/${script:ARCH}_static"
write-host "Building static library"
build

# remaining llama.cpp builds use MSVC 
206
207
    init_vars
    $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
208
    $script:buildDir="../build/windows/${script:ARCH}/cpu"
209
    $script:distDir="$script:DIST_BASE\cpu"
210
211
212
    write-host "Building LCD CPU"
    build
    sign
213
    install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
214

215
216
    init_vars
    $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
217
    $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
218
    $script:distDir="$script:DIST_BASE\cpu_avx"
219
220
221
    write-host "Building AVX CPU"
    build
    sign
222
    install
223
224
225

    init_vars
    $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
226
    $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
227
    $script:distDir="$script:DIST_BASE\cpu_avx2"
228
229
230
    write-host "Building AVX2 CPU"
    build
    sign
231
    install
232
233
234
} else {
    write-host "Skipping CPU generation step as requested"
}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
235

236
237
if ($null -ne $script:CUDA_LIB_DIR) {
    # Then build cuda as a dynamically loaded library
238
239
    $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
    $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
240
241
242
243
    if ($null -ne $script:CUDA_VERSION) {
        $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
    }
    init_vars
244
    $script:buildDir="../build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
245
    $script:distDir="$script:DIST_BASE\cuda$script:CUDA_VARIANT"
246
    $script:cmakeDefs += @("-A", "x64", "-DLLAMA_CUDA=ON", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
Jeremy's avatar
Jeremy committed
247
248
249
    if ($null -ne $env:OLLAMA_CUSTOM_CUDA_DEFS) {
        write-host "OLLAMA_CUSTOM_CUDA_DEFS=`"${env:OLLAMA_CUSTOM_CUDA_DEFS}`""
        $script:cmakeDefs +=@("${env:OLLAMA_CUSTOM_CUDA_DEFS}")
Jeremy's avatar
Jeremy committed
250
251
        write-host "building custom CUDA GPU"
    }
252
    build
253
    sign
254
    install
255
}
256

Daniel Hiltgen's avatar
Daniel Hiltgen committed
257
258
259
260
261
262
263
if ($null -ne $env:HIP_PATH) {
    $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
    if ($null -ne $script:ROCM_VERSION) {
        $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
    }

    init_vars
264
    $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
265
    $script:distDir="$script:DIST_BASE\rocm$script:ROCM_VARIANT"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
266
267
268
269
270
    $script:cmakeDefs += @(
        "-G", "Ninja", 
        "-DCMAKE_C_COMPILER=clang.exe",
        "-DCMAKE_CXX_COMPILER=clang++.exe",
        "-DLLAMA_HIPBLAS=on",
271
        "-DHIP_PLATFORM=amd",
Daniel Hiltgen's avatar
Daniel Hiltgen committed
272
273
274
275
276
277
278
279
        "-DLLAMA_AVX=on",
        "-DLLAMA_AVX2=off",
        "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
        "-DAMDGPU_TARGETS=$(amdGPUs)",
        "-DGPU_TARGETS=$(amdGPUs)"
        )

    # Make sure the ROCm binary dir is first in the path
280
    $env:PATH="$env:HIP_PATH\bin;$env:PATH"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
281
282
283

    # We have to clobber the LIB var from the developer shell for clang to work properly
    $env:LIB=""
Jeremy's avatar
Jeremy committed
284
285
286
    if ($null -ne $env:OLLAMA_CUSTOM_ROCM_DEFS) {
        write-host "OLLAMA_CUSTOM_ROCM_DEFS=`"${env:OLLAMA_CUSTOM_ROCM_DEFS}`""
        $script:cmakeDefs += @("${env:OLLAMA_CUSTOM_ROCM_DEFS}")
Jeremy's avatar
Jeremy committed
287
288
        write-host "building custom ROCM GPU"
    }
Daniel Hiltgen's avatar
Daniel Hiltgen committed
289
290
291
292
293
    write-host "Building ROCm"
    build
    # Ninja doesn't prefix with config name
    ${script:config}=""
    if ($null -ne $script:DUMPBIN) {
294
        & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
295
296
    }
    sign
297
    install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
298
}
299

300

301
cleanup
302
write-host "`ngo generate completed.  LLM runners: $(get-childitem -path $script:DIST_BASE)"