PyTorch
PyTorch provides tensor operations, automatic differentiation and the building blocks for neural networks. Its central trick is to let a normal Python program describe a computation while the runtime selects hardware kernels and records how to differentiate it.
Python model
Your forward method calls tensor operations using the current batch and parameters. Ordinary Python control flow determines which operations actually run. In eager mode, the program does not need to declare every possible execution path in advance.
- Python model Operator dispatchertensor calls
- Parameters + gradients Python modelcurrent weights
- Python model Optional torch.compileoptional graph capture
One pass through the system.
Select a step to highlight the components involved in the diagram.
Why it is built this way.
Dynamic graphs cost bookkeeping
Eager execution makes Python branches easy to inspect, but each step still dispatches operations and records gradient history. Compilation can reduce overhead and fuse work; graph breaks, changing shapes and compilation time can reduce the benefit.
Backward needs more than weights
Saved activations, gradients and optimizer state compete for memory. A model fitting during inference does not imply its training step will fit. Checkpointing can exchange extra recomputation for fewer saved activations.
Measure the device, not just Python
Accelerator work can run asynchronously. Timing only the Python call can measure launch overhead instead of completed computation. Use the profiler or appropriate synchronization when investigating performance.
Read the implementation.
aten/src/ATen/core/dispatch/Dispatcher.hOperator registration and dispatch machinery.
torch/csrc/autograd/engine.cppBackward task execution and dependency scheduling.
torch/optim/optimizer.pyShared optimizer behavior, state and gradient clearing.
Go deeper with the official docs
Educational diagrams, synthesized from the linked project documentation. Components and boundaries vary by version and deployment.