
The deep learning framework to pretrain and finetune AI models.
Serving models? Use LitServe to build custom inference servers in pure Python.
<a href="#quick-start" style="margin: 0 10px;">Quick start</a> •
Examples •
PyTorch Lightning •
Fabric •
Lightning Cloud •
Community •
Docs
Training models in plain PyTorch requires writing and maintaining a lot of repetitive engineering code. Handling backpropagation, mixed precision, multi-GPU, and distributed training is error-prone and often reimplemented for every project. PyTorch Lightning organizes PyTorch code to automate this infrastructure while keeping full control over your model logic. You write the science. Lightning handles the engineering, and scales from CPU to multi-node GPUs without changing your core code. PyTorch experts can still opt into expert-level control.
Fun analogy: If PyTorch is Javascript, PyTorch Lightning is ReactJS or NextJS.
Lightning Cloud is the easiest way to run PyTorch Lightning without managing infrastructure. Start training with one command and get GPUs, autoscaling, monitoring, and a free tier. No cloud setup required.
You can also run PyTorch Lightning on your own hardware or cloud.
PyTorch Lightning: Train and deploy PyTorch at scale.
Lightning Fabric: Expert control.
Lightning gives you granular control over how much abstraction you want to add over PyTorch.
<img src="https://pl-public-data.s3.amazonaws.com/assets_lightning/continuum.png" width="80%">
Install Lightning:
pip install lightning
Advanced install options
pip install lightning['extra']
conda install lightning -c conda-forge
Install future release from the source
pip install https://github.com/Lightning-AI/lightning/archive/refs/heads/release/stable.zip -U
Install nightly from the source (no guarantees)
pip install https://github.com/Lightning-AI/lightning/archive/refs/heads/master.zip -U
or from testing PyPI
pip install -iU https://test.pypi.org/simple/ pytorch-lightning
Define the training workflow. Here's a toy example (explore real examples):
# main.py
# ! pip install torchvision
import torch, torch.nn as nn, torch.utils.data as data, torchvision as tv, torch.nn.functional as F
import lightning as L
# --------------------------------
# Step 1: Define a LightningModule
# --------------------------------
# A LightningModule (nn.Module subclass) defines a full *system*
# (ie: an LLM, diffusion model, autoencoder, or simple image classifier).
class LitAutoEncoder(L.LightningModule):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(nn.Linear(28 * 28, 128), nn.ReLU(), nn.Linear(128, 3))
self.decoder = nn.Sequential(nn.Linear(3, 128), nn.ReLU(), nn.Linear(128, 28 * 28))
def forward(self, x):
# in lightning, forward defines the prediction/inference actions
embedding = self.encoder(x)
return embedding
def training_step(self, batch, batch_idx):
# training_step defines the train loop. It is independent of forward
x, _ = batch
x = x.view(x.size(0), -1)
z = self.encoder(x)
x_hat = self.decoder(z)
loss = F.mse_loss(x_hat, x)
self.log("train_loss", loss)
return loss
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
return optimizer
# -------------------
# Step 2: Define data
# -------------------
dataset = tv.datasets.MNIST(".", download=True, transform=tv.transforms.ToTensor())
train, val = data.random_split(dataset, [55000, 5000])
# -------------------
# Step 3: Train
# -------------------
autoencoder = LitAutoEncoder()
trainer = L.Trainer()
trainer.fit(autoencoder, data.DataLoader(train), data.DataLoader(val))
Run the model on your terminal
pip install torchvision
python main.py
PyTorch Lightning is just organized PyTorch - Lightning disentangles PyTorch code to decouple the science from the engineering.

Explore various types of training possible with PyTorch Lightning. Pretrain and finetune ANY kind of model to perform ANY task like classification, segmentation, summarization and more:
| Task | Description | Run |
|---|---|---|
| Hello world | Pretrain - Hello world example | |
| Image classification | Finetune - ResNet-34 model to classify images of cars | |
| Image segmentation | Finetune - ResNet-50 model to segment images | |
| Object detection | Finetune - Faster R-CNN model to detect objects | |
| Text classification | Finetune - text classifier (BERT model) | |
| Text summarization | Finetune - text summarization (Hugging Face transformer model) | |
| Audio generation | Finetune - audio generator (transformer model) | |
| LLM finetuning | Finetune - LLM (Meta Llama 3.1 8B) | |
| Image generation | Pretrain - Image generator (diffusion model) | |
| Recommendation system | Train - recommendation system (factorization and embedding) | |
| Time-series forecasting | Train - Time-series forecasting with LSTM |
Lightning has over 40+ advanced features designed for professional AI research at scale.
Here are some examples:
<img src="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/features_2.jpg" max-height="600px">
Train on 1000s of GPUs without code changes
```python
$ claude mcp add pytorch-lightning \
-- python -m otcore.mcp_server <graph>