Structured PyTorch training · LightningModule + Trainer · verified against Lightning 2.5+ (2026)

PyTorch Lightning cheat sheet

Lightning removes the training-loop boilerplate from PyTorch: you organize your model into a LightningModule (the science) and hand it to a Trainer (the engineering — devices, precision, distributed, checkpoints, logging). The same code runs on CPU, one GPU, many GPUs, or TPUs by changing Trainer flags, not your model. This sheet targets Lightning 2.5+ with the modern import lightning as L package.

LightningModule Trainer data logging & callbacks scale & deploy gotcha most common

Verified 2026-08-24 against the official docs at lightning.ai/docs/pytorch (2.5/2.6). The lightning package is the current distribution (pip install lightning); the standalone pytorch_lightning package still mirrors it. Fabric (lightning.fabric) is the lighter-weight alternative.

Outline

Define a LightningModule, wrap data (optionally) in a DataModule, then let the Trainer run it. Scale by changing Trainer flags.

Define

  1. 1 · Install & the big picture
  2. 2 · LightningModule hooks
  3. 3 · configure_optimizers

Train

  1. 4 · Trainer & fit/test
  2. 5 · Key Trainer flags

Data & tracking

  1. 6 · LightningDataModule
  2. 7 · Logging (self.log)
  3. 8 · Callbacks & checkpoints

Scale & ship

  1. 9 · Multi-GPU, precision, FSDP
  2. 10 · Checkpoints, inference, Fabric
  3. 11 · Gotchas
  4. Worth memorizing

Define the Model

One class holds the model, the steps, and the optimizer.

1Install & the big picture2.5+
2LightningModule hooksthe 6 sections
3configure_optimizersoptimizer + schedule

Train

The Trainer runs everything; flags control hardware, precision, and length.

4Trainer & fit/validate/testthe loop
5Key Trainer flagsmost-used knobs

Data & Tracking

Package data loading, log metrics, and hook in callbacks.

6LightningDataModuleshareable data
7Logging (self.log)metrics
8Callbacks & checkpointsautomate the run

Scale & Ship

The same model on many GPUs, and getting weights back out.

9Multi-GPU, precision, FSDPjust flags
10Checkpoints, inference, Fabricget weights out

Gotchas

The Lightning-specific traps.

!Common gotchasread before shipping

Worth memorizing

science vs engineeringLightningModule = model + steps; Trainer = devices, precision, loop
return loss from training_stepLightning runs backward + optimizer.step for you
save_hyperparameters()makes load_from_checkpoint restore args cleanly
scale by flags, not codedevices=4, strategy="ddp"/"fsdp", precision="bf16-mixed"
self.log auto-reducesuse on_epoch + sync_dist=True (or TorchMetrics) under DDP
ModelCheckpoint + EarlyStoppingthe two callbacks nearly every run needs
setup() not prepare_data() for stateprepare_data runs once; setup runs per process
fast_dev_run before the long runone batch through the whole pipeline catches bugs early
Fabric = raw loop + scalingwhen you want DDP/FSDP but keep your own training loop