gen_windows.ps1 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!powershell
  2. $ErrorActionPreference = "Stop"
  3. function init_vars {
  4. $script:SRC_DIR = $(resolve-path "..\..\")
  5. $script:llamacppDir = "../llama.cpp"
  6. $script:cmakeDefs = @("-DBUILD_SHARED_LIBS=on", "-DLLAMA_NATIVE=off", "-A", "x64")
  7. $script:cmakeTargets = @("ext_server")
  8. $script:ARCH = "amd64" # arm not yet supported.
  9. if ($env:CGO_CFLAGS -contains "-g") {
  10. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on")
  11. $script:config = "RelWithDebInfo"
  12. } else {
  13. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off")
  14. $script:config = "Release"
  15. }
  16. # Try to find the CUDA dir
  17. if ($env:CUDA_LIB_DIR -eq $null) {
  18. $d=(get-command -ea 'silentlycontinue' nvcc).path
  19. if ($d -ne $null) {
  20. $script:CUDA_LIB_DIR=($d| split-path -parent)
  21. $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
  22. }
  23. } else {
  24. $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
  25. }
  26. $script:GZIP=(get-command -ea 'silentlycontinue' gzip).path
  27. $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
  28. if ($null -eq $env:CMAKE_CUDA_ARCHITECTURES) {
  29. $script:CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  30. } else {
  31. $script:CMAKE_CUDA_ARCHITECTURES=$env:CMAKE_CUDA_ARCHITECTURES
  32. }
  33. # Note: 10 Windows Kit signtool crashes with GCP's plugin
  34. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  35. if ("${env:KEY_CONTAINER}") {
  36. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  37. }
  38. }
  39. function git_module_setup {
  40. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  41. & git submodule init
  42. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  43. & git submodule update --force "${script:llamacppDir}"
  44. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  45. }
  46. function apply_patches {
  47. # Wire up our CMakefile
  48. if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
  49. Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama'
  50. }
  51. # Apply temporary patches until fix is upstream
  52. $patches = Get-ChildItem "../patches/*.diff"
  53. foreach ($patch in $patches) {
  54. # Extract file paths from the patch file
  55. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  56. $parts = $_ -split ' '
  57. ($parts[1] -split '/', 2)[1]
  58. }
  59. # Checkout each file
  60. Set-Location -Path ${script:llamacppDir}
  61. foreach ($file in $filePaths) {
  62. git checkout $file
  63. }
  64. }
  65. # Apply each patch
  66. foreach ($patch in $patches) {
  67. Set-Location -Path ${script:llamacppDir}
  68. git apply $patch.FullName
  69. }
  70. # Avoid duplicate main symbols when we link into the cgo binary
  71. $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
  72. $content = $content -replace 'int main\(', 'int __main('
  73. Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
  74. }
  75. function build {
  76. write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
  77. & cmake --version
  78. & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
  79. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  80. write-host "building with: cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })"
  81. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
  82. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  83. }
  84. function install {
  85. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  86. md "${script:buildDir}/lib" -ea 0 > $null
  87. cp "${script:buildDir}/bin/${script:config}/ext_server.dll" "${script:buildDir}/lib"
  88. cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
  89. # Display the dll dependencies in the build log
  90. if ($script:DUMPBIN -ne $null) {
  91. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/${script:config}/ext_server.dll" | select-string ".dll"
  92. }
  93. }
  94. function sign {
  95. if ("${env:KEY_CONTAINER}") {
  96. write-host "Signing ${script:buildDir}/lib/*.dll"
  97. foreach ($file in (get-childitem "${script:buildDir}/lib/*.dll")){
  98. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  99. /csp "Google Cloud KMS Provider" /kc "${env:KEY_CONTAINER}" $file
  100. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  101. }
  102. }
  103. }
  104. function compress_libs {
  105. if ($script:GZIP -eq $null) {
  106. write-host "gzip not installed, not compressing files"
  107. return
  108. }
  109. write-host "Compressing dlls..."
  110. $libs = dir "${script:buildDir}/lib/*.dll"
  111. foreach ($file in $libs) {
  112. & "$script:GZIP" --best -f $file
  113. }
  114. }
  115. function cleanup {
  116. $patches = Get-ChildItem "../patches/*.diff"
  117. foreach ($patch in $patches) {
  118. # Extract file paths from the patch file
  119. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  120. $parts = $_ -split ' '
  121. ($parts[1] -split '/', 2)[1]
  122. }
  123. # Checkout each file
  124. Set-Location -Path ${script:llamacppDir}
  125. foreach ($file in $filePaths) {
  126. git checkout $file
  127. }
  128. }
  129. Set-Location "${script:llamacppDir}/examples/server"
  130. git checkout CMakeLists.txt server.cpp
  131. }
  132. init_vars
  133. git_module_setup
  134. apply_patches
  135. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  136. # -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  137. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  138. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  139. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on", "-DLLAMA_NATIVE=off")
  140. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  141. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu"
  142. write-host "Building LCD CPU"
  143. build
  144. install
  145. sign
  146. compress_libs
  147. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  148. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx"
  149. write-host "Building AVX CPU"
  150. build
  151. install
  152. sign
  153. compress_libs
  154. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
  155. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx2"
  156. write-host "Building AVX2 CPU"
  157. build
  158. install
  159. sign
  160. compress_libs
  161. if ($null -ne $script:CUDA_LIB_DIR) {
  162. # Then build cuda as a dynamically loaded library
  163. $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
  164. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  165. if ($null -ne $script:CUDA_VERSION) {
  166. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  167. }
  168. init_vars
  169. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  170. $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
  171. build
  172. install
  173. sign
  174. compress_libs
  175. }
  176. # TODO - actually implement ROCm support on windows
  177. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/rocm"
  178. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  179. md "${script:buildDir}/lib" -ea 0 > $null
  180. echo $null >> "${script:buildDir}/lib/.generated"
  181. cleanup
  182. write-host "`ngo generate completed"