Unit 1 of 9 · about 3 hours · explain

Eager execution, autograd nodes and saved tensors

Every memory number and profiler row later in this section is a consequence of this graph. If you cannot name the saved tensors, you cannot explain what checkpointing or the profiler is measuring.

You'll be able to

  • Trace one eager operation from the Python call to its backward Function node and name each node on the path.
  • Explain which forward tensors autograd must keep for backward, how long they live, and what frees them.
  • Predict whether an in-place edit raises at the edit or at backward, and state what the version numbers mean.

Covers

Eager execution, dispatch and autograd nodesSaved tensors and their lifetimes
1

Read

In this order, only the parts listed

0 of 3 done

  1. How autograd encodes the historyPyTorch - checkpointing, numerical accuracy and CUDA semantics

    How autograd encodes the history: Function graph rebuilt each iteration, grad_fn entry points, what-you-run-is-what-you-differentiate

  2. Saved tensorsPyTorch - checkpointing, numerical accuracy and CUDA semantics

    Saved tensors: the x-squared example, save_for_backward, _saved attribute inspection, the pow identity-True and exp identity-False examples with shared storage

  3. In-place checks and saved-tensor hooksPyTorch - checkpointing, numerical accuracy and CUDA semantics

    In-place operations with autograd and In-place correctness checks: version counters and the no-error-means-correct-gradients guarantee

    Skim Hooks for saved tensors after the pack_hook and unpack_hook contract. Skip the multithreading and complex-number sections.

2

Focus on

  • The graph is rebuilt from scratch every iteration. A grad_fn object from a previous iteration is stale and must never be reused.
  • Forward keeps only what backward needs. For squared it keeps ; the rest of the forward temporaries are free to go.
  • Object identity of a saved tensor is an implementation detail. The pow case returns the same object and the exp case returns a different object, but both share storage and compare equal.
  • The first backward frees saved tensors. A second backward without retain_graph fails because the saved values are gone, not because the graph forgot the formula.
Common mistakes · 3
  • Reading _saved_self is as identity proof. Packing may return a new object sharing storage, so test storage and equality, never identity.
  • Editing a leaf with requires_grad errors at the edit, but editing a saved non-leaf passes and fails at backward with a version error. The two messages describe different checks.
  • Assuming no-grad regions keep no state and therefore cost nothing. They keep no graph, but the forward compute and its live outputs still cost time and memory.
3

Try it yourself

Closed-book, before checking anything

Closed-book on CPU: build y = (x @ W + b).relu().sum() with a , a and a bias of 4, all requiring grad. Walk y.grad_fn through next_functions and write down SumBackward0, ReluBackward0, AddBackward0, MmBackward0 and AccumulateGrad in order. Then show (x ** 2).grad_fn._saved_self is x gives True while its storage is shared.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.For y = (x @ W + b).relu().sum(), name the backward node types from y down to the leaves.Answer

    SumBackward0, ReluBackward0, AddBackward0, MmBackward0, then AccumulateGrad at each leaf.

  2. 2.x.pow(2).grad_fn._saved_self is x is True, but x.exp().grad_fn._saved_result is y is False. Do both saved tensors share storage with the named tensor?Answer

    Yes. Both compare equal and share storage. Whether packing returns the same object depends on whether the saved tensor is an output of its own grad_fn, which is an implementation detail.

  3. 3.The first loss.backward() succeeds and the second raises about saved intermediate values. What was freed, and what flag keeps it?Answer

    The saved tensors were freed after the first backward. Pass retain_graph=True to keep them for a second backward.

  4. 4.Backward fails with expected version 0 but found version 1 on a saved tensor. What do the two numbers mean?Answer

    The tensor was saved at version 0 and edited in place once since. Gradients computed from the edited value could be wrong, so autograd refuses instead of guessing.

You're done when

You complete the reproduce walk without notes, and can predict for any small graph which tensors survive until backward and which edit raises where.

Only when you can do this without notes.

Leads to

Notes

Resources · 1 for this unit, 8 more in the section

Also in this section

  • PyTorch - reliability and performance recipesPyTorch · primary

    Use the Profiler, Automatic Mixed Precision, model loading and Benchmark recipes, then the Performance Tuning Guide. Apply each to one compact model from an earlier section and keep a CPU fallback when CUDA is unavailable.

  • PyTorch - compiler troubleshootingPyTorch · required

    Read graph breaks, guards, recompilations and logging. Compile one stable fragment, intentionally trigger a graph break or recompilation, inspect it, and compare eager and compiled outputs and gradients.

  • Deep Learning Training DebuggerRoadmap-Final · reference

    Use the failure-injection, measurement and evidence rules as the deeper standard. The full Tier 2 project is a later route and is not added to this stage milestone.

  • TensorTonic - mixed-precision memory and transformer FLOPsTensorTonic · support

    Solve only Mixed-Precision Training Memory Accountant and Transformer Training FLOP Estimator. Reconcile both with the compact model's profiler and document every included and excluded term.

  • Learn PyTorch - profiler walkthroughDaniel Bourke · optional

    Use notebook 10 only if the official profiler route remains unclear. Transfer the relevant annotations into the compact model instead of keeping the tutorial code.

  • TrenTorch - precision, checkpointing and resumeTrenTorch · support

    Solve Loss Scaling, FP16/BF16 Representable Range, Gradient Checkpointing and Resume-from-checkpoint in NumPy, then reproduce each behavior with the PyTorch API in the compact model.

  • Python os - `os.replace`Python Software Foundation · reference

    Read os.replace for the atomic-rename step of checkpoint writes: save to a temp file in the same directory, flush and fsync, then replace the final name.

  • Hands-On Machine Learning with Scikit-Learn and PyTorchAurelien Geron (O'Reilly, 2025) · support

    Read Appendix B, Reduced Precision Models and Mixed-Precision Training, for why float16 training underflows and what fp32 primary weights and loss scaling fix.