byhandPro Tools

Size

Every axis a slider

For AI and ML professionals, sizing a model is a recurring pain. Will it fit at 8k context? At the batch you need? On the GPU you have? The usual answers are a config edit, a load and an out-of-memory error, or back-of-envelope formulas that miss what dominates at long context: SmolLM-135M has 513 MiB of weights but 5.6 GiB of activations at 4,096 tokens.

The Size tool turns every axis of a model into a slider. Drag one and it counts the model as you go, from the program itself: parameters, weights, peak activations, KV cache and compute, and the line where memory peaks. The sliders come in two groups:

  • Input, what the data decides: batch, sequence length, an image's height and width.
  • Model, what the architecture decides: width, heads, layers, channels.

An axis that cannot move is greyed out, with the reason: a language model's vocabulary is set by its tokenizer, and its heads follow the width.

Try it on a small MLP

Drag the width. The table counts the new model, and Shape redraws every tensor at its new size.

Size
fp32fp16
Parameters4646
Weights184 B92 B
Peak activations176 B88 B
KV cache per tokennot a decoder
Compute per input90 FLOPs90 FLOPs

Input

  • batch · b4

Model

  • width · d4
Shape
b4d3
d3b1b4xd4b1b4d4b1b4d4b1b4d4b1b4d2b1b4d2b1b4yd4biasd4d3weight0d4biasd4d4weight0d2biasd2d4weightΣ=1
Open in byhand Pro →

Then on SmolLM-135M

Now a real language model: 30 layers, 135 million parameters, a vocabulary of 49,152.

Size

The program does not check: nothing to size.

Shape
Open in byhand Pro →

Input Size

Drag the sequence from 8 to 4,096 tokens. At a batch of 4, the peak activations go from 6 MiB to 389 MiB at 512, and to 5.6 GiB at 4,096. The weights stay at 513 MiB, and the KV cache at 45 KiB a token: at long context, what you pay for is the activations.

The table also says where the peak is, and it moves. At short lengths it is the last line, which turns every position into 49,152 vocabulary scores. From about 1,760 tokens the attention scores take over: they grow with the square of the length. In Shape, every box along the sequence widens as you drag.

Model Size

Drag the width from 576 to 1,152 and SmolLM goes from 135M parameters to 481M; its heads follow, 3 to 6, each still 64 wide. Take the layers from 30 to 69 and it is a 1.03B model. The count is the same at fp32 and fp16: the type changes the bytes, not the count.

How it counts

Every model in the library is a .byhand program, and a program states the shape of every tensor it stores and every tensor it makes. So the Size tool does not guess from a config, and runs nothing on a GPU: it counts from the program. Put the program beside it and drag:

byhand
  1. 1# A small MLP, small enough to draw every neuron: 3 inputs, two hidden layers of 4, 2 outputs.
  2. 2axis b = batch
  3. 3axis d = feature
  4. 4byhand x[b4,d3]
  5. 5 | linear d4 bias
  6. 6 | relu
  7. 7 | linear d4 bias
  8. 8 | relu
  9. 9 | linear d2 bias
  10. 10 | softmax d
  11. 11 => y
Size
fp32fp16
Parameters4646
Weights184 B92 B
Peak activations176 B88 B
KV cache per tokennot a decoder
Compute per input90 FLOPs90 FLOPs

Input

  • batch · b4

Model

  • width · d4
Open in byhand Pro →

A slider edits the program: the new size goes into the code, the changed lines marked. ↶ undoes it, Reset sizes goes back to the original, and the link keeps your sizes.

Each number is a sum over the program, at bytes a number (4 at fp32, 2 at fp16; for the weights, for the activations, apart once a model is quantized). For example:

numberformula
Parameters
Weights
Peak activations
KV cache per token
Compute per token

While you drag, the numbers are quick estimates so they keep up with your hand. When you let go, they are counted exactly.

Roadmap

  • Training memory: gradients and optimizer state, for a fine-tune or a run from scratch. Today Size counts a forward pass.
  • Fused kernels: the peak as FlashAttention runs it, the attention scores never stored. Today they are counted in full.
  • Runtime overhead: a framework's allocator and workspace. Today the peak is the program's, not the GPU's.
  • Fit on a GPU: pick a device, see the largest batch and context that fit.