gen_windows.ps1 14 KB

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