gpu_darwin.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //go:build darwin
  2. package gpu
  3. /*
  4. #cgo CFLAGS: -x objective-c
  5. #cgo LDFLAGS: -framework Foundation -framework CoreGraphics -framework Metal
  6. #include "gpu_info_darwin.h"
  7. */
  8. import "C"
  9. import (
  10. "fmt"
  11. "log/slog"
  12. "os"
  13. "runtime"
  14. "strconv"
  15. )
  16. // CheckVRAM returns the free VRAM in bytes on Linux machines with NVIDIA GPUs
  17. func CheckVRAM() (int64, error) {
  18. userLimit := os.Getenv("OLLAMA_MAX_VRAM")
  19. if userLimit != "" {
  20. avail, err := strconv.ParseInt(userLimit, 10, 64)
  21. if err != nil {
  22. return 0, fmt.Errorf("Invalid OLLAMA_MAX_VRAM setting %s: %s", userLimit, err)
  23. }
  24. slog.Info(fmt.Sprintf("user override OLLAMA_MAX_VRAM=%d", avail))
  25. return avail, nil
  26. }
  27. if runtime.GOARCH == "amd64" {
  28. // gpu not supported, this may not be metal
  29. return 0, nil
  30. }
  31. recommendedMaxVRAM := int64(C.getRecommendedMaxVRAM())
  32. return recommendedMaxVRAM, nil
  33. }
  34. func GetGPUInfo() GpuInfo {
  35. mem, _ := getCPUMem()
  36. if runtime.GOARCH == "amd64" {
  37. return GpuInfo{
  38. Library: "cpu",
  39. Variant: GetCPUVariant(),
  40. memInfo: mem,
  41. }
  42. }
  43. return GpuInfo{
  44. Library: "metal",
  45. memInfo: mem,
  46. }
  47. }
  48. func getCPUMem() (memInfo, error) {
  49. return memInfo{
  50. TotalMemory: 0,
  51. FreeMemory: 0,
  52. DeviceCount: 0,
  53. }, nil
  54. }