Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

Merge pull request #1819 from dhiltgen/multi_variant

Daniel Hiltgende2fbdec991a
diff --cc llm/llm.go
index 4f709538e,a414c3daa..d76676754
--- a/llm/llm.go
+++ b/llm/llm.go
@@@ -53,66 -73,64 +51,67 @@@ func New(workDir, model string, adapter
info := gpu.GetGPUInfo()
library := info.Library
+ switch runtime.GOOS {
+ case "darwin":
+ if opts.NumGPU == 0 {
+ break
+ }
- if opts.NumGPU == -1 {
- // default to offloading all layers
- opts.NumGPU = int(ggml.NumLayers()) + 1
- }
+ if size+kv+graph > vram {
+ log.Println("not enough vram available, falling back to CPU only")
+ opts.NumGPU = 0
+ break
+ }
+
+ opts.NumGPU = 1
+ default:
+ if library == "cpu" || library == "default" {
+ log.Println("GPU not available, falling back to CPU")
+ opts.NumGPU = 0
+ break
+ }
+
+ // don't use GPU at all if no layers are loaded
+ if opts.NumGPU == 0 {
+ library = "cpu"
+ break
+ }
+
+ // user-defined GPU count
+ if opts.NumGPU != -1 {
+ break
+ }
+
+ // the "main" GPU needs the most memory and determines the limit
+ // of how many layers can be loaded. It needs to fit:
+ // 1. the full compute graph allocation for all devices (graph)
+ // 2. the proportional kv cache for all devices (kv * % layers)
+ // 3. the proportional model (size * % layers / # devices)
+ // This estimates the number of layers
+ maxlayers := int64(ggml.NumLayers()) + 1
+ devices := int64(info.DeviceCount)
+ avg := vram / devices
+ layers := maxlayers * (avg - graph) / (kv + size/devices)
+ if layers > maxlayers {
+ layers = maxlayers
+ }
- // decide how many layers to put on the GPU
- if opts.NumGPU > 0 {
- switch runtime.GOOS {
- case "darwin":
- if requiredTotal > available {
- log.Println("not enough vram available, falling back to CPU only")
- opts.NumGPU = 0
- }
- default:
- if library == "cpu" || library == "default" {
- opts.NumGPU = 0
- break
- }
-
- // alloc buffer and kv cache is allocated as a fixed amount on the main gpu
- // TODO: find the largest GPU and only reserve memory there
- avgAvailable := available / int64(info.DeviceCount)
- if requiredAlloc > avgAvailable {
- log.Printf("not enough vram available, falling back to CPU only")
- library = "cpu"
- opts.NumGPU = 0
- break
- }
-
- // we don't know which GPU will be used, so estimate
- // the scratch buffer space on all of them
- // TODO: allocate less layers to the GPU with the scratch buffer
- // and more to the others (based on their available memory)
- available -= requiredAlloc * int64(info.DeviceCount)
-
- // no offloading required
- if requiredModel+requiredKv <= available {
- break
- }
-
- // fill remaining vram with layers
- log.Println("splitting", available, "of available memory bytes into layers")
- bytesPerLayer := int64((requiredModel + requiredKv) / int64(ggml.NumLayers()))
- log.Println("bytes per layer:", bytesPerLayer)
- layers := available / bytesPerLayer
- log.Println("total required with split:", requiredAlloc+(layers*bytesPerLayer))
- if layers < int64(opts.NumGPU) {
- opts.NumGPU = int(layers)
- }
+ // 1 + 2 must fit on the main gpu
+ min := graph + kv*layers/maxlayers
+ if layers <= 0 || min > avg {
+ log.Printf("not enough vram available, falling back to CPU only")
+ library = "cpu"
+ opts.NumGPU = 0
+ break
}
+
+ opts.NumGPU = int(layers)
}
- opts.NumGQA = 0
opts.RopeFrequencyBase = 0.0
opts.RopeFrequencyScale = 0.0
- return newLlmServer(library, model, adapters, projectors, opts)
+ gpuInfo := gpu.GetGPUInfo()
+ return newLlmServer(gpuInfo, model, adapters, projectors, opts)
}
// Give any native cgo implementations an opportunity to initialize