gpu.go 10 KB

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