gen_windows.ps1 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. # https://rocm.docs.amd.com/projects/install-on-windows/en/latest/reference/system-requirements.html#windows-supported-gpus
  9. $GPU_LIST = @(
  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_cpu($gen_arch) {
  163. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu"))) {
  164. # remaining llama.cpp builds use MSVC
  165. init_vars
  166. $script:cmakeDefs = $script:commonCpuDefs + @("-A", $gen_arch, "-DGGML_AVX=off", "-DGGML_AVX2=off", "-DGGML_AVX512=off", "-DGGML_FMA=off", "-DGGML_F16C=off") + $script:cmakeDefs
  167. $script:buildDir="../build/windows/${script:ARCH}/cpu"
  168. $script:distDir="$script:DIST_BASE\cpu"
  169. write-host "Building LCD CPU"
  170. build
  171. sign
  172. install
  173. } else {
  174. write-host "Skipping CPU generation step as requested"
  175. }
  176. }
  177. function build_cpu_avx() {
  178. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu_avx"))) {
  179. init_vars
  180. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DGGML_AVX=on", "-DGGML_AVX2=off", "-DGGML_AVX512=off", "-DGGML_FMA=off", "-DGGML_F16C=off") + $script:cmakeDefs
  181. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx"
  182. $script:distDir="$script:DIST_BASE\cpu_avx"
  183. write-host "Building AVX CPU"
  184. build
  185. sign
  186. install
  187. } else {
  188. write-host "Skipping CPU AVX generation step as requested"
  189. }
  190. }
  191. function build_cpu_avx2() {
  192. if ((-not "${env:OLLAMA_SKIP_CPU_GENERATE}" ) -and ((-not "${env:OLLAMA_CPU_TARGET}") -or ("${env:OLLAMA_CPU_TARGET}" -eq "cpu_avx2"))) {
  193. init_vars
  194. $script:cmakeDefs = $script:commonCpuDefs + @("-A", "x64", "-DGGML_AVX=on", "-DGGML_AVX2=on", "-DGGML_AVX512=off", "-DGGML_FMA=on", "-DGGML_F16C=on") + $script:cmakeDefs
  195. $script:buildDir="../build/windows/${script:ARCH}/cpu_avx2"
  196. $script:distDir="$script:DIST_BASE\cpu_avx2"
  197. write-host "Building AVX2 CPU"
  198. build
  199. sign
  200. install
  201. } else {
  202. write-host "Skipping CPU AVX2 generation step as requested"
  203. }
  204. }
  205. function build_cuda() {
  206. if ((-not "${env:OLLAMA_SKIP_CUDA_GENERATE}") -and ("${script:CUDA_LIB_DIR}")) {
  207. # Then build cuda as a dynamically loaded library
  208. $nvcc = "$script:CUDA_LIB_DIR\nvcc.exe"
  209. $script:CUDA_VERSION=(get-item ($nvcc | split-path | split-path)).Basename
  210. if ($null -ne $script:CUDA_VERSION) {
  211. $script:CUDA_VARIANT="_"+$script:CUDA_VERSION
  212. }
  213. init_vars
  214. $script:buildDir="../build/windows/${script:ARCH}/cuda$script:CUDA_VARIANT"
  215. $script:distDir="$script:DIST_BASE\cuda$script:CUDA_VARIANT"
  216. $script:cmakeDefs += @(
  217. "-A", "x64",
  218. "-DGGML_CUDA=ON",
  219. "-DGGML_AVX=on",
  220. "-DGGML_AVX2=off",
  221. "-DCUDAToolkit_INCLUDE_DIR=$script:CUDA_INCLUDE_DIR",
  222. "-DCMAKE_CUDA_FLAGS=-t8",
  223. "-DCMAKE_CUDA_ARCHITECTURES=${script:CMAKE_CUDA_ARCHITECTURES}"
  224. )
  225. if ($null -ne $env:OLLAMA_CUSTOM_CUDA_DEFS) {
  226. write-host "OLLAMA_CUSTOM_CUDA_DEFS=`"${env:OLLAMA_CUSTOM_CUDA_DEFS}`""
  227. $script:cmakeDefs +=@("${env:OLLAMA_CUSTOM_CUDA_DEFS}")
  228. write-host "building custom CUDA GPU"
  229. }
  230. build
  231. sign
  232. install
  233. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  234. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\" -ea 0 > $null
  235. write-host "copying CUDA dependencies to ${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  236. cp "${script:CUDA_LIB_DIR}\cudart64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  237. cp "${script:CUDA_LIB_DIR}\cublas64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  238. cp "${script:CUDA_LIB_DIR}\cublasLt64_*.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\cuda\"
  239. } else {
  240. write-host "Skipping CUDA generation step"
  241. }
  242. }
  243. function build_oneapi() {
  244. if ((-not "${env:OLLAMA_SKIP_ONEAPI_GENERATE}") -and ("${env:ONEAPI_ROOT}")) {
  245. # Get oneAPI version
  246. $script:ONEAPI_VERSION = icpx --version
  247. $script:ONEAPI_VERSION = [regex]::Match($script:ONEAPI_VERSION, '(?<=oneAPI DPC\+\+/C\+\+ Compiler )(?<version>\d+\.\d+\.\d+)').Value
  248. if ($null -ne $script:ONEAPI_VERSION) {
  249. $script:ONEAPI_VARIANT = "_v" + $script:ONEAPI_VERSION
  250. }
  251. init_vars
  252. $script:buildDir = "../build/windows/${script:ARCH}/oneapi$script:ONEAPI_VARIANT"
  253. $script:distDir ="$script:DIST_BASE\oneapi$script:ONEAPI_VARIANT"
  254. $script:cmakeDefs += @(
  255. "-G", "MinGW Makefiles",
  256. "-DGGML_SYCL=ON",
  257. "-DCMAKE_C_COMPILER=icx",
  258. "-DCMAKE_CXX_COMPILER=icx",
  259. "-DCMAKE_BUILD_TYPE=Release"
  260. )
  261. Write-Host "Building oneAPI"
  262. build
  263. # Ninja doesn't prefix with config name
  264. if ($null -ne $script:DUMPBIN) {
  265. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | Select-String ".dll"
  266. }
  267. sign
  268. install
  269. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  270. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\" -ea 0 > $null
  271. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\libirngmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  272. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\libmmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  273. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_level_zero.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  274. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_unified_runtime.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  275. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\pi_win_proxy_loader.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  276. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\svml_dispmd.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  277. cp "${env:ONEAPI_ROOT}\compiler\latest\bin\sycl7.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  278. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_core.2.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  279. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_sycl_blas.4.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  280. cp "${env:ONEAPI_ROOT}\mkl\latest\bin\mkl_tbb_thread.2.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\oneapi\"
  281. } else {
  282. Write-Host "Skipping oneAPI generation step"
  283. }
  284. }
  285. function build_rocm() {
  286. if ((-not "${env:OLLAMA_SKIP_ROCM_GENERATE}") -and ("${env:HIP_PATH}")) {
  287. $script:ROCM_VERSION=(get-item $env:HIP_PATH).Basename
  288. if ($null -ne $script:ROCM_VERSION) {
  289. $script:ROCM_VARIANT="_v"+$script:ROCM_VERSION
  290. }
  291. init_vars
  292. $script:buildDir="../build/windows/${script:ARCH}/rocm$script:ROCM_VARIANT"
  293. $script:distDir="$script:DIST_BASE\rocm$script:ROCM_VARIANT"
  294. $script:cmakeDefs += @(
  295. "-G", "Ninja",
  296. "-DCMAKE_C_COMPILER=clang.exe",
  297. "-DCMAKE_CXX_COMPILER=clang++.exe",
  298. "-DGGML_HIPBLAS=on",
  299. "-DLLAMA_CUDA_NO_PEER_COPY=on",
  300. "-DHIP_PLATFORM=amd",
  301. "-DGGML_AVX=on",
  302. "-DGGML_AVX2=off",
  303. "-DCMAKE_POSITION_INDEPENDENT_CODE=on",
  304. "-DAMDGPU_TARGETS=$(amdGPUs)",
  305. "-DGPU_TARGETS=$(amdGPUs)"
  306. )
  307. # Make sure the ROCm binary dir is first in the path
  308. $env:PATH="$env:HIP_PATH\bin;$env:PATH"
  309. # We have to clobber the LIB var from the developer shell for clang to work properly
  310. $env:LIB=""
  311. if ($null -ne $env:OLLAMA_CUSTOM_ROCM_DEFS) {
  312. write-host "OLLAMA_CUSTOM_ROCM_DEFS=`"${env:OLLAMA_CUSTOM_ROCM_DEFS}`""
  313. $script:cmakeDefs += @("${env:OLLAMA_CUSTOM_ROCM_DEFS}")
  314. write-host "building custom ROCM GPU"
  315. }
  316. write-host "Building ROCm"
  317. build
  318. # Ninja doesn't prefix with config name
  319. ${script:config}=""
  320. if ($null -ne $script:DUMPBIN) {
  321. & "$script:DUMPBIN" /dependents "${script:buildDir}/bin/ollama_llama_server.exe" | select-string ".dll"
  322. }
  323. sign
  324. install
  325. rm -ea 0 -recurse -force -path "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  326. md "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\" -ea 0 > $null
  327. cp "${env:HIP_PATH}\bin\hipblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  328. cp "${env:HIP_PATH}\bin\rocblas.dll" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\"
  329. # amdhip64.dll dependency comes from the driver and must be installed on the host to use AMD GPUs
  330. cp "${env:HIP_PATH}\bin\rocblas\library\*" "${script:SRC_DIR}\dist\windows-${script:ARCH}\rocm\rocblas\library\"
  331. } else {
  332. write-host "Skipping ROCm generation step"
  333. }
  334. }
  335. init_vars
  336. if ($($args.count) -eq 0) {
  337. git_module_setup
  338. apply_patches
  339. if ($script:ARCH -eq "arm64") {
  340. build_cpu("ARM64")
  341. } else { # amd64
  342. build_cpu("x64")
  343. build_cpu_avx
  344. build_cpu_avx2
  345. build_cuda
  346. build_oneapi
  347. build_rocm
  348. }
  349. cleanup
  350. write-host "`ngo generate completed. LLM runners: $(get-childitem -path $script:DIST_BASE)"
  351. } else {
  352. for ( $i = 0; $i -lt $args.count; $i++ ) {
  353. write-host "performing $($args[$i])"
  354. & $($args[$i])
  355. }
  356. }