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 headroom
Weights13.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.json

Usage

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

  1. One formula per bucket.
    • weights = modelSizeBn * 1e9 * bytesPerParam
    • grads = weights
    • optimizer = 2 * weights (Adam's m + v moments, simplified)
    • activations = batchSize * seqLen * hidden * layers * bytesPerParam with hidden = 4096, layers = 32 (a 7B-shaped reference).
  2. 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 in cb-error.
  3. Verdict line. Below the bar, the caption switches between {headroom} GiB headroom and exceeds by {overflow} GiB so the pass/fail state is stated textually too — colour is never the only signal.
  4. 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).
  5. Controlled or uncontrolled. params follows the Radix pattern — pass params plus onParamsChange for controlled, or defaultParams for uncontrolled. The precision pills are a role="radiogroup" of role="radio" buttons.
  6. SPRINGS.smooth everywhere. Bar-width changes animate with the canonical smooth spring; prefers-reduced-motion: reduce collapses every spring to an instant swap.

Props

PropTypeDefaultDescription
params{ modelSizeBn, batchSize, seqLen, precision }Controlled parameter bag.
defaultParams{ modelSizeBn, batchSize, seqLen, precision }{ 7, 1, 2048, "bf16" }Uncontrolled initial parameter bag.
onParamsChange(params) => voidFires when any slider commits a new value.
capacityGbnumber80GPU memory capacity in gibibytes (the budget line and verdict divisor).
showBreakdownbooleantrueRender the four per-bucket slices and legend.
modelSizeBnMinnumber0.5Minimum modelSizeBn the slider allows.
modelSizeBnMaxnumber175Maximum modelSizeBn the slider allows.
batchSizeMinnumber1Minimum batchSize the slider allows.
batchSizeMaxnumber64Maximum batchSize the slider allows.
seqLenMinnumber512Minimum seqLen the slider allows.
seqLenMaxnumber32768Maximum seqLen the slider allows.
transitionTransitionSPRINGS.smoothSpring used for slice-width transitions.
classNamestringMerged 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" of role="radio" pills with aria-checked. Tab focuses the group; Space and Enter commit a selection.
  • Sliders are native <input type="range"> via the library's LabeledSlider with aria-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 / uncontrolled params bag.