2023-11-14 09:20:34 +08:00
|
|
|
//go:build darwin
|
|
|
|
|
|
2023-11-30 03:00:37 +08:00
|
|
|
package gpu
|
2024-03-07 08:53:51 +08:00
|
|
|
|
2024-02-26 07:16:45 +08:00
|
|
|
/*
|
|
|
|
|
#cgo CFLAGS: -x objective-c
|
|
|
|
|
#cgo LDFLAGS: -framework Foundation -framework CoreGraphics -framework Metal
|
|
|
|
|
#include "gpu_info_darwin.h"
|
|
|
|
|
*/
|
2023-11-30 03:00:37 +08:00
|
|
|
import "C"
|
2023-11-14 09:20:34 +08:00
|
|
|
import (
|
2024-03-07 08:53:51 +08:00
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"os"
|
2023-12-20 05:32:24 +08:00
|
|
|
"runtime"
|
2024-03-07 08:53:51 +08:00
|
|
|
"strconv"
|
2023-11-14 09:20:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CheckVRAM returns the free VRAM in bytes on Linux machines with NVIDIA GPUs
|
2024-04-06 05:50:38 +08:00
|
|
|
func CheckVRAM() (uint64, error) {
|
2024-03-07 08:53:51 +08:00
|
|
|
userLimit := os.Getenv("OLLAMA_MAX_VRAM")
|
|
|
|
|
if userLimit != "" {
|
|
|
|
|
avail, err := strconv.ParseInt(userLimit, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("Invalid OLLAMA_MAX_VRAM setting %s: %s", userLimit, err)
|
|
|
|
|
}
|
|
|
|
|
slog.Info(fmt.Sprintf("user override OLLAMA_MAX_VRAM=%d", avail))
|
2024-04-06 05:50:38 +08:00
|
|
|
return uint64(avail), nil
|
2024-03-07 08:53:51 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-09 05:42:00 +08:00
|
|
|
if runtime.GOARCH == "amd64" {
|
|
|
|
|
// gpu not supported, this may not be metal
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
2024-04-06 05:50:38 +08:00
|
|
|
return uint64(C.getRecommendedMaxVRAM()), nil
|
2023-11-14 09:20:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-11-30 03:00:37 +08:00
|
|
|
func GetGPUInfo() GpuInfo {
|
2023-12-23 07:43:31 +08:00
|
|
|
mem, _ := getCPUMem()
|
2024-01-10 12:29:58 +08:00
|
|
|
if runtime.GOARCH == "amd64" {
|
|
|
|
|
return GpuInfo{
|
2024-01-12 06:43:16 +08:00
|
|
|
Library: "cpu",
|
2024-01-10 12:29:58 +08:00
|
|
|
Variant: GetCPUVariant(),
|
|
|
|
|
memInfo: mem,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-30 03:00:37 +08:00
|
|
|
return GpuInfo{
|
2024-01-10 12:29:58 +08:00
|
|
|
Library: "metal",
|
2023-12-23 07:43:31 +08:00
|
|
|
memInfo: mem,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCPUMem() (memInfo, error) {
|
|
|
|
|
return memInfo{
|
2023-11-30 03:00:37 +08:00
|
|
|
TotalMemory: 0,
|
|
|
|
|
FreeMemory: 0,
|
2024-01-10 04:53:33 +08:00
|
|
|
DeviceCount: 0,
|
2023-12-23 07:43:31 +08:00
|
|
|
}, nil
|
2023-11-30 03:00:37 +08:00
|
|
|
}
|