Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pytorch-lightning

> DevOps
Open source

Pretrain, finetune ANY AI model of ANY size on 1 or 10,000+ GPUs with zero code changes.

31.3K stars0 likes3 views
WebsiteGitHub

About

Pretrain, finetune ANY AI model of ANY size on 1 or 10,000+ GPUs with zero code changes.

 

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.

 

Quick start

Install Lightning:

bash
pip install lightning
Advanced install options

Install with optional dependencies

bash
pip install lightning['extra']

Conda

bash
conda install lightning -c conda-forge

Install stable version

Install future release from the source

bash
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)

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

or from testing PyPI

bash
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):

…

Run the model on your terminal

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

 


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

Advanced features

Lightning has over 40+ advanced features designed for professional AI research at scale.

Here are some examples:

Train on 1000s of GPUs without code changes
python
# 8 GPUs
# no code changes needed
trainer = Trainer(accelerator="gpu", devices=8)

# 256 GPUs
trainer = Trainer(accelerator="gpu", devices=8, num_nodes=32)
Train on other accelerators like TPUs without code changes
python
# no code changes needed
trainer = Trainer(accelerator="tpu", devices=8)
16-bit precision
python
# no code changes needed
trainer = Trainer(precision=16)
Experiment managers
python
from lightning import loggers

# litlogger
trainer = Trainer(logger=LitLogger())

# tensorboard
trainer = Trainer(logger=TensorBoardLogger("logs/"))

# weights and biases
trainer = Trainer(logger=loggers.WandbLogger())

# comet
trainer = Trainer(logger=loggers.CometLogger())

# mlflow
trainer = Trainer(logger=loggers.MLFlowLogger())

# ... and dozens more
Early Stopping
python
es = EarlyStopping(monitor="val_loss")
trainer = Trainer(callbacks=[es])
Checkpointing
python
checkpointing = ModelCheckpoint(monitor="val_loss")
trainer = Trainer(callbacks=[checkpointing])
Export to torchscript (JIT) (production use)
python
# torchscript
autoencoder = LitAutoEncoder()
torch.jit.save(autoencoder.to_torchscript(), "model.pt")
Export to ONNX (production use)
python
# onnx
with tempfile.NamedTemporaryFile(suffix=".onnx", delete=False) as tmpfile:
    autoencoder = LitAutoEncoder()
    input_sample = torch.randn((1, 64))
    autoencoder.to_onnx(tmpfile.name, input_sample, export_params=True)
    os.path.isfile(tmpfile.name)

Advantages over unstructured PyTorch

  • Models become hardware agnostic
  • Code is clear to read because engineering code is abstracted away
  • Easier to reproduce
  • Make fewer mistakes because lightning handles the tricky engineering
  • Keeps all the flexibility (LightningModules are still PyTorch modules), but removes a ton of boilerplate
  • Lightning has dozens of integrations with popular machine learning tools.
  • Tested rigorously with every new PR. We test every combination of PyTorch and Python supported versions, every OS, multi GPUs and even TPUs.
  • Minimal running speed overhead (about 300 ms per epoch compared with pure PyTorch).


   

Lightning Fabric: Expert control

Run on any device at any scale with expert-level control over PyTorch training loop and scaling strategy. You can even write your own Trainer.

Fabric is designed for the most complex models like foundation model scaling, LLMs, diffusion, transformers, reinforcement learning, active learning. Of any size.

What to change Resulting Fabric Code (copy me!)
…
…

Key features

Easily switch from running on CPU to GPU (Apple Silicon, CUDA, …), TPU, multi-GPU or even multi-node training
python
# Use your available hardware
# no code changes needed
fabric = Fabric()

# Run on GPUs (CUDA or MPS)
fabric = Fabric(accelerator="gpu")

# 8 GPUs
fabric = Fabric(accelerator="gpu", devices=8)

# 256 GPUs, multi-node
fabric = Fabric(accelerator="gpu", devices=8, num_nodes=32)

# Run on TPUs
fabric = Fabric(accelerator="tpu")
Use state-of-the-art distributed training strategies (DDP, FSDP, DeepSpeed) and mixed precision out of the box
python
# Use state-of-the-art distributed training techniques
fabric = Fabric(strategy="ddp")
fabric = Fabric(strategy="deepspeed")
fabric = Fabric(strategy="fsdp")

# Switch th

Issues· 1086 open

View all issuesOpen on GitHub
  • #21404

    Make the default auto-requeue signal in SLURM environments configurable

    featureUpdated Sep 30, 2026
  • #21406

    Checkpointing in signal handlers for SLURM auto-requeueing leads to intermittent failures

    bugver: 2.5.xUpdated Sep 30, 2026
  • #21448

    Manual `Dataloader` reloading during training

    featureUpdated Sep 30, 2026
  • #19964

    Documentation: writing custom samplers compatible with multi GPU training

    help wanteddocsUpdated Sep 30, 2026
  • #16431

    `self.log` raised error when number of dataloader is not consistent

    bughelp wantedloggingUpdated Sep 30, 2026
  • #21531

    `BackboneFinetuning`: `train_bn` only applied during unfreezing phase

    refactorcallback: finetuningUpdated Sep 29, 2026
  • #21588

    The example_input_array in LightningModule does not support using NestedTensor with layout=torch.jagged within the model.

    bugver: 2.5.xUpdated Sep 29, 2026
  • #21868

    Optimize remote checkpoint loading with parallel multiprocess downloads and zero-copy mmap

    featuredistributedcheckpointingUpdated Sep 29, 2026
  • #21895

    Many time passes between epochs

    bugwaiting on authorver: 2.2.xver: 2.4.xUpdated Sep 29, 2026
  • #21905

    Unified storage_options support for FSDPStrategy, ModelParallelStrategy, and TorchCheckpointIO

    featuredistributedcheckpointingstrategy: fsdpUpdated Sep 29, 2026

> Tags

Pythonaiartificial-intelligencedata-sciencedeep-learning

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 30, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理