types.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 string `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. computeMajor int //nolint:unused,nolintlint
  45. computeMinor int //nolint:unused,nolintlint
  46. }
  47. type CudaGPUInfoList []CudaGPUInfo
  48. type RocmGPUInfo struct {
  49. GpuInfo
  50. usedFilepath string //nolint:unused,nolintlint
  51. index int //nolint:unused,nolintlint
  52. }
  53. type RocmGPUInfoList []RocmGPUInfo
  54. type OneapiGPUInfo struct {
  55. GpuInfo
  56. driverIndex int //nolint:unused,nolintlint
  57. gpuIndex int //nolint:unused,nolintlint
  58. }
  59. type OneapiGPUInfoList []OneapiGPUInfo
  60. type GpuInfoList []GpuInfo
  61. // Split up the set of gpu info's by Library and variant
  62. func (l GpuInfoList) ByLibrary() []GpuInfoList {
  63. resp := []GpuInfoList{}
  64. libs := []string{}
  65. for _, info := range l {
  66. found := false
  67. requested := info.Library
  68. if info.Variant != CPUCapabilityNone.String() {
  69. requested += "_" + info.Variant
  70. }
  71. for i, lib := range libs {
  72. if lib == requested {
  73. resp[i] = append(resp[i], info)
  74. found = true
  75. break
  76. }
  77. }
  78. if !found {
  79. libs = append(libs, requested)
  80. resp = append(resp, []GpuInfo{info})
  81. }
  82. }
  83. return resp
  84. }
  85. // Report the GPU information into the log an Info level
  86. func (l GpuInfoList) LogDetails() {
  87. for _, g := range l {
  88. slog.Info("inference compute",
  89. "id", g.ID,
  90. "library", g.Library,
  91. "variant", g.Variant,
  92. "compute", g.Compute,
  93. "driver", fmt.Sprintf("%d.%d", g.DriverMajor, g.DriverMinor),
  94. "name", g.Name,
  95. "total", format.HumanBytes2(g.TotalMemory),
  96. "available", format.HumanBytes2(g.FreeMemory),
  97. )
  98. }
  99. }
  100. // Sort by Free Space
  101. type ByFreeMemory []GpuInfo
  102. func (a ByFreeMemory) Len() int { return len(a) }
  103. func (a ByFreeMemory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  104. func (a ByFreeMemory) Less(i, j int) bool { return a[i].FreeMemory < a[j].FreeMemory }
  105. type CPUCapability uint32
  106. // Override at build time when building base GPU runners
  107. var GPURunnerCPUCapability = CPUCapabilityAVX
  108. const (
  109. CPUCapabilityNone CPUCapability = iota
  110. CPUCapabilityAVX
  111. CPUCapabilityAVX2
  112. // TODO AVX512
  113. )
  114. func (c CPUCapability) String() string {
  115. switch c {
  116. case CPUCapabilityAVX:
  117. return "avx"
  118. case CPUCapabilityAVX2:
  119. return "avx2"
  120. default:
  121. return "no vector extensions"
  122. }
  123. }