gen_windows.ps1 6.5 KB

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