Browse documentation
aner.llm · Reference

CPU and GPU model memory placement

Plan a compatible local GGUF within host and GPU budgets, run CPU inference and distinguish estimated buffers from measured memory.

ImplementedAner 0.1.0 or newer

Memory planning and fitted native loading were introduced in 0.0.9-dev.4 and are included in stable 0.0.9. Published portable packages enable the pinned native inference engine. Accelerator support depends on the worker/build profile; every accelerator has not been physically qualified. The existing llm.load contracts remain available. See Native local models for build requirements and the release record for package scope.

CPU inference is a supported execution choice. A GPU is optional. A machine with an 8 GiB GPU can run a larger supported model using host RAM, provided the weights, context and execution workspace fit the resources actually available. Speed depends on CPU, memory bandwidth, GPU placement and the workload. Moving weights to RAM does not reduce their precision or guarantee interactive speed.

Choose a model and memory policy

The current loader admits compatible single file GGUF llama and qwen2 chat models and a restricted dense qwen35 raw completion path. The latter does not support the chat server examples below. File extension and parameter count alone do not establish compatibility. Other architectures, MoE implementations, multimodal input and split GGUF require separate support and qualification.

Acquisition remains separate from placement. llm.fetch_artifact requires an explicit HTTPS URL, expected byte count and SHA256, and retains its 16 GiB single artifact cap. Planning accepts an already local compatible file; this feature does not add model repository discovery or multi shard downloading.

Choose a quantized artifact such as a supported Q4 or Q5 GGUF when appropriate for the task. Weight quantization is part of that artifact. Neither llm.plan nor llm.load_fitted converts, requantizes, truncates or otherwise modifies it. kv_cache_type controls request KV storage separately; q8_0 requires compatible Flash Attention and is not supported by the restricted Qwen3.5 path.

Use a context appropriate to the application. A longer context consumes more KV memory; simultaneous contexts consume additional memory. A 24 GiB allocation requirement cannot be assumed to become a 12 GiB allocation merely by halving the weight precision: KV, workspace, runtime and device allocations remain.

Run a CPU server today

The complete CPU example uses existing APIs:

aner
import aner.llm
import aner.api

let base = llm.load("models/local-instruct-q4.gguf", context: 2048,
    device: "cpu", kv_cache_type: "f16", flash_attention: "auto")
let tuned = base.with_optimizer(
    threads: 4, prefill_threads: 4,
    batch_tokens: 128, micro_batch_tokens: 32,
    prefix_cache_entries: 0, prefix_cache_tokens: 1024,
    speculative_tokens: 0)
let serving = tuned.with_batching(max_sequences: 1)
tuned.close()
base.close()

let warmup = serving.generate("Say hello.", max_tokens: 1)
print(warmup.text())
print(serving.memory_summary())

let app = api.app("Aner CPU chat").llm(path: "/chat", model: serving)
    .bind(host: "127.0.0.1", port: 11435)
    .limits(max_rows: 64, timeout_ms: 120000)
    .output_limit(max_tokens: 128)
app.serve()
serving.close()

Replace the path with an actual compatible local model. Check and run the saved example with the matching source build runtime:

sh
aner check examples/llm_cpu_server.aner
aner run examples/llm_cpu_server.aner

Checking validates source without loading weights or starting a server. Running loads weights, performs a one token warmup to exercise context/workspace allocation, then listens on loopback. CPU threads four, context 2048 and the shown batches are starting settings, not a fit or throughput guarantee. This existing llm.load route does not enforce a RAM budget.

The one sequence scheduler shares one resident inference context and bounds its queue. Named sessions are unavailable on this profile; clients send the complete conversation each time. The request body must contain exactly one of prompt or messages. For a short request:

sh
curl http://127.0.0.1:11435/chat \
  -H 'Content-Type: application/json' \
  -d '{"prompt":"Explain quantization simply.","max_tokens":128,"temperature":0.0}'

output_limit bounds output tokens. timeout_ms bounds admitted request time with cooperative cancellation; it cannot preempt a running native kernel. max_rows belongs to tabular routes and does not cap LLM memory. A session count or requests per minute limit is also not a total memory reservation. The API's connection workers and queued connections are separate from inference contexts.

Plan, then load within a budget

The new API accepts the same model/context/cache settings plus budgets in binary MiB (1 MiB = 1,048,576 bytes):

aner
print(llm.devices())
print(llm.plan("models/local-instruct-q4.gguf", context: 2048,
    device: "cuda:0", kv_cache_type: "f16", flash_attention: "auto",
    gpu_budget_mib: 7168, ram_budget_mib: 24576, reserve_mib: 512))
let model = llm.load_fitted("models/local-instruct-q4.gguf", context: 2048,
    device: "cuda:0", kv_cache_type: "f16", flash_attention: "auto",
    gpu_budget_mib: 7168, ram_budget_mib: 24576, reserve_mib: 512)

llm.devices() returns JSON describing devices visible to the compiled backend. llm.plan(...) inspects the model and current resources without allocating model tensors or starting inference. It reports a placement or fails with a reason if the requested resources cannot accommodate one. Planning still reads and hashes the local GGUF. It does not download a model, establish its provenance or reserve hardware for later use.

gpu_budget_mib caps each selected discrete GPU's usable budget against its reported free memory. ram_budget_mib caps host admission against reported available RAM. Zero chooses the reported available amount; a positive budget cannot grant memory the host does not have. reserve_mib leaves headroom on the host and selected GPUs. Budgets and reserve must be nonnegative integers. Shared memory devices require shared accounting rather than adding host and GPU capacity as though they were independent physical memory.

The example uses a cap below 8 GiB and a host cap below 32 GiB. Substitute budgets for the actual machine and leave room for the OS and other applications. This does not assert that an unspecified model fits those numbers.

llm.load_fitted(...) recomputes the plan, loads the original artifact with the selected GPU layer count and validates its execution allocation. Remaining layers execute on the CPU. It fails if planning or allocation cannot satisfy the profile; an explicit unavailable accelerator is an error. The selected placement and its accounting are included in load/memory diagnostics. The original model SHA256 remains the fingerprint.

A selected GPU with unknown available memory or no budget remaining after the reserve is omitted from fitted execution. If no GPU placement fits, the planner also evaluates CPU only execution against the host budget. Read resolved_device and the actual device entries to see whether acceleration was admitted.

The fitted profile fixes context, cache, attention, batch and microbatch settings for one shared execution context. Batch capacity is min(512, context) tokens; microbatch capacity is min(128, context) tokens. Stateless worker leases share that bounded allocation and inference admission, including through aner.api. Creating sessions, new context pools, a different optimizer, adapters, sampling profiles or a continuous batching profile from a fitted lease is rejected. These restrictions prevent derived configurations from multiplying or changing the allocation that was admitted. They do not change ordinary llm.load leases.

The complete fitted server example prints the plan and effective diagnostics, warms up the model, then serves the same loopback chat route. It uses device: "auto" so it can run with the supported local backend, including CPU only builds. Fitted auto considers the first supported visible accelerator and then CPU; select an explicit CUDA list to consider other cards or split layers across multiple GPUs. Change both calls to device: "cpu" to request budgeted CPU only loading, or to an explicit CUDA/Metal device when appropriate. The legacy llm.load("...", device: "auto") retains its existing accelerator selection behavior; it does not invoke the fitted planner.

Select more than one GPU

Enumerate devices with llm.devices(), then pass an explicit CUDA list to both planning and fitted loading:

aner
print(llm.plan("models/local-instruct-q4.gguf", context: 2048,
    device: "cuda:0,cuda:1", kv_cache_type: "f16", flash_attention: "auto",
    gpu_budget_mib: 7168, ram_budget_mib: 24576, reserve_mib: 512))

Each selected device must exist. The GPU cap applies separately to each selected GPU. The planner must account for layers and execution allocations on their actual devices; a sum of advertised VRAM sizes is not evidence of fit. Examine the per device entries and effective split mode in the returned plan. For two 32 GiB cards, a cap such as gpu_budget_mib: 30720 applies to each card before the reserve. This can admit a model exceeding either card's capacity, but the cards remain separate allocators; assigned layers, context and workspace must fit each card. The full 64 GiB cannot be treated as usable weight storage. Different GPUs and interconnects can produce different speeds. A source test of planning arithmetic does not constitute a physical multi GPU inference qualification.

Understand diagnostics and limits

Plans describe weight placement and estimated execution allocations. Model tensor bytes, KV bytes and compute bytes have separate scopes. Runtime/driver overheads, allocator fragmentation and unrelated applications can differ from estimates. Reported free memory is an observation, not an operating system reservation. A successful validated load is evidence for that model, context, profile and machine at that time, not a promise that future external memory pressure is harmless. memory_summary() is not peak process RSS or an OS enforced hard RAM/VRAM limit.

The plan uses schema aner.llm.plan/1, including gpu_layers, resolved_device, host, and per device devices entries. allocation_validated:false identifies a simulation. A successful fitted load records allocation_validated:true in the placement member of load_summary() and memory_summary(). These records identify one admitted context and bind the model's original SHA256. Planning failures raise an error; they do not return a successful fits:false record.

Keep exact model hashes, backend/runtime identity, requested and effective placement, context/cache settings and physical hardware when measuring results. Measure peak host/device allocation separately from estimated tensor payload. Report cold/warm loading, prefill, time to first output and decode separately; test representative output quality when choosing a lower precision artifact.

This feature uses CPU RAM and supported GPUs in one process. It does not stream MoE experts from SSD, turn disk capacity into VRAM, pool memory across machines or implement distributed tensor/pipeline inference. Aner's numerical cluster workers are a separate execution facility. Independent inference servers on several hosts each need their own admitted model and resources.

See the Language model connections module reference for the shared API.

Aner documentation · Reference and guides