gen_windows.ps1 5.8 KB

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