mirror of
https://github.com/ollama/ollama.git
synced 2026-06-05 21:05:00 +08:00
* broad lint fixes to sidestep CI scope glitch * runner: Remove CGO engines, use llama-server exclusively for GGML models Remove the vendored GGML and llama.cpp backend, CGO runner, Go model implementations, and sample. llama-server (built from upstream llama.cpp via FetchContent) is now the sole inference engine for GGUF-based models. (Safetensor based models continue to run on the new MLX engine.) This allows us to more rapidly pick up new capabilities and fixes from llama.cpp as they come out. On windows this now requires recent AMD driver versions to support ROCm v7 as llama.cpp currently does not support building against v6. * llama/compat: load Ollama-format GGUFs in llama-server Squashed from upstream/jmorganca/llama-compat on 2026-04-29. Source tip:0c33775d37. Original source commits: -25223160dllama/compat: add in-memory shim so llama-server can load Ollama-format GGUFs -7449b539allm,server: route Ollama-format gemma3 blobs through llama/compat -436f2e2b1llama/compat: make patch-apply idempotent -8c2c9d4c8llama/compat: extend gemma3 handler to cover 1B and 270M blobs -021389f7bllama/compat: shrink clip.cpp injection from 18 lines to 1 -61b367ec2llama/compat: shrink patch to pure call-site hooks (34 -> 20 lines) -36049361cllama/compat: simplify shim (gemma3-tested) -8fa664865llama/compat: add qwen35moe text handler -db0c74530llama/compat: add qwen35moe vision (clip) support -2a388da77llama/compat: split shared infra into a util TU -9a69a17dcllama/compat: document non-public API dependencies -d0f38a915llama/compat: add gpt-oss and lfm2 handlers -086071822llama/compat: add mistral3 text handler (vision TODO) -63bde9ff7llama/compat: add mistral3 vision (clip) support -3a57b89d5llama/compat: apply LLaMA RoPE permute to mistral3 vision Q/K -99cb87439llama/compat: add qwen35, gemma4, deepseek-ocr handlers -2c7850dballama/compat: add nemotron_h_moe handler (latent FFN + MTP skip) -9e3b54225llama/compat: add llama4 text + clip handlers -034fee349llama/compat: add gemma4 clip handler (gemma4v projector) -9945c5a93server: remove dhiltgen/* compat redirect table -5d4539101llama/compat: rewrite gemma4 tokenizer model to BPE -7e0765327llama/compat: add glm-ocr text handler + text-loader load-op hook -f1bd1a25allama/compat: add glm-ocr clip handler (glm4v projector) -4b5cf3420llama/compat: collapse text-loader hook back to one new patch line -eb4ecf4fcllama/compat: extend gemma4 clip handler to gemma4a (audio) -a23a5e76fllama/compat: fix gemma4a per-block norm tensor mapping -cd2dcaff4llama/compat: add embeddinggemma handler -1ce8a6b26llama/compat: add qwen3-vl + qwen2.5-vl handlers -fd98ffa1ellama/compat: add gemma3n + glm4moelite handlers -cc7bdf0bcllama/compat: handle null buft in maybe_load_tensor -0c33775d3llama/compat: disable mmap when load_op transforms text-side tensors * refine implementation * ci: fix windows MLX build * ci: fix windows llama-server build * ci: fix windows rocm build * ci: windows mlx tuning Shorten long-tail on build, and get OllamaSetup.exe back under 2g limit * ci: fix windows dependencies * win: fix dependency gathering * disable openmp * win: arm64 cross-compile build also DRY out CI steps * scheduler improvements * ci: improvements from #15982 * win: favor ninja for faster developer builds * win: fix build * win: fix arm64 cross-compile * win: avoid spaces in compiler path * misc discovery fixes, and bos handling * lint fixes * win: fix arm cross-compile build/CI bugs * llama.cpp update * win: handle multiple CRT dirs * vulkan: add windows iGPU detection * fix creation bugs for patched models, other refactoring work * tune batch size for better performance * ci and lint fixes * fix repeat_last_n bug * build: revamp build for better developer UX * amd, sampler, qwen3next fixes * version bump * fix mlx build * revamp GPU discovery Scanning the output of llama-server is turning out to be too error prone across llama.cpp updates, so this switches to a thin dynamic library load against the bundled GGML libraries so more details can be gathered from the API. * version bump * missing file * ci: fix cache miss on rocm build * refine vulkan dep handling * fix ps reporting bug on full GPU load * improve cmake wiring for customized local builds * version bump * docker build arg cleanup * improve windows exit error logs * fix community gemma4 support and ci flakes * fix mlx unit test * tighten up ps logic to avoid double counting fit log lines * version bump * fix ps view for full gpu layer offload * add MTP wiring for llama-server and create with GGUFs * pick best template by capabilities * version bump * ci: harden apt repos * remove unused cpu core discovery * adjust batch default logic to reduce OOMs * support larger tool calls * fix audio support, template show * qwen35 mtp patch support * flesh out dtypes * rocm deps * version bump * lint fix * block broken gfx1150 on windows * fix qwen3.5 moe mtp tensors in patch * mmproj oom fallback and vulkan on by default * qwen MTP compat fix * version bump * ci: fix WoA cross-compile * ci: workaround ui tool in cross-compile * version bump * win: enable OpenMP for CPU builds * build: improve developer UX * ci: windows path workaround for CPU build * win: fix WoA dependencies * win: fix large offset reads for mmproj patched loads * version bump * fix vulkan dup detection * add OLLAMA_IGPU_ENABLE and largely disable iGPUs by default * opt-in MTP, win large offset, integraton fixes * fix unit test scheduler interaction hang * fix multi-gpu filtering * version bump * review comments * fix thinking level * fix linux rocm ordering and granite 3.3 template * version bump * ci fix - non-shallow MLX checkout * bypass linux sysfs unit test on windows --------- Co-authored-by: jmorganca <jmorganca@gmail.com>
861 lines
22 KiB
Go
861 lines
22 KiB
Go
package ml
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"hash/maphash"
|
|
"io"
|
|
"log/slog"
|
|
"math"
|
|
"net/http"
|
|
"os"
|
|
"runtime"
|
|
"slices"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/ollama/ollama/format"
|
|
"github.com/ollama/ollama/logutil"
|
|
)
|
|
|
|
// GPULayers is a set of layers to be allocated on a single GPU
|
|
type GPULayers struct {
|
|
DeviceID
|
|
|
|
// Layers is a set of layer indicies to load
|
|
Layers []int
|
|
}
|
|
|
|
// FirstLayer returns the smallest layer index scheduled on this GPU, or MaxInt when empty.
|
|
func (g GPULayers) FirstLayer() int {
|
|
if len(g.Layers) == 0 {
|
|
return math.MaxInt
|
|
}
|
|
|
|
first := g.Layers[0]
|
|
for i := 1; i < len(g.Layers); i++ {
|
|
if g.Layers[i] < first {
|
|
first = g.Layers[i]
|
|
}
|
|
}
|
|
|
|
return first
|
|
}
|
|
|
|
func (g GPULayers) String() string {
|
|
if len(g.Layers) == 0 {
|
|
return ""
|
|
}
|
|
|
|
slices.Sort(g.Layers)
|
|
|
|
contiguous := true
|
|
base := g.Layers[0]
|
|
for i := range g.Layers {
|
|
if g.Layers[i] != base+i {
|
|
contiguous = false
|
|
break
|
|
}
|
|
}
|
|
|
|
if contiguous {
|
|
return fmt.Sprintf("ID:%v Layers:%v(%v..%v)", g.ID, len(g.Layers), g.Layers[0], g.Layers[len(g.Layers)-1])
|
|
} else {
|
|
return fmt.Sprintf("ID:%v Layers:%v%v", g.ID, len(g.Layers), g.Layers)
|
|
}
|
|
}
|
|
|
|
// GPULayersList is a set of layer allocations across multiple GPUs
|
|
type GPULayersList []GPULayers
|
|
|
|
func (l GPULayersList) Len() int { return len(l) }
|
|
func (l GPULayersList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
// Sort by the ordering of the layers offloaded
|
|
func (l GPULayersList) Less(i, j int) bool {
|
|
li := l[i].FirstLayer()
|
|
lj := l[j].FirstLayer()
|
|
|
|
return li < lj
|
|
}
|
|
|
|
func (l GPULayersList) String() string {
|
|
if l.Sum() > 0 {
|
|
return fmt.Sprintf("%v%v", l.Sum(), []GPULayers(l))
|
|
} else {
|
|
return fmt.Sprintf("%v", []GPULayers(l))
|
|
}
|
|
}
|
|
|
|
// Sum is the total number of layers assigned across all GPUs
|
|
func (l GPULayersList) Sum() int {
|
|
var sum int
|
|
|
|
for _, g := range l {
|
|
sum += len(g.Layers)
|
|
}
|
|
|
|
return sum
|
|
}
|
|
|
|
var h maphash.Hash
|
|
|
|
// Hash is an identifier of this layer assignment
|
|
func (l GPULayersList) Hash() uint64 {
|
|
h.Reset()
|
|
for _, g := range l {
|
|
if len(g.Layers) > 0 {
|
|
h.WriteString(g.ID + g.Library)
|
|
for _, l := range g.Layers {
|
|
binary.Write(&h, binary.NativeEndian, int64(l))
|
|
}
|
|
}
|
|
}
|
|
|
|
return h.Sum64()
|
|
}
|
|
|
|
// ErrNoMem is returned when panicing due to insufficient memory. It includes
|
|
// the attempted memory allocation.
|
|
type ErrNoMem struct {
|
|
BackendMemory
|
|
}
|
|
|
|
func (e ErrNoMem) Error() string {
|
|
return fmt.Sprintf("insufficient memory - required allocations: %+v", e.BackendMemory)
|
|
}
|
|
|
|
// Minimal unique device identification
|
|
type DeviceID struct {
|
|
// ID is an identifier for the device for matching with system
|
|
// management libraries. The ID is only unique for other devices
|
|
// using the same Library.
|
|
// This ID represents a "post filtered" view of the enumerated devices
|
|
// if the ID is numeric
|
|
ID string `json:"id"`
|
|
|
|
// Library identifies which library is used for the device (e.g. CUDA, ROCm, etc.)
|
|
Library string `json:"backend,omitempty"`
|
|
}
|
|
|
|
// DeviceMemory provides a breakdown of the memory needed
|
|
// per device, such as a CPU or GPU.
|
|
type DeviceMemory struct {
|
|
DeviceID
|
|
|
|
// Name is the name of the device as labeled by the backend. It
|
|
// may not be persistent across instances of the runner.
|
|
Name string
|
|
|
|
// Weights is the per-layer memory needed for the model weights.
|
|
Weights []uint64
|
|
|
|
// Cache is the per-layer memory needed for the KV cache.
|
|
Cache []uint64
|
|
|
|
// Graph is the size of the compute graph. It is not per-layer.
|
|
Graph uint64
|
|
}
|
|
|
|
func sumMemory(mem []uint64) uint64 {
|
|
var sum uint64
|
|
|
|
for _, m := range mem {
|
|
sum += m
|
|
}
|
|
|
|
return sum
|
|
}
|
|
|
|
// Size returns the total size of the memory required by this device
|
|
func (m DeviceMemory) Size() uint64 {
|
|
return sumMemory(m.Weights) + sumMemory(m.Cache) + m.Graph
|
|
}
|
|
|
|
func memoryPresent(mem []uint64) bool {
|
|
return slices.ContainsFunc(mem, func(m uint64) bool { return m != 0 })
|
|
}
|
|
|
|
func (m DeviceMemory) LogValue() slog.Value {
|
|
var attrs []slog.Attr
|
|
if memoryPresent(m.Weights) {
|
|
attrs = append(attrs, slog.Any("Weights", m.Weights))
|
|
}
|
|
|
|
if memoryPresent(m.Cache) {
|
|
attrs = append(attrs, slog.Any("Cache", m.Cache))
|
|
}
|
|
|
|
if m.Graph != 0 {
|
|
attrs = append(attrs, slog.Any("Graph", m.Graph))
|
|
}
|
|
|
|
if len(attrs) > 0 && m.ID != "" {
|
|
attrs = append([]slog.Attr{slog.String("ID", m.ID)}, attrs...)
|
|
}
|
|
|
|
return slog.GroupValue(attrs...)
|
|
}
|
|
|
|
// BackendMemory provides the amount of memory required to load the model
|
|
// per device based on the BackendParams. In some cases, not all required
|
|
// allocations will be known at this point. However, the size of the most recent
|
|
// allocation is guaranteed to be provided so that if it failed, the caller can
|
|
// accommodate that to make forward progress.
|
|
type BackendMemory struct {
|
|
// InputWeights are always located on the CPU and cannot be moved
|
|
InputWeights uint64
|
|
|
|
// CPU model components are located in system memory. This does not
|
|
// include unified memory allocated through the GPU.
|
|
CPU DeviceMemory
|
|
|
|
// GPU model components are located on one or more GPUs.
|
|
GPUs []DeviceMemory
|
|
}
|
|
|
|
func (m BackendMemory) LogValue() slog.Value {
|
|
var attrs []slog.Attr
|
|
if m.InputWeights != 0 {
|
|
attrs = append(attrs, slog.Any("InputWeights", m.InputWeights))
|
|
}
|
|
|
|
attrs = append(attrs, slog.Any(m.CPU.Name, m.CPU))
|
|
for _, g := range m.GPUs {
|
|
attrs = append(attrs, slog.Any(g.Name, g))
|
|
}
|
|
|
|
return slog.GroupValue(attrs...)
|
|
}
|
|
|
|
// Log prints a high level summary of the memory
|
|
func (m BackendMemory) Log(level slog.Level) {
|
|
var total uint64
|
|
|
|
for _, gpu := range m.GPUs {
|
|
if sum := sumMemory(gpu.Weights); sum > 0 {
|
|
slog.Log(context.TODO(), level, "model weights", "device", gpu.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
}
|
|
if sum := m.InputWeights + sumMemory(m.CPU.Weights); sum > 0 {
|
|
slog.Log(context.TODO(), level, "model weights", "device", m.CPU.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
|
|
for _, gpu := range m.GPUs {
|
|
if sum := sumMemory(gpu.Cache); sum > 0 {
|
|
slog.Log(context.TODO(), level, "kv cache", "device", gpu.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
}
|
|
if sum := sumMemory(m.CPU.Cache); sum > 0 {
|
|
slog.Log(context.TODO(), level, "kv cache", "device", m.CPU.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
|
|
for _, gpu := range m.GPUs {
|
|
if sum := gpu.Graph; sum > 0 {
|
|
slog.Log(context.TODO(), level, "compute graph", "device", gpu.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
}
|
|
if sum := m.CPU.Graph; sum > 0 {
|
|
slog.Log(context.TODO(), level, "compute graph", "device", m.CPU.Name, "size", format.HumanBytes2(sum))
|
|
total += sum
|
|
}
|
|
|
|
if total > 0 {
|
|
slog.Log(context.TODO(), level, "total memory", "size", format.HumanBytes2(total))
|
|
}
|
|
}
|
|
|
|
type DeviceInfo struct {
|
|
DeviceID
|
|
|
|
// Name is the name of the device as labeled by the backend. It
|
|
// may not be persistent across instances of the runner.
|
|
Name string `json:"name"`
|
|
|
|
// Description is the longer user-friendly identification of the device
|
|
Description string `json:"description"`
|
|
|
|
// FilterID is populated with the unfiltered device ID if a numeric ID is used
|
|
// so the device can be included.
|
|
FilterID string `json:"filter_id,omitempty"`
|
|
|
|
// Integrated is set true for integrated GPUs, false for Discrete GPUs
|
|
Integrated bool `json:"integration,omitempty"`
|
|
|
|
// PCIID is the bus, device and domain ID of the device for deduplication
|
|
// when discovered by multiple backends
|
|
PCIID string `json:"pci_id,omitempty"`
|
|
|
|
// TotalMemory is the total amount of memory the device can use for loading models
|
|
TotalMemory uint64 `json:"total_memory"`
|
|
|
|
// FreeMemory is the amount of memory currently available on the device for loading models
|
|
FreeMemory uint64 `json:"free_memory,omitempty"`
|
|
|
|
// ComputeMajor is the major version of capabilities of the device
|
|
// if unsupported by the backend, -1 will be returned
|
|
ComputeMajor int
|
|
|
|
// ComputeMinor is the minor version of capabilities of the device
|
|
// if unsupported by the backend, -1 will be returned
|
|
ComputeMinor int
|
|
|
|
// Driver Information
|
|
DriverMajor int `json:"driver_major,omitempty"`
|
|
DriverMinor int `json:"driver_minor,omitempty"`
|
|
|
|
// NVIDIADriverMajor is the NVIDIA kernel driver branch. CUDA driver APIs
|
|
// expose a separate CUDA compatibility version, so keep this distinct.
|
|
NVIDIADriverMajor int `json:"-"`
|
|
|
|
// GFXTarget is the AMD GPU gfx target string (e.g. "gfx1100") for ROCm
|
|
// device validation. Empty on non-AMD devices.
|
|
GFXTarget string `json:"gfx_target,omitempty"`
|
|
|
|
// Where backends were loaded from
|
|
LibraryPath []string
|
|
|
|
// RunnerEnvOverrides stores exceptional per-device runner environment
|
|
// overrides discovered during bootstrap. This is internal server state and
|
|
// is not serialized.
|
|
RunnerEnvOverrides map[string]string `json:"-"`
|
|
}
|
|
|
|
type SystemInfo struct {
|
|
// TotalMemory is the total amount of system memory
|
|
TotalMemory uint64 `json:"total_memory,omitempty"`
|
|
|
|
// FreeMemory is the amount of memory currently available on the system for loading models
|
|
FreeMemory uint64 `json:"free_memory,omitempty"`
|
|
|
|
// FreeSwap is the amount of system swap space reported as available
|
|
FreeSwap uint64 `json:"free_swap,omitempty"`
|
|
}
|
|
|
|
func (d DeviceInfo) Compute() string {
|
|
// AMD gfx is encoded into the major minor in hex form
|
|
if strings.EqualFold(d.Library, "ROCm") {
|
|
return fmt.Sprintf("gfx%x%02x", d.ComputeMajor, d.ComputeMinor)
|
|
}
|
|
return strconv.Itoa(d.ComputeMajor) + "." + strconv.Itoa(d.ComputeMinor)
|
|
}
|
|
|
|
func (d DeviceInfo) Driver() string {
|
|
return strconv.Itoa(d.DriverMajor) + "." + strconv.Itoa(d.DriverMinor)
|
|
}
|
|
|
|
// MinimumMemory reports the amount of memory that should be set aside
|
|
// on the device for overhead (e.g. VRAM consumed by context structures independent
|
|
// of model allocations)
|
|
func (d DeviceInfo) MinimumMemory() uint64 {
|
|
if d.Library == "Metal" {
|
|
return 512 * format.MebiByte
|
|
}
|
|
return 457 * format.MebiByte
|
|
}
|
|
|
|
// Sort by Free Space.
|
|
// iGPUs are reported first, thus Reverse() yields the largest discrete GPU first
|
|
type ByFreeMemory []DeviceInfo
|
|
|
|
func (a ByFreeMemory) Len() int { return len(a) }
|
|
func (a ByFreeMemory) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
func (a ByFreeMemory) Less(i, j int) bool {
|
|
if a[i].Integrated && !a[j].Integrated {
|
|
return true
|
|
} else if !a[i].Integrated && a[j].Integrated {
|
|
return false
|
|
}
|
|
return a[i].FreeMemory < a[j].FreeMemory
|
|
}
|
|
|
|
// ByPerformance groups devices by similar speed
|
|
func ByPerformance(l []DeviceInfo) [][]DeviceInfo {
|
|
resp := [][]DeviceInfo{}
|
|
scores := []bool{}
|
|
for _, info := range l {
|
|
found := false
|
|
requested := info.Integrated
|
|
for i, score := range scores {
|
|
if score == requested {
|
|
resp[i] = append(resp[i], info)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
scores = append(scores, requested)
|
|
resp = append(resp, []DeviceInfo{info})
|
|
}
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func ByLibrary(l []DeviceInfo) [][]DeviceInfo {
|
|
resp := [][]DeviceInfo{}
|
|
libs := []string{}
|
|
for _, info := range l {
|
|
found := false
|
|
requested := info.Library
|
|
for i, lib := range libs {
|
|
if lib == requested {
|
|
resp[i] = append(resp[i], info)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
libs = append(libs, requested)
|
|
resp = append(resp, []DeviceInfo{info})
|
|
}
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func LibraryPaths(l []DeviceInfo) []string {
|
|
gpuLibs := []string{LibOllamaPath}
|
|
for _, gpu := range l {
|
|
for _, dir := range gpu.LibraryPath {
|
|
needed := true
|
|
for _, existing := range gpuLibs {
|
|
if dir == existing {
|
|
needed = false
|
|
break
|
|
}
|
|
}
|
|
if needed {
|
|
gpuLibs = append(gpuLibs, dir)
|
|
}
|
|
}
|
|
}
|
|
return gpuLibs
|
|
}
|
|
|
|
type DeviceComparison int
|
|
|
|
const (
|
|
UniqueDevice DeviceComparison = iota
|
|
SameBackendDevice // The device is the same, and the library/backend is the same
|
|
DuplicateDevice // The same physical device but different library/backend (overlapping device)
|
|
)
|
|
|
|
func (a DeviceInfo) Compare(b DeviceInfo) DeviceComparison {
|
|
if a.PCIID != "" && b.PCIID != "" {
|
|
if !strings.EqualFold(a.PCIID, b.PCIID) {
|
|
return UniqueDevice
|
|
}
|
|
if a.Library == b.Library {
|
|
return SameBackendDevice
|
|
}
|
|
return DuplicateDevice
|
|
}
|
|
if likelyVulkanDuplicate(a, b) {
|
|
return DuplicateDevice
|
|
}
|
|
if a.PCIID != b.PCIID {
|
|
return UniqueDevice
|
|
}
|
|
// If PCIID is empty, we have to use ID + library for uniqueness
|
|
if a.PCIID == "" && a.DeviceID != b.DeviceID {
|
|
return UniqueDevice
|
|
}
|
|
if a.Library == b.Library {
|
|
return SameBackendDevice
|
|
}
|
|
return DuplicateDevice
|
|
}
|
|
|
|
func likelyVulkanDuplicate(a, b DeviceInfo) bool {
|
|
if a.Library == b.Library {
|
|
return false
|
|
}
|
|
vulkan, other := a, b
|
|
if b.Library == "Vulkan" {
|
|
vulkan, other = b, a
|
|
}
|
|
if vulkan.Library != "Vulkan" {
|
|
return false
|
|
}
|
|
if other.Library != "CUDA" && other.Library != "ROCm" {
|
|
return false
|
|
}
|
|
if normalizeDeviceDescription(vulkan.Description) == "" {
|
|
return false
|
|
}
|
|
if !SimilarDeviceDescription(vulkan.Description, other.Description) {
|
|
return false
|
|
}
|
|
return SimilarDeviceMemory(vulkan.TotalMemory, other.TotalMemory)
|
|
}
|
|
|
|
// SimilarDeviceDescription reports whether two backend device descriptions are
|
|
// close enough to identify the same physical GPU across different libraries.
|
|
func SimilarDeviceDescription(a, b string) bool {
|
|
normalizedA := normalizeDeviceDescription(a)
|
|
return normalizedA != "" && normalizedA == normalizeDeviceDescription(b)
|
|
}
|
|
|
|
func normalizeDeviceDescription(s string) string {
|
|
s = strings.ToLower(strings.TrimSpace(s))
|
|
var b strings.Builder
|
|
depth := 0
|
|
for _, r := range s {
|
|
switch {
|
|
case r == '(':
|
|
depth++
|
|
continue
|
|
case r == ')':
|
|
if depth > 0 {
|
|
depth--
|
|
continue
|
|
}
|
|
case depth > 0:
|
|
continue
|
|
case r >= 'a' && r <= 'z' || r >= '0' && r <= '9':
|
|
b.WriteRune(r)
|
|
default:
|
|
b.WriteByte(' ')
|
|
}
|
|
}
|
|
return strings.Join(strings.Fields(b.String()), " ")
|
|
}
|
|
|
|
func SimilarDeviceMemory(a, b uint64) bool {
|
|
if a == 0 || b == 0 {
|
|
return false
|
|
}
|
|
maxMemory := max(a, b)
|
|
tolerance := maxMemory / 20
|
|
if tolerance < 512*1024*1024 {
|
|
tolerance = 512 * 1024 * 1024
|
|
}
|
|
return maxMemory-min(a, b) <= tolerance
|
|
}
|
|
|
|
// For a SameBackendDevice, return true if b is better than a
|
|
// e.g. newer GPU library version
|
|
func (a DeviceInfo) IsBetter(b DeviceInfo) bool {
|
|
aLib := a.LibraryPath[len(a.LibraryPath)-1]
|
|
bLib := b.LibraryPath[len(b.LibraryPath)-1]
|
|
if aLib == bLib {
|
|
return false
|
|
}
|
|
aLibSplit := strings.SplitN(aLib, "_", 2)
|
|
bLibSplit := strings.SplitN(bLib, "_", 2)
|
|
if len(aLibSplit) < 2 || len(bLibSplit) < 2 {
|
|
return false
|
|
}
|
|
if aLibSplit[0] != bLibSplit[0] {
|
|
slog.Debug("unexpected libraries", "a", aLib, "b", bLib)
|
|
return false
|
|
}
|
|
if aLibSplit[1] == bLibSplit[1] {
|
|
return false
|
|
}
|
|
cmp := []string{aLibSplit[1], bLibSplit[1]}
|
|
sort.Sort(sort.Reverse(sort.StringSlice(cmp)))
|
|
return cmp[0] == bLibSplit[1]
|
|
}
|
|
|
|
// FlashAttentionSupported reports whether flash attention can be used across
|
|
// all selected devices.
|
|
func FlashAttentionSupported(l []DeviceInfo) bool {
|
|
for _, gpu := range l {
|
|
supportsFA := gpu.Library == "cpu" ||
|
|
gpu.Name == "Metal" || gpu.Library == "Metal" ||
|
|
cudaFlashAttentionSupported(gpu) ||
|
|
gpu.Library == "ROCm" ||
|
|
gpu.Library == "Vulkan"
|
|
|
|
if !supportsFA {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func cudaFlashAttentionSupported(gpu DeviceInfo) bool {
|
|
if gpu.Library != "CUDA" ||
|
|
gpu.ComputeMajor < 7 ||
|
|
(gpu.ComputeMajor == 7 && gpu.ComputeMinor == 2) {
|
|
return false
|
|
}
|
|
|
|
if gpu.DriverMajor == 0 {
|
|
slog.Warn("CUDA driver version unavailable; allowing flash attention based on compute capability",
|
|
"device", gpu.Description, "compute", gpu.Compute())
|
|
return true
|
|
}
|
|
|
|
return gpu.DriverMajor >= 7
|
|
}
|
|
|
|
type FlashAttentionType int32
|
|
|
|
const (
|
|
// Aligned with llama_flash_attn_type
|
|
FlashAttentionAuto FlashAttentionType = -1
|
|
FlashAttentionDisabled FlashAttentionType = 0
|
|
FlashAttentionEnabled FlashAttentionType = 1
|
|
)
|
|
|
|
func (f FlashAttentionType) LogValue() slog.Value {
|
|
return slog.AnyValue(f.String())
|
|
}
|
|
|
|
func (f FlashAttentionType) String() string {
|
|
switch f {
|
|
case FlashAttentionAuto:
|
|
return "Auto"
|
|
case FlashAttentionDisabled:
|
|
return "Disabled"
|
|
case FlashAttentionEnabled:
|
|
return "Enabled"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
// Given the list of GPUs this instantiation is targeted for,
|
|
// figure out the device environment variables and any recorded
|
|
// per-device runner environment overrides.
|
|
func GetDevicesEnv(l []DeviceInfo) map[string]string {
|
|
if len(l) == 0 {
|
|
return nil
|
|
}
|
|
// CUDA-only groups need filtering so devices removed during discovery do
|
|
// not reappear in the child process.
|
|
mustFilter := len(l) == 1 || allDevicesUseLibrary(l, "CUDA")
|
|
env := map[string]string{}
|
|
for _, d := range l {
|
|
d.updateVisibleDevicesEnv(env, mustFilter)
|
|
for k, v := range d.RunnerEnvOverrides {
|
|
if existing, ok := env[k]; ok && existing != v {
|
|
slog.Warn("conflicting device environment override", "key", k, "existing", existing, "new", v, "library", d.Library, "id", d.ID)
|
|
}
|
|
env[k] = v
|
|
}
|
|
}
|
|
|
|
return env
|
|
}
|
|
|
|
func allDevicesUseLibrary(l []DeviceInfo, library string) bool {
|
|
for _, d := range l {
|
|
if d.Library != library {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// NeedsInitValidation returns true if the device in question has the potential
|
|
// to crash at inference time and requires deeper validation before we include
|
|
// it in the supported devices list.
|
|
func (d DeviceInfo) NeedsInitValidation() bool {
|
|
// ROCm: rocblas will crash on unsupported devices.
|
|
// CUDA: verify CC is supported by the version of the library
|
|
return d.Library == "ROCm" || d.Library == "CUDA"
|
|
}
|
|
|
|
// Set the init validation environment variable
|
|
func (d DeviceInfo) AddInitValidation(env map[string]string) {
|
|
env["GGML_CUDA_INIT"] = "1" // force deep initialization to trigger crash on unsupported GPUs
|
|
}
|
|
|
|
// PreferredLibrary returns true if this library is preferred over the other input
|
|
// library
|
|
// Used to filter out Vulkan in favor of CUDA or ROCm
|
|
func (d DeviceInfo) PreferredLibrary(other DeviceInfo) bool {
|
|
// TODO in the future if we find Vulkan is better than ROCm on some devices
|
|
// that implementation can live here.
|
|
|
|
if d.Library == "CUDA" || d.Library == "ROCm" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (d DeviceInfo) updateVisibleDevicesEnv(env map[string]string, mustFilter bool) {
|
|
var envVar string
|
|
var rocmOrdinalEnv string
|
|
switch d.Library {
|
|
case "ROCm":
|
|
// ROCm must be filtered as it can crash the runner on unsupported devices
|
|
envVar = "ROCR_VISIBLE_DEVICES"
|
|
if runtime.GOOS != "linux" {
|
|
envVar = rocmNonLinuxVisibleDevicesEnv()
|
|
} else {
|
|
rocmOrdinalEnv = rocmLinuxOrdinalVisibleDevicesEnv()
|
|
}
|
|
case "CUDA":
|
|
if !mustFilter {
|
|
// By default we try to avoid filtering CUDA devices because ROCm also
|
|
// looks at the CUDA env var, and gets confused in mixed-vendor environments.
|
|
return
|
|
}
|
|
envVar = "CUDA_VISIBLE_DEVICES"
|
|
case "Vulkan":
|
|
if !mustFilter {
|
|
return
|
|
}
|
|
envVar = "GGML_VK_VISIBLE_DEVICES"
|
|
default:
|
|
return
|
|
}
|
|
v, existing := env[envVar]
|
|
childOrdinal := visibleDeviceCount(v)
|
|
if existing {
|
|
v = v + ","
|
|
}
|
|
if d.FilterID != "" {
|
|
v = v + d.FilterID
|
|
} else {
|
|
v = v + d.ID
|
|
}
|
|
env[envVar] = v
|
|
|
|
if rocmOrdinalEnv != "" {
|
|
v, existing = env[rocmOrdinalEnv]
|
|
if existing {
|
|
v = v + ","
|
|
}
|
|
v = v + strconv.Itoa(childOrdinal)
|
|
env[rocmOrdinalEnv] = v
|
|
}
|
|
}
|
|
|
|
func visibleDeviceCount(value string) int {
|
|
count := 0
|
|
for _, field := range strings.Split(value, ",") {
|
|
if strings.TrimSpace(field) != "" {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func rocmLinuxOrdinalVisibleDevicesEnv() string {
|
|
if runtime.GOOS != "linux" || os.Getenv("ROCR_VISIBLE_DEVICES") != "" {
|
|
return ""
|
|
}
|
|
for _, name := range []string{"HIP_VISIBLE_DEVICES", "GPU_DEVICE_ORDINAL", "CUDA_VISIBLE_DEVICES"} {
|
|
if numericVisibleDeviceList(os.Getenv(name)) {
|
|
return name
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func rocmNonLinuxVisibleDevicesEnv() string {
|
|
for _, name := range []string{"HIP_VISIBLE_DEVICES", "GPU_DEVICE_ORDINAL", "CUDA_VISIBLE_DEVICES"} {
|
|
if numericVisibleDeviceList(os.Getenv(name)) {
|
|
return name
|
|
}
|
|
}
|
|
return "HIP_VISIBLE_DEVICES"
|
|
}
|
|
|
|
func numericVisibleDeviceList(value string) bool {
|
|
fields := strings.Split(value, ",")
|
|
found := false
|
|
for _, field := range fields {
|
|
field = strings.TrimSpace(field)
|
|
if field == "" {
|
|
continue
|
|
}
|
|
index, err := strconv.Atoi(field)
|
|
if err != nil || index < 0 {
|
|
return false
|
|
}
|
|
found = true
|
|
}
|
|
return found
|
|
}
|
|
|
|
type BaseRunner interface {
|
|
// GetPort returns the localhost port number the runner is running on
|
|
GetPort() int
|
|
|
|
// HasExited indicates if the runner is no longer running. This can be used during
|
|
// bootstrap to detect if a given filtered device is incompatible and triggered an assert
|
|
HasExited() bool
|
|
}
|
|
|
|
type RunnerDiscovery interface {
|
|
BaseRunner
|
|
|
|
// GetDeviceInfos will perform a query of the underlying device libraries
|
|
// for device identification and free VRAM information
|
|
// During bootstrap scenarios, this routine may take seconds to complete
|
|
GetDeviceInfos(ctx context.Context) []DeviceInfo
|
|
}
|
|
|
|
type FilteredRunnerDiscovery interface {
|
|
RunnerDiscovery
|
|
|
|
// GetActiveDeviceIDs returns the filtered set of devices actively in
|
|
// use by this runner for running models. If the runner is a bootstrap runner, no devices
|
|
// will be active yet so no device IDs are returned.
|
|
// This routine will not query the underlying device and will return immediately
|
|
GetActiveDeviceIDs() []DeviceID
|
|
}
|
|
|
|
func GetDevicesFromRunner(ctx context.Context, runner BaseRunner) ([]DeviceInfo, error) {
|
|
var moreDevices []DeviceInfo
|
|
port := runner.GetPort()
|
|
tick := time.Tick(10 * time.Millisecond)
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, fmt.Errorf("failed to finish discovery before timeout")
|
|
case <-tick:
|
|
r, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/info", port), nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(r)
|
|
if err != nil {
|
|
// slog.Warn("failed to send request", "error", err)
|
|
if runner.HasExited() {
|
|
return nil, fmt.Errorf("runner crashed")
|
|
}
|
|
continue
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
// old runner, fall back to bootstrapping model
|
|
return nil, fmt.Errorf("llamarunner free vram reporting not supported")
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
slog.Warn("failed to read response", "error", err)
|
|
continue
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
logutil.Trace("runner failed to discover free VRAM", "status", resp.StatusCode, "response", body)
|
|
return nil, fmt.Errorf("runner error: %s", string(body))
|
|
}
|
|
|
|
if err := json.Unmarshal(body, &moreDevices); err != nil {
|
|
slog.Warn("unmarshal encode response", "error", err)
|
|
continue
|
|
}
|
|
return moreDevices, nil
|
|
}
|
|
}
|
|
}
|