bugger.

How PyTorch works: architecture & execution flow

Training7 MIN READ

PyTorch

GitHub

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.

AutogradATenCUDATorchInductor
Explore the components or trace execution
SYSTEM ARCHITECTURE SELECT A COMPONENT TO EXPLORE
Orchestration Compute State & storage┄ Feedback / repeat
A simplified single-process eager training step. The dashed compiler branch is optional; distributed training and the input pipeline are outside this view.

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.

4 steps

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.h

Operator registration and dispatch machinery.

torch/csrc/autograd/engine.cpp

Backward task execution and dependency scheduling.

torch/optim/optimizer.py

Shared 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.

More AI systems under the hood.

Practice AI debugging
Inference · 8 min read

vLLM

An inference engine and serving stack for language models. Its central problem is sharing finite GPU memory and compute among requests whose prompts and response lengths are different.

Inference · 8 min read

SGLang

A language-model serving runtime with scheduling, attention kernels and reusable prefix state. Its RadixAttention idea is especially useful when conversations or related prompts repeatedly share an initial token sequence.

Inference · 7 min read

Ray Serve

A model-serving framework built on Ray actors. It routes requests, composes deployments and scales replicas; a replica can run a PyTorch model, ordinary Python code or an LLM inference engine such as vLLM.

Models & tooling · 6 min read

Hugging Face Transformers

Hugging Face is an ecosystem; Transformers is its open-source model library. It connects pretrained checkpoints, model-specific implementations, preprocessing and generation APIs. Here we follow decoder-only text generation using the PyTorch backend.

Training · 7 min read

DeepSpeed

DeepSpeed wraps a PyTorch model with a training engine that coordinates precision, optimization and distributed execution. Its ZeRO optimizer removes replicated training state across data-parallel workers, enabling configurations that would otherwise exceed device memory.

Training · 8 min read

Unsloth

Unsloth adapts model loading and training operations around the PyTorch, Transformers, PEFT and TRL ecosystem. Follow a supervised LoRA or QLoRA run to see where the base model stays frozen, where gradients flow, and why specialized kernels help.

Inference · 8 min read

llama.cpp

llama.cpp provides C/C++ model inference across CPU and accelerator backends. Its core connects a model file, tokenization, a computation graph, device buffers and a sampling loop. A server can wrap this library, but HTTP is not required for local generation.