memory

package
v0.9.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	// DecisionEncoderSlots: slots cost an encoder no memory (measured:
	// Julia-1 with 1, 4 or 11 slots has the same footprint) and its
	// requests are evaluated one after another anyway.
	DecisionEncoderSlots = 4
	// DecisionEncoderContext caps an encoder's per-slot context, which is
	// also its batch size and the whole of its memory cost: Julia-1 holds
	// ~1 MB per token of the longest prompt it has seen (8 GB after one
	// 7,500-token prompt against a 168 MB file). 2,048 tokens is a state
	// of some 1,500 words; runtime.context_length raises it.
	DecisionEncoderContext = 2048
	// DecisionCausalSlots and DecisionCausalContext are the plan for a
	// causal decision model (Kev, Clef): each slot costs its context in
	// KV cache like a chat model's. 8,192 is the length Kev's accuracy is
	// validated to; runtime.context_length raises it.
	DecisionCausalSlots   = 2
	DecisionCausalContext = 8192

	// DefaultEncoderMBPerToken is the batch cost assumed for an encoder
	// family nobody measured: Julia-1's, the larger of the two known.
	DefaultEncoderMBPerToken = 1.1
)

Decision model planning (flockd#53). A decision request is one forward pass over a short prompt (measured on Metal, llama.cpp b11382: Laya and Julia-1 10-30 ms, Kev-4B 100-240 ms), and a decision model usually shares the node with a chat model, so its plan is modest and fixed instead of growing into whatever the budget has free.

View Source
const (
	// DefaultMinContext is the least context a request should get by
	// default: "almost nobody is ok with a tiny context" (flockd#46).
	DefaultMinContext = 8192
	// DefaultGPUSlots is the slot ceiling on accelerated nodes. Measured on
	// Metal (flockd#46): per-slot decode stops falling at ~8 slots, so
	// throughput is roughly linear in slots past that and 16 sits past the
	// knee at ~0.6 s p50 TTFB. To be re-measured on CUDA.
	DefaultGPUSlots = 16
	// DefaultCPUSlots keeps today's behaviour where nothing was measured.
	DefaultCPUSlots = 2
)
View Source
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.

View Source
const MiB = 1024 * 1024

MiB is one mebibyte in bytes.

Variables

View Source
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).

View Source
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

func AMDVRAMUsedMB(ctx context.Context) (int64, error)

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 DefaultSlots added in v0.6.6

func DefaultSlots(hw hardwareProfile) int

DefaultSlots is the slot ceiling when budget.max_concurrent is 0: by accelerator class, since that is what the measured curve depends on.

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 EncoderMBPerToken added in v0.7.2

func EncoderMBPerToken(family string) float64

EncoderMBPerToken is the batch cost per token for an encoder family.

func EstimateMB

func EstimateMB(fileBytes int64, minRAMMB int64, ctx int) int64

func FloorContext added in v0.6.6

func FloorContext(in PlanInput) int

FloorContext is the smallest total context a plan for in can produce: the per-slot floor on one slot. Admission asks for room for this much; the plan then grows into whatever is free once idle models have been unloaded for it.

func NvidiaVRAMUsedMB added in v0.5.0

func NvidiaVRAMUsedMB(ctx context.Context) (int64, error)

NvidiaVRAMUsedMB sums memory.used across every NVIDIA GPU nvidia-smi reports. ErrNoGPUTool when the tool is not installed.

func ProcessFootprintMB

func ProcessFootprintMB(pid int) (int64, error)

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

func ResolveContext(override, model, maxCtx int) int

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 Plan added in v0.6.6

type Plan struct {
	// Slots is llama-server's --parallel: requests decoded concurrently.
	Slots int
	// CtxPerSlot is the context every request gets. Evenly split for now;
	// a per-model request from the coordinator is a later phase.
	CtxPerSlot int
	// TotalCtx is --ctx-size (Slots × CtxPerSlot), what the KV cache is
	// sized by — once, whatever the slot count (see EstimateMB).
	TotalCtx int
	// EstimateMB is the load's predicted footprint at TotalCtx.
	EstimateMB int64
	// Squeezed reports that memory, not the caps, decided the layout:
	// fewer slots than the ceiling, or a context below the floor because
	// even one slot did not fit. Logged so operators can see what their
	// budget bought.
	Squeezed bool
}

Plan is the context and slot layout one model load runs with, decided by PlanContext from the memory the node can actually spare (flockd#46).

The order of things is the point: memory is the input and context is what it buys. Before this, the model's training window (capped by runtime.max_context) was passed as --ctx-size, llama-server split it across --parallel slots, and per-request context was whatever that division produced; the footprint was checked afterwards.

func PlanContext added in v0.6.6

func PlanContext(in PlanInput) Plan

PlanContext decides slots and context for one load.

spare      = BudgetMB − UsedMB − weights − overhead   (MB)
kv/token   = FileBytes / 65536                         (EstimateMB's constant)
tokens     = spare / kv per token
slots      = ceiling, lowered until tokens/slots ≥ floor
ctx/slot   = min(cap, tokens/slots)

With no budget known the caps alone decide. If even one slot cannot reach the floor the plan is one slot at the floor and Squeezed; whether that loads is admission's call (EstimateMB says what it would cost).

type PlanInput added in v0.6.6

type PlanInput struct {
	// BudgetMB is the admission budget and UsedMB what loaded models
	// already charge against it. BudgetMB <= 0 means no budget is known:
	// nothing limits the plan but the caps.
	BudgetMB int64
	UsedMB   int64
	// FileBytes is the model's on-disk size (weights and KV cost derive
	// from it, as in EstimateMB); MinRAMMB the catalog's floor, 0 = none.
	FileBytes int64
	MinRAMMB  int64
	// KVBytesPerToken is the model's KV-cache cost per context token from
	// its GGUF header (gguf.Meta.KVBytesPerToken); 0 = unknown, and the
	// file-size heuristic in EstimateMB stands in. The heuristic is 2–4x low
	// for small GQA models, which is exactly where over-planning hurts.
	KVBytesPerToken int64
	// Window is the model's training context; 0 = unknown, DefaultContext.
	Window int
	// Slots is the ceiling on --parallel (budget.max_concurrent, resolved
	// by DefaultSlots when the operator left it auto). < 1 is treated as 1.
	Slots int
	// MinCtx is the per-slot floor (runtime.min_context; <= 0 =
	// DefaultMinContext): slots are given up before a request gets less.
	MinCtx int
	// MaxCtx caps per-slot context (runtime.max_context; 0 = the window).
	MaxCtx int
	// CtxPin is runtime.context_length: the operator pinning per-slot
	// context exactly (0 = plan it). Slots still adapt to memory.
	CtxPin int
	// EncoderMBPerToken > 0 marks a whole-prompt-batch encoder (a
	// decision model on a BERT-family architecture: Laya, Julia). It has
	// no KV cache: what grows is the compute buffer for one batch, by
	// this many MB per token of the longest prompt a slot admits, once,
	// whatever the slot count. See DecisionPlanInput.
	EncoderMBPerToken float64
}

PlanInput is what PlanContext needs to know.

func DecisionPlanInput added in v0.7.2

func DecisionPlanInput(in PlanInput, encoder bool, family string) PlanInput

DecisionPlanInput turns in into the plan input for a decision model: few slots and a per-slot context sized for decision prompts, capped by whatever the operator already capped (runtime.max_context). encoder says the model is a whole-prompt-batch encoder (GGUF architecture *bert*), which is then also costed as one: no KV cache, a batch buffer. A runtime.context_length pin still wins (caps treats it as exact).

func (PlanInput) EstimateAt added in v0.6.7

func (in PlanInput) EstimateAt(ctx int) int64

EstimateAt is the load's footprint at a total context of ctx tokens, with the header's KV cost when in carries one.

For an encoder (EncoderMBPerToken > 0) ctx is the per-slot context: its footprint does not depend on the slot count.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL