|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// preferredGGUFBackend is tried first when auto-detecting the backend for a |
| 9 | +// GGUF model, since GGUF is overwhelmingly llama.cpp's native format. |
| 10 | +const preferredGGUFBackend = "llama-cpp" |
| 11 | + |
| 12 | +// llmCapableBackend reports whether the named backend can serve a text/LLM GGUF |
| 13 | +// model. The backend capability table lives in core/config, which is a |
| 14 | +// higher-level package that already imports pkg/model; importing it back here |
| 15 | +// would form a core/config -> pkg/model -> core/config cycle. So core/config |
| 16 | +// registers the predicate via RegisterLLMCapableBackendFunc instead (see #9287). |
| 17 | +// When unset (e.g. a build that never imports core/config) GGUF capability |
| 18 | +// filtering is skipped and auto-detect falls back to the deterministic set. |
| 19 | +var llmCapableBackend func(name string) bool |
| 20 | + |
| 21 | +// RegisterLLMCapableBackendFunc wires the LLM-capability predicate used by |
| 22 | +// SelectAutoLoadBackends. It is called from core/config's init so pkg/model |
| 23 | +// need not import core/config (see #9287). |
| 24 | +func RegisterLLMCapableBackendFunc(fn func(name string) bool) { |
| 25 | + llmCapableBackend = fn |
| 26 | +} |
| 27 | + |
| 28 | +// SelectAutoLoadBackends returns the ordered, deterministic list of backend |
| 29 | +// names to try when loading a model that declares no explicit backend. |
| 30 | +// |
| 31 | +// available is the set of installed backend names (unordered, as it comes from a |
| 32 | +// Go map). modelFile is the model file name/path (may be empty). |
| 33 | +// |
| 34 | +// The trial loop in (*ModelLoader).Load picks the first backend whose gRPC |
| 35 | +// LoadModel succeeds, so the order and membership of this list directly decide |
| 36 | +// which backend wins. The previous implementation ranged a Go map (random |
| 37 | +// order) with no filtering, so an unrelated installed backend such as the |
| 38 | +// "opus" audio codec could win a GGUF/LLM model load (#9287). |
| 39 | +// |
| 40 | +// Behaviour: |
| 41 | +// - The result is always deterministically ordered, so auto-detect no longer |
| 42 | +// depends on map iteration order. |
| 43 | +// - For a GGUF model file the list is filtered to LLM-capable backends and |
| 44 | +// llama-cpp is placed first, so an incompatible audio/codec/image backend |
| 45 | +// can never win the trial loop. |
| 46 | +// - If filtering would leave no candidate, the full sorted set is returned |
| 47 | +// instead, so a model that previously loaded never becomes unloadable. |
| 48 | +func SelectAutoLoadBackends(available []string, modelFile string) []string { |
| 49 | + sorted := append([]string(nil), available...) |
| 50 | + sort.Strings(sorted) |
| 51 | + |
| 52 | + if !isGGUFModelFile(modelFile) { |
| 53 | + return sorted |
| 54 | + } |
| 55 | + |
| 56 | + // No capability predicate wired (core/config not linked in): skip filtering |
| 57 | + // rather than risk dropping a valid candidate. |
| 58 | + if llmCapableBackend == nil { |
| 59 | + return sorted |
| 60 | + } |
| 61 | + |
| 62 | + filtered := make([]string, 0, len(sorted)) |
| 63 | + hasLlama := false |
| 64 | + for _, b := range sorted { |
| 65 | + if b == preferredGGUFBackend { |
| 66 | + hasLlama = true |
| 67 | + continue // added explicitly first below |
| 68 | + } |
| 69 | + if isLLMCapableBackend(b) { |
| 70 | + filtered = append(filtered, b) |
| 71 | + } |
| 72 | + } |
| 73 | + if hasLlama { |
| 74 | + filtered = append([]string{preferredGGUFBackend}, filtered...) |
| 75 | + } |
| 76 | + |
| 77 | + if len(filtered) == 0 { |
| 78 | + // Conservative fallback: no known LLM-capable backend is installed, so |
| 79 | + // rather than refuse to load, fall back to the previous behaviour of |
| 80 | + // trying every installed backend (now at least in a deterministic order). |
| 81 | + return sorted |
| 82 | + } |
| 83 | + return filtered |
| 84 | +} |
| 85 | + |
| 86 | +func isGGUFModelFile(modelFile string) bool { |
| 87 | + return strings.HasSuffix(strings.ToLower(modelFile), ".gguf") |
| 88 | +} |
| 89 | + |
| 90 | +// isLLMCapableBackend reports whether a backend is known to serve text/LLM |
| 91 | +// models. Backends absent from the capability map (unknown) are treated as |
| 92 | +// not LLM-capable here: for GGUF auto-detection we only want backends we can |
| 93 | +// positively confirm handle LLMs, and the zero-candidate fallback keeps unknown |
| 94 | +// setups working. Callers must ensure llmCapableBackend is non-nil. |
| 95 | +func isLLMCapableBackend(name string) bool { |
| 96 | + return llmCapableBackend(name) |
| 97 | +} |
0 commit comments