gen_windows.ps1 4.42 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
    $script:cmakeTargets = @("ggml", "ggml_static", "llama", "build_info", "common", "ext_server_shared", "llava_static")
9
    if ($env:CGO_CFLAGS -contains "-g") {
10
        $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
11
        $script:config = "RelWithDebInfo"
12
    } else {
13
        $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
14
        $script:config = "Release"
15
16
17
18
19
20
    }
}

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
21
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
22
    & git submodule update --force "${script:llamacppDir}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
23
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
24
25
26
}

function apply_patches {
27
    # Wire up our CMakefile
28
29
    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'
30
    }
31
    # Avoid duplicate main symbols when we link into the cgo binary
32
    $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
33
    $content = $content -replace 'int main\(', 'int __main('
34
    Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
35
36
37
}

function build {
38
    write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
39
    & cmake --version
40
    & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
Daniel Hiltgen's avatar
Daniel Hiltgen committed
41
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
42
43
    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
44
    if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
45
46
}

47
48
49
50
51
function install {
    rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
    md "${script:buildDir}/lib" -ea 0 > $null
    cp "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" "${script:buildDir}/lib"
    cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
52
53
54

    # Display the dll dependencies in the build log
    dumpbin /dependents "${script:buildDir}/bin/${script:config}/ext_server_shared.dll" | select-string ".dll"
55
56
}

57
function cleanup {
58
    Set-Location "${script:llamacppDir}/examples/server"
59
    git checkout CMakeLists.txt server.cpp
60
61
62
63
64
}

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

66
67
68
69
70
71
72
73
# -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
74
$script:buildDir="${script:llamacppDir}/build/windows/cpu"
75
76
77
78
79
80
81
82
83
write-host "Building LCD CPU"
build
install

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

85
86
87
$script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
$script:buildDir="${script:llamacppDir}/build/windows/cpu_avx2"
write-host "Building AVX2 CPU"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
88
build
89
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
90
91

# Then build cuda as a dynamically loaded library
92
# TODO figure out how to detect cuda version
Daniel Hiltgen's avatar
Daniel Hiltgen committed
93
init_vars
94
$script:buildDir="${script:llamacppDir}/build/windows/cuda"
95
$script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on")
96
build
97
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
98

99
# TODO - actually implement ROCm support on windows
100
$script:buildDir="${script:llamacppDir}/build/windows/rocm"
101

102
103
104
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
105
106

cleanup
107
write-host "`ngo generate completed"