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

$ErrorActionPreference = "Stop"

function init_vars {
    $script:patches = @("0001-Expose-callable-API-for-server.patch")
Daniel Hiltgen's avatar
Daniel Hiltgen committed
7
    $script:cmakeDefs = @("-DBUILD_SHARED_LIBS=on", "-DLLAMA_NATIVE=off", "-DLLAMA_F16C=off", "-DLLAMA_FMA=off", "-DLLAMA_AVX512=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX=on", "-DLLAMA_K_QUANTS=on", "-DLLAMA_ACCELERATE=on", "-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 gguf
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
Daniel Hiltgen's avatar
Daniel Hiltgen committed
28
29
    if (!(Select-String -Path "gguf/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
        Add-Content -Path "gguf/examples/server/CMakeLists.txt" -Value 'include (../../../CMakeLists.txt) # ollama'
30
    }
31
32
33
34
    # Avoid duplicate main symbols when we link into the cgo binary
    $content = Get-Content -Path "./gguf/examples/server/server.cpp"
    $content = $content -replace 'int main\(', 'int __main('
    Set-Content -Path "./gguf/examples/server/server.cpp" -Value $content
35
36
37
}

function build {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
38
    write-host "generating config with: cmake -S gguf -B $script:buildDir $script:cmakeDefs"
39
    & cmake --version
Daniel Hiltgen's avatar
Daniel Hiltgen committed
40
41
    & cmake -S gguf -B $script:buildDir $script:cmakeDefs
    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
52
53
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"
}

54
55
56
function cleanup {
    Set-Location "gguf/examples/server"
    git checkout CMakeLists.txt server.cpp
57
58
59
60
61
}

init_vars
git_module_setup
apply_patches
Daniel Hiltgen's avatar
Daniel Hiltgen committed
62
63

# first build CPU based
64
$script:buildDir="gguf/build/windows/cpu"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
65
66

build
67
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
68
69
70

# Then build cuda as a dynamically loaded library
init_vars
71
72
$script:buildDir="gguf/build/windows/cuda"
$script:cmakeDefs += @("-DLLAMA_CUBLAS=ON")
73
build
74
install
Daniel Hiltgen's avatar
Daniel Hiltgen committed
75

76
77
# TODO - actually implement ROCm support on windows
$script:buildDir="gguf/build/windows/rocm"
78

79
80
81
rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
md "${script:buildDir}/lib" -ea 0 > $null
echo $null >> "${script:buildDir}/lib/.generated"
82
83

cleanup
84
write-host "`ngo generate completed"