gpu.go 10 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. cuda *C.cuda_handle_t
  22. rocm *C.rocm_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 CudaLinuxGlobs = []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/local/lib*/libnvidia-ml.so*",
  38. "/usr/lib/aarch64-linux-gnu/nvidia/current/libnvidia-ml.so*",
  39. "/usr/lib/aarch64-linux-gnu/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 CudaWindowsGlobs = []string{
  44. "c:\\Windows\\System32\\nvml.dll",
  45. }
  46. var RocmLinuxGlobs = []string{
  47. "/opt/rocm*/lib*/librocm_smi64.so*",
  48. }
  49. var RocmWindowsGlobs = []string{
  50. "c:\\Windows\\System32\\rocm_smi64.dll",
  51. }
  52. // Note: gpuMutex must already be held
  53. func initGPUHandles() {
  54. // TODO - if the ollama build is CPU only, don't do these checks as they're irrelevant and confusing
  55. gpuHandles = &handles{nil, nil}
  56. var cudaMgmtName string
  57. var cudaMgmtPatterns []string
  58. var rocmMgmtName string
  59. var rocmMgmtPatterns []string
  60. switch runtime.GOOS {
  61. case "windows":
  62. cudaMgmtName = "nvml.dll"
  63. cudaMgmtPatterns = make([]string, len(CudaWindowsGlobs))
  64. copy(cudaMgmtPatterns, CudaWindowsGlobs)
  65. rocmMgmtName = "rocm_smi64.dll"
  66. rocmMgmtPatterns = make([]string, len(RocmWindowsGlobs))
  67. copy(rocmMgmtPatterns, RocmWindowsGlobs)
  68. case "linux":
  69. cudaMgmtName = "libnvidia-ml.so"
  70. cudaMgmtPatterns = make([]string, len(CudaLinuxGlobs))
  71. copy(cudaMgmtPatterns, CudaLinuxGlobs)
  72. rocmMgmtName = "librocm_smi64.so"
  73. rocmMgmtPatterns = make([]string, len(RocmLinuxGlobs))
  74. copy(rocmMgmtPatterns, RocmLinuxGlobs)
  75. default:
  76. return
  77. }
  78. slog.Info("Detecting GPU type")
  79. cudaLibPaths := FindGPULibs(cudaMgmtName, cudaMgmtPatterns)
  80. if len(cudaLibPaths) > 0 {
  81. cuda := LoadCUDAMgmt(cudaLibPaths)
  82. if cuda != nil {
  83. slog.Info("Nvidia GPU detected")
  84. gpuHandles.cuda = cuda
  85. return
  86. }
  87. }
  88. rocmLibPaths := FindGPULibs(rocmMgmtName, rocmMgmtPatterns)
  89. if len(rocmLibPaths) > 0 {
  90. rocm := LoadROCMMgmt(rocmLibPaths)
  91. if rocm != nil {
  92. slog.Info("Radeon GPU detected")
  93. gpuHandles.rocm = rocm
  94. return
  95. }
  96. }
  97. }
  98. func GetGPUInfo() GpuInfo {
  99. // TODO - consider exploring lspci (and equivalent on windows) to check for
  100. // GPUs so we can report warnings if we see Nvidia/AMD but fail to load the libraries
  101. gpuMutex.Lock()
  102. defer gpuMutex.Unlock()
  103. if gpuHandles == nil {
  104. initGPUHandles()
  105. }
  106. // All our GPU builds on x86 have AVX enabled, so fallback to CPU if we don't detect at least AVX
  107. cpuVariant := GetCPUVariant()
  108. if cpuVariant == "" && runtime.GOARCH == "amd64" {
  109. slog.Warn("CPU does not have AVX or AVX2, disabling GPU support.")
  110. }
  111. var memInfo C.mem_info_t
  112. resp := GpuInfo{}
  113. if gpuHandles.cuda != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {
  114. C.cuda_check_vram(*gpuHandles.cuda, &memInfo)
  115. if memInfo.err != nil {
  116. slog.Info(fmt.Sprintf("error looking up CUDA GPU memory: %s", C.GoString(memInfo.err)))
  117. C.free(unsafe.Pointer(memInfo.err))
  118. } else if memInfo.count > 0 {
  119. // Verify minimum compute capability
  120. var cc C.cuda_compute_capability_t
  121. C.cuda_compute_capability(*gpuHandles.cuda, &cc)
  122. if cc.err != nil {
  123. slog.Info(fmt.Sprintf("error looking up CUDA GPU compute capability: %s", C.GoString(cc.err)))
  124. C.free(unsafe.Pointer(cc.err))
  125. } else if cc.major > CudaComputeMin[0] || (cc.major == CudaComputeMin[0] && cc.minor >= CudaComputeMin[1]) {
  126. slog.Info(fmt.Sprintf("CUDA Compute Capability detected: %d.%d", cc.major, cc.minor))
  127. resp.Library = "cuda"
  128. } else {
  129. slog.Info(fmt.Sprintf("CUDA GPU is too old. Falling back to CPU mode. Compute Capability detected: %d.%d", cc.major, cc.minor))
  130. }
  131. }
  132. } else if AMDDetected() && gpuHandles.rocm != nil && (cpuVariant != "" || runtime.GOARCH != "amd64") {
  133. ver, err := AMDDriverVersion()
  134. if err == nil {
  135. slog.Info("AMD Driver: " + ver)
  136. } else {
  137. // For now this is benign, but we may eventually need to fail compatibility checks
  138. slog.Debug("error looking up amd driver version: %s", err)
  139. }
  140. gfx := AMDGFXVersions()
  141. tooOld := false
  142. for _, v := range gfx {
  143. if v.Major < 9 {
  144. slog.Info("AMD GPU too old, falling back to CPU " + v.ToGFXString())
  145. tooOld = true
  146. break
  147. }
  148. // TODO - remap gfx strings for unsupporetd minor/patch versions to supported for the same major
  149. // e.g. gfx1034 works if we map it to gfx1030 at runtime
  150. }
  151. if !tooOld {
  152. // TODO - this algo can be shifted over to use sysfs instead of the rocm info library...
  153. C.rocm_check_vram(*gpuHandles.rocm, &memInfo)
  154. if memInfo.err != nil {
  155. slog.Info(fmt.Sprintf("error looking up ROCm GPU memory: %s", C.GoString(memInfo.err)))
  156. C.free(unsafe.Pointer(memInfo.err))
  157. } else if memInfo.igpu_index >= 0 && memInfo.count == 1 {
  158. // Only one GPU detected and it appears to be an integrated GPU - skip it
  159. slog.Info("ROCm unsupported integrated GPU detected")
  160. } else if memInfo.count > 0 {
  161. if memInfo.igpu_index >= 0 {
  162. // We have multiple GPUs reported, and one of them is an integrated GPU
  163. // so we have to set the env var to bypass it
  164. // If the user has specified their own ROCR_VISIBLE_DEVICES, don't clobber it
  165. val := os.Getenv("ROCR_VISIBLE_DEVICES")
  166. if val == "" {
  167. devices := []string{}
  168. for i := 0; i < int(memInfo.count); i++ {
  169. if i == int(memInfo.igpu_index) {
  170. continue
  171. }
  172. devices = append(devices, strconv.Itoa(i))
  173. }
  174. val = strings.Join(devices, ",")
  175. os.Setenv("ROCR_VISIBLE_DEVICES", val)
  176. }
  177. slog.Info(fmt.Sprintf("ROCm integrated GPU detected - ROCR_VISIBLE_DEVICES=%s", val))
  178. }
  179. resp.Library = "rocm"
  180. var version C.rocm_version_resp_t
  181. C.rocm_get_version(*gpuHandles.rocm, &version)
  182. verString := C.GoString(version.str)
  183. if version.status == 0 {
  184. resp.Variant = "v" + verString
  185. } else {
  186. slog.Info(fmt.Sprintf("failed to look up ROCm version: %s", verString))
  187. }
  188. C.free(unsafe.Pointer(version.str))
  189. }
  190. }
  191. }
  192. if resp.Library == "" {
  193. C.cpu_check_ram(&memInfo)
  194. resp.Library = "cpu"
  195. resp.Variant = cpuVariant
  196. }
  197. if memInfo.err != nil {
  198. slog.Info(fmt.Sprintf("error looking up CPU memory: %s", C.GoString(memInfo.err)))
  199. C.free(unsafe.Pointer(memInfo.err))
  200. return resp
  201. }
  202. resp.DeviceCount = uint32(memInfo.count)
  203. resp.FreeMemory = uint64(memInfo.free)
  204. resp.TotalMemory = uint64(memInfo.total)
  205. return resp
  206. }
  207. func getCPUMem() (memInfo, error) {
  208. var ret memInfo
  209. var info C.mem_info_t
  210. C.cpu_check_ram(&info)
  211. if info.err != nil {
  212. defer C.free(unsafe.Pointer(info.err))
  213. return ret, fmt.Errorf(C.GoString(info.err))
  214. }
  215. ret.FreeMemory = uint64(info.free)
  216. ret.TotalMemory = uint64(info.total)
  217. return ret, nil
  218. }
  219. func CheckVRAM() (int64, error) {
  220. gpuInfo := GetGPUInfo()
  221. if gpuInfo.FreeMemory > 0 && (gpuInfo.Library == "cuda" || gpuInfo.Library == "rocm") {
  222. // leave 10% or 1024MiB of VRAM free per GPU to handle unaccounted for overhead
  223. overhead := gpuInfo.FreeMemory / 10
  224. gpus := uint64(gpuInfo.DeviceCount)
  225. if overhead < gpus*1024*1024*1024 {
  226. overhead = gpus * 1024 * 1024 * 1024
  227. }
  228. avail := int64(gpuInfo.FreeMemory - overhead)
  229. slog.Debug(fmt.Sprintf("%s detected %d devices with %dM available memory", gpuInfo.Library, gpuInfo.DeviceCount, avail/1024/1024))
  230. return avail, nil
  231. }
  232. return 0, fmt.Errorf("no GPU detected") // TODO - better handling of CPU based memory determiniation
  233. }
  234. func FindGPULibs(baseLibName string, patterns []string) []string {
  235. // Multiple GPU libraries may exist, and some may not work, so keep trying until we exhaust them
  236. var ldPaths []string
  237. gpuLibPaths := []string{}
  238. slog.Info(fmt.Sprintf("Searching for GPU management library %s", baseLibName))
  239. switch runtime.GOOS {
  240. case "windows":
  241. ldPaths = strings.Split(os.Getenv("PATH"), ";")
  242. case "linux":
  243. ldPaths = strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":")
  244. default:
  245. return gpuLibPaths
  246. }
  247. // Start with whatever we find in the PATH/LD_LIBRARY_PATH
  248. for _, ldPath := range ldPaths {
  249. d, err := filepath.Abs(ldPath)
  250. if err != nil {
  251. continue
  252. }
  253. patterns = append(patterns, filepath.Join(d, baseLibName+"*"))
  254. }
  255. slog.Debug(fmt.Sprintf("gpu management search paths: %v", patterns))
  256. for _, pattern := range patterns {
  257. // Ignore glob discovery errors
  258. matches, _ := filepath.Glob(pattern)
  259. for _, match := range matches {
  260. // Resolve any links so we don't try the same lib multiple times
  261. // and weed out any dups across globs
  262. libPath := match
  263. tmp := match
  264. var err error
  265. for ; err == nil; tmp, err = os.Readlink(libPath) {
  266. if !filepath.IsAbs(tmp) {
  267. tmp = filepath.Join(filepath.Dir(libPath), tmp)
  268. }
  269. libPath = tmp
  270. }
  271. new := true
  272. for _, cmp := range gpuLibPaths {
  273. if cmp == libPath {
  274. new = false
  275. break
  276. }
  277. }
  278. if new {
  279. gpuLibPaths = append(gpuLibPaths, libPath)
  280. }
  281. }
  282. }
  283. slog.Info(fmt.Sprintf("Discovered GPU libraries: %v", gpuLibPaths))
  284. return gpuLibPaths
  285. }
  286. func LoadCUDAMgmt(cudaLibPaths []string) *C.cuda_handle_t {
  287. var resp C.cuda_init_resp_t
  288. resp.ch.verbose = getVerboseState()
  289. for _, libPath := range cudaLibPaths {
  290. lib := C.CString(libPath)
  291. defer C.free(unsafe.Pointer(lib))
  292. C.cuda_init(lib, &resp)
  293. if resp.err != nil {
  294. slog.Info(fmt.Sprintf("Unable to load CUDA management library %s: %s", libPath, C.GoString(resp.err)))
  295. C.free(unsafe.Pointer(resp.err))
  296. } else {
  297. return &resp.ch
  298. }
  299. }
  300. return nil
  301. }
  302. func LoadROCMMgmt(rocmLibPaths []string) *C.rocm_handle_t {
  303. var resp C.rocm_init_resp_t
  304. resp.rh.verbose = getVerboseState()
  305. for _, libPath := range rocmLibPaths {
  306. lib := C.CString(libPath)
  307. defer C.free(unsafe.Pointer(lib))
  308. C.rocm_init(lib, &resp)
  309. if resp.err != nil {
  310. slog.Info(fmt.Sprintf("Unable to load ROCm management library %s: %s", libPath, C.GoString(resp.err)))
  311. C.free(unsafe.Pointer(resp.err))
  312. } else {
  313. return &resp.rh
  314. }
  315. }
  316. return nil
  317. }
  318. func getVerboseState() C.uint16_t {
  319. if debug := os.Getenv("OLLAMA_DEBUG"); debug != "" {
  320. return C.uint16_t(1)
  321. }
  322. return C.uint16_t(0)
  323. }