gpu_darwin.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //go:build darwin
  2. package gpu
  3. import "C"
  4. import (
  5. "runtime"
  6. "github.com/pbnjay/memory"
  7. )
  8. // CheckVRAM returns the free VRAM in bytes on Linux machines with NVIDIA GPUs
  9. func CheckVRAM() (int64, error) {
  10. if runtime.GOARCH == "amd64" {
  11. // gpu not supported, this may not be metal
  12. return 0, nil
  13. }
  14. // on macOS, there's already buffer for available vram (see below) so just return the total
  15. systemMemory := int64(memory.TotalMemory())
  16. // macOS limits how much memory is available to the GPU based on the amount of system memory
  17. // TODO: handle case where iogpu.wired_limit_mb is set to a higher value
  18. if systemMemory <= 36*1024*1024*1024 {
  19. systemMemory = systemMemory * 2 / 3
  20. } else {
  21. systemMemory = systemMemory * 3 / 4
  22. }
  23. return systemMemory, nil
  24. }
  25. func GetGPUInfo() GpuInfo {
  26. mem, _ := getCPUMem()
  27. if runtime.GOARCH == "amd64" {
  28. return GpuInfo{
  29. Library: "cpu",
  30. Variant: GetCPUVariant(),
  31. memInfo: mem,
  32. }
  33. }
  34. return GpuInfo{
  35. Library: "metal",
  36. memInfo: mem,
  37. }
  38. }
  39. func getCPUMem() (memInfo, error) {
  40. return memInfo{
  41. TotalMemory: 0,
  42. FreeMemory: 0,
  43. DeviceCount: 0,
  44. }, nil
  45. }