gen_windows.ps1 6.06 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
    # 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
    }
26
    $script:GZIP=(get-command -ea 'silentlycontinue' gzip).path
27
    $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
28
29
30
31
32
    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
    }
33
34
35
36
37
}

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
38
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
39
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
40
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
41
42
43
}

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

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

64
65
66
function install {
    rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
    md "${script:buildDir}/lib" -ea 0 > $null
67
    cp "${script:buildDir}/bin/${script:config}/ext_server.dll" "${script:buildDir}/lib"
68
    cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
69
70

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

function compress_libs {
77
78
    if ($script:GZIP -eq $null) {
        write-host "gzip not installed, not compressing files"
79
80
81
82
83
        return
    }
    write-host "Compressing dlls..."
    $libs = dir "${script:buildDir}/lib/*.dll"
    foreach ($file in $libs) {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
84
        & "$script:GZIP" --best -f $file
85
    }
86
87
}

88
function cleanup {
89
    Set-Location "${script:llamacppDir}/examples/server"
90
    git checkout CMakeLists.txt server.cpp
91
92
93
94
95
}

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

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

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

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

125
126
127
128
129
130
131
132
133
134
135
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"
136
    $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
137
138
139
140
141
142
143
    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
}
144
# TODO - actually implement ROCm support on windows
145
$script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/rocm"
146

147
148
149
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
150
151

cleanup
152
write-host "`ngo generate completed"