Neural Ninjas · PyTorch Internals Lab

Tensor Shape Lab: The Memory-Level Reality

Same blocks moving = zero-copy. New blocks appearing = a real memory allocation. Watch which one each operation actually does.

Base tensor — torch.randn(2, 3, 4)
shape [2, 3, 4] · contiguous · fresh memory
💥

Click an operation above. Watch the memory panel on the right — that's where the real story is.

📦 Memory Panel
🟢 fresh allocation
Address0x7f8a3c2d4e00
Shape[2, 3, 4]
Strides[12, 4, 1]
Contiguous✅ True
Footprint96 bytes
vs. before
Op time0.0001 ms
offset = Σ stride[k] × idx[k]
current: [12,4,1]
Real copies made this session: 0
📖 strides = page numbers
📚 contiguous = books in order
🏷️ view = relabel, don't move
🧠 Mental models
ConceptVisual analogy
StridesPage numbers in a book — how far to skip to reach the next item
ContiguousA bookshelf with books in order — no gaps, no jumps
.permute() / .transpose()Relabeling which shelf is "row" vs "column" — same books, new labels, shelf now out of natural order
.view()Changing book labels without moving a single book — fails if the shelf's already out of order
.reshape()Relabels if it can — reorganizes the whole shelf (copies) if it can't