gen_windows.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. $script:DIST_BASE = "${script:SRC_DIR}\dist\windows-${script:ARCH}\ollama_runners"
  36. if ($env:CGO_CFLAGS -contains "-g") {
  37. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on", "-DCMAKE_BUILD_TYPE=RelWithDebInfo")
  38. $script:config = "RelWithDebInfo"
  39. } else {
  40. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off", "-DCMAKE_BUILD_TYPE=Release")
  41. $script:config = "Release"
  42. }
  43. if ($null -ne $env:CMAKE_SYSTEM_VERSION) {
  44. $script:cmakeDefs += @("-DCMAKE_SYSTEM_VERSION=${env:CMAKE_SYSTEM_VERSION}")
  45. }
  46. # Try to find the CUDA dir
  47. if ($env:CUDA_LIB_DIR -eq $null) {
  48. $d=(get-command -ea 'silentlycontinue' nvcc).path
  49. if ($d -ne $null) {
  50. $script:CUDA_LIB_DIR=($d| split-path -parent)
  51. $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
  52. }
  53. } else {
  54. $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
  55. }
  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 install {
  127. write-host "Installing binaries to dist dir ${script:distDir}"
  128. mkdir ${script:distDir} -ErrorAction SilentlyContinue
  129. $binaries = dir "${script:buildDir}/bin/*.exe"
  130. foreach ($file in $binaries) {
  131. copy-item -Path $file -Destination ${script:distDir} -Force
  132. }
  133. write-host "Installing dlls to dist dir ${script:distDir}"
  134. $dlls = dir "${script:buildDir}/bin/*.dll"
  135. foreach ($file in $dlls) {
  136. copy-item -Path $file -Destination ${script:distDir} -Force
  137. }
  138. }
  139. function cleanup {
  140. $patches = Get-ChildItem "../patches/*.diff"
  141. foreach ($patch in $patches) {
  142. # Extract file paths from the patch file
  143. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  144. $parts = $_ -split ' '
  145. ($parts[1] -split '/', 2)[1]
  146. }
  147. # Checkout each file
  148. foreach ($file in $filePaths) {
  149. git -C "${script:llamacppDir}" checkout $file
  150. }
  151. git -C "${script:llamacppDir}" checkout CMakeLists.txt
  152. }
  153. }
  154. init_vars
  155. git_module_setup
  156. apply_patches
  157. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  158. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  159. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  160. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on")
  161. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  162. # GCC build for direct linking into the Go binary
  163. init_vars
  164. # cmake will silently fallback to msvc compilers if mingw isn't in the path, so detect and fail fast
  165. # as we need this to be compiled by gcc for golang to be able to link with itx
  166. write-host "Checking for MinGW..."
  167. # error action ensures we exit on failure
  168. get-command gcc
  169. get-command mingw32-make
  170. $script:cmakeTargets = @("llama", "ggml")
  171. $script:cmakeDefs = @(
  172. "-G", "MinGW Makefiles"
  173. "-DCMAKE_C_COMPILER=gcc.exe",
  174. "-DCMAKE_CXX_COMPILER=g++.exe",
  175. "-DBUILD_SHARED_LIBS=off",
  176. "-DLLAMA_NATIVE=off",
  177. "-DLLAMA_AVX=off",
  178. "-DLLAMA_AVX2=off",
  179. "-DLLAMA_AVX512=off",
  180. "-DLLAMA_F16C=off",
  181. "-DLLAMA_FMA=off")
  182. $script:buildDir="../build/windows/${script:ARCH}_static"
  183. write-host "Building static library"
  184. build
  185. # remaining llama.cpp builds use MSVC
  186. init_vars
  187. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  188. $script:buildDir="../build/windows/${script:ARCH}/cpu"
  189. $script:distDir="$script:DIST_BASE\cpu"
  190. write-host "Building LCD CPU"
  191. build
  192. sign
  193. install
  194. init_vars
  195. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  196. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
  197. $script:distDir="$script:DIST_BASE\cpu_avx"
  198. write-host "Building AVX CPU"
  199. build
  200. sign
  201. install
  202. init_vars
  203. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
  204. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
  205. $script:distDir="$script:DIST_BASE\cpu_avx2"
  206. write-host "Building AVX2 CPU"
  207. build
  208. sign
  209. install
  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:distDir="$script:DIST_BASE\cuda$script:CUDA_VARIANT"
  223. $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}")
  224. if ($null -ne $env:OLLAMA_CUSTOM_CUDA_DEFS) {
  225. write-host "OLLAMA_CUSTOM_CUDA_DEFS=`"${env:OLLAMA_CUSTOM_CUDA_DEFS}`""
  226. $script:cmakeDefs +=@("${env:OLLAMA_CUSTOM_CUDA_DEFS}")
  227. write-host "building custom CUDA GPU"
  228. }
  229. build
  230. sign
  231. install
  232. }
  233. if ($null -ne $env:HIP_PATH) {
  234. $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
  235. if ($null -ne $script:ROCM_VERSION) {
  236. $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
  237. }
  238. init_vars
  239. $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
  240. $script:distDir="$script:DIST_BASE\rocm$script:ROCM_VARIANT"
  241. $script:cmakeDefs += @(
  242. "-G", "Ninja",
  243. "-DCMAKE_C_COMPILER=clang.exe",
  244. "-DCMAKE_CXX_COMPILER=clang++.exe",
  245. "-DLLAMA_HIPBLAS=on",
  246. "-DHIP_PLATFORM=amd",
  247. "-DLLAMA_AVX=on",
  248. "-DLLAMA_AVX2=off",
  249. "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
  250. "-DAMDGPU_TARGETS=$(amdGPUs)",
  251. "-DGPU_TARGETS=$(amdGPUs)"
  252. )
  253. # Make sure the ROCm binary dir is first in the path
  254. $env:PATH="$env:HIP_PATH\bin;$env:PATH"
  255. # We have to clobber the LIB var from the developer shell for clang to work properly
  256. $env:LIB=""
  257. if ($null -ne $env:OLLAMA_CUSTOM_ROCM_DEFS) {
  258. write-host "OLLAMA_CUSTOM_ROCM_DEFS=`"${env:OLLAMA_CUSTOM_ROCM_DEFS}`""
  259. $script:cmakeDefs += @("${env:OLLAMA_CUSTOM_ROCM_DEFS}")
  260. write-host "building custom ROCM GPU"
  261. }
  262. write-host "Building ROCm"
  263. build
  264. # Ninja doesn't prefix with config name
  265. ${script:config}=""
  266. if ($null -ne $script:DUMPBIN) {
  267. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
  268. }
  269. sign
  270. install
  271. }
  272. cleanup
  273. write-host "`ngo generate completed. LLM runners: $(get-childitem -path $script:DIST_BASE)"