Memory Budget Allocator
A stacked-bar visualisation of the four cohabiting memory buckets that fight for a single GPU's HBM during training: weights, gradients, optimizer state (Adam's m + v moments), and activations. Users drag four sliders — model size, batch size, sequence length, precision — and read off whether the configuration fits in a fixed capacity (default 80 GiB, an A100).
Training memory budget. 7B parameters, batch 1, sequence 2K, BF16. Total 52.7 GiB of 80.0 GiB — fits.
Training memory7B params · b=1 · s=2K · BF16
Totalof 80.0 GiB budget52.7 GiB
27.3 GiB headroomWeights13.0 GiB
Grads13.0 GiB
Optimizer26.1 GiB
Activations0.50 GiB
7B
1
2K
Customize
Model
7.0B
BF16
Workload
1
2K tokens
Budget
80 GiB
Installation
npx shadcn@latest add https://craftbits.dev/r/memory-budget-allocator.jsonUsage
import { MemoryBudgetAllocator } from "@craft-bits/core";
<MemoryBudgetAllocator
defaultParams={{
modelSizeBn: 7,
batchSize: 1,
seqLen: 2048,
precision: "bf16",
}}
capacityGb={80}
/>Drive the parameters from outside:
<MemoryBudgetAllocator
params={params}
onParamsChange={setParams}
capacityGb={80}
/>Hide the per-bucket breakdown when you want a single total bar:
<MemoryBudgetAllocator showBreakdown={false} />Understanding the component
- One formula per bucket.
weights = modelSizeBn * 1e9 * bytesPerParamgrads = weightsoptimizer = 2 * weights(Adam's m + v moments, simplified)activations = batchSize * seqLen * hidden * layers * bytesPerParamwithhidden = 4096,layers = 32(a 7B-shaped reference).
- Stacked bar with capacity tick. The bar normalises against
max(capacityGb, totalGb)so the capacity hairline stays anchored at the right edge until the total exceeds the budget. Anything past the tick paints incb-error. - Verdict line. Below the bar, the caption switches between
{headroom} GiB headroomandexceeds by {overflow} GiBso the pass/fail state is stated textually too — colour is never the only signal. - Breakdown legend. Each of the four slices has a tinted swatch, a textual label, and a GiB readout. Tints map to the same semantic tokens as elsewhere in the library (
cb-accent/cb-info/cb-warning/cb-success). - Controlled or uncontrolled.
paramsfollows the Radix pattern — passparamsplusonParamsChangefor controlled, ordefaultParamsfor uncontrolled. The precision pills are arole="radiogroup"ofrole="radio"buttons. SPRINGS.smootheverywhere. Bar-width changes animate with the canonical smooth spring;prefers-reduced-motion: reducecollapses every spring to an instant swap.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
params | { modelSizeBn, batchSize, seqLen, precision } | — | Controlled parameter bag. |
defaultParams | { modelSizeBn, batchSize, seqLen, precision } | { 7, 1, 2048, "bf16" } | Uncontrolled initial parameter bag. |
onParamsChange | (params) => void | — | Fires when any slider commits a new value. |
capacityGb | number | 80 | GPU memory capacity in gibibytes (the budget line and verdict divisor). |
showBreakdown | boolean | true | Render the four per-bucket slices and legend. |
modelSizeBnMin | number | 0.5 | Minimum modelSizeBn the slider allows. |
modelSizeBnMax | number | 175 | Maximum modelSizeBn the slider allows. |
batchSizeMin | number | 1 | Minimum batchSize the slider allows. |
batchSizeMax | number | 64 | Maximum batchSize the slider allows. |
seqLenMin | number | 512 | Minimum seqLen the slider allows. |
seqLenMax | number | 32768 | Maximum seqLen the slider allows. |
transition | Transition | SPRINGS.smooth | Spring used for slice-width transitions. |
className | string | — | Merged onto the root via cn(). |
Accessibility
- The figure is
role="figure"with a hidden summary listing model size, batch, sequence, precision, total, capacity, and the verdict — screen readers hear the story whenever props change. - The precision picker is a
role="radiogroup"ofrole="radio"pills witharia-checked. Tab focuses the group; Space and Enter commit a selection. - Sliders are native
<input type="range">via the library'sLabeledSliderwitharia-valuemin/aria-valuemax/aria-valuenow/aria-valuetext— full keyboard + screen-reader semantics for free. - Colour is never the only signal — every bucket has a textual label and a GiB readout, and the fits / exceeds verdict is stated as words too.
- Motion respects
prefers-reduced-motion: reduce.
Credits
- Extracted from:
craftingattention(app/src/lessons/primitives/nn/MemoryBudgetAllocator.tsx). Stripped the inference-focused stacked bar (weights / KV cache / activations), the phase-based narration state machine (observe / optimizing / insight), the GPU-capacity threshold markers, the breathing-pulse hint, the throughput readout, and the discrete-step sliders. The library extract is the pure plotting primitive: training-time memory (weights + grads + optimizer + activations) against a single configurable capacity line, driven entirely by props with motion-library springs and a controlled / uncontrolledparamsbag.