gpu.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. cuda *C.cuda_handle_t
  22. }
  23. var gpuMutex sync.Mutex
  24. var gpuHandles *handles = nil
  25. // With our current CUDA compile flags, older than 5.0 will not work properly
  26. var CudaComputeMin = [2]C.int{5, 0}
  27. // Possible locations for the nvidia-ml library
  28. var CudaLinuxGlobs = []string{
  29. "/usr/local/cuda/lib64/libnvidia-ml.so*",
  30. "/usr/lib/x86_64-linux-gnu/nvidia/current/libnvidia-ml.so*",
  31. "/usr/lib/x86_64-linux-gnu/libnvidia-ml.so*",
  32. "/usr/lib/wsl/lib/libnvidia-ml.so*",
  33. "/usr/lib/wsl/drivers/*/libnvidia-ml.so*",
  34. "/opt/cuda/lib64/libnvidia-ml.so*",
  35. "/usr/lib*/libnvidia-ml.so*",
  36. "/usr/local/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. // TODO: are these stubs ever valid?
  40. "/opt/cuda/targets/x86_64-linux/lib/stubs/libnvidia-ml.so*",
  41. }
  42. var CudaWindowsGlobs = []string{
  43. "c:\\Windows\\System32\\nvml.dll",
  44. }
  45. // Note: gpuMutex must already be held
  46. func initGPUHandles() {
  47. // TODO - if the ollama build is CPU only, don't do these checks as they're irrelevant and confusing
  48. gpuHandles = &handles{nil}
  49. var cudaMgmtName string
  50. var cudaMgmtPatterns []string
  51. switch runtime.GOOS {
  52. case "windows":
  53. cudaMgmtName = "nvml.dll"
  54. cudaMgmtPatterns = make([]string, len(CudaWindowsGlobs))
  55. copy(cudaMgmtPatterns, CudaWindowsGlobs)
  56. case "linux":
  57. cudaMgmtName = "libnvidia-ml.so"
  58. cudaMgmtPatterns = make([]string, len(CudaLinuxGlobs))
  59. copy(cudaMgmtPatterns, CudaLinuxGlobs)
  60. default:
  61. return
  62. }
  63. slog.Info("Detecting GPU type")
  64. cudaLibPaths := FindGPULibs(cudaMgmtName, cudaMgmtPatterns)
  65. if len(cudaLibPaths) > 0 {
  66. cuda := LoadCUDAMgmt(cudaLibPaths)
  67. if cuda != nil {
  68. slog.Info("Nvidia GPU detected")
  69. gpuHandles.cuda = cuda
  70. return
  71. }
  72. }
  73. }
  74. func GetGPUInfo() GpuInfo {
  75. // TODO - consider exploring lspci (and equivalent on windows) to check for
  76. // GPUs so we can report warnings if we see Nvidia/AMD but fail to load the libraries
  77. gpuMutex.Lock()
  78. defer gpuMutex.Unlock()
  79. if gpuHandles == nil {
  80. initGPUHandles()
  81. }
  82. // All our GPU builds on x86 have AVX enabled, so fallback to CPU if we don't detect at least AVX
  83. cpuVariant := GetCPUVariant()
  84. if cpuVariant == "" && runtime.GOARCH == "amd64" {
  85. slog.Warn("CPU does not have AVX or AVX2, disabling GPU support.")
  86. }
  87. var memInfo C.mem_info_t
  88. resp := GpuInfo{}
  89. if gpuHandles.cuda != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {
  90. C.cuda_check_vram(*gpuHandles.cuda, &memInfo)
  91. if memInfo.err != nil {
  92. slog.Info(fmt.Sprintf("error looking up CUDA GPU memory: %s", C.GoString(memInfo.err)))
  93. C.free(unsafe.Pointer(memInfo.err))
  94. } else if memInfo.count > 0 {
  95. // Verify minimum compute capability
  96. var cc C.cuda_compute_capability_t
  97. C.cuda_compute_capability(*gpuHandles.cuda, &cc)
  98. if cc.err != nil {
  99. slog.Info(fmt.Sprintf("error looking up CUDA GPU compute capability: %s", C.GoString(cc.err)))
  100. C.free(unsafe.Pointer(cc.err))
  101. } else if cc.major > CudaComputeMin[0] || (cc.major == CudaComputeMin[0] && cc.minor >= CudaComputeMin[1]) {
  102. slog.Info(fmt.Sprintf("CUDA Compute Capability detected: %d.%d", cc.major, cc.minor))
  103. resp.Library = "cuda"
  104. } else {
  105. slog.Info(fmt.Sprintf("CUDA GPU is too old. Falling back to CPU mode. Compute Capability detected: %d.%d", cc.major, cc.minor))
  106. }
  107. }
  108. } else {
  109. AMDGetGPUInfo(&resp)
  110. if resp.Library != "" {
  111. return resp
  112. }
  113. }
  114. if resp.Library == "" {
  115. C.cpu_check_ram(&memInfo)
  116. resp.Library = "cpu"
  117. resp.Variant = cpuVariant
  118. }
  119. if memInfo.err != nil {
  120. slog.Info(fmt.Sprintf("error looking up CPU memory: %s", C.GoString(memInfo.err)))
  121. C.free(unsafe.Pointer(memInfo.err))
  122. return resp
  123. }
  124. resp.DeviceCount = uint32(memInfo.count)
  125. resp.FreeMemory = uint64(memInfo.free)
  126. resp.TotalMemory = uint64(memInfo.total)
  127. return resp
  128. }
  129. func getCPUMem() (memInfo, error) {
  130. var ret memInfo
  131. var info C.mem_info_t
  132. C.cpu_check_ram(&info)
  133. if info.err != nil {
  134. defer C.free(unsafe.Pointer(info.err))
  135. return ret, fmt.Errorf(C.GoString(info.err))
  136. }
  137. ret.FreeMemory = uint64(info.free)
  138. ret.TotalMemory = uint64(info.total)
  139. return ret, nil
  140. }
  141. func CheckVRAM() (int64, error) {
  142. userLimit := os.Getenv("OLLAMA_MAX_VRAM")
  143. if userLimit != "" {
  144. avail, err := strconv.ParseInt(userLimit, 10, 64)
  145. if err != nil {
  146. return 0, fmt.Errorf("Invalid OLLAMA_MAX_VRAM setting %s: %s", userLimit, err)
  147. }
  148. slog.Info(fmt.Sprintf("user override OLLAMA_MAX_VRAM=%d", avail))
  149. return avail, nil
  150. }
  151. gpuInfo := GetGPUInfo()
  152. if gpuInfo.FreeMemory > 0 && (gpuInfo.Library == "cuda" || gpuInfo.Library == "rocm") {
  153. // leave 10% or 1024MiB of VRAM free per GPU to handle unaccounted for overhead
  154. overhead := gpuInfo.FreeMemory / 10
  155. gpus := uint64(gpuInfo.DeviceCount)
  156. if overhead < gpus*1024*1024*1024 {
  157. overhead = gpus * 1024 * 1024 * 1024
  158. }
  159. avail := int64(gpuInfo.FreeMemory - overhead)
  160. slog.Debug(fmt.Sprintf("%s detected %d devices with %dM available memory", gpuInfo.Library, gpuInfo.DeviceCount, avail/1024/1024))
  161. return avail, nil
  162. }
  163. return 0, fmt.Errorf("no GPU detected") // TODO - better handling of CPU based memory determiniation
  164. }
  165. func FindGPULibs(baseLibName string, patterns []string) []string {
  166. // Multiple GPU libraries may exist, and some may not work, so keep trying until we exhaust them
  167. var ldPaths []string
  168. gpuLibPaths := []string{}
  169. slog.Info(fmt.Sprintf("Searching for GPU management library %s", baseLibName))
  170. switch runtime.GOOS {
  171. case "windows":
  172. ldPaths = strings.Split(os.Getenv("PATH"), ";")
  173. case "linux":
  174. ldPaths = strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":")
  175. default:
  176. return gpuLibPaths
  177. }
  178. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  179. for _, ldPath := range ldPaths {
  180. d, err := filepath.Abs(ldPath)
  181. if err != nil {
  182. continue
  183. }
  184. patterns = append(patterns, filepath.Join(d, baseLibName+"*"))
  185. }
  186. slog.Debug(fmt.Sprintf("gpu management search paths: %v", patterns))
  187. for _, pattern := range patterns {
  188. // Ignore glob discovery errors
  189. matches, _ := filepath.Glob(pattern)
  190. for _, match := range matches {
  191. // Resolve any links so we don't try the same lib multiple times
  192. // and weed out any dups across globs
  193. libPath := match
  194. tmp := match
  195. var err error
  196. for ; err == nil; tmp, err = os.Readlink(libPath) {
  197. if !filepath.IsAbs(tmp) {
  198. tmp = filepath.Join(filepath.Dir(libPath), tmp)
  199. }
  200. libPath = tmp
  201. }
  202. new := true
  203. for _, cmp := range gpuLibPaths {
  204. if cmp == libPath {
  205. new = false
  206. break
  207. }
  208. }
  209. if new {
  210. gpuLibPaths = append(gpuLibPaths, libPath)
  211. }
  212. }
  213. }
  214. slog.Info(fmt.Sprintf("Discovered GPU libraries: %v", gpuLibPaths))
  215. return gpuLibPaths
  216. }
  217. func LoadCUDAMgmt(cudaLibPaths []string) *C.cuda_handle_t {
  218. var resp C.cuda_init_resp_t
  219. resp.ch.verbose = getVerboseState()
  220. for _, libPath := range cudaLibPaths {
  221. lib := C.CString(libPath)
  222. defer C.free(unsafe.Pointer(lib))
  223. C.cuda_init(lib, &resp)
  224. if resp.err != nil {
  225. slog.Info(fmt.Sprintf("Unable to load CUDA management library %s: %s", libPath, C.GoString(resp.err)))
  226. C.free(unsafe.Pointer(resp.err))
  227. } else {
  228. return &resp.ch
  229. }
  230. }
  231. return nil
  232. }
  233. func getVerboseState() C.uint16_t {
  234. if debug := os.Getenv("OLLAMA_DEBUG"); debug != "" {
  235. return C.uint16_t(1)
  236. }
  237. return C.uint16_t(0)
  238. }