gpu.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. )
  20. type handles struct {
  21. nvml *C.nvml_handle_t
  22. cudart *C.cudart_handle_t
  23. }
  24. var gpuMutex sync.Mutex
  25. var gpuHandles *handles = nil
  26. // With our current CUDA compile flags, older than 5.0 will not work properly
  27. var CudaComputeMin = [2]C.int{5, 0}
  28. // Possible locations for the nvidia-ml library
  29. var NvmlLinuxGlobs = []string{
  30. "/usr/local/cuda/lib64/libnvidia-ml.so*",
  31. "/usr/lib/x86_64-linux-gnu/nvidia/current/libnvidia-ml.so*",
  32. "/usr/lib/x86_64-linux-gnu/libnvidia-ml.so*",
  33. "/usr/lib/wsl/lib/libnvidia-ml.so*",
  34. "/usr/lib/wsl/drivers/*/libnvidia-ml.so*",
  35. "/opt/cuda/lib64/libnvidia-ml.so*",
  36. "/usr/lib*/libnvidia-ml.so*",
  37. "/usr/lib/aarch64-linux-gnu/nvidia/current/libnvidia-ml.so*",
  38. "/usr/lib/aarch64-linux-gnu/libnvidia-ml.so*",
  39. "/usr/local/lib*/libnvidia-ml.so*",
  40. // TODO: are these stubs ever valid?
  41. "/opt/cuda/targets/x86_64-linux/lib/stubs/libnvidia-ml.so*",
  42. }
  43. var NvmlWindowsGlobs = []string{
  44. "c:\\Windows\\System32\\nvml.dll",
  45. }
  46. var CudartLinuxGlobs = []string{
  47. "/usr/local/cuda/lib64/libcudart.so*",
  48. "/usr/lib/x86_64-linux-gnu/nvidia/current/libcudart.so*",
  49. "/usr/lib/x86_64-linux-gnu/libcudart.so*",
  50. "/usr/lib/wsl/lib/libcudart.so*",
  51. "/usr/lib/wsl/drivers/*/libcudart.so*",
  52. "/opt/cuda/lib64/libcudart.so*",
  53. "/usr/local/cuda*/targets/aarch64-linux/lib/libcudart.so*",
  54. "/usr/lib/aarch64-linux-gnu/nvidia/current/libcudart.so*",
  55. "/usr/lib/aarch64-linux-gnu/libcudart.so*",
  56. "/usr/local/cuda/lib*/libcudart.so*",
  57. "/usr/lib*/libcudart.so*",
  58. "/usr/local/lib*/libcudart.so*",
  59. }
  60. var CudartWindowsGlobs = []string{
  61. "c:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v*\\bin\\cudart64_*.dll",
  62. }
  63. // Jetson devices have JETSON_JETPACK="x.y.z" factory set to the Jetpack version installed.
  64. // Included to drive logic for reducing Ollama-allocated overhead on L4T/Jetson devices.
  65. var CudaTegra string = os.Getenv("JETSON_JETPACK")
  66. // Note: gpuMutex must already be held
  67. func initGPUHandles() {
  68. // TODO - if the ollama build is CPU only, don't do these checks as they're irrelevant and confusing
  69. gpuHandles = &handles{nil, nil}
  70. var nvmlMgmtName string
  71. var nvmlMgmtPatterns []string
  72. var cudartMgmtName string
  73. var cudartMgmtPatterns []string
  74. tmpDir, _ := PayloadsDir()
  75. switch runtime.GOOS {
  76. case "windows":
  77. nvmlMgmtName = "nvml.dll"
  78. nvmlMgmtPatterns = make([]string, len(NvmlWindowsGlobs))
  79. copy(nvmlMgmtPatterns, NvmlWindowsGlobs)
  80. cudartMgmtName = "cudart64_*.dll"
  81. localAppData := os.Getenv("LOCALAPPDATA")
  82. cudartMgmtPatterns = []string{filepath.Join(localAppData, "Programs", "Ollama", cudartMgmtName)}
  83. cudartMgmtPatterns = append(cudartMgmtPatterns, CudartWindowsGlobs...)
  84. case "linux":
  85. nvmlMgmtName = "libnvidia-ml.so"
  86. nvmlMgmtPatterns = make([]string, len(NvmlLinuxGlobs))
  87. copy(nvmlMgmtPatterns, NvmlLinuxGlobs)
  88. cudartMgmtName = "libcudart.so*"
  89. if tmpDir != "" {
  90. // TODO - add "payloads" for subprocess
  91. cudartMgmtPatterns = []string{filepath.Join(tmpDir, "cuda*", cudartMgmtName)}
  92. }
  93. cudartMgmtPatterns = append(cudartMgmtPatterns, CudartLinuxGlobs...)
  94. default:
  95. return
  96. }
  97. slog.Info("Detecting GPU type")
  98. cudartLibPaths := FindGPULibs(cudartMgmtName, cudartMgmtPatterns)
  99. if len(cudartLibPaths) > 0 {
  100. cudart := LoadCUDARTMgmt(cudartLibPaths)
  101. if cudart != nil {
  102. slog.Info("Nvidia GPU detected via cudart")
  103. gpuHandles.cudart = cudart
  104. return
  105. }
  106. }
  107. // TODO once we build confidence, remove this and the gpu_info_nvml.[ch] files
  108. nvmlLibPaths := FindGPULibs(nvmlMgmtName, nvmlMgmtPatterns)
  109. if len(nvmlLibPaths) > 0 {
  110. nvml := LoadNVMLMgmt(nvmlLibPaths)
  111. if nvml != nil {
  112. slog.Info("Nvidia GPU detected via nvidia-ml")
  113. gpuHandles.nvml = nvml
  114. return
  115. }
  116. }
  117. }
  118. func GetGPUInfo() GpuInfo {
  119. // TODO - consider exploring lspci (and equivalent on windows) to check for
  120. // GPUs so we can report warnings if we see Nvidia/AMD but fail to load the libraries
  121. gpuMutex.Lock()
  122. defer gpuMutex.Unlock()
  123. if gpuHandles == nil {
  124. initGPUHandles()
  125. }
  126. // All our GPU builds on x86 have AVX enabled, so fallback to CPU if we don't detect at least AVX
  127. cpuVariant := GetCPUVariant()
  128. if cpuVariant == "" && runtime.GOARCH == "amd64" {
  129. slog.Warn("CPU does not have AVX or AVX2, disabling GPU support.")
  130. }
  131. var memInfo C.mem_info_t
  132. resp := GpuInfo{}
  133. if gpuHandles.nvml != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {
  134. C.nvml_check_vram(*gpuHandles.nvml, &memInfo)
  135. if memInfo.err != nil {
  136. slog.Info(fmt.Sprintf("[nvidia-ml] error looking up NVML GPU memory: %s", C.GoString(memInfo.err)))
  137. C.free(unsafe.Pointer(memInfo.err))
  138. } else if memInfo.count > 0 {
  139. // Verify minimum compute capability
  140. var cc C.nvml_compute_capability_t
  141. C.nvml_compute_capability(*gpuHandles.nvml, &cc)
  142. if cc.err != nil {
  143. slog.Info(fmt.Sprintf("[nvidia-ml] error looking up NVML GPU compute capability: %s", C.GoString(cc.err)))
  144. C.free(unsafe.Pointer(cc.err))
  145. } else if cc.major > CudaComputeMin[0] || (cc.major == CudaComputeMin[0] && cc.minor >= CudaComputeMin[1]) {
  146. slog.Info(fmt.Sprintf("[nvidia-ml] NVML CUDA Compute Capability detected: %d.%d", cc.major, cc.minor))
  147. resp.Library = "cuda"
  148. } else {
  149. slog.Info(fmt.Sprintf("[nvidia-ml] CUDA GPU is too old. Falling back to CPU mode. Compute Capability detected: %d.%d", cc.major, cc.minor))
  150. }
  151. }
  152. } else if gpuHandles.cudart != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {
  153. C.cudart_check_vram(*gpuHandles.cudart, &memInfo)
  154. if memInfo.err != nil {
  155. slog.Info(fmt.Sprintf("[cudart] error looking up CUDART GPU memory: %s", C.GoString(memInfo.err)))
  156. C.free(unsafe.Pointer(memInfo.err))
  157. } else if memInfo.count > 0 {
  158. // Verify minimum compute capability
  159. var cc C.cudart_compute_capability_t
  160. C.cudart_compute_capability(*gpuHandles.cudart, &cc)
  161. if cc.err != nil {
  162. slog.Info(fmt.Sprintf("[cudart] error looking up CUDA compute capability: %s", C.GoString(cc.err)))
  163. C.free(unsafe.Pointer(cc.err))
  164. } else if cc.major > CudaComputeMin[0] || (cc.major == CudaComputeMin[0] && cc.minor >= CudaComputeMin[1]) {
  165. slog.Info(fmt.Sprintf("[cudart] CUDART CUDA Compute Capability detected: %d.%d", cc.major, cc.minor))
  166. resp.Library = "cuda"
  167. } else {
  168. slog.Info(fmt.Sprintf("[cudart] CUDA GPU is too old. Falling back to CPU mode. Compute Capability detected: %d.%d", cc.major, cc.minor))
  169. }
  170. }
  171. } else {
  172. AMDGetGPUInfo(&resp)
  173. if resp.Library != "" {
  174. return resp
  175. }
  176. }
  177. if resp.Library == "" {
  178. C.cpu_check_ram(&memInfo)
  179. resp.Library = "cpu"
  180. resp.Variant = cpuVariant
  181. }
  182. if memInfo.err != nil {
  183. slog.Info(fmt.Sprintf("error looking up CPU memory: %s", C.GoString(memInfo.err)))
  184. C.free(unsafe.Pointer(memInfo.err))
  185. return resp
  186. }
  187. resp.DeviceCount = uint32(memInfo.count)
  188. resp.FreeMemory = uint64(memInfo.free)
  189. resp.TotalMemory = uint64(memInfo.total)
  190. return resp
  191. }
  192. func getCPUMem() (memInfo, error) {
  193. var ret memInfo
  194. var info C.mem_info_t
  195. C.cpu_check_ram(&info)
  196. if info.err != nil {
  197. defer C.free(unsafe.Pointer(info.err))
  198. return ret, fmt.Errorf(C.GoString(info.err))
  199. }
  200. ret.FreeMemory = uint64(info.free)
  201. ret.TotalMemory = uint64(info.total)
  202. return ret, nil
  203. }
  204. func CheckVRAM() (int64, error) {
  205. userLimit := os.Getenv("OLLAMA_MAX_VRAM")
  206. if userLimit != "" {
  207. avail, err := strconv.ParseInt(userLimit, 10, 64)
  208. if err != nil {
  209. return 0, fmt.Errorf("Invalid OLLAMA_MAX_VRAM setting %s: %s", userLimit, err)
  210. }
  211. slog.Info(fmt.Sprintf("user override OLLAMA_MAX_VRAM=%d", avail))
  212. return avail, nil
  213. }
  214. gpuInfo := GetGPUInfo()
  215. if gpuInfo.FreeMemory > 0 && (gpuInfo.Library == "cuda" || gpuInfo.Library == "rocm") {
  216. // leave 10% or 1024MiB of VRAM free per GPU to handle unaccounted for overhead
  217. overhead := gpuInfo.FreeMemory / 10
  218. gpus := uint64(gpuInfo.DeviceCount)
  219. if overhead < gpus*1024*1024*1024 {
  220. overhead = gpus * 1024 * 1024 * 1024
  221. }
  222. // Assigning full reported free memory for Tegras due to OS controlled caching.
  223. if CudaTegra != "" {
  224. // Setting overhead for non-Tegra devices
  225. overhead = 0
  226. }
  227. avail := int64(gpuInfo.FreeMemory - overhead)
  228. slog.Debug(fmt.Sprintf("%s detected %d devices with %dM available memory", gpuInfo.Library, gpuInfo.DeviceCount, avail/1024/1024))
  229. return avail, nil
  230. }
  231. return 0, fmt.Errorf("no GPU detected") // TODO - better handling of CPU based memory determiniation
  232. }
  233. func FindGPULibs(baseLibName string, patterns []string) []string {
  234. // Multiple GPU libraries may exist, and some may not work, so keep trying until we exhaust them
  235. var ldPaths []string
  236. gpuLibPaths := []string{}
  237. slog.Info(fmt.Sprintf("Searching for GPU management library %s", baseLibName))
  238. switch runtime.GOOS {
  239. case "windows":
  240. ldPaths = strings.Split(os.Getenv("PATH"), ";")
  241. case "linux":
  242. ldPaths = strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":")
  243. default:
  244. return gpuLibPaths
  245. }
  246. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  247. for _, ldPath := range ldPaths {
  248. d, err := filepath.Abs(ldPath)
  249. if err != nil {
  250. continue
  251. }
  252. patterns = append(patterns, filepath.Join(d, baseLibName+"*"))
  253. }
  254. slog.Debug(fmt.Sprintf("gpu management search paths: %v", patterns))
  255. for _, pattern := range patterns {
  256. // Ignore glob discovery errors
  257. matches, _ := filepath.Glob(pattern)
  258. for _, match := range matches {
  259. // Resolve any links so we don't try the same lib multiple times
  260. // and weed out any dups across globs
  261. libPath := match
  262. tmp := match
  263. var err error
  264. for ; err == nil; tmp, err = os.Readlink(libPath) {
  265. if !filepath.IsAbs(tmp) {
  266. tmp = filepath.Join(filepath.Dir(libPath), tmp)
  267. }
  268. libPath = tmp
  269. }
  270. new := true
  271. for _, cmp := range gpuLibPaths {
  272. if cmp == libPath {
  273. new = false
  274. break
  275. }
  276. }
  277. if new {
  278. gpuLibPaths = append(gpuLibPaths, libPath)
  279. }
  280. }
  281. }
  282. slog.Info(fmt.Sprintf("Discovered GPU libraries: %v", gpuLibPaths))
  283. return gpuLibPaths
  284. }
  285. func LoadNVMLMgmt(nvmlLibPaths []string) *C.nvml_handle_t {
  286. var resp C.nvml_init_resp_t
  287. resp.ch.verbose = getVerboseState()
  288. for _, libPath := range nvmlLibPaths {
  289. lib := C.CString(libPath)
  290. defer C.free(unsafe.Pointer(lib))
  291. C.nvml_init(lib, &resp)
  292. if resp.err != nil {
  293. slog.Info(fmt.Sprintf("Unable to load NVML management library %s: %s", libPath, C.GoString(resp.err)))
  294. C.free(unsafe.Pointer(resp.err))
  295. } else {
  296. return &resp.ch
  297. }
  298. }
  299. return nil
  300. }
  301. func LoadCUDARTMgmt(cudartLibPaths []string) *C.cudart_handle_t {
  302. var resp C.cudart_init_resp_t
  303. resp.ch.verbose = getVerboseState()
  304. for _, libPath := range cudartLibPaths {
  305. lib := C.CString(libPath)
  306. defer C.free(unsafe.Pointer(lib))
  307. C.cudart_init(lib, &resp)
  308. if resp.err != nil {
  309. slog.Info(fmt.Sprintf("Unable to load cudart CUDA management library %s: %s", libPath, C.GoString(resp.err)))
  310. C.free(unsafe.Pointer(resp.err))
  311. } else {
  312. return &resp.ch
  313. }
  314. }
  315. return nil
  316. }
  317. func getVerboseState() C.uint16_t {
  318. if debug := os.Getenv("OLLAMA_DEBUG"); debug != "" {
  319. return C.uint16_t(1)
  320. }
  321. return C.uint16_t(0)
  322. }