gen_windows.ps1 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  21. } else {
  22. $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
  23. }
  24. $script:GZIP=(get-command -ea 'silentlycontinue' gzip).path
  25. $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
  26. if ($null -eq $env:CMAKE_CUDA_ARCHITECTURES) {
  27. $script:CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  28. } else {
  29. $script:CMAKE_CUDA_ARCHITECTURES=$env:CMAKE_CUDA_ARCHITECTURES
  30. }
  31. }
  32. function git_module_setup {
  33. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  34. & git submodule init
  35. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  36. & git submodule update --force "${script:llamacppDir}"
  37. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  38. }
  39. function apply_patches {
  40. # Wire up our CMakefile
  41. if (!(Select-String -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Pattern 'ollama')) {
  42. Add-Content -Path "${script:llamacppDir}/examples/server/CMakeLists.txt" -Value 'include (../../../ext_server/CMakeLists.txt) # ollama'
  43. }
  44. # Apply temporary patches until fix is upstream
  45. $patches = Get-ChildItem "../patches/*.diff"
  46. foreach ($patch in $patches) {
  47. # Extract file paths from the patch file
  48. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  49. $parts = $_ -split ' '
  50. ($parts[1] -split '/', 2)[1]
  51. }
  52. # Checkout each file
  53. foreach ($file in $filePaths) {
  54. Set-Location -Path ${script:llamacppDir}
  55. git checkout $file
  56. }
  57. }
  58. # Apply each patch
  59. foreach ($patch in $patches) {
  60. Set-Location -Path ${script:llamacppDir}
  61. git apply $patch.FullName
  62. }
  63. # Avoid duplicate main symbols when we link into the cgo binary
  64. $content = Get-Content -Path "${script:llamacppDir}/examples/server/server.cpp"
  65. $content = $content -replace 'int main\(', 'int __main('
  66. Set-Content -Path "${script:llamacppDir}/examples/server/server.cpp" -Value $content
  67. }
  68. function build {
  69. write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
  70. & cmake --version
  71. & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
  72. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  73. write-host "building with: cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })"
  74. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
  75. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  76. }
  77. function install {
  78. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  79. md "${script:buildDir}/lib" -ea 0 > $null
  80. cp "${script:buildDir}/bin/${script:config}/ext_server.dll" "${script:buildDir}/lib"
  81. cp "${script:buildDir}/bin/${script:config}/llama.dll" "${script:buildDir}/lib"
  82. # Display the dll dependencies in the build log
  83. if ($script:DUMPBIN -ne $null) {
  84. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/${script:config}/ext_server.dll" | select-string ".dll"
  85. }
  86. }
  87. function compress_libs {
  88. if ($script:GZIP -eq $null) {
  89. write-host "gzip not installed, not compressing files"
  90. return
  91. }
  92. write-host "Compressing dlls..."
  93. $libs = dir "${script:buildDir}/lib/*.dll"
  94. foreach ($file in $libs) {
  95. & "$script:GZIP" --best -f $file
  96. }
  97. }
  98. function cleanup {
  99. Set-Location "${script:llamacppDir}/examples/server"
  100. git checkout CMakeLists.txt server.cpp
  101. }
  102. init_vars
  103. git_module_setup
  104. apply_patches
  105. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  106. # -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  107. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  108. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  109. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on", "-DLLAMA_NATIVE=off")
  110. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  111. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu"
  112. write-host "Building LCD CPU"
  113. build
  114. install
  115. compress_libs
  116. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  117. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx"
  118. write-host "Building AVX CPU"
  119. build
  120. install
  121. compress_libs
  122. $script:cmakeDefs = $script:commonCpuDefs + @("-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
  123. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cpu_avx2"
  124. write-host "Building AVX2 CPU"
  125. build
  126. install
  127. compress_libs
  128. if ($null -ne $script:CUDA_LIB_DIR) {
  129. # Then build cuda as a dynamically loaded library
  130. $nvcc = (get-command -ea 'silentlycontinue' nvcc)
  131. if ($null -ne $nvcc) {
  132. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  133. }
  134. if ($null -ne $script:CUDA_VERSION) {
  135. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  136. }
  137. init_vars
  138. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  139. $script:cmakeDefs += @("-DLLAMA_CUBLAS=ON", "-DLLAMA_AVX=on", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
  140. build
  141. install
  142. cp "${script:CUDA_LIB_DIR}/cudart64_*.dll" "${script:buildDir}/lib"
  143. cp "${script:CUDA_LIB_DIR}/cublas64_*.dll" "${script:buildDir}/lib"
  144. cp "${script:CUDA_LIB_DIR}/cublasLt64_*.dll" "${script:buildDir}/lib"
  145. compress_libs
  146. }
  147. # TODO - actually implement ROCm support on windows
  148. $script:buildDir="${script:llamacppDir}/build/windows/${script:ARCH}/rocm"
  149. rm -ea 0 -recurse -force -path "${script:buildDir}/lib"
  150. md "${script:buildDir}/lib" -ea 0 > $null
  151. echo $null >> "${script:buildDir}/lib/.generated"
  152. cleanup
  153. write-host "`ngo generate completed"