types.go 3.4 KB

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