gen_windows.ps1 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!powershell
  2. $ErrorActionPreference = "Stop"
  3. function amdGPUs {
  4. if ($env:AMDGPU_TARGETS) {
  5. return $env:AMDGPU_TARGETS
  6. }
  7. # TODO - load from some common data file for linux + windows build consistency
  8. $GPU_LIST = @(
  9. "gfx900"
  10. "gfx906:xnack-"
  11. "gfx908:xnack-"
  12. "gfx90a:xnack+"
  13. "gfx90a:xnack-"
  14. "gfx940"
  15. "gfx941"
  16. "gfx942"
  17. "gfx1010"
  18. "gfx1012"
  19. "gfx1030"
  20. "gfx1100"
  21. "gfx1101"
  22. "gfx1102"
  23. )
  24. $GPU_LIST -join ';'
  25. }
  26. function init_vars {
  27. $script:SRC_DIR = $(resolve-path "..\..\")
  28. $script:llamacppDir = "../llama.cpp"
  29. $script:cmakeDefs = @(
  30. "-DBUILD_SHARED_LIBS=on",
  31. "-DLLAMA_NATIVE=off"
  32. )
  33. $script:cmakeTargets = @("ollama_llama_server")
  34. $script:ARCH = "amd64" # arm not yet supported.
  35. if ($env:CGO_CFLAGS -contains "-g") {
  36. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on", "-DCMAKE_BUILD_TYPE=RelWithDebInfo")
  37. $script:config = "RelWithDebInfo"
  38. } else {
  39. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off", "-DCMAKE_BUILD_TYPE=Release")
  40. $script:config = "Release"
  41. }
  42. if ($null -ne $env:CMAKE_SYSTEM_VERSION) {
  43. $script:cmakeDefs += @("-DCMAKE_SYSTEM_VERSION=${env:CMAKE_SYSTEM_VERSION}")
  44. }
  45. # Try to find the CUDA dir
  46. if ($env:CUDA_LIB_DIR -eq $null) {
  47. $d=(get-command -ea 'silentlycontinue' nvcc).path
  48. if ($d -ne $null) {
  49. $script:CUDA_LIB_DIR=($d| split-path -parent)
  50. $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
  51. }
  52. } else {
  53. $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
  54. }
  55. $script:GZIP=(get-command -ea 'silentlycontinue' gzip).path
  56. $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
  57. if ($null -eq $env:CMAKE_CUDA_ARCHITECTURES) {
  58. $script:CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  59. } else {
  60. $script:CMAKE_CUDA_ARCHITECTURES=$env:CMAKE_CUDA_ARCHITECTURES
  61. }
  62. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  63. if ($null -eq $env:SIGN_TOOL) {
  64. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  65. } else {
  66. ${script:SignTool}=${env:SIGN_TOOL}
  67. }
  68. if ("${env:KEY_CONTAINER}") {
  69. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  70. }
  71. }
  72. function git_module_setup {
  73. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  74. & git submodule init
  75. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  76. & git submodule update --force "${script:llamacppDir}"
  77. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  78. }
  79. function apply_patches {
  80. # Wire up our CMakefile
  81. if (!(Select-String -Path "${script:llamacppDir}/CMakeLists.txt" -Pattern 'ollama')) {
  82. Add-Content -Path "${script:llamacppDir}/CMakeLists.txt" -Value 'add_subdirectory(../ext_server ext_server) # ollama'
  83. }
  84. # Apply temporary patches until fix is upstream
  85. $patches = Get-ChildItem "../patches/*.diff"
  86. foreach ($patch in $patches) {
  87. # Extract file paths from the patch file
  88. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  89. $parts = $_ -split ' '
  90. ($parts[1] -split '/', 2)[1]
  91. }
  92. # Checkout each file
  93. foreach ($file in $filePaths) {
  94. git -C "${script:llamacppDir}" checkout $file
  95. }
  96. }
  97. # Apply each patch
  98. foreach ($patch in $patches) {
  99. git -C "${script:llamacppDir}" apply $patch.FullName
  100. }
  101. }
  102. function build {
  103. write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
  104. & cmake --version
  105. & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
  106. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  107. write-host "building with: cmake --build $script:buildDir --config $script:config $($script:cmakeTargets | ForEach-Object { `"--target`", $_ })"
  108. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
  109. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  110. # Rearrange output to be consistent between different generators
  111. if ($null -ne ${script:config} -And (test-path -path "${script:buildDir}/bin/${script:config}" ) ) {
  112. mv -force "${script:buildDir}/bin/${script:config}/*" "${script:buildDir}/bin/"
  113. remove-item "${script:buildDir}/bin/${script:config}"
  114. }
  115. }
  116. function sign {
  117. if ("${env:KEY_CONTAINER}") {
  118. write-host "Signing ${script:buildDir}/bin/*.exe ${script:buildDir}/bin/*.dll"
  119. foreach ($file in @(get-childitem "${script:buildDir}/bin/*.exe") + @(get-childitem "${script:buildDir}/bin/*.dll")){
  120. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  121. /csp "Google Cloud KMS Provider" /kc "${env:KEY_CONTAINER}" $file
  122. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  123. }
  124. }
  125. }
  126. function compress {
  127. if ($script:GZIP -eq $null) {
  128. write-host "gzip not installed, not compressing files"
  129. return
  130. }
  131. write-host "Compressing binaries..."
  132. $binaries = dir "${script:buildDir}/bin/*.exe"
  133. foreach ($file in $binaries) {
  134. & "$script:GZIP" --best -f $file
  135. }
  136. write-host "Compressing dlls..."
  137. $dlls = dir "${script:buildDir}/bin/*.dll"
  138. foreach ($file in $dlls) {
  139. & "$script:GZIP" --best -f $file
  140. }
  141. }
  142. function cleanup {
  143. $patches = Get-ChildItem "../patches/*.diff"
  144. foreach ($patch in $patches) {
  145. # Extract file paths from the patch file
  146. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  147. $parts = $_ -split ' '
  148. ($parts[1] -split '/', 2)[1]
  149. }
  150. # Checkout each file
  151. foreach ($file in $filePaths) {
  152. git -C "${script:llamacppDir}" checkout $file
  153. }
  154. git -C "${script:llamacppDir}" checkout CMakeLists.txt
  155. }
  156. }
  157. init_vars
  158. git_module_setup
  159. apply_patches
  160. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  161. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  162. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  163. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on")
  164. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  165. # GCC build for direct linking into the Go binary
  166. init_vars
  167. # cmake will silently fallback to msvc compilers if mingw isn't in the path, so detect and fail fast
  168. # as we need this to be compiled by gcc for golang to be able to link with itx
  169. write-host "Checking for MinGW..."
  170. # error action ensures we exit on failure
  171. get-command gcc
  172. get-command mingw32-make
  173. $script:cmakeTargets = @("llama", "ggml")
  174. $script:cmakeDefs = @(
  175. "-G", "MinGW Makefiles"
  176. "-DCMAKE_C_COMPILER=gcc.exe",
  177. "-DCMAKE_CXX_COMPILER=g++.exe",
  178. "-DBUILD_SHARED_LIBS=off",
  179. "-DLLAMA_NATIVE=off",
  180. "-DLLAMA_AVX=off",
  181. "-DLLAMA_AVX2=off",
  182. "-DLLAMA_AVX512=off",
  183. "-DLLAMA_F16C=off",
  184. "-DLLAMA_FMA=off")
  185. $script:buildDir="../build/windows/${script:ARCH}_static"
  186. write-host "Building static library"
  187. build
  188. # remaining llama.cpp builds use MSVC
  189. init_vars
  190. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  191. $script:buildDir="../build/windows/${script:ARCH}/cpu"
  192. write-host "Building LCD CPU"
  193. build
  194. sign
  195. compress
  196. init_vars
  197. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  198. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
  199. write-host "Building AVX CPU"
  200. build
  201. sign
  202. compress
  203. init_vars
  204. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
  205. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
  206. write-host "Building AVX2 CPU"
  207. build
  208. sign
  209. compress
  210. } else {
  211. write-host "Skipping CPU generation step as requested"
  212. }
  213. if ($null -ne $script:CUDA_LIB_DIR) {
  214. # Then build cuda as a dynamically loaded library
  215. $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
  216. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  217. if ($null -ne $script:CUDA_VERSION) {
  218. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  219. }
  220. init_vars
  221. $script:buildDir="../build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  222. $script:cmakeDefs += @("-A", "x64", "-DLLAMA_CUDA=ON", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR", "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}")
  223. build
  224. sign
  225. compress
  226. }
  227. if ($null -ne $env:HIP_PATH) {
  228. $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
  229. if ($null -ne $script:ROCM_VERSION) {
  230. $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
  231. }
  232. init_vars
  233. $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
  234. $script:cmakeDefs += @(
  235. "-G", "Ninja",
  236. "-DCMAKE_C_COMPILER=clang.exe",
  237. "-DCMAKE_CXX_COMPILER=clang++.exe",
  238. "-DLLAMA_HIPBLAS=on",
  239. "-DHIP_PLATFORM=amd",
  240. "-DLLAMA_AVX=on",
  241. "-DLLAMA_AVX2=off",
  242. "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
  243. "-DAMDGPU_TARGETS=$(amdGPUs)",
  244. "-DGPU_TARGETS=$(amdGPUs)"
  245. )
  246. # Make sure the ROCm binary dir is first in the path
  247. $env:PATH="$env:HIP_PATH\bin;$env:PATH"
  248. # We have to clobber the LIB var from the developer shell for clang to work properly
  249. $env:LIB=""
  250. write-host "Building ROCm"
  251. build
  252. # Ninja doesn't prefix with config name
  253. ${script:config}=""
  254. if ($null -ne $script:DUMPBIN) {
  255. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
  256. }
  257. sign
  258. compress
  259. }
  260. cleanup
  261. write-host "`ncode generation completed. LLM runners: $(get-childitem -path ${script:SRC_DIR}\llm\build\windows\${script:ARCH})"