gen_windows.ps1 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. if (!$script:SRC_DIR) {
  28. $script:SRC_DIR = $(resolve-path "..\..\")
  29. }
  30. if (!$script:llamacppDir) {
  31. $script:llamacppDir = "../llama.cpp"
  32. }
  33. $script:cmakeTargets = @("ollama_llama_server")
  34. $script:cmakeDefs = @(
  35. "-DBUILD_SHARED_LIBS=on",
  36. "-DLLAMA_NATIVE=off"
  37. )
  38. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on")
  39. $script:ARCH = "amd64" # arm not yet supported.
  40. $script:DIST_BASE = "${script:SRC_DIR}\dist\windows-${script:ARCH}\ollama_runners"
  41. if ($env:CGO_CFLAGS -contains "-g") {
  42. $script:cmakeDefs += @("-DCMAKE_VERBOSE_MAKEFILE=on", "-DLLAMA_SERVER_VERBOSE=on", "-DCMAKE_BUILD_TYPE=RelWithDebInfo")
  43. $script:config = "RelWithDebInfo"
  44. } else {
  45. $script:cmakeDefs += @("-DLLAMA_SERVER_VERBOSE=off", "-DCMAKE_BUILD_TYPE=Release")
  46. $script:config = "Release"
  47. }
  48. if ($null -ne $env:CMAKE_SYSTEM_VERSION) {
  49. $script:cmakeDefs += @("-DCMAKE_SYSTEM_VERSION=${env:CMAKE_SYSTEM_VERSION}")
  50. }
  51. # Try to find the CUDA dir
  52. if ($env:CUDA_LIB_DIR -eq $null) {
  53. $d=(get-command -ea 'silentlycontinue' nvcc).path
  54. if ($d -ne $null) {
  55. $script:CUDA_LIB_DIR=($d| split-path -parent)
  56. $script:CUDA_INCLUDE_DIR=($script:CUDA_LIB_DIR|split-path -parent)+"\include"
  57. }
  58. } else {
  59. $script:CUDA_LIB_DIR=$env:CUDA_LIB_DIR
  60. }
  61. $script:DUMPBIN=(get-command -ea 'silentlycontinue' dumpbin).path
  62. if ($null -eq $env:CMAKE_CUDA_ARCHITECTURES) {
  63. $script:CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  64. } else {
  65. $script:CMAKE_CUDA_ARCHITECTURES=$env:CMAKE_CUDA_ARCHITECTURES
  66. }
  67. # Note: Windows Kits 10 signtool crashes with GCP's plugin
  68. if ($null -eq $env:SIGN_TOOL) {
  69. ${script:SignTool}="C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe"
  70. } else {
  71. ${script:SignTool}=${env:SIGN_TOOL}
  72. }
  73. if ("${env:KEY_CONTAINER}") {
  74. ${script:OLLAMA_CERT}=$(resolve-path "${script:SRC_DIR}\ollama_inc.crt")
  75. }
  76. }
  77. function git_module_setup {
  78. # TODO add flags to skip the init/patch logic to make it easier to mod llama.cpp code in-repo
  79. & git submodule init
  80. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  81. & git submodule update --force "${script:llamacppDir}"
  82. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  83. }
  84. function apply_patches {
  85. # Wire up our CMakefile
  86. if (!(Select-String -Path "${script:llamacppDir}/CMakeLists.txt" -Pattern 'ollama')) {
  87. Add-Content -Path "${script:llamacppDir}/CMakeLists.txt" -Value 'add_subdirectory(../ext_server ext_server) # ollama'
  88. }
  89. # Apply temporary patches until fix is upstream
  90. $patches = Get-ChildItem "../patches/*.diff"
  91. foreach ($patch in $patches) {
  92. # Extract file paths from the patch file
  93. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  94. $parts = $_ -split ' '
  95. ($parts[1] -split '/', 2)[1]
  96. }
  97. # Checkout each file
  98. foreach ($file in $filePaths) {
  99. git -C "${script:llamacppDir}" checkout $file
  100. }
  101. }
  102. # Apply each patch
  103. foreach ($patch in $patches) {
  104. git -C "${script:llamacppDir}" apply $patch.FullName
  105. }
  106. }
  107. function build {
  108. write-host "generating config with: cmake -S ${script:llamacppDir} -B $script:buildDir $script:cmakeDefs"
  109. & cmake --version
  110. & cmake -S "${script:llamacppDir}" -B $script:buildDir $script:cmakeDefs
  111. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  112. write-host "building with: cmake --build $script:buildDir --config $script:config $($script:cmakeTargets | ForEach-Object { `"--target`", $_ })"
  113. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ })
  114. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  115. # Rearrange output to be consistent between different generators
  116. if ($null -ne ${script:config} -And (test-path -path "${script:buildDir}/bin/${script:config}" ) ) {
  117. mv -force "${script:buildDir}/bin/${script:config}/*" "${script:buildDir}/bin/"
  118. remove-item "${script:buildDir}/bin/${script:config}"
  119. }
  120. }
  121. function sign {
  122. if ("${env:KEY_CONTAINER}") {
  123. write-host "Signing ${script:buildDir}/bin/*.exe ${script:buildDir}/bin/*.dll"
  124. foreach ($file in @(get-childitem "${script:buildDir}/bin/*.exe") + @(get-childitem "${script:buildDir}/bin/*.dll")){
  125. & "${script:SignTool}" sign /v /fd sha256 /t http://timestamp.digicert.com /f "${script:OLLAMA_CERT}" `
  126. /csp "Google Cloud KMS Provider" /kc "${env:KEY_CONTAINER}" $file
  127. if ($LASTEXITCODE -ne 0) { exit($LASTEXITCODE)}
  128. }
  129. }
  130. }
  131. function install {
  132. write-host "Installing binaries to dist dir ${script:distDir}"
  133. mkdir ${script:distDir} -ErrorAction SilentlyContinue
  134. $binaries = dir "${script:buildDir}/bin/*.exe"
  135. foreach ($file in $binaries) {
  136. copy-item -Path $file -Destination ${script:distDir} -Force
  137. }
  138. write-host "Installing dlls to dist dir ${script:distDir}"
  139. $dlls = dir "${script:buildDir}/bin/*.dll"
  140. foreach ($file in $dlls) {
  141. copy-item -Path $file -Destination ${script:distDir} -Force
  142. }
  143. }
  144. function cleanup {
  145. $patches = Get-ChildItem "../patches/*.diff"
  146. foreach ($patch in $patches) {
  147. # Extract file paths from the patch file
  148. $filePaths = Get-Content $patch.FullName | Where-Object { $_ -match '^\+\+\+ ' } | ForEach-Object {
  149. $parts = $_ -split ' '
  150. ($parts[1] -split '/', 2)[1]
  151. }
  152. # Checkout each file
  153. foreach ($file in $filePaths) {
  154. git -C "${script:llamacppDir}" checkout $file
  155. }
  156. git -C "${script:llamacppDir}" checkout CMakeLists.txt
  157. }
  158. }
  159. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  160. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  161. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  162. function build_static() {
  163. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  164. # GCC build for direct linking into the Go binary
  165. init_vars
  166. # cmake will silently fallback to msvc compilers if mingw isn't in the path, so detect and fail fast
  167. # as we need this to be compiled by gcc for golang to be able to link with itx
  168. write-host "Checking for MinGW..."
  169. # error action ensures we exit on failure
  170. get-command gcc
  171. get-command mingw32-make
  172. $script:cmakeTargets = @("llama", "ggml")
  173. $script:cmakeDefs = @(
  174. "-G", "MinGW Makefiles"
  175. "-DCMAKE_C_COMPILER=gcc.exe",
  176. "-DCMAKE_CXX_COMPILER=g++.exe",
  177. "-DBUILD_SHARED_LIBS=off",
  178. "-DLLAMA_NATIVE=off",
  179. "-DLLAMA_AVX=off",
  180. "-DLLAMA_AVX2=off",
  181. "-DLLAMA_AVX512=off",
  182. "-DLLAMA_F16C=off",
  183. "-DLLAMA_FMA=off")
  184. $script:buildDir="../build/windows/${script:ARCH}_static"
  185. write-host "Building static library"
  186. build
  187. } else {
  188. write-host "Skipping CPU generation step as requested"
  189. }
  190. }
  191. function build_cpu() {
  192. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  193. # remaining llama.cpp builds use MSVC
  194. init_vars
  195. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=off", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  196. $script:buildDir="../build/windows/${script:ARCH}/cpu"
  197. $script:distDir="$script:DIST_BASE\cpu"
  198. write-host "Building LCD CPU"
  199. build
  200. sign
  201. install
  202. } else {
  203. write-host "Skipping CPU generation step as requested"
  204. }
  205. }
  206. function build_cpu_avx() {
  207. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  208. init_vars
  209. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=off", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=off", "-DLLAMA_F16C=off") + $script:cmakeDefs
  210. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
  211. $script:distDir="$script:DIST_BASE\cpu_avx"
  212. write-host "Building AVX CPU"
  213. build
  214. sign
  215. install
  216. } else {
  217. write-host "Skipping CPU generation step as requested"
  218. }
  219. }
  220. function build_cpu_avx2() {
  221. if ($null -eq ${env:OLLAMA_SKIP_CPU_GENERATE}) {
  222. init_vars
  223. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DLLAMA_AVX=on", "-DLLAMA_AVX2=on", "-DLLAMA_AVX512=off", "-DLLAMA_FMA=on", "-DLLAMA_F16C=on") + $script:cmakeDefs
  224. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
  225. $script:distDir="$script:DIST_BASE\cpu_avx2"
  226. write-host "Building AVX2 CPU"
  227. build
  228. sign
  229. install
  230. } else {
  231. write-host "Skipping CPU generation step as requested"
  232. }
  233. }
  234. function build_cuda() {
  235. if ($null -ne $script:CUDA_LIB_DIR) {
  236. # Then build cuda as a dynamically loaded library
  237. $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
  238. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  239. if ($null -ne $script:CUDA_VERSION) {
  240. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  241. }
  242. init_vars
  243. $script:buildDir="../build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  244. $script:distDir="$script:DIST_BASE\cuda$script:CUDA_VARIANT"
  245. $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}")
  246. if ($null -ne $env:OLLAMA_CUSTOM_CUDA_DEFS) {
  247. write-host "OLLAMA_CUSTOM_CUDA_DEFS=`"${env:OLLAMA_CUSTOM_CUDA_DEFS}`""
  248. $script:cmakeDefs +=@("${env:OLLAMA_CUSTOM_CUDA_DEFS}")
  249. write-host "building custom CUDA GPU"
  250. }
  251. build
  252. sign
  253. install
  254. write-host "copying CUDA dependencies to ${script:SRC_DIR}\dist\windows-${script:ARCH}\"
  255. cp "${script:CUDA_LIB_DIR}\cudart64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\"
  256. cp "${script:CUDA_LIB_DIR}\cublas64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\"
  257. cp "${script:CUDA_LIB_DIR}\cublasLt64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\"
  258. }
  259. }
  260. function build_rocm() {
  261. if ($null -ne $env:HIP_PATH) {
  262. $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
  263. if ($null -ne $script:ROCM_VERSION) {
  264. $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
  265. }
  266. init_vars
  267. $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
  268. $script:distDir="$script:DIST_BASE\rocm$script:ROCM_VARIANT"
  269. $script:cmakeDefs += @(
  270. "-G", "Ninja",
  271. "-DCMAKE_C_COMPILER=clang.exe",
  272. "-DCMAKE_CXX_COMPILER=clang++.exe",
  273. "-DLLAMA_HIPBLAS=on",
  274. "-DHIP_PLATFORM=amd",
  275. "-DLLAMA_AVX=on",
  276. "-DLLAMA_AVX2=off",
  277. "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
  278. "-DAMDGPU_TARGETS=$(amdGPUs)",
  279. "-DGPU_TARGETS=$(amdGPUs)"
  280. )
  281. # Make sure the ROCm binary dir is first in the path
  282. $env:PATH="$env:HIP_PATH\bin;$env:PATH"
  283. # We have to clobber the LIB var from the developer shell for clang to work properly
  284. $env:LIB=""
  285. if ($null -ne $env:OLLAMA_CUSTOM_ROCM_DEFS) {
  286. write-host "OLLAMA_CUSTOM_ROCM_DEFS=`"${env:OLLAMA_CUSTOM_ROCM_DEFS}`""
  287. $script:cmakeDefs += @("${env:OLLAMA_CUSTOM_ROCM_DEFS}")
  288. write-host "building custom ROCM GPU"
  289. }
  290. write-host "Building ROCm"
  291. build
  292. # Ninja doesn't prefix with config name
  293. ${script:config}=""
  294. if ($null -ne $script:DUMPBIN) {
  295. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
  296. }
  297. sign
  298. install
  299. # Assumes v5.7, may need adjustments for v6
  300. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  301. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\" -ea 0 > $null
  302. cp "${env:HIP_PATH}\bin\hipblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  303. cp "${env:HIP_PATH}\bin\rocblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  304. # amdhip64.dll dependency comes from the driver and must be installed on the host to use AMD GPUs
  305. cp "${env:HIP_PATH}\bin\rocblas\library\*" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\"
  306. }
  307. }
  308. init_vars
  309. if ($($args.count) -eq 0) {
  310. git_module_setup
  311. apply_patches
  312. build_static
  313. build_cpu
  314. build_cpu_avx
  315. build_cpu_avx2
  316. build_cuda
  317. build_rocm
  318. cleanup
  319. write-host "`ngo generate completed. LLM runners: $(get-childitem -path $script:DIST_BASE)"
  320. } else {
  321. for ( $i = 0; $i -lt $args.count; $i++ ) {
  322. write-host "performing $($args[$i])"
  323. & $($args[$i])
  324. }
  325. }