gpu.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //go:build linux || windows
  2. package gpu
  3. /*
  4. #cgo linux LDFLAGS: -lrt -lpthread -ldl -lstdc++ -lm
  5. #cgo windows LDFLAGS: -lpthread
  6. #include "gpu_info.h"
  7. */
  8. import "C"
  9. import (
  10. "fmt"
  11. "log/slog"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "unsafe"
  19. "github.com/ollama/ollama/format"
  20. "github.com/ollama/ollama/envconfig"
  21. )
  22. type handles struct {
  23. deviceCount int
  24. cudart *C.cudart_handle_t
  25. nvcuda *C.nvcuda_handle_t
  26. oneapi *C.oneapi_handle_t
  27. }
  28. const (
  29. cudaMinimumMemory = 457 * format.MebiByte
  30. rocmMinimumMemory = 457 * format.MebiByte
  31. )
  32. var gpuMutex sync.Mutex
  33. // With our current CUDA compile flags, older than 5.0 will not work properly
  34. var CudaComputeMin = [2]C.int{5, 0}
  35. var RocmComputeMin = 9
  36. // TODO find a better way to detect iGPU instead of minimum memory
  37. const IGPUMemLimit = 1 * format.GibiByte // 512G is what they typically report, so anything less than 1G must be iGPU
  38. var CudartLinuxGlobs = []string{
  39. "/usr/local/cuda/lib64/libcudart.so*",
  40. "/usr/lib/x86_64-linux-gnu/nvidia/current/libcudart.so*",
  41. "/usr/lib/x86_64-linux-gnu/libcudart.so*",
  42. "/usr/lib/wsl/lib/libcudart.so*",
  43. "/usr/lib/wsl/drivers/*/libcudart.so*",
  44. "/opt/cuda/lib64/libcudart.so*",
  45. "/usr/local/cuda*/targets/aarch64-linux/lib/libcudart.so*",
  46. "/usr/lib/aarch64-linux-gnu/nvidia/current/libcudart.so*",
  47. "/usr/lib/aarch64-linux-gnu/libcudart.so*",
  48. "/usr/local/cuda/lib*/libcudart.so*",
  49. "/usr/lib*/libcudart.so*",
  50. "/usr/local/lib*/libcudart.so*",
  51. }
  52. var CudartWindowsGlobs = []string{
  53. "c:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v*\\bin\\cudart64_*.dll",
  54. }
  55. var NvcudaLinuxGlobs = []string{
  56. "/usr/local/cuda*/targets/*/lib/libcuda.so*",
  57. "/usr/lib/*-linux-gnu/nvidia/current/libcuda.so*",
  58. "/usr/lib/*-linux-gnu/libcuda.so*",
  59. "/usr/lib/wsl/lib/libcuda.so*",
  60. "/usr/lib/wsl/drivers/*/libcuda.so*",
  61. "/opt/cuda/lib*/libcuda.so*",
  62. "/usr/local/cuda/lib*/libcuda.so*",
  63. "/usr/lib*/libcuda.so*",
  64. "/usr/local/lib*/libcuda.so*",
  65. }
  66. var NvcudaWindowsGlobs = []string{
  67. "c:\\windows\\system*\\nvcuda.dll",
  68. }
  69. var OneapiWindowsGlobs = []string{
  70. "c:\\Windows\\System32\\DriverStore\\FileRepository\\*\\ze_intel_gpu64.dll",
  71. }
  72. var OneapiLinuxGlobs = []string{
  73. "/usr/lib/x86_64-linux-gnu/libze_intel_gpu.so*",
  74. "/usr/lib*/libze_intel_gpu.so*",
  75. }
  76. // Jetson devices have JETSON_JETPACK="x.y.z" factory set to the Jetpack version installed.
  77. // Included to drive logic for reducing Ollama-allocated overhead on L4T/Jetson devices.
  78. var CudaTegra string = os.Getenv("JETSON_JETPACK")
  79. // Note: gpuMutex must already be held
  80. func initGPUHandles() *handles {
  81. // TODO - if the ollama build is CPU only, don't do these checks as they're irrelevant and confusing
  82. gpuHandles := &handles{}
  83. var cudartMgmtName string
  84. var cudartMgmtPatterns []string
  85. var nvcudaMgmtName string
  86. var nvcudaMgmtPatterns []string
  87. var oneapiMgmtName string
  88. var oneapiMgmtPatterns []string
  89. tmpDir, _ := PayloadsDir()
  90. switch runtime.GOOS {
  91. case "windows":
  92. cudartMgmtName = "cudart64_*.dll"
  93. localAppData := os.Getenv("LOCALAPPDATA")
  94. cudartMgmtPatterns = []string{filepath.Join(localAppData, "Programs", "Ollama", cudartMgmtName)}
  95. cudartMgmtPatterns = append(cudartMgmtPatterns, CudartWindowsGlobs...)
  96. // Aligned with driver, we can't carry as payloads
  97. nvcudaMgmtName = "nvcuda.dll"
  98. nvcudaMgmtPatterns = NvcudaWindowsGlobs
  99. oneapiMgmtName = "ze_intel_gpu64.dll"
  100. oneapiMgmtPatterns = OneapiWindowsGlobs
  101. case "linux":
  102. cudartMgmtName = "libcudart.so*"
  103. if tmpDir != "" {
  104. // TODO - add "payloads" for subprocess
  105. cudartMgmtPatterns = []string{filepath.Join(tmpDir, "cuda*", cudartMgmtName)}
  106. }
  107. cudartMgmtPatterns = append(cudartMgmtPatterns, CudartLinuxGlobs...)
  108. // Aligned with driver, we can't carry as payloads
  109. nvcudaMgmtName = "libcuda.so*"
  110. nvcudaMgmtPatterns = NvcudaLinuxGlobs
  111. oneapiMgmtName = "libze_intel_gpu.so"
  112. oneapiMgmtPatterns = OneapiLinuxGlobs
  113. default:
  114. return gpuHandles
  115. }
  116. slog.Debug("Detecting GPUs")
  117. nvcudaLibPaths := FindGPULibs(nvcudaMgmtName, nvcudaMgmtPatterns)
  118. if len(nvcudaLibPaths) > 0 {
  119. deviceCount, nvcuda, libPath := LoadNVCUDAMgmt(nvcudaLibPaths)
  120. if nvcuda != nil {
  121. slog.Debug("detected GPUs", "count", deviceCount, "library", libPath)
  122. gpuHandles.nvcuda = nvcuda
  123. gpuHandles.deviceCount = deviceCount
  124. return gpuHandles
  125. }
  126. }
  127. cudartLibPaths := FindGPULibs(cudartMgmtName, cudartMgmtPatterns)
  128. if len(cudartLibPaths) > 0 {
  129. deviceCount, cudart, libPath := LoadCUDARTMgmt(cudartLibPaths)
  130. if cudart != nil {
  131. slog.Debug("detected GPUs", "library", libPath, "count", deviceCount)
  132. gpuHandles.cudart = cudart
  133. gpuHandles.deviceCount = deviceCount
  134. return gpuHandles
  135. }
  136. }
  137. oneapiLibPaths := FindGPULibs(oneapiMgmtName, oneapiMgmtPatterns)
  138. if len(oneapiLibPaths) > 0 {
  139. deviceCount, oneapi, libPath := LoadOneapiMgmt(oneapiLibPaths)
  140. if oneapi != nil {
  141. slog.Debug("detected Intel GPUs", "library", libPath, "count", deviceCount)
  142. gpuHandles.oneapi = oneapi
  143. gpuHandles.deviceCount = deviceCount
  144. return gpuHandles
  145. }
  146. }
  147. return gpuHandles
  148. }
  149. func GetGPUInfo() GpuInfoList {
  150. // TODO - consider exploring lspci (and equivalent on windows) to check for
  151. // GPUs so we can report warnings if we see Nvidia/AMD but fail to load the libraries
  152. gpuMutex.Lock()
  153. defer gpuMutex.Unlock()
  154. gpuHandles := initGPUHandles()
  155. defer func() {
  156. if gpuHandles.cudart != nil {
  157. C.cudart_release(*gpuHandles.cudart)
  158. }
  159. if gpuHandles.nvcuda != nil {
  160. C.nvcuda_release(*gpuHandles.nvcuda)
  161. }
  162. }()
  163. // All our GPU builds on x86 have AVX enabled, so fallback to CPU if we don't detect at least AVX
  164. cpuVariant := GetCPUVariant()
  165. if cpuVariant == "" && runtime.GOARCH == "amd64" {
  166. slog.Warn("CPU does not have AVX or AVX2, disabling GPU support.")
  167. }
  168. // On windows we bundle the nvidia library one level above the runner dir
  169. depPath := ""
  170. if runtime.GOOS == "windows" && envconfig.RunnersDir != "" {
  171. depPath = filepath.Dir(envconfig.RunnersDir)
  172. }
  173. var memInfo C.mem_info_t
  174. resp := []GpuInfo{}
  175. // NVIDIA first
  176. for i := 0; i < gpuHandles.deviceCount; i++ {
  177. // TODO once we support CPU compilation variants of GPU libraries refine this...
  178. if cpuVariant == "" && runtime.GOARCH == "amd64" {
  179. continue
  180. }
  181. if gpuHandles.cudart != nil || gpuHandles.nvcuda != nil {
  182. gpuInfo := GpuInfo{
  183. Library: "cuda",
  184. }
  185. var driverMajor int
  186. var driverMinor int
  187. if gpuHandles.cudart != nil {
  188. C.cudart_check_vram(*gpuHandles.cudart, C.int(i), &memInfo)
  189. } else {
  190. C.nvcuda_check_vram(*gpuHandles.nvcuda, C.int(i), &memInfo)
  191. driverMajor = int(gpuHandles.nvcuda.driver_major)
  192. driverMinor = int(gpuHandles.nvcuda.driver_minor)
  193. }
  194. if memInfo.err != nil {
  195. slog.Info("error looking up nvidia GPU memory", "error", C.GoString(memInfo.err))
  196. C.free(unsafe.Pointer(memInfo.err))
  197. continue
  198. }
  199. if memInfo.major < CudaComputeMin[0] || (memInfo.major == CudaComputeMin[0] && memInfo.minor < CudaComputeMin[1]) {
  200. slog.Info(fmt.Sprintf("[%d] CUDA GPU is too old. Compute Capability detected: %d.%d", i, memInfo.major, memInfo.minor))
  201. continue
  202. }
  203. gpuInfo.TotalMemory = uint64(memInfo.total)
  204. gpuInfo.FreeMemory = uint64(memInfo.free)
  205. gpuInfo.ID = C.GoString(&memInfo.gpu_id[0])
  206. gpuInfo.Compute = fmt.Sprintf("%d.%d", memInfo.major, memInfo.minor)
  207. gpuInfo.MinimumMemory = cudaMinimumMemory
  208. gpuInfo.DependencyPath = depPath
  209. gpuInfo.Name = C.GoString(&memInfo.gpu_name[0])
  210. gpuInfo.DriverMajor = int(driverMajor)
  211. gpuInfo.DriverMinor = int(driverMinor)
  212. // TODO potentially sort on our own algorithm instead of what the underlying GPU library does...
  213. resp = append(resp, gpuInfo)
  214. }
  215. if gpuHandles.oneapi != nil {
  216. gpuInfo := GpuInfo{
  217. Library: "oneapi",
  218. }
  219. C.oneapi_check_vram(*gpuHandles.oneapi, &memInfo)
  220. var totalFreeMem float64 = float64(memInfo.free) * 0.95 // work-around: leave some reserve vram for mkl lib used in ggml-sycl backend.
  221. memInfo.free = C.uint64_t(totalFreeMem)
  222. gpuInfo.TotalMemory = uint64(memInfo.total)
  223. gpuInfo.FreeMemory = uint64(memInfo.free)
  224. gpuInfo.ID = strconv.Itoa(i)
  225. resp = append(resp, gpuInfo)
  226. }
  227. }
  228. // Then AMD
  229. resp = append(resp, AMDGetGPUInfo()...)
  230. if len(resp) == 0 {
  231. C.cpu_check_ram(&memInfo)
  232. if memInfo.err != nil {
  233. slog.Info("error looking up CPU memory", "error", C.GoString(memInfo.err))
  234. C.free(unsafe.Pointer(memInfo.err))
  235. return resp
  236. }
  237. gpuInfo := GpuInfo{
  238. Library: "cpu",
  239. Variant: cpuVariant,
  240. }
  241. gpuInfo.TotalMemory = uint64(memInfo.total)
  242. gpuInfo.FreeMemory = uint64(memInfo.free)
  243. gpuInfo.ID = C.GoString(&memInfo.gpu_id[0])
  244. resp = append(resp, gpuInfo)
  245. }
  246. return resp
  247. }
  248. func GetCPUMem() (memInfo, error) {
  249. var ret memInfo
  250. var info C.mem_info_t
  251. C.cpu_check_ram(&info)
  252. if info.err != nil {
  253. defer C.free(unsafe.Pointer(info.err))
  254. return ret, fmt.Errorf(C.GoString(info.err))
  255. }
  256. ret.FreeMemory = uint64(info.free)
  257. ret.TotalMemory = uint64(info.total)
  258. return ret, nil
  259. }
  260. func FindGPULibs(baseLibName string, defaultPatterns []string) []string {
  261. // Multiple GPU libraries may exist, and some may not work, so keep trying until we exhaust them
  262. var ldPaths []string
  263. var patterns []string
  264. gpuLibPaths := []string{}
  265. slog.Debug("Searching for GPU library", "name", baseLibName)
  266. switch runtime.GOOS {
  267. case "windows":
  268. ldPaths = strings.Split(os.Getenv("PATH"), ";")
  269. case "linux":
  270. ldPaths = strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":")
  271. default:
  272. return gpuLibPaths
  273. }
  274. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  275. for _, ldPath := range ldPaths {
  276. d, err := filepath.Abs(ldPath)
  277. if err != nil {
  278. continue
  279. }
  280. patterns = append(patterns, filepath.Join(d, baseLibName+"*"))
  281. }
  282. patterns = append(patterns, defaultPatterns...)
  283. slog.Debug("gpu library search", "globs", patterns)
  284. for _, pattern := range patterns {
  285. // Nvidia PhysX known to return bogus results
  286. if strings.Contains(pattern, "PhysX") {
  287. slog.Debug("skipping PhysX cuda library path", "path", pattern)
  288. }
  289. // Ignore glob discovery errors
  290. matches, _ := filepath.Glob(pattern)
  291. for _, match := range matches {
  292. // Resolve any links so we don't try the same lib multiple times
  293. // and weed out any dups across globs
  294. libPath := match
  295. tmp := match
  296. var err error
  297. for ; err == nil; tmp, err = os.Readlink(libPath) {
  298. if !filepath.IsAbs(tmp) {
  299. tmp = filepath.Join(filepath.Dir(libPath), tmp)
  300. }
  301. libPath = tmp
  302. }
  303. new := true
  304. for _, cmp := range gpuLibPaths {
  305. if cmp == libPath {
  306. new = false
  307. break
  308. }
  309. }
  310. if new {
  311. gpuLibPaths = append(gpuLibPaths, libPath)
  312. }
  313. }
  314. }
  315. slog.Debug("discovered GPU libraries", "paths", gpuLibPaths)
  316. return gpuLibPaths
  317. }
  318. func LoadCUDARTMgmt(cudartLibPaths []string) (int, *C.cudart_handle_t, string) {
  319. var resp C.cudart_init_resp_t
  320. resp.ch.verbose = getVerboseState()
  321. for _, libPath := range cudartLibPaths {
  322. lib := C.CString(libPath)
  323. defer C.free(unsafe.Pointer(lib))
  324. C.cudart_init(lib, &resp)
  325. if resp.err != nil {
  326. slog.Debug("Unable to load cudart", "library", libPath, "error", C.GoString(resp.err))
  327. C.free(unsafe.Pointer(resp.err))
  328. } else {
  329. return int(resp.num_devices), &resp.ch, libPath
  330. }
  331. }
  332. return 0, nil, ""
  333. }
  334. func LoadNVCUDAMgmt(nvcudaLibPaths []string) (int, *C.nvcuda_handle_t, string) {
  335. var resp C.nvcuda_init_resp_t
  336. resp.ch.verbose = getVerboseState()
  337. for _, libPath := range nvcudaLibPaths {
  338. lib := C.CString(libPath)
  339. defer C.free(unsafe.Pointer(lib))
  340. C.nvcuda_init(lib, &resp)
  341. if resp.err != nil {
  342. slog.Debug("Unable to load nvcuda", "library", libPath, "error", C.GoString(resp.err))
  343. C.free(unsafe.Pointer(resp.err))
  344. } else {
  345. return int(resp.num_devices), &resp.ch, libPath
  346. }
  347. }
  348. return 0, nil, ""
  349. }
  350. func LoadOneapiMgmt(oneapiLibPaths []string) (int, *C.oneapi_handle_t, string) {
  351. var resp C.oneapi_init_resp_t
  352. resp.oh.verbose = getVerboseState()
  353. for _, libPath := range oneapiLibPaths {
  354. lib := C.CString(libPath)
  355. defer C.free(unsafe.Pointer(lib))
  356. C.oneapi_init(lib, &resp)
  357. if resp.err != nil {
  358. slog.Debug("Unable to load oneAPI management library", "library", libPath, "error", C.GoString(resp.err))
  359. C.free(unsafe.Pointer(resp.err))
  360. } else {
  361. return int(resp.num_devices), &resp.oh, libPath
  362. }
  363. }
  364. return 0, nil, ""
  365. }
  366. func getVerboseState() C.uint16_t {
  367. if envconfig.Debug {
  368. return C.uint16_t(1)
  369. }
  370. return C.uint16_t(0)
  371. }
  372. // Given the list of GPUs this instantiation is targeted for,
  373. // figure out the visible devices environment variable
  374. //
  375. // If different libraries are detected, the first one is what we use
  376. func (l GpuInfoList) GetVisibleDevicesEnv() (string, string) {
  377. if len(l) == 0 {
  378. return "", ""
  379. }
  380. switch l[0].Library {
  381. case "cuda":
  382. return cudaGetVisibleDevicesEnv(l)
  383. case "rocm":
  384. return rocmGetVisibleDevicesEnv(l)
  385. case "oneapi":
  386. return oneapiGetVisibleDevicesEnv(l)
  387. default:
  388. slog.Debug("no filter required for library " + l[0].Library)
  389. return "", ""
  390. }
  391. }