types.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package gpu
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "github.com/ollama/ollama/format"
  6. )
  7. type memInfo struct {
  8. TotalMemory uint64 `json:"total_memory,omitempty"`
  9. FreeMemory uint64 `json:"free_memory,omitempty"`
  10. FreeSwap uint64 `json:"free_swap,omitempty"`
  11. }
  12. // Beginning of an `ollama info` command
  13. type GpuInfo struct {
  14. memInfo
  15. Library string `json:"library,omitempty"`
  16. // Optional variant to select (e.g. versions, cpu feature flags)
  17. Variant CPUCapability `json:"variant"`
  18. // MinimumMemory represents the minimum memory required to use the GPU
  19. MinimumMemory uint64 `json:"-"`
  20. // Any extra PATH/LD_LIBRARY_PATH dependencies required for the Library to operate properly
  21. DependencyPath string `json:"lib_path,omitempty"`
  22. // Extra environment variables specific to the GPU as list of [key,value]
  23. EnvWorkarounds [][2]string `json:"envs,omitempty"`
  24. // Set to true if we can NOT reliably discover FreeMemory. A value of true indicates
  25. // the FreeMemory is best effort, and may over or under report actual memory usage
  26. // False indicates FreeMemory can generally be trusted on this GPU
  27. UnreliableFreeMemory bool
  28. // GPU information
  29. ID string `json:"gpu_id"` // string to use for selection of this specific GPU
  30. Name string `json:"name"` // user friendly name if available
  31. Compute string `json:"compute"` // Compute Capability or gfx
  32. // Driver Information - TODO no need to put this on each GPU
  33. DriverMajor int `json:"driver_major,omitempty"`
  34. DriverMinor int `json:"driver_minor,omitempty"`
  35. // TODO other performance capability info to help in scheduling decisions
  36. }
  37. type CPUInfo struct {
  38. GpuInfo
  39. }
  40. type CudaGPUInfo struct {
  41. GpuInfo
  42. OSOverhead uint64 // Memory overhead between the driver library and management library
  43. index int //nolint:unused,nolintlint
  44. }
  45. type CudaGPUInfoList []CudaGPUInfo
  46. type RocmGPUInfo struct {
  47. GpuInfo
  48. usedFilepath string //nolint:unused,nolintlint
  49. index int //nolint:unused,nolintlint
  50. }
  51. type RocmGPUInfoList []RocmGPUInfo
  52. type OneapiGPUInfo struct {
  53. GpuInfo
  54. driverIndex int //nolint:unused,nolintlint
  55. gpuIndex int //nolint:unused,nolintlint
  56. }
  57. type OneapiGPUInfoList []OneapiGPUInfo
  58. type GpuInfoList []GpuInfo
  59. // Split up the set of gpu info's by Library and variant
  60. func (l GpuInfoList) ByLibrary() []GpuInfoList {
  61. resp := []GpuInfoList{}
  62. libs := []string{}
  63. for _, info := range l {
  64. found := false
  65. requested := info.Library
  66. if info.Variant != CPUCapabilityNone {
  67. requested += "_" + info.Variant.String()
  68. }
  69. for i, lib := range libs {
  70. if lib == requested {
  71. resp[i] = append(resp[i], info)
  72. found = true
  73. break
  74. }
  75. }
  76. if !found {
  77. libs = append(libs, info.Library)
  78. resp = append(resp, []GpuInfo{info})
  79. }
  80. }
  81. return resp
  82. }
  83. // Report the GPU information into the log an Info level
  84. func (l GpuInfoList) LogDetails() {
  85. for _, g := range l {
  86. slog.Info("inference compute",
  87. "id", g.ID,
  88. "library", g.Library,
  89. "compute", g.Compute,
  90. "driver", fmt.Sprintf("%d.%d", g.DriverMajor, g.DriverMinor),
  91. "name", g.Name,
  92. "total", format.HumanBytes2(g.TotalMemory),
  93. "available", format.HumanBytes2(g.FreeMemory),
  94. )
  95. }
  96. }
  97. // Sort by Free Space
  98. type ByFreeMemory []GpuInfo
  99. func (a ByFreeMemory) Len() int { return len(a) }
  100. func (a ByFreeMemory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  101. func (a ByFreeMemory) Less(i, j int) bool { return a[i].FreeMemory < a[j].FreeMemory }
  102. type CPUCapability uint32
  103. // Override at build time when building base GPU runners
  104. var GPURunnerCPUCapability = CPUCapabilityAVX
  105. const (
  106. CPUCapabilityNone CPUCapability = iota
  107. CPUCapabilityAVX
  108. CPUCapabilityAVX2
  109. // TODO AVX512
  110. )
  111. func (c CPUCapability) String() string {
  112. switch c {
  113. case CPUCapabilityAVX:
  114. return "avx"
  115. case CPUCapabilityAVX2:
  116. return "avx2"
  117. default:
  118. return "no vector extensions"
  119. }
  120. }