types.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 string `json:"variant,omitempty"`
  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 GpuInfoList []GpuInfo
  31. // Split up the set of gpu info's by Library and variant
  32. func (l GpuInfoList) ByLibrary() []GpuInfoList {
  33. resp := []GpuInfoList{}
  34. libs := []string{}
  35. for _, info := range l {
  36. found := false
  37. requested := info.Library
  38. if info.Variant != "" {
  39. requested += "_" + info.Variant
  40. }
  41. for i, lib := range libs {
  42. if lib == requested {
  43. resp[i] = append(resp[i], info)
  44. found = true
  45. break
  46. }
  47. }
  48. if !found {
  49. libs = append(libs, info.Library)
  50. resp = append(resp, []GpuInfo{info})
  51. }
  52. }
  53. return resp
  54. }
  55. // Report the GPU information into the log an Info level
  56. func (l GpuInfoList) LogDetails() {
  57. for _, g := range l {
  58. slog.Info("inference compute",
  59. "id", g.ID,
  60. "library", g.Library,
  61. "compute", g.Compute,
  62. "driver", fmt.Sprintf("%d.%d", g.DriverMajor, g.DriverMinor),
  63. "name", g.Name,
  64. "total", format.HumanBytes2(g.TotalMemory),
  65. "available", format.HumanBytes2(g.FreeMemory),
  66. )
  67. }
  68. }
  69. // Sort by Free Space
  70. type ByFreeMemory []GpuInfo
  71. func (a ByFreeMemory) Len() int { return len(a) }
  72. func (a ByFreeMemory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  73. func (a ByFreeMemory) Less(i, j int) bool { return a[i].FreeMemory < a[j].FreeMemory }