7 routes42 sections324 units

Section 8 of 8 · Deep Learning

Reliable PyTorch Execution, State & Profiling

Framework fluency becomes engineering evidence when precision, memory, state, compilation and timing claims can be predicted, measured and reproduced.

9 units 22 topics 0/9

Units

9 in this section

  1. 8.1 Eager execution, autograd nodes and saved tensors
    explain
    3 h
  2. 8.2 Streams, asynchronous copies and honest timing
    explain · measure
    4 h
  3. 8.3 GPU memory: allocated, reserved, peak and memory models
    derive · measure
    5 h
  4. 8.4 Precision formats, autocast and gradient scaling
    explain · implement
    5 h
  5. 8.5 Overflow, underflow and finding the first invalid value
    measure
    4 h
  6. 8.6 Activation checkpointing and recomputation
    implement · measure
    4 h
  7. 8.7 Complete checkpoints, atomic writes and reproducibility boundaries
    implement
    5 h
  8. 8.8 `torch.compile`: capture, guards, graph breaks and dynamic shapes
    measure
    4 h
  9. 8.9 Profiling training phases and verifying optimizations
    measure
    5 h

Before moving on

Checkpoint

Predict memory categories and the slowest phase, produce a synchronized profile, explain one discrepancy, repair a precision, state, graph or performance fault, and show retained learning and resume behavior.

Deliberately out of scope

Leave for later

  • Distributed training and sharded state
  • Custom kernel development
  • Production trainer and fleet operations

Resources for this section

Shared by every unit above

  • PyTorch - reliability and performance recipes
    PyTorchcourse
    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 troubleshooting
    PyTorchreference
    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.
  • PyTorch - checkpointing, numerical accuracy and CUDA semantics
    PyTorchreference
    required
    Use the official user-guide entries for torch.utils.checkpoint, numerical accuracy, CUDA semantics, reproducibility and serialization. Predict memory categories before measuring and define resume equivalence before running.
  • Deep Learning Training Debugger
    Roadmap-Finalassessment
    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 FLOPs
    TensorTonicpractice
    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 walkthrough
    Daniel Bourkeimplementation
    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 resume
    TrenTorchpractice
    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 Foundationreference
    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 PyTorch
    Aurelien Geron (O'Reilly, 2025)theory
    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.

Apply the section

Mini-project · Profiled, resumable training with one repaired fault (10 to 14 h)

Take your compact model and trainer, predict its memory and slowest phase before measuring, and profile it with synchronized phases. Then repair one precision, state, graph or performance fault, and show the repaired run still learns and resumes.

Steps

  1. Write predictions first: parameter, gradient, optimizer-state and activation bytes for your model and batch, and which phase (data wait, transfer, forward, backward, optimizer) will be slowest.
  2. Annotate one training step with record_function ranges, time data wait outside the step, and use CUDA events or synchronize at phase boundaries. Profile a few steps with a schedule; on a CPU, profile there and say so.
  3. Read allocated, reserved and peak memory per phase, reset peaks before each measured region, list the saved tensors of one layer, and explain one gap between prediction and measurement.
  4. Instrument numerics: count non-finite values per step, capture the first invalid value with forward hooks, and replay a fixed batch under anomaly detection to name the backward node.
  5. Seed one fault and repair it: fp16 autocast without a GradScaler, clipping before unscale_, an .item() sync inside the step, a graph break under torch.compile, or an incomplete checkpoint.
  6. Apply one optimization (autocast, activation checkpointing, compile or loader settings). Show outputs and gradients match eager within tolerance, report a measured gain or an honest null result, and rerun your learning controls.
  7. Interrupt the optimized run and resume it from an atomic checkpoint written to a temp file and moved with os.replace, and compare with an uninterrupted run under a declared tolerance.

Deliverables

  • The prediction sheet and the synchronized profile, with a phase table and memory per phase.
  • One fault postmortem with hypotheses, the discriminating measurement, the repair and a regression guard.
  • The optimization report: equivalence, timing method and results, learning controls and the resume check.

Checks

  • external-tests · TensorTonic: Mixed-Precision Training Memory Accountant and Transformer Training FLOP Estimator
  • rubric · Self-grade with the rubric below

Rubric

CriterionPassStrong
Prediction before measurementPredictions are committed before the profile is run.Each gap between predicted and measured memory or time is explained by a named term, such as the cuBLAS workspace, allocator reserve, a sync or recomputation.
Honest timingPhase times use events or synchronize, and data wait is measured outside the step.You show one timing that changes when a sync moves, and explain why.
Fault repairOne seeded fault is found, repaired and guarded by a test.The postmortem shows the discriminating measurement and one rejected hypothesis.
Optimization validityOptimized outputs and gradients match eager within tolerance, and the learning controls still pass.The gain is reported with its spread over repeated runs, or a null result is reported with its cause.
ResumeResume from the atomic checkpoint matches within a declared tolerance.A test that kills the process during a write shows the previous checkpoint survives.

Feeds into: Capstone artifact 3: the performance or compile postmortem comes from here, and the resume result also serves artifact 2.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels