Documentation
¶
Overview ¶
Package memory is the daemon's model-memory accounting: the budget loaded models may use, the footprint estimate for a model before it is loaded, and the measured physical footprint of a running runtime process.
Measurement is deliberately *not* RSS. llama.cpp mmaps GGUF weights, so RSS counts clean file-backed pages the kernel can drop at will and double counts the page cache; the numbers that matter are what macOS calls the physical footprint (proc_pid_rusage ri_phys_footprint, the figure Activity Monitor's "Memory" column and `footprint` show) and what Linux calls the proportional set size (/proc/<pid>/smaps_rollup Pss).
Index ¶
- Constants
- Variables
- func AMDVRAMUsedMB(ctx context.Context) (int64, error)
- func AutoBudgetMB(hw *typesv1.CapabilityProfile, maxVRAMPercent int) int64
- func BudgetMB(configured int64, hw *typesv1.CapabilityProfile, maxVRAMPercent int) int64
- func Discrete(hw *typesv1.CapabilityProfile) bool
- func EstimateMB(fileBytes int64, minRAMMB int64, ctx int) int64
- func NvidiaVRAMUsedMB(ctx context.Context) (int64, error)
- func ProcessFootprintMB(pid int) (int64, error)
- func ResolveContext(override, model, maxCtx int) int
- func Unified(hw *typesv1.CapabilityProfile) bool
- type VRAMSampler
Constants ¶
const ( // DefaultContext is the context length assumed when neither the catalog // nor the operator sets one. llama-server's own default is 4096; the // larger figure errs on the side of not overfilling the machine. DefaultContext = 8192 )
Estimate parameters.
const MiB = 1024 * 1024
MiB is one mebibyte in bytes.
Variables ¶
var ErrNoGPUTool = errors.New("memory: no VRAM query tool available")
ErrNoGPUTool means no VRAM source for this node's GPU vendor is available (nvidia-smi / rocm-smi not on PATH, no amdgpu sysfs).
var ErrUnsupported = errors.New("memory: footprint measurement not supported on this platform")
ErrUnsupported means footprint measurement is not implemented on this platform; callers keep the pre-load estimate.
Functions ¶
func AMDVRAMUsedMB ¶ added in v0.6.1
AMDVRAMUsedMB sums used VRAM across every AMD GPU: sysfs first, rocm-smi when sysfs has no amdgpu card. ErrNoGPUTool when neither is available.
func AutoBudgetMB ¶
func AutoBudgetMB(hw *typesv1.CapabilityProfile, maxVRAMPercent int) int64
AutoBudgetMB derives the model-memory budget when budget.max_ram_mb is 0: half of physical RAM on unified-memory (and CPU-only) machines, and vram × max_vram_percent on discrete GPUs, where the weights live on the card and host RAM is not the constraint.
func BudgetMB ¶
func BudgetMB(configured int64, hw *typesv1.CapabilityProfile, maxVRAMPercent int) int64
BudgetMB resolves the configured budget: configured > 0 wins, else auto.
func Discrete ¶
func Discrete(hw *typesv1.CapabilityProfile) bool
Discrete reports whether the node has a discrete (non-unified) GPU, where weights live in VRAM that host-side footprint measurement cannot see.
func NvidiaVRAMUsedMB ¶ added in v0.5.0
NvidiaVRAMUsedMB sums memory.used across every NVIDIA GPU nvidia-smi reports. ErrNoGPUTool when the tool is not installed.
func ProcessFootprintMB ¶
ProcessFootprintMB measures the physical footprint of a process in MiB. Returns ErrUnsupported where no implementation exists; callers then keep their estimate.
func ResolveContext ¶ added in v0.4.1
EstimateMB predicts the physical footprint of loading a model before it is loaded:
estimate = file_bytes × 1.15 + ctx × (file_bytes / 65536) + 256 MB
ctx is the total --ctx-size handed to llama-server. It is *not* multiplied by the slot count: llama-server splits --ctx-size across its --parallel slots (each slot gets ctx / parallel), so the KV cache is sized by the total context exactly once. The 1.15 weights factor and the 256 MB overhead keep the figure slightly conservative.
When the catalog states min_ram_mb the larger of the two wins: the catalog value is authoritative for the weights but was written for a specific context, so a much larger operator-configured context still raises the estimate. ctx <= 0 uses DefaultContext. ResolveContext is the context window a runtime is launched with: the operator's explicit override if set, else the model's own length, then capped by maxCtx (0 = no cap). Both the llama.cpp adapter and the admission estimate go through it so they agree on the KV-cache size.
func Unified ¶
func Unified(hw *typesv1.CapabilityProfile) bool
Unified reports whether the node's GPU shares system memory (Apple Silicon). On such machines the RAM budget is the whole story: there is no separate VRAM to fill.
Types ¶
type VRAMSampler ¶ added in v0.5.0
type VRAMSampler struct {
// Query runs the vendor's source (NvidiaVRAMUsedMB / AMDVRAMUsedMB);
// tests substitute it.
Query func(context.Context) (int64, error)
// contains filtered or unexported fields
}
VRAMSampler measures used VRAM on nodes where that is possible: a discrete NVIDIA or AMD GPU with its query source available. Elsewhere Sample reports no measurement (once-logged) and callers keep their estimates.
func NewVRAMSampler ¶ added in v0.5.0
func NewVRAMSampler(hw *typesv1.CapabilityProfile, log *slog.Logger) *VRAMSampler
NewVRAMSampler builds a sampler for the node's hardware.
func (*VRAMSampler) Sample ¶ added in v0.5.0
func (s *VRAMSampler) Sample(ctx context.Context) (mb int64, ok bool)
Sample measures used VRAM in MiB. ok is false when this node cannot be measured (no discrete GPU, unsupported vendor, tool missing) or when the query failed this time — the caller keeps its previous figure.
func (*VRAMSampler) Vendor ¶ added in v0.5.0
func (s *VRAMSampler) Vendor() string
Vendor is the discrete GPU vendor a sampler would measure ("nvidia", "amd", …), or "" when the node has no discrete GPU.