gen_windows.ps1 16 KB

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