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

3
package gpu
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
		return []GpuInfo{
			{
				Library: "cpu",
30
				Variant: GetCPUCapability().String(),
Daniel Hiltgen's avatar
Daniel Hiltgen committed
31
32
				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
func GetCPUInfo() GpuInfoList {
	mem, _ := GetCPUMem()
	return []GpuInfo{
		{
			Library: "cpu",
53
			Variant: GetCPUCapability().String(),
54
55
56
57
58
			memInfo: mem,
		},
	}
}

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

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

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

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