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

3
package discover
4

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

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

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

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

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

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

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

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

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

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

func GetSystemInfo() SystemInfo {
	mem, _ := GetCPUMem()
72
73
74
75
76
77
78
79
80
81
82
83
	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)

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