gpu_darwin.go 2.1 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

	"github.com/ollama/ollama/format"
18
	"github.com/ollama/ollama/runners"
19
20
21
)

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
		return []GpuInfo{
			{
				Library: "cpu",
31
				Variant: runners.GetCPUCapability().String(),
Daniel Hiltgen's avatar
Daniel Hiltgen committed
32
33
				memInfo: mem,
			},
34
35
		}
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
36
	info := GpuInfo{
37
		Library: "metal",
Daniel Hiltgen's avatar
Daniel Hiltgen committed
38
		ID:      "0",
39
	}
Daniel Hiltgen's avatar
Daniel Hiltgen committed
40
41
42
43
44
	info.TotalMemory = uint64(C.getRecommendedMaxVRAM())

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

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

49
50
51
52
53
func GetCPUInfo() GpuInfoList {
	mem, _ := GetCPUMem()
	return []GpuInfo{
		{
			Library: "cpu",
54
			Variant: runners.GetCPUCapability().String(),
55
56
57
58
59
			memInfo: mem,
		},
	}
}

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

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

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

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