gen_windows.ps1 16 KB

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