gpu_darwin.go 1.99 KB
Newer Older
1
2
//go:build darwin

3
package discover
4

5
6
/*
#cgo CFLAGS: -x objective-c
Michael Yang's avatar
Michael Yang committed
7
#cgo CPPFLAGS: -O3
8
9
10
#cgo LDFLAGS: -framework Foundation -framework CoreGraphics -framework Metal
#include "gpu_info_darwin.h"
*/
11
import "C"
Michael Yang's avatar
lint  
Michael Yang committed
12

13
import (
14
	"log/slog"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
15
	"runtime"
16
	"syscall"
17
18
19
20
21

	"github.com/ollama/ollama/format"
)

const (
Daniel Hiltgen's avatar
Daniel Hiltgen committed
22
	metalMinimumMemory = 512 * format.MebiByte
23
24
)

Daniel Hiltgen's avatar
Daniel Hiltgen committed
25
26
func GetGPUInfo() GpuInfoList {
	mem, _ := GetCPUMem()
27
	if runtime.GOARCH == "amd64" {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
28
29
30
31
32
		return []GpuInfo{
			{
				Library: "cpu",
				memInfo: mem,
			},
33
34
		}
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
35
	info := GpuInfo{
36
		Library: "metal",
Daniel Hiltgen's avatar
Daniel Hiltgen committed
37
		ID:      "0",
38
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
39
40
41
42
43
	info.TotalMemory = uint64(C.getRecommendedMaxVRAM())

	// TODO is there a way to gather actual allocated video memory? (currentAllocatedSize doesn't work)
	info.FreeMemory = info.TotalMemory

44
	info.MinimumMemory = metalMinimumMemory
Daniel Hiltgen's avatar
Daniel Hiltgen committed
45
	return []GpuInfo{info}
46
47
}

48
49
50
51
52
53
54
55
56
57
func GetCPUInfo() GpuInfoList {
	mem, _ := GetCPUMem()
	return []GpuInfo{
		{
			Library: "cpu",
			memInfo: mem,
		},
	}
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
58
func GetCPUMem() (memInfo, error) {
59
	return memInfo{
60
		TotalMemory: uint64(C.getPhysicalMemory()),
61
		FreeMemory:  uint64(C.getFreeMemory()),
62
		// FreeSwap omitted as Darwin uses dynamic paging
63
	}, nil
64
}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
65
66
67
68
69

func (l GpuInfoList) GetVisibleDevicesEnv() (string, string) {
	// No-op on darwin
	return "", ""
}
70
71
72

func GetSystemInfo() SystemInfo {
	mem, _ := GetCPUMem()
73
74
75
76
77
78
79
80
81
82
83
84
	query := "hw.perflevel0.physicalcpu"
	perfCores, err := syscall.SysctlUint32(query)
	if err != nil {
		slog.Warn("failed to discover physical CPU details", "query", query, "error", err)
	}
	query = "hw.perflevel1.physicalcpu"
	efficiencyCores, _ := syscall.SysctlUint32(query) // On x86 xeon this wont return data

	// Determine thread count
	query = "hw.logicalcpu"
	logicalCores, _ := syscall.SysctlUint32(query)

85
86
87
88
89
	return SystemInfo{
		System: CPUInfo{
			GpuInfo: GpuInfo{
				memInfo: mem,
			},
90
91
92
93
94
95
96
			CPUs: []CPU{
				{
					CoreCount:           int(perfCores + efficiencyCores),
					EfficiencyCoreCount: int(efficiencyCores),
					ThreadCount:         int(logicalCores),
				},
			},
97
98
99
100
		},
		GPUs: GetGPUInfo(),
	}
}