gen_windows.ps1 7.7 KB

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