interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / docs / design / gb10-weight-ownership.md

350 lines · 28432 bytes Raw Blame History Source

GB10 weight ownership and serving-time memory (design, for #277, not a fix)

In plain words

On the GB10 the graphics chip has no memory of its own. It uses ordinary system memory that the driver pins for it. Today the Qwen3-4B model takes that memory twice: a full 32-bit copy of every weight stays in the program (about 15 GiB), and a second, smaller 16-bit copy is handed to the driver (about 7.5 GiB). When the machine is busy and its "free" memory is low, the driver sometimes cannot get the pinned memory it asks for, even though plenty of memory could be freed by dropping cached files. That is the failure in #277 and omega#327. This note does two things. First, it lists every place the program asks the driver for memory, and shows that some requests happen while the model is serving, not only at start-up (so just opening the GPU earlier, as draft PR #278 does, cannot cover them). Second, it proposes the longer-term fix: send each weight straight from the model file to the GPU, never keep the big 32-bit copy, and ask the driver for all serving memory once, at start-up. That would cut the machine's memory use from about 22 GiB to about 9 GiB and remove the late requests. Nothing here changes runtime code, and nothing is proven on the chip. The default refusal of Qwen3 on the GB10 stays in place.

Status and evidence labels

Model numbers used (Qwen3-4B-Instruct-2507, config.json): hidden 2560, intermediate 9728, 36 layers, 32 query heads, 8 KV heads, head_dim 128, vocab 151936, tied embeddings, declared context

Update 2026-10-07 (after omega#332 and omega#333; still no chip run)

Update 2026-10-07 (cut C written, host-measured, not run on the chip)

1. Allocation audit

All GPU-side allocations go through omega_gpu_session_alloc (omega src/omega_gpu_session.c:78-82) which calls nvrm_alloc_gpu_uncached (physics nvrm/nvrm.c:473-475, body :477-). The size is rounded up to 4 KiB (nvrm.c:480), so any growth of even one page is a new request. A refused request sets rm->faulted = 1 (nvrm.c:506-509) and later requests then fail in the same function (nvrm.c:478): one refused allocation ends the GPU session for the rest of the process. OBSERVED.

(a) Daemon and session start-up

WhatSizeWhenFreed
Channel and its context buffers (m16_native_create_channel, session.c:125)driver decides (the kgrctxAllocMainCtxBuffer that failed in #274)first chip call, or the probe in open_gpu_session_with_retry (omega_backend.rs:968-)at session close only; nothing in the daemon calls close
Session buffers: pushbuffer 64 KiB, cbank 4 KiB, marker 4 KiB, QMD 64 KiB (session.c:127-128)136 KiBat openat close

Daemon order on main (commands.rs, run_daemon_server): weights loaded (build_native_daemon_backend, :1332), KV pool built (:1508), then the session opened by the probe (:1528). Draft #278 moves the open before the load. The session never reopens after a success.

(b) First forward pass (warm-up turn, server.enable_warm_up, commands.rs:1554)

WhatSize formulaNotes
Resident weight matrix, one per matrix (omega_gpu_tensor_upload_bf16, matmul_api.c:210)kp * np * 2 bytes, kp = k rounded to 16, np = n rounded to 8layers: 36 x (q 2560x4096, k 2560x1024, v 2560x1024, o 4096x2560, gate 2560x9728, up 2560x9728, down 9728x2560) = 3.63 B values = 6.76 GiB COMPUTED. Plus lm_head: compute_logits goes through chip_matmul with the tied embedding (omega_backend.rs:833-848), 151936x2560 = 0.72 GiB COMPUTED. Total resident 7.48 GiB. #277 quotes about 6.8 GiB, which matches the layers only; the lm_head copy is extra (INFERRED, check at the next run).
Matmul kernel code, one per (kp, np, grid_x) shape (matmul_api.c:163)about one to a few pages each (code + 2048 B tail, rounded to 4 KiB, code_alloc.h)see (c): the cache has 8 slots
Elementwise kernel code (rmsnorm, rope, swiglu)pages eachelementwise_api.c:930, cache of 8
Attention kernel code (one shape for Qwen3)pagesattention_api.c:553, cache of 8
Scratch first sizessee (c)all scratch starts at 0 and is created by the first call that needs it

Today these resident uploads happen lazily on the first call that touches each weight, which is the warm-up turn when a tokenizer is loaded (otherwise the first real request). Each upload also makes transient host copies: a transposed f32 copy of the whole matrix (wt, omega_backend.rs:205-212), a bf16 copy (matmul_api.c:omega_gpu_tensor_upload_f32), and a packing buffer (64 column tiles). For lm_head that is 1.45 GiB + 0.72 GiB of ordinary memory at once.

(c) Per-request serving

The key finding: scratch buffers are **grow-only with exact size, freed and re-requested from the driver on every increase**. omega_gpu_session_scratch (session.c:97-105): if the request is larger than the current capacity, free the old buffer and allocate a new one of exactly the new high-water size. No headroom, no geometric growth, no pre-reservation. OBSERVED.

BufferSize formulaGrows withReallocated mid-serving?Freed
Attention staging pool g_pool (attention_api.c:600, run_launch :609)f32 path: 2 * seq_len * kv_heads * head_dim * 4 = 8 KiB per token for Qwen3 (attention_api.c:711-714)context lengthYes. Each new context length is 8 KiB more than the last, which is more than the 4 KiB rounding, so every generated token (and every prefill chunk) triggers one free plus one new driver allocation, first layer of the step only; the other 35 layers reuse itat session close
Attention q / outq_heads * head_dim * 4 = 16 KiB each, times number of sequences in the callbatch sizeonly when the number of sequences risesat close
Attention tablea few bytesbatchtinyat close
Matmul activations a_bufround16(m) * kp * 2rows per call mwhen m or kp exceeds the previous maximumat close
Matmul results c_bufround16(m) * np * 4; logits step: m = number of decoding sequences, np = 151936 (9.3 MiB at m <= 16, 37 MiB at 64, about 148 MiB at 256)concurrent decodes, prefill chunk (128 rows)when m rises past the previous maximum, or a bigger np appearsat close
Elementwise a/b/c (run_abc, elementwise_api.c:978, :1074)each launch is split to at most 64 CTAs (EW_MAX_CTAS), so at most 64 rows x 2560 x 4 = 640 KiB per buffer, independent of contextnothing past the first full-size callno (after the first 64-row call)at close
Kernel code, matmul (matmul_api.c:144-167)pages(kp, np, grid_x)INFERRED yes; since OBSERVED on the fake layer, see Update. The key includes grid_x, which depends on rows (grid_x = budget / ceil(m/16), capped by np/8). With the daemon CTA budget of 256: decode (m = 1) and prefill chunk (m = 128) give 6 shapes each (12 distinct, k and v share one), and the cache has 8 slots, evicted round-robin (cache_next). Alternating prefill and decode, or different decode batch sizes, therefore frees and re-allocates code buffers during servingat close
Pushbuffer, cbank, marker, QMDfixed (session.c:127)noneno (each launch rewrites the same buffers)at close
Resident weightsfixed after (b)nonenowhen the ResidentTensor drops
KV cache pool (UnifiedKvTensorPool, Fp32, shared_kv.rs:44-57)288 KiB per token (36 layers x 8 heads x 128 x 2 x 4 B) COMPUTED; declared context 262144 would be 72 GiB, so AIEN_KV_CONTEXT_TOKENS must lower itcontextn/a: host memory, not driver memorydaemon exit
Per-call host gather of K and V for the f32 attention kernel (omega_backend.rs:464-490)2 * n * 4096 B host vectorscontexthost onlyper call

Bound on the attention scratch: the f32 attention kernel refuses seq_len > 4096 (OMEGA_GPU_ATTN_MAX_GQA_CTX, attention_api.c:703, header :83). A longer context is a chip error, which is a fatal fallback in a production build (strict.rs). So on this path the pool is at most 2 * 4096 * 8 * 128 * 4 = 32 MiB COMPUTED, and Qwen3 serving above 4096 tokens is not possible natively today regardless of memory. That makes a pre-reserved envelope small and cheap.

Answer to #277's open question

Yes. Anything else allocates driver memory per call later: the attention staging pool grows by one request per generated token, matmul scratch grows with batch rows and decode batch size, and matmul kernel code buffers are probably evicted and rebuilt when prefill and decode alternate. OBSERVED for the scratch rule, INFERRED for the code-cache thrash. Opening the session first (#278) only guarantees the channel context and the first allocations happen early. It cannot by itself cover these later requests. Candidate 1 is therefore incomplete unless start-up also reserves every scratch at its maximum, and only candidate 2 (smaller footprint) keeps MemFree high for the whole run. The comment on #277 (source check at omega 8887454) reached the same conclusion; this table re-checks it at 01f6a74 and adds the 4 KiB per-token rule, the 4096 token bound, the lm_head resident copy and the code-cache thrash.

2. Ownership design (candidate 2)

Goal

On the GB10 production path, the process never holds an f32 copy of a matmul weight. Each matrix goes from the checkpoint's bf16 bytes to the GPU, and the host keeps only what the host really reads.

What the host still needs as f32

TensorUsed byPlan
embed_tokens (1.45 GiB f32)token embedding row gather (transformer_backend.rs:733, :1036)keep as f32 in the first cut. Later cut: keep bf16 (0.72 GiB) and decode a row on demand
norm weights (input_layernorm, post_attention_layernorm, final_norm, q_norm, k_norm)the rmsnorm ops take &[f32] and stage them to the GPU every callkeep as f32, about 1 M values, negligible
lm_head (tied)compute_logitsresident only (shares the embedding's values, own transposed device copy)
q/k/v/o/gate/up/downmatmulsresident only

Storage type

In aien-inference-abi/src/weights.rs:

pub enum MatrixWeight {
    Host(Vec<f32>),            // CPU reference, dev builds, tests, non-GB10 models
    Resident(ResidentWeight),  // GB10 production: owns the device copy, no host values
}
pub struct ResidentWeight { id: WeightId, k: usize, n: usize, tensor: ResidentTensor }

TransformerLayerWeights fields q_proj .. down_proj become MatrixWeight; lm_head becomes Option<MatrixWeight>; embed_tokens and the norms stay Vec<f32>. TensorBackend::matmul_vec, matmul_batch and compute_logits take &MatrixWeight instead of &[f32]. The 23 call sites in transformer_backend.rs change mechanically. ReferenceCpuBackend accepts only Host; given a Resident it returns a loud error (it has no host values to compute with), which is consistent with strict.rs (a production build refuses CPU fallback anyway). The dev build with AIEN_DEV_FALLBACK keeps working by loading Host (no residency) or by a documented "resident plus host" load mode that keeps both for parity tests.

Backend resident cache

OmegaGb10Backend today keys its cache on (pointer, length, in, out) plus a sampled fingerprint (omega_backend.rs:52, :194-199) because it cannot tell when a borrowed slice changes. With ResidentWeight the device copy is owned by the weight and immutable, so no cache and no fingerprint is needed for it: the backend calls matmul_f32 on the handle. A Host weight on the Omega backend keeps the existing lazy path (tests, TinyLlama, Llama-3.2-1B continue to pass unchanged until they are migrated). Dropping TransformerWeights drops each ResidentTensor, which calls omega_gpu_tensor_free (aien-omega-gpu lib.rs:306-311). Ownership becomes one-way and visible in the type.

Upload straight from the checkpoint

Serving-time reservation (no driver allocation after start-up)

Two parts, because scratch and code buffers behave differently.

  1. Scratch, no omega change (first cut): after the session opens and the weights are resident, run a "serving envelope" warm-up that calls the backend once at the maximum of each dimension: attention at seq_len 4096 (32 MiB pool), matmul at the largest planned rows for each shape (prefill chunk 128 rows; decode rows = the scheduler's max_batch_size, 256 in the daemon, commands.rs:1478) including the logits shape, and elementwise at 64 rows. Because scratch only grows and never shrinks (session.c:97-105), every later call fits and no realloc happens. Envelope size COMPUTED: about 32 MiB attention + about 150 MiB matmul results at 256 decode rows
    • about 20 MiB matmul inputs + under 3 MiB elementwise, roughly 0.2 GiB. The envelope limits come from startup configuration (context cap, scheduler limits), logged, and a request beyond it is refused with a clear message instead of reaching the driver.
  2. Kernel code, needs a small omega change: raise CACHE_SLOTS in matmul_api.c:28 from 8 to 16 (covers 12 shapes with headroom), so no code buffer is evicted. Before relying on it, confirm the thrash with a CPU-only test on omega's fake native layer (tests/fake_m16_native.c already counts uncached_calls and free_calls): replay one prefill chunk, one decode and one more prefill and assert the allocation counters. If the thrash is not real, skip the change.
  3. Guard (omega, optional): a "seal" call after warm-up so that any later allocation attempt returns a named error (ENVELOPE_EXCEEDED) instead of an opaque NV_ERR_NO_MEMORY, and a pair of counters (allocs, frees since seal) exposed through the Rust crate for the receipts.

Peak memory estimate (COMPUTED, Qwen3-4B bf16, to be measured)

Today (measured / computed)After cut 1 and 2
Process memory at load peak22.48 GiB measured (f32 14.98 + raw bf16 7.49)about 2.2 GiB (embedding 1.45 f32 + largest transient 0.72 for lm_head bf16 transpose); shard pages are reclaimable cache
Process memory in steady state14.98 GiB f32about 1.5 GiB (embedding + norms)
Pinned driver memory, steadyabout 6.8 GiB quoted, 7.48 GiB computed7.48 GiB resident + about 0.2 GiB envelope + session
Total the machine must findabout 22.5 GiBabout 9.2 GiB
Transient at each uploadup to 2.2 GiB (lm_head f32 transpose + bf16)up to 0.72 GiB

Saved: about 13.5 GiB of ordinary memory for the layer matrices, 14.2 GiB if the embedding is later kept as bf16. The KV pool (host) is unchanged and is set by AIEN_KV_CONTEXT_TOKENS.

Failure modes and how the design handles them

3. Declared test conditions and acceptance criteria

These follow the conventions of ~/workspace/hive/WQWEN/c277/run-277.sh (sealed script, written declaration before any run, one variable, base first, fixed run order, camera, no kills, receipts with SHA256SUMS). They are written now and must be frozen in a DECLARATION file before the first run.

Question: with the weight-ownership change, does the Qwen3-4B GB10 path pass in the organic low-MemFree state where the unchanged baseline fails, without dropping the page cache?

4. CPU-side changes that are safe to land now (behind the existing refusal)

ChangeDiff estimateRiskNotes
MemFree, Cached and MemAvailable in the start-up log and in each session-open attempt line (#277 item 3): extend read_mem_available into a small meminfo reader, add a second closure or a struct to open_gpu_session_with_retryabout 60 lines plus 2 unit tests (parse, missing field)none: logging only, stub-testabletouches the same function as #278; land after or rebase on it
Print the planned resident total and the planned serving envelope at start-up (computed from the config, like KvPoolPlan), and the expected pinned total next to MemFreeabout 80 lines plus testsnone: no allocation behaviour changesgives the operator the numbers the declaration needs
OmegaGb10Backend::resident_bytes() and a count of uploadsabout 25 linesnonefeeds receipts
Safe wrapper ResidentTensor::upload_bf16 and its stub/native testsabout 50 lineslow: not called by defaultprepares cut 1
Streaming checkpoint reader that yields one tensor at a time from a memory map (new function, old one unchanged)about 120 lines plus testslow: unused until wiredprepares cut 1

Not safe to land now without a chip run: the serving-envelope warm-up (it launches kernels at new sizes on models that are accepted today, so it needs chip qualification), the MatrixWeight type change (cross-crate, about 700 to 900 lines changed across abi, inference-runtime, runtime, cli and tests; it needs its own reviewed PR and the parity test), and the omega CACHE_SLOTS / seal changes (separate omega PR, then an omega.lock bump).

  1. Cut A (CPU, small, safe now): MemFree log plus resident and envelope plan lines plus resident byte counters. Gives the next declared attempt its numbers and costs no chip time.
  2. Cut B (DONE, see Update: omega#332 measured, omega#333 reserves) (CPU test on omega's fake layer): confirm or refute the matmul code-cache thrash and the per-token pool reallocation with allocation counters, no GPU. This decides whether the omega changes are needed.
  3. Cut C (the real change): MatrixWeight, streaming bf16 upload, session-first order, envelope warm-up; stub parity tests on CPU first, then one declared chip attempt as in section 3.
  4. Cut D: flip the default refusal, only after a PASS verdict.

Open items I could not settle from source: why the baseline passed in the scripted state (trigger UNKNOWN); whether the driver needs physically contiguous pages for the failing buffers (DOCS SILENT per #277); how many rows a real mixed prefill and decode step puts into one matmul in the daemon (the 256-row envelope is a bound from max_batch_size, not a measurement); the exact resident total (7.48 GiB computed against about 6.8 GiB quoted; the gap is about the lm_head: 6.76 GiB is the layers alone, the lm_head adds 0.72 GiB, so the quote likely omits it).