Unit 6 of 7 · about 3 hours · explain, implement

Tensor layout: shape, stride, views and copies

A tensor is storage plus an interpretation. Backward passes that assume contiguous input misread transposed data, and view-versus-copy mistakes turn a reshape into silent corruption.

You'll be able to

  • Compute an element address from shape, stride and index, and predict contiguity after transpose or slicing.
  • Classify each reshape, transpose, slice and index operation as view or copy, and state the reshape and indexing rules you may not rely on.
  • Handle noncontiguous inputs in your own engine: read them correctly through strides or copy to contiguous first, and justify the choice.

Covers

Shape, axes, dtype, layout, stride and storageView, copy, reshape, transpose and noncontiguous inputs
1

Read

In this order, only the parts listed

0 of 3 done

  1. Tensor creation, shape and memory reuseD2L - tensors, autograd, MLP and backpropagation

    Getting Started: arange, zeros, ones, shape, size and reshape with -1 inference; Indexing and Slicing; Saving Memory: Y = Y + X reallocates (id changes) while Z[:] = X + Y and X += Y reuse storage

    The page never defines stride, view or storage: those come from the two PyTorch notes below. Use this page for construction, shape inference and the realloc-versus-reuse demos.

  2. Which ops share storagePyTorch Tensor Views

    The view/storage sharing example with data_ptr and mutation through the view; the transpose example turning a contiguous tensor non-contiguous with no data movement; the full view-ops list; reshape may-copy and contiguous conditional-copy notes; basic indexing returns views while advanced indexing copies

  3. Strides as element steps`torch.Tensor.stride` - stride and storage offset

    Stride definition: per-dimension element steps that, with storage_offset, locate every element; combine with the Tensor Views transpose and expand layouts

2

Focus on

  • A tensor is storage plus shape, stride, offset and dtype. Element [2, 1] of a (3, 4) C-contiguous float32 tensor sits at offset , so the strides are (4, 1) in elements. NumPy reports the same layout in bytes: (16, 4) for float32.
  • Transpose swaps shape and strides without moving data: (3, 4) with strides (4, 1) becomes (4, 3) with strides (1, 4), non-contiguous but still a view, so [1, 2] of the transpose aliases offset 9 of the base.
  • Reshape, reshape_as and flatten may return a view or a copy: never rely on which. view() demands a compatible layout instead. Basic indexing gives views; advanced indexing (integer or boolean arrays) copies.
  • Expand is a stride-0 view: no storage advances along the stretched axis, which is why perturbing one expanded element moves all its aliases and breaks point-wise gradient checks.
Common mistakes · 3
  • Assuming transpose or reshape copies. Both can be views: writing through them mutates the base, and the docs mutate t through b to prove it.
  • Assuming view() always works. It needs a compatible layout; a transposed tensor needs reshape (which may copy) or .contiguous() first.
  • Mixing byte and element strides. Torch strides count elements; NumPy .strides counts bytes. Convert with itemsize before comparing.
3

Try it yourself

Closed-book, before checking anything

Closed-book, for a C-contiguous (3, 4) float32 tensor: give strides (4, 1) and offset 9 for [2, 1]; after transpose give shape (4, 3), strides (1, 4), non-contiguous, with [1, 2] aliasing offset 9. Then classify view, reshape, transpose, x[0] and x[[0, 2]] as copy or view.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A (3, 4) float32 tensor is C-contiguous. What are its strides, and what is the offset of [2, 1]?Answer

    Strides (4, 1) in elements. Offset .

  2. 2.Transpose it. What are the new shape, strides and contiguity, and which base offset does [1, 2] read?Answer

    Shape (4, 3), strides (1, 4), non-contiguous view. Offset , the same element.

  3. 3.Which of these copy: x[0], x[[0, 2]], x.reshape(-1), x.transpose(0, 1)?Answer

    Only x[[0, 2]] is guaranteed to copy (advanced indexing). x[0] and the transpose are views; reshape may be either, so code must not rely on it.

  4. 4.Y = Y + X versus Y[:] = X + Y: which reallocates, and how do you tell?Answer

    The first reallocates: id(Y) changes. The second reuses storage. D2L demonstrates both with id().

You're done when

You compute strides, offsets and aliasing for transpose and expand cases from memory, and your engine reads a transposed input correctly either through strides or through an explicit contiguous copy.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 10 more in the section
  • D2L - tensors, autograd, MLP and backpropagationD2L authors · required

    Use chapter_preliminaries ndarray and autograd, then MLP, backpropagation, and numerical stability and initialization. Recreate scalar operations, affine maps, activation, reduction, stable cross-entropy, topological backward and unbroadcast in a blank file.

  • PyTorch Tensor ViewsPyTorch · reference

    Read the view and storage sharing example (data_ptr, mutating the view changes the base), the transpose non-contiguous example, the full view-ops list, and the special notes: reshape may copy, contiguous copies only when needed, basic indexing returns views while advanced indexing copies.

  • `torch.Tensor.stride` - stride and storage offsetPyTorch · reference

    Read the stride definition: strides are per-dimension element steps, and with storage_offset they locate every element. Combine with the Tensor Views page for transposed and expanded layouts.

Also in this section

  • Understanding Deep Learning - Chapters 3 to 7Simon J. D. Prince · primary

    Read Chapters 3-7, emphasizing Chapter 5 for likelihood-derived losses and Chapter 7 for backpropagation and initialization. Use notebooks 3.1, 4.1, 5.1, 6.1 and 7.1 only as equation and plot companions.

  • TensorTonic - Build Micrograd from ScratchTensorTonic · required

    Use the 25-problem Micrograd study plan in order for derivatives, gradient checks, value graphs, reverse mode, accumulation, operators, modules, losses and training. Preserve local tests as the evidence instead of a platform score.

  • Deep-ML candidate - autograd and loss problemsDeep-ML · optional

    After the five-sample platform check passes, use at most one of 26 Basic Autograd Operations, 997 Manual Cross-Entropy Backprop or 1227 Stable Softmax as a changed-prompt drill.

  • CS231n Linear Classification - Softmax classifier and numeric stabilityStanford CS231n · reference

    Read the Softmax classifier section for the cross-entropy form , then the Practical issues: Numeric stability part for the max-subtraction code f -= np.max(f).

  • CS231n Neural Networks 3 - Gradient checksStanford CS231n · reference

    Read the Gradient Checks section only: centered formula with error, relative error with thresholds, double precision, step size near 1e-5, kinks, few datapoints, burn-in, regularization and dropout discipline, and checking a few dimensions per parameter.

  • PyTorch Broadcasting semanticsPyTorch · reference

    Read General semantics for the trailing-dimension rules, prepend-1 alignment and max-size result, with the (5, 1, 4, 1) plus (3, 1, 1) examples; read In-place semantics for why an in-place tensor may not change shape.

  • `torch.autograd.gradcheck` API and tolerancesPyTorch · reference

    Read the gradcheck signature defaults (eps 1e-6, atol 1e-5, rtol 1e-3) and the note that inputs with overlapping memory, such as expand outputs, will likely fail the check.

  • `torch.nn.functional.cross_entropy` - `reduction` and `ignore_index`PyTorch · reference

    Read the ignore_index and reduction parameters: ignored targets contribute no gradient, and mean reduction averages over the non-ignored targets only.

  • PyTorch Autograd mechanics - in-place operationsPyTorch · reference

    Read In-place operations with autograd (why in-place rewrites the graph and is discouraged) and In-place correctness checks (the version counter and the error on accessing a modified saved tensor).

  • `torch.nn.Module` - parameters, buffers and statePyTorch · reference

    Read the parameters and buffers documentation: modules collect parameters recursively for the optimizer, while register_buffer holds persistent non-parameter tensors saved alongside them.