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

$ErrorActionPreference = "Stop"

function init_vars {
6
    $script:llamacppDir = "../llama.cpp"
7
    $script:cmakeDefs = @("-DBUILD_SHARED_LIBS=on", "-DLLAMA_NATIVE=off",  "-A","x64")
8
9
    $script:cmakeTargets = @("ext_server")
    $script:ARCH = "amd64" # arm not yet supported.
10
    if ($env:CGO_CFLAGS -contains "-g") {
11
        $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
        $script:config = "RelWithDebInfo"
13
    } else {
14
        $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
15
        $script:config = "Release"
16
    }
17
18
19
20
21
22
23
24
25
26
27
    # 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)
        }
    } else {
        $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
    }
    $script:BZIP2=(get-command -ea 'silentlycontinue' bzip2).path
    $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
28
29
30
31
32
}

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
33
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
34
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
35
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
36
37
38
}

function apply_patches {
39
    # Wire up our CMakefile
40
41
    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'
42
    }
43
    # Avoid duplicate main symbols when we link into the cgo binary
44
    $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
45
    $content = $content -replace 'int main\(', 'int __main('
46
    Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
47
48
49
}

function build {
50
    write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
51
    & cmake --version
52
    & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
53
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
54
55
    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
56
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
57
58
}

59
60
61
function install {
    rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
    md "${script:buildDir}/lib" -ea 0 > $null
62
    cp "${script:buildDir}/bin/${script:config}/ext_server.dll" "${script:buildDir}/lib"
63
    cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
64
65

    # Display the dll dependencies in the build log
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    if ($script:DUMPBIN -ne $null) {
        & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/${script:config}/ext_server.dll" | select-string ".dll"
    }
}

function compress_libs {
    if ($script:BZIP2 -eq $null) {
        write-host "bzip2 not installed, not compressing files"
        return
    }
    write-host "Compressing dlls..."
    $libs = dir "${script:buildDir}/lib/*.dll"
    foreach ($file in $libs) {
        & "$script:BZIP2" -v9 $file
    }
81
82
}

83
function cleanup {
84
    Set-Location "${script:llamacppDir}/examples/server"
85
    git checkout CMakeLists.txt server.cpp
86
87
88
89
90
}

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

92
93
94
95
96
97
98
99
# -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
100
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu"
101
102
103
write-host "Building LCD CPU"
build
install
104
compress_libs
105
106

$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
107
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx"
108
109
110
write-host "Building AVX CPU"
build
install
111
compress_libs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
112

113
$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
114
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx2"
115
write-host "Building AVX2 CPU"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
116
build
117
install
118
compress_libs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
if ($null -ne $script:CUDA_LIB_DIR) {
    # Then build cuda as a dynamically loaded library
    $nvcc = (get-command -ea 'silentlycontinue' nvcc)
    if ($null -ne $nvcc) {
        $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
    }
    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"
    $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on")
    build
    install
    cp "${script:CUDA_LIB_DIR}/cudart64_*.dll" "${script:buildDir}/lib"
    cp "${script:CUDA_LIB_DIR}/cublas64_*.dll" "${script:buildDir}/lib"
    cp "${script:CUDA_LIB_DIR}/cublasLt64_*.dll" "${script:buildDir}/lib"
    compress_libs
}
139
# TODO - actually implement ROCm support on windows
140
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/rocm"
141

142
143
144
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
145
146

cleanup
147
write-host "`ngo generate completed"