gpu.go 8.1 KB

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