Log inStart free
LESSON 6.01 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Tensors, views and strides

Watch one row of memory become a grid, a transpose and a GPU copy.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 6.01 · TENSOR BAY12 NUMBERS · INT64 · 96 BYTES
01
Stage 1 of 6 · Storage
torch.arange(0, 120, 10) writes 12 numbers into one row of memory. That row is the storage.
Explain it simply

Every tensor keeps its numbers in one long row of memory, called its storage. Here there are 12 numbers, 0 to 110 in steps of 10. Each is an int64 of 8 bytes, so the row takes 96 bytes. Other makers like zeros or rand fill a new storage the same way.

12 storage cells hold 0 to 110 in steps of 10. Offsets 0 to 11 sit under the cells. The tensor t has shape (12,) and stride (1,).

storage and xshape, strides, offsetviews and copiesa jump no stride fitsa view's cells
001012023034045056067078089091001011011t = torch.arange(0, 120, 10)t: shape (12,) · stride (1,)12 × 8 = 96 bytesstorageCPU memoryoffset12 numbers × 8 bytes = 96 bytes · addr(k) = base + 8·k001012023034045056067078089091001011011t = torch.arange(0, 120, 10)t: shape (12,) · stride (1,)12 × 8 = 96 bytesstorageCPU memoryoffset12 numbers × 8 bytes = 96 bytes · addr(k) = base + 8·k
Toy: 12 int64 numbers · PyTorch 2 rules · GPU copy drawn, not run
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
addr(k)=base+8 k\mathrm{addr}(\sy{k}{data}{k}) = \sy{base}{param}{\mathrm{base}} + \sy{e}{param}{8}\,\sy{k}{data}{k}
addr(6)=base+8⋅6=base+48\mathrm{addr}(6) = \mathrm{base} + 8 \cdot 6 = \mathrm{base} + 48

Each number sits 8 bytes after the number before it.

Common mix-ups

MYTHA transpose or a slice copies the numbers.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With shape 4×3, which storage offset does x[1, 2] read?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.