MCPcopy
hub / github.com/Lightning-AI/pytorch-lightning

github.com/Lightning-AI/pytorch-lightning @2.6.5 sqlite

repository ↗ · DeepWiki ↗ · release 2.6.5 ↗
9,420 symbols 40,808 edges 647 files 2,756 documented · 29%
README

Lightning

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> •

ExamplesPyTorch LightningFabricLightning Cloud
CommunityDocs

PyPI - Python Version PyPI Status PyPI - Downloads Conda codecov

Discord GitHub commit activity license

 

Get started

 

Why PyTorch Lightning?

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.

Looking for GPUs?

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.

Lightning has 2 core packages

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%">

 

Quick start

Install Lightning:

pip install lightning

Advanced install options

Install with optional dependencies

pip install lightning['extra']

Conda

conda install lightning -c conda-forge

Install stable version

Install future release from the source

pip install https://github.com/Lightning-AI/lightning/archive/refs/heads/release/stable.zip -U

Install bleeding-edge

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

PyTorch Lightning example

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

 

Convert from PyTorch to PyTorch Lightning

PyTorch Lightning is just organized PyTorch - Lightning disentangles PyTorch code to decouple the science from the engineering.

PT to PL

 


Examples

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 Open In Studio
Image classification Finetune - ResNet-34 model to classify images of cars Open In Studio
Image segmentation Finetune - ResNet-50 model to segment images Open In Studio
Object detection Finetune - Faster R-CNN model to detect objects Open In Studio
Text classification Finetune - text classifier (BERT model) Open In Studio
Text summarization Finetune - text summarization (Hugging Face transformer model) Open In Studio
Audio generation Finetune - audio generator (transformer model) Open In Studio
LLM finetuning Finetune - LLM (Meta Llama 3.1 8B) Open In Studio
Image generation Pretrain - Image generator (diffusion model) Open In Studio
Recommendation system Train - recommendation system (factorization and embedding) Open In Studio
Time-series forecasting Train - Time-series forecasting with LSTM Open In Studio

Advanced features

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

8 GPU

Core symbols most depended-on inside this repo

fit
called by 788
src/lightning/pytorch/trainer/trainer.py
log
called by 309
src/lightning/fabric/fabric.py
join
called by 297
tests/tests_pytorch/helpers/threading.py
append
called by 279
src/lightning/fabric/utilities/throughput.py
device
called by 247
src/lightning/fabric/fabric.py
get
called by 206
src/lightning/fabric/strategies/registry.py
test
called by 142
src/lightning/pytorch/trainer/trainer.py
load
called by 108
src/lightning/fabric/fabric.py

Shape

Method 4,773
Function 3,046
Class 1,239
Route 362

Languages

Python100%

Modules by API surface

tests/tests_pytorch/checkpointing/test_model_checkpoint.py192 symbols
tests/tests_pytorch/trainer/test_trainer.py162 symbols
tests/tests_pytorch/test_cli.py159 symbols
tests/tests_pytorch/models/test_hparams.py145 symbols
tests/tests_pytorch/trainer/test_dataloaders.py120 symbols
tests/tests_fabric/test_connector.py119 symbols
tests/tests_pytorch/strategies/test_deepspeed.py100 symbols
tests/tests_fabric/test_fabric.py100 symbols
tests/tests_pytorch/trainer/connectors/test_accelerator_connector.py98 symbols
tests/tests_pytorch/trainer/optimization/test_manual_optimization.py87 symbols
tests/tests_pytorch/strategies/test_fsdp.py82 symbols
src/lightning/pytorch/trainer/trainer.py78 symbols

Dependencies from manifests, versioned

lightning1.9.0 · 1×
torchao0.7.0 · 1×

For agents

$ claude mcp add pytorch-lightning \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact