types.go 3.2 KB

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