gpu_darwin.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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() (uint64, 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 uint64(avail), nil
  26. }
  27. if runtime.GOARCH == "amd64" {
  28. // gpu not supported, this may not be metal
  29. return 0, nil
  30. }
  31. return uint64(C.getRecommendedMaxVRAM()), nil
  32. }
  33. func GetGPUInfo() GpuInfo {
  34. mem, _ := getCPUMem()
  35. if runtime.GOARCH == "amd64" {
  36. return GpuInfo{
  37. Library: "cpu",
  38. Variant: GetCPUVariant(),
  39. memInfo: mem,
  40. }
  41. }
  42. return GpuInfo{
  43. Library: "metal",
  44. memInfo: mem,
  45. }
  46. }
  47. func getCPUMem() (memInfo, error) {
  48. return memInfo{
  49. TotalMemory: 0,
  50. FreeMemory: 0,
  51. DeviceCount: 0,
  52. }, nil
  53. }