This LLM Systems concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.
LLM Systems
GPU Memory Accounting
A byte-ledger mental model for what fills GPU memory during LLM training and serving, and why the dominant bucket changes by scenario.
Concept Structure
GPU Memory Accounting
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learner Contract
What this page should let you do.
3 prerequisites listed; refresh them before leaning on the math or code.
Explain the mechanism, trace the main notation, and test one prediction in the live demo.
Read the intuition before the notation; the math should name a mechanism you already felt.
Follow this edge after making one prediction here; the next page should reuse the result, not restart the route.
Claim/source review status
Substantive review recorded
1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
GPU memory errors are often reported as one blunt fact: "out of memory." That message hides the important question: which object was trying to fit?
Source spine: Rajbhandari et al., ZeRO, Chen et al., Training Deep Nets with Sublinear Memory Cost, Kingma and Ba, Adam, Kwon et al., PagedAttention, PyTorch CUDA memory docs, Hugging Face KV cache docs, and Stanford CS336.
The first move is to stop treating memory as one number. Treat it as a ledger:
- Weights: the model parameters you must keep around.
- Gradients and optimizer state: training-only state such as gradients, Adam moments, and sometimes a master parameter copy.
- Saved activations: intermediate values kept from the forward pass so backward can run.
- KV cache: serving-time attention keys and values saved for previous tokens during autoregressive generation.
- Temporary buffers: workspaces, communication buffers, kernel scratch space, and framework internals.
- Allocator slack: memory reserved by the runtime but not currently occupied by live tensors.
The learner trap is assuming one bucket always dominates. In an Adam training step, optimizer and gradient state can dwarf the weights. In short-prompt inference, weights often dominate. In long-context serving, the KV cache can become the thing that tips the request over the budget.
This page is a conceptual byte ledger. It is not a profiler. The numbers below are deliberately small enough to inspect and deliberately explicit about their assumptions.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be the number of parameters and be bytes per stored model weight. The parameter memory is
During training, a simple Adam-style mixed-precision ledger can include gradients, two moment buffers, and sometimes a full-precision master copy. If is gradient bytes per parameter and is optimizer-state bytes per parameter, then
For a toy Transformer training step with batch size , sequence length , hidden width , layers , activation dtype bytes , and saved activation-like tensors per layer, a coarse saved-activation ledger is
Activation checkpointing changes : fewer activations are saved, but more forward computation is recomputed during backward. That is why "fits" and "fast" are different questions.
During autoregressive serving, the backward buckets disappear, but the KV cache grows with generated or retained context:
The factor of stores both keys and values. is the number of key/value heads, not necessarily the number of query heads. Grouped-query or multi-query attention changes this ledger by changing .
Finally, the runtime memory you see can be larger than live tensor memory:
That distinction matters because an allocator may reserve memory for reuse even when fewer live tensors are currently occupying it.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
This witness computes two toy ledgers: one Adam training step and one long-context serving request. It is not a hardware profiler; it just makes every assumption explicit.
def gb(bytes_):
return bytes_ / 1e9
def train_ledger(params_b=1.5, layers=24, hidden=2048, batch=8, seq=1024):
p = params_b * 1e9
weights = p * 2
gradients = p * 2
optimizer = p * (4 + 4 + 4) # master copy + Adam m,v
activations = batch * seq * hidden * layers * 6 * 2
temporary = 0.08 * (weights + gradients + optimizer + activations)
return {
"weights": gb(weights),
"optimizer+gradients": gb(optimizer + gradients),
"activations": gb(activations),
"temporary": gb(temporary),
}
def serving_ledger(params_b=7, layers=32, batch=8, tokens=32768, h_kv=8, d_head=128):
weights = params_b * 1e9 * 2
kv = batch * layers * tokens * h_kv * d_head * 2 * 2
temporary = 0.05 * (weights + kv)
return {"weights": gb(weights), "kv_cache": gb(kv), "temporary": gb(temporary)}
for name, ledger in {"train": train_ledger(), "serve": serving_ledger()}.items():
winner = max(ledger, key=ledger.get)
print(name, winner, {k: round(v, 2) for k, v in ledger.items()})
The point is not that these constants are universal. The point is that every memory estimate should say which buckets are included, which dtype was assumed, and whether it is training or serving.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Before reveal, choose which bucket you think dominates the selected scenario. The lab hides exact GB values, fit verdicts, and allocator slack until you commit.
Live Concept Demo
Explore GPU Memory Accounting
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what GPU Memory Accounting should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
A byte-ledger mental model for what fills GPU memory during LLM training and serving, and why the dominant bucket changes by scenario.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change GPU Memory Accounting should make visible.
Visual Inquiry
Make the image answer a mathematical question
A byte-ledger mental model for what fills GPU memory during LLM training and serving, and why the dominant bucket changes by scenario.
Which visible object should carry the first intuition?
Pick the cue that should make GPU Memory Accounting easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Primary training-memory source for model states, optimizer states, gradients, parameters, activations, temporary buffers, and fragmented memory.
Open sourcePrimary activation-checkpointing source for trading saved intermediate activations for recomputation.
Open sourcePrimary optimizer source for first- and second-moment moving averages used by Adam-style memory accounting.
Open sourcePrimary serving-memory source for dynamic KV-cache growth, fragmentation, and paged/block allocation.
Open sourceDocumentation source for the difference between tensor-allocated memory and caching-allocator-reserved memory.
Open sourceDocumentation source for dynamic/static/offloaded/quantized KV-cache strategy tradeoffs.
Open sourceClaim Review
A byte-ledger mental model for what fills GPU memory during LLM training and serving, and why the dominant bucket changes by scenario.
Claims without a substantive review badge still need exact source-support review.
rajbhandari-2020-zero, chen-2016-sublinear-memory, kingma-ba-2015-adam, kwon-2023-pagedattention, pytorch-cuda-memory-212, hf-transformers-kv-cache
Use equations, runnable code, and demos to check whether the source support is operational.
ZeRO supports the training-state bucket split; Adam supports first/second moment optimizer state; Chen et al. support activation checkpointing as a memory/recompute tradeoff; PagedAttention supports KV-cache growth and fragmentation as serving constraints; PyTorch distinguishes tensor-allocated from allocator-reserved memory; Hugging Face documents cache strategies with memory/latency tradeoffs.
Sources: ZeRO: Memory Optimizations Toward Training Trillion Parameter Models, Training Deep Nets with Sublinear Memory Cost, Adam: A Method for Stochastic Optimization, Efficient Memory Management for Large Language Model Serving with PagedAttention, PyTorch CUDA memory APIs, Transformers KV cache strategiesThe page uses toy byte formulas and fixed assumptions. It is not a profiler, hardware sizing guide, vLLM benchmark, PyTorch allocator guarantee, or production OOM predictor.A bounded review summary is present; still check caveats and exact reference scope.Downloaded and searched ZeRO, activation-checkpointing, PagedAttention, and Adam PDFs plus current PyTorch/Hugging Face docs. The concept is source-supported as a scenario-explicit pedagogical ledger, not as a measured runtime or current serving-stack claim. GPT Pro publication critique remains pending because 127.0.0.1:51672 refused connection.
Reviewer: codex-local-primary-source-audit; reviewed 2026-07-02Source support candidates
paper 2020ZeRO: Memory Optimizations Toward Training Trillion Parameter ModelsPrimary training-memory source for model states, optimizer states, gradients, parameters, activations, temporary buffers, and fragmented memory.
paper 2016Training Deep Nets with Sublinear Memory CostPrimary activation-checkpointing source for trading saved intermediate activations for recomputation.
paper 2015Adam: A Method for Stochastic OptimizationPrimary optimizer source for first- and second-moment moving averages used by Adam-style memory accounting.
paper 2023Efficient Memory Management for Large Language Model Serving with PagedAttentionPrimary serving-memory source for dynamic KV-cache growth, fragmentation, and paged/block allocation.
Practice Loop
Try the idea before it explains itself
A byte-ledger mental model for what fills GPU memory during LLM training and serving, and why the dominant bucket changes by scenario.
Before touching the demo, predict one visible change that should happen in GPU Memory Accounting.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to a claim, equation, code, or demo
Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.Open the draft below to save one note and next action in this browser.
GPU Memory Accounting
What is the smallest example that makes GPU Memory Accounting click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays in this browser, attached to the selected learning item.
- References to inspect: attached references on this page.
- Definition, prerequisite, and contrast concept links
- The equation or runnable code that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - GPU Memory Accounting Selected item key: recorded for copy. Context: LLM Systems Page anchor: recorded for copy. Open question: What is the smallest example that makes GPU Memory Accounting click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/llm-systems/gpu-memory-accounting
concept:llm-systems/gpu-memory-accounting