gen_windows.ps1 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #!powershell
  2. $ErrorActionPreference = "Stop"
  3. function amdGPUs {
  4. if ($env:AMDGPU_TARGETS) {
  5. return $env:AMDGPU_TARGETS
  6. }
  7. # Current supported rocblas list from ROCm v6.1.2 on windows
  8. $GPU_LIST = @(
  9. "gfx906:xnack-"
  10. "gfx1030"
  11. "gfx1100"
  12. "gfx1101"
  13. "gfx1102"
  14. )
  15. $GPU_LIST -join ';'
  16. }
  17. function init_vars {
  18. if (!$script:SRC_DIR) {
  19. $script:SRC_DIR = $(resolve-path "..\..\")
  20. }
  21. if (!$script:llamacppDir) {
  22. $script:llamacppDir = "../llama.cpp"
  23. }
  24. if (!$script:cmakeTargets) {
  25. $script:cmakeTargets = @("ollama_llama_server")
  26. }
  27. $script:cmakeDefs = @(
  28. "-DBUILD_SHARED_LIBS=on",
  29. "-DGGML_NATIVE=off",
  30. "-DGGML_OPENMP=off"
  31. )
  32. $script:commonCpuDefs = @("-DCMAKE_POSITION_INDEPENDENT_CODE=on")
  33. $script:ARCH = $Env:PROCESSOR_ARCHITECTURE.ToLower()
  34. $script:DIST_BASE = "${script:SRC_DIR}\dist\windows-${script:ARCH}\ollama_runners"
  35. md "$script:DIST_BASE" -ea 0 > $null
  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. if ($cmakeDefs -contains "-G") {
  108. $extra=@("-j8")
  109. } else {
  110. $extra= @("--", "/p:CL_MPcount=8")
  111. }
  112. write-host "building with: cmake --build $script:buildDir --config $script:config $($script:cmakeTargets | ForEach-Object { `"--target`", $_ }) $extra"
  113. & cmake --build $script:buildDir --config $script:config ($script:cmakeTargets | ForEach-Object { "--target", $_ }) $extra
  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. # -DGGML_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  160. # -DGGML_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  161. # -DGGML_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  162. function build_static() {
  163. if ((-not "${env:OLLAMA_SKIP_STATIC_GENERATE}") -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "static"))) {
  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. $oldTargets = $script:cmakeTargets
  173. $script:cmakeTargets = @("llama", "ggml")
  174. $script:cmakeDefs = @(
  175. "-G", "MinGW Makefiles"
  176. "-DCMAKE_C_COMPILER=gcc.exe",
  177. "-DCMAKE_CXX_COMPILER=g++.exe",
  178. "-DBUILD_SHARED_LIBS=off",
  179. "-DGGML_NATIVE=off",
  180. "-DGGML_AVX=off",
  181. "-DGGML_AVX2=off",
  182. "-DGGML_AVX512=off",
  183. "-DGGML_F16C=off",
  184. "-DGGML_FMA=off",
  185. "-DGGML_OPENMP=off")
  186. $script:buildDir="../build/windows/${script:ARCH}_static"
  187. write-host "Building static library"
  188. build
  189. $script:cmakeTargets = $oldTargets
  190. } else {
  191. write-host "Skipping CPU generation step as requested"
  192. }
  193. }
  194. function build_cpu($gen_arch) {
  195. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu"))) {
  196. # remaining llama.cpp builds use MSVC
  197. init_vars
  198. $script:cmakeDefs = $script:commonCpuDefs + @("-A", $gen_arch, "-DGGML_AVX=off", "-DGGML_AVX2=off", "-DGGML_AVX512=off", "-DGGML_FMA=off", "-DGGML_F16C=off") + $script:cmakeDefs
  199. $script:buildDir="../build/windows/${script:ARCH}/cpu"
  200. $script:distDir="$script:DIST_BASE\cpu"
  201. write-host "Building LCD CPU"
  202. build
  203. sign
  204. install
  205. } else {
  206. write-host "Skipping CPU generation step as requested"
  207. }
  208. }
  209. function build_cpu_avx() {
  210. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu_avx"))) {
  211. init_vars
  212. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DGGML_AVX=on", "-DGGML_AVX2=off", "-DGGML_AVX512=off", "-DGGML_FMA=off", "-DGGML_F16C=off") + $script:cmakeDefs
  213. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
  214. $script:distDir="$script:DIST_BASE\cpu_avx"
  215. write-host "Building AVX CPU"
  216. build
  217. sign
  218. install
  219. } else {
  220. write-host "Skipping CPU AVX generation step as requested"
  221. }
  222. }
  223. function build_cpu_avx2() {
  224. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu_avx2"))) {
  225. init_vars
  226. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DGGML_AVX=on", "-DGGML_AVX2=on", "-DGGML_AVX512=off", "-DGGML_FMA=on", "-DGGML_F16C=on") + $script:cmakeDefs
  227. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
  228. $script:distDir="$script:DIST_BASE\cpu_avx2"
  229. write-host "Building AVX2 CPU"
  230. build
  231. sign
  232. install
  233. } else {
  234. write-host "Skipping CPU AVX2 generation step as requested"
  235. }
  236. }
  237. function build_cuda() {
  238. if ((-not "${env:OLLAMA_SKIP_CUDA_GENERATE}") -and ("${script:CUDA_LIB_DIR}")) {
  239. # Then build cuda as a dynamically loaded library
  240. $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
  241. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  242. if ($null -ne $script:CUDA_VERSION) {
  243. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  244. }
  245. init_vars
  246. $script:buildDir="../build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  247. $script:distDir="$script:DIST_BASE\cuda$script:CUDA_VARIANT"
  248. $script:cmakeDefs += @(
  249. "-A", "x64",
  250. "-DGGML_CUDA=ON",
  251. "-DGGML_AVX=on",
  252. "-DGGML_AVX2=off",
  253. "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR",
  254. "-DCMAKE_CUDA_FLAGS=-t8",
  255. "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}"
  256. )
  257. if ($null -ne $env:OLLAMA_CUSTOM_CUDA_DEFS) {
  258. write-host "OLLAMA_CUSTOM_CUDA_DEFS=`"${env:OLLAMA_CUSTOM_CUDA_DEFS}`""
  259. $script:cmakeDefs +=@("${env:OLLAMA_CUSTOM_CUDA_DEFS}")
  260. write-host "building custom CUDA GPU"
  261. }
  262. build
  263. sign
  264. install
  265. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  266. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\" -ea 0 > $null
  267. write-host "copying CUDA dependencies to ${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  268. cp "${script:CUDA_LIB_DIR}\cudart64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  269. cp "${script:CUDA_LIB_DIR}\cublas64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  270. cp "${script:CUDA_LIB_DIR}\cublasLt64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  271. } else {
  272. write-host "Skipping CUDA generation step"
  273. }
  274. }
  275. function build_oneapi() {
  276. if ((-not "${env:OLLAMA_SKIP_ONEAPI_GENERATE}") -and ("${env:ONEAPI_ROOT}")) {
  277. # Get oneAPI version
  278. $script:ONEAPI_VERSION = icpx --version
  279. $script:ONEAPI_VERSION = [regex]::Match($script:ONEAPI_VERSION, '(?<=oneAPI DPC\+\+/C\+\+ Compiler )(?<version>\d+\.\d+\.\d+)').Value
  280. if ($null -ne $script:ONEAPI_VERSION) {
  281. $script:ONEAPI_VARIANT = "_v" + $script:ONEAPI_VERSION
  282. }
  283. init_vars
  284. $script:buildDir = "../build/windows/${script:ARCH}/oneapi$script:ONEAPI_VARIANT"
  285. $script:distDir ="$script:DIST_BASE\oneapi$script:ONEAPI_VARIANT"
  286. $script:cmakeDefs += @(
  287. "-G", "MinGW Makefiles",
  288. "-DGGML_SYCL=ON",
  289. "-DCMAKE_C_COMPILER=icx",
  290. "-DCMAKE_CXX_COMPILER=icx",
  291. "-DCMAKE_BUILD_TYPE=Release"
  292. )
  293. Write-Host "Building oneAPI"
  294. build
  295. # Ninja doesn't prefix with config name
  296. if ($null -ne $script:DUMPBIN) {
  297. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | Select-String ".dll"
  298. }
  299. sign
  300. install
  301. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  302. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\" -ea 0 > $null
  303. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\libirngmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  304. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\libmmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  305. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_level_zero.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  306. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_unified_runtime.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  307. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_win_proxy_loader.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  308. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\svml_dispmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  309. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\sycl7.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  310. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_core.2.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  311. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_sycl_blas.4.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  312. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_tbb_thread.2.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  313. } else {
  314. Write-Host "Skipping oneAPI generation step"
  315. }
  316. }
  317. function build_rocm() {
  318. if ((-not "${env:OLLAMA_SKIP_ROCM_GENERATE}") -and ("${env:HIP_PATH}")) {
  319. $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
  320. if ($null -ne $script:ROCM_VERSION) {
  321. $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
  322. }
  323. init_vars
  324. $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
  325. $script:distDir="$script:DIST_BASE\rocm$script:ROCM_VARIANT"
  326. $script:cmakeDefs += @(
  327. "-G", "Ninja",
  328. "-DCMAKE_C_COMPILER=clang.exe",
  329. "-DCMAKE_CXX_COMPILER=clang++.exe",
  330. "-DGGML_HIPBLAS=on",
  331. "-DLLAMA_CUDA_NO_PEER_COPY=on",
  332. "-DHIP_PLATFORM=amd",
  333. "-DGGML_AVX=on",
  334. "-DGGML_AVX2=off",
  335. "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
  336. "-DAMDGPU_TARGETS=$(amdGPUs)",
  337. "-DGPU_TARGETS=$(amdGPUs)"
  338. )
  339. # Make sure the ROCm binary dir is first in the path
  340. $env:PATH="$env:HIP_PATH\bin;$env:PATH"
  341. # We have to clobber the LIB var from the developer shell for clang to work properly
  342. $env:LIB=""
  343. if ($null -ne $env:OLLAMA_CUSTOM_ROCM_DEFS) {
  344. write-host "OLLAMA_CUSTOM_ROCM_DEFS=`"${env:OLLAMA_CUSTOM_ROCM_DEFS}`""
  345. $script:cmakeDefs += @("${env:OLLAMA_CUSTOM_ROCM_DEFS}")
  346. write-host "building custom ROCM GPU"
  347. }
  348. write-host "Building ROCm"
  349. build
  350. # Ninja doesn't prefix with config name
  351. ${script:config}=""
  352. if ($null -ne $script:DUMPBIN) {
  353. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
  354. }
  355. sign
  356. install
  357. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  358. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\" -ea 0 > $null
  359. cp "${env:HIP_PATH}\bin\hipblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  360. cp "${env:HIP_PATH}\bin\rocblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  361. # amdhip64.dll dependency comes from the driver and must be installed on the host to use AMD GPUs
  362. cp "${env:HIP_PATH}\bin\rocblas\library\*" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\"
  363. } else {
  364. write-host "Skipping ROCm generation step"
  365. }
  366. }
  367. init_vars
  368. if ($($args.count) -eq 0) {
  369. git_module_setup
  370. apply_patches
  371. build_static
  372. if ($script:ARCH -eq "arm64") {
  373. build_cpu("ARM64")
  374. } else { # amd64
  375. build_cpu("x64")
  376. build_cpu_avx
  377. build_cpu_avx2
  378. build_cuda
  379. build_oneapi
  380. build_rocm
  381. }
  382. cleanup
  383. write-host "`ngo generate completed. LLM runners: $(get-childitem -path $script:DIST_BASE)"
  384. } else {
  385. for ( $i = 0; $i -lt $args.count; $i++ ) {
  386. write-host "performing $($args[$i])"
  387. & $($args[$i])
  388. }
  389. }