Documentation
¶
Overview ¶
Package modelops is the daemon's on-demand model operations service: catalog browsing, operator-triggered downloads (with cancel), and runtime load/unload/default switching. It is the write-half the local API lacked — before it, models moved only at daemon startup or via the coordinator's ModelAssignment stub.
Index ¶
- Variables
- type Loader
- type MemorySnapshot
- type Service
- func (s *Service) CancelDownload(id string) bool
- func (s *Service) Catalog(ctx context.Context, refresh bool) (*models.Catalog, error)
- func (s *Service) CheckRuntime(ctx context.Context, m models.CatalogModel) error
- func (s *Service) ConfiguredMemoryBudgetMB() int64
- func (s *Service) Downloading(id string) bool
- func (s *Service) Fetch(ctx context.Context, id, origin string) error
- func (s *Service) IdleSince(id string) (time.Time, bool)
- func (s *Service) IdleUnload() time.Duration
- func (s *Service) Load(ctx context.Context, id string) error
- func (s *Service) LoadInstance(ctx context.Context, id string) (rt.Instance, error)
- func (s *Service) LoadInstanceOrigin(ctx context.Context, id, origin string) (rt.Instance, error)
- func (s *Service) Loading(id string) bool
- func (s *Service) Lookup(ctx context.Context, id string) (models.CatalogModel, bool, error)
- func (s *Service) Measure(ctx context.Context)
- func (s *Service) Memory() MemorySnapshot
- func (s *Service) MemoryBudgetMB() int64
- func (s *Service) Remove(ctx context.Context, id string) error
- func (s *Service) RunCatalogRefresh(ctx context.Context, interval time.Duration)
- func (s *Service) RunHousekeeping(ctx context.Context)
- func (s *Service) SetDefault(id string) error
- func (s *Service) SetIdleUnload(d time.Duration)
- func (s *Service) SetMemoryBudgetMB(mb int64)
- func (s *Service) StartDownload(ctx context.Context, id string) (bool, error)
- func (s *Service) Unload(ctx context.Context, id string) error
- func (s *Service) UnloadIdle(ctx context.Context) []string
Constants ¶
This section is empty.
Variables ¶
var ErrDownloadRunning = errors.New("modelops: download already running")
ErrDownloadRunning is returned when a download for the id is in flight.
var ErrOverMemory = errors.New("modelops: over memory budget")
ErrOverMemory means a load does not fit the memory budget even after unloading every idle instance admission is allowed to touch. The assign service reports it to the coordinator as `cached` (the artifact stays on disk, loadable when demand returns); the local API returns it to the operator as an error.
var ErrUnknownModel = errors.New("modelops: model not in catalog")
ErrUnknownModel is returned when an id is in neither the catalog nor the local cache.
Functions ¶
This section is empty.
Types ¶
type Loader ¶
type Loader interface {
Load(ctx context.Context, m rt.ModelSpec, res rt.ResourceBudget) (rt.Instance, error)
}
Loader loads a model artifact into a serving runtime instance (*llamacpp.Adapter in production; fakes in tests).
type MemorySnapshot ¶ added in v0.4.0
type MemorySnapshot struct {
// UsedMB is what admission charges against BudgetMB: the sum of
// per-model footprints, or on a discrete GPU with a VRAM sample the
// card's measured usage (plus estimates for loads newer than it).
UsedMB int64
BudgetMB int64
TotalMB int64
// HostMB is the runtimes' host-side footprint (measured where the
// platform supports it, else the estimate) — the heartbeat's
// ram_used_mb. Equal to UsedMB on unified memory.
HostMB int64
// VRAMMB is the last card sample (nvidia-smi, or amdgpu sysfs /
// rocm-smi; discrete GPUs); VRAMMeasured is false when no sample
// exists and callers fall back to UsedMB.
VRAMMB int64
VRAMMeasured bool
// Models is the per-model footprint (measured when available, else the
// estimate) for every loaded model.
Models map[string]int64
}
MemorySnapshot is the /api/v1/status memory view and the heartbeat's ram_used_mb / vram_used_mb source.
type Service ¶
type Service struct {
Mgr *models.Manager
Eng *engine.Engine
Loader Loader
Budget rt.ResourceBudget
Log *slog.Logger
// Catalog sources, mirroring config (path wins).
ManifestPath string
ManifestURL string
// OnLoaded is called after a model becomes servable (used to stamp the
// runtime build id into the capability profile). May be nil.
OnLoaded func(inst rt.Instance)
// Events receives models_changed on download-complete, load, unload and
// default switches. May be nil.
Events *events.Hub
// Activity receives loaded/unloaded rows. May be nil.
Activity *activity.Ring
// Hardware sizes the auto memory budget and tells unified from
// discrete memory. Nil disables admission unless a budget is
// configured explicitly (SetMemoryBudgetMB).
Hardware *typesv1.CapabilityProfile
// MaxContext mirrors runtime.max_context: the per-slot context cap
// (0 = the model's window).
MaxContext int
// MinContext mirrors runtime.min_context: the per-slot floor the
// planner gives up slots to protect (0 = memory.DefaultMinContext).
MinContext int
// ContextLength is the operator's runtime.context_length pin on
// per-slot context (0 = planned from the budget).
ContextLength int
// OnUnloaded is called after any unload (operator, idle, memory
// pressure) with the model id; the assign service reports `cached` to
// the coordinator from it. May be nil.
OnUnloaded func(id string)
// VRAM measures used VRAM on discrete GPUs (nvidia-smi). Nil = built
// from Hardware on first use; tests inject one.
VRAM *memory.VRAMSampler
// contains filtered or unexported fields
}
Service wires catalog + cache + runtime together. Nil Service (mock runtime) means the local API reports these operations unsupported.
func (*Service) CancelDownload ¶
CancelDownload aborts an in-flight download. The .partial file is kept so a later attempt resumes.
func (*Service) Catalog ¶
Catalog returns the model catalog, cached for catalogTTL. refresh forces a refetch. A failed refetch keeps the last good copy.
func (*Service) CheckRuntime ¶ added in v0.7.0
CheckRuntime reports whether the runtime build this node runs can serve a catalog model, without downloading or loading anything. It returns an error wrapping rt.ErrRuntimeTooOld for a decision model on a llama.cpp build that predates decision support (the reason names both builds), and nil when the loader cannot tell (mock, an operator's own binary).
func (*Service) ConfiguredMemoryBudgetMB ¶ added in v0.4.0
ConfiguredMemoryBudgetMB returns the raw configured value (0 = auto).
func (*Service) Downloading ¶
Downloading reports whether a download for id is in flight.
func (*Service) Fetch ¶ added in v0.5.0
Fetch makes sure a catalog model is on disk — download and verify inside max_disk_mb, with the given cache origin — without loading it. It backs staged coordinator placements (ModelAssignment.stage). A cache hit returns immediately. models.ErrOverBudget when it cannot fit.
func (*Service) IdleSince ¶ added in v0.4.0
IdleSince reports when a loaded model last served a request, if it is idle right now (no requests in flight).
func (*Service) IdleUnload ¶ added in v0.4.0
IdleUnload is the live idle-unload threshold (0 = never).
func (*Service) Load ¶
Load makes a model servable: cache hit or download, then runtime load and engine registration. Synchronous — llama-server startup takes seconds and callers want the outcome. No-op if already loaded.
func (*Service) LoadInstance ¶
LoadInstance is Load returning the runtime instance (startup needs it).
func (*Service) LoadInstanceOrigin ¶
LoadInstanceOrigin is LoadInstance with ownership of a fresh download (models.OriginMesh for coordinator placements: the fetch may then only evict other mesh-placed models to make room).
Memory admission happens after the artifact is on disk and before the runtime starts: the load's estimated footprint must fit the budget, idle instances are unloaded to make room (see admit), and ErrOverMemory is returned — with the file kept — when they cannot.
func (*Service) Loading ¶ added in v0.4.0
Loading reports whether a load of id is in progress (download, admission or runtime start). The store's retention pass treats it like loaded.
func (*Service) Lookup ¶ added in v0.7.2
Lookup finds a catalog entry by id. A miss triggers one refetch before the answer is "not in catalog" — a model promoted since the last fetch is found without waiting for the TTL or a restart — rate-limited to one refetch per catalogMissRefetch so unknown ids cannot hammer the host.
func (*Service) Measure ¶ added in v0.4.0
Measure samples every loaded runtime's footprint and replaces the estimates. Cheap on macOS/Linux/Windows (one syscall / one procfs read per process, inside Instance.Health). On a discrete GPU it also takes one sample of used VRAM (nvidia-smi, or amdgpu sysfs / rocm-smi; ≤ 1 call per housekeeping tick, 3 s timeout; a failure keeps the previous figure or the estimates).
func (*Service) Memory ¶ added in v0.4.0
func (s *Service) Memory() MemorySnapshot
Memory reports the current accounting.
func (*Service) MemoryBudgetMB ¶ added in v0.4.0
MemoryBudgetMB is the live budget for loaded models: the configured budget.max_ram_mb, or the auto derivation from hardware when it is 0. 0 means no hardware profile and no configured budget: admission is off.
func (*Service) Remove ¶ added in v0.4.0
Remove unloads id if it is loaded and deletes it from the store. Unlike Unload followed by Manager.Remove, it never reports the model as `cached` to the coordinator in between: the file is going away.
func (*Service) RunCatalogRefresh ¶ added in v0.7.2
RunCatalogRefresh refetches the catalog every interval (<= 0: catalogTTL) until ctx ends, so a daemon that nobody asks about models still learns of a promoted catalog. Failures keep the last good copy.
func (*Service) RunHousekeeping ¶ added in v0.4.0
RunHousekeeping measures footprints and applies idle unload every housekeepInterval until ctx ends.
func (*Service) SetDefault ¶
SetDefault switches the model served when a request names none.
func (*Service) SetIdleUnload ¶ added in v0.4.0
SetIdleUnload changes the threshold live.
func (*Service) SetMemoryBudgetMB ¶ added in v0.4.0
SetMemoryBudgetMB changes the configured budget live (0 = auto). Nothing is unloaded immediately; the next load and the idle loop apply it.
func (*Service) StartDownload ¶
StartDownload begins fetching a catalog model in the background. It returns (false, nil) if the model is already cached and ready, and ErrDownloadRunning if a download is already in flight.