gen_windows.ps1 14 KB

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