Home / Open Source / pytorch-lightning

pytorch-lightning

A lightweight PyTorch wrapper that organizes deep learning code and scales training from a single CPU to thousands of GPUs with zero code changes.

PythonApache-2.0Framework
โญ GitHubhttps://github.com/Lightning-AI/pytorch-lightning
31,259
Stars
+0
Star growth
Jul 30, 2026
Last updated
0
Clicks

1. Project Overview

PyTorch Lightning is a lightweight, high-level wrapper around PyTorch that organizes deep learning code into a clean, standardized structure so researchers and engineers can train and scale any model โ€” from a single CPU to 10,000+ GPUs โ€” without rewriting their core logic or hand-coding distributed training boilerplate.

2. Background & Positioning

PyTorch Lightning was created to solve a recurring problem in deep learning research: the same engineering code (distributed training loops, checkpointing, mixed precision, logging, multi-node orchestration) gets copy-pasted and re-debugged in every new project, while the actual research idea is only a small fraction of the codebase. Lightning's core mission is to separate "research code" (the model, the loss, the optimization step) from "engineering code" (the training loop, hardware orchestration, precision handling), so that switching from a laptop CPU to a multi-node GPU cluster requires zero changes to the model definition.

Compared to writing raw PyTorch training loops, Lightning removes hundreds of lines of repetitive boilerplate while keeping every component a plain torch.nn.Module, so nothing is hidden or "magic." Compared to heavier, more opinionated frameworks, Lightning stays unopinionated about model architecture and data โ€” it only structures how training is organized. For teams that need finer-grained control over the training loop itself rather than a fully managed Trainer, the same repository also ships Lightning Fabric, a thinner layer that scales existing PyTorch loops with minimal code changes, making the project useful both for standard model training and for advanced use cases like custom foundation-model pretraining.

3. Feature Categories

๐Ÿงฉ Core Abstractions
Three main building blocks. Representative examples: LightningModule (wraps model, training/validation/test steps, and optimizers), Trainer (manages the training loop, devices, and precision), LightningDataModule (encapsulates dataset preparation and dataloaders), and Callback (injects custom logic without touching the training loop). Purpose: cleanly separate research logic from engineering concerns.

โšก Distributed & Accelerated Training
Built-in strategies for scaling across hardware. Representative examples: multi-GPU data-parallel and model-parallel training, multi-node orchestration, TPU and HPU support, and 16/32/64-bit mixed-precision training. Purpose: scale identical model code from one device to thousands of accelerators without code changes.

๐Ÿ“Š Experiment Tracking & Logging
Native integrations for monitoring training runs. Representative examples: TensorBoard, Weights & Biases, MLflow, Comet, and Neptune loggers, plus built-in self.log() metric logging. Purpose: give visibility into training progress without extra instrumentation code.

๐Ÿ› ๏ธ Training Utilities & Callbacks
Ready-made behaviors that plug into the Trainer. Representative examples: automatic checkpointing, early stopping, learning-rate finder, stochastic weight averaging, and gradient accumulation. Purpose: cover common training needs declaratively instead of imperatively.

๐Ÿชถ Lightning Fabric
A lighter-weight scaling layer for teams that want to keep their own training loop. Representative examples: fabric.setup() for models and optimizers, fabric.backward(), distributed samplers, and precision plugins. Purpose: add multi-GPU/multi-node scaling to existing PyTorch code with only a few added lines.

4. Key Highlights

  • Zero-code-change scaling โ€” the same LightningModule runs on CPU, a single GPU, multiple GPUs, multiple nodes, or TPUs by only changing Trainer flags.
  • 40+ built-in training features โ€” checkpointing, early stopping, gradient clipping, mixed precision, and more ship out of the box, configurable via Trainer arguments.
  • Full flexibility retained โ€” a LightningModule is still a standard PyTorch nn.Module, so existing PyTorch models and layers work unmodified.
  • Reproducibility and rigor โ€” every pull request is tested across supported PyTorch/Python version combinations, operating systems, and multi-GPU/TPU configurations.
  • Minimal overhead โ€” the abstraction adds only a small, measured runtime cost (roughly milliseconds per epoch) compared to hand-written PyTorch loops.
  • Dozens of ecosystem integrations โ€” works with popular loggers, profilers, and companion libraries such as TorchMetrics for scalable metric computation.

5. Use Cases by Role

  • General developers โ€” structure a personal or production model into a LightningModule to get checkpointing, logging, and multi-GPU support without writing custom training-loop code.
  • Data/research scientists โ€” prototype and benchmark new architectures quickly, then scale the exact same code from a laptop to a multi-node GPU cluster for large-scale experiments or foundation-model training.
  • DevOps/SRE โ€” rely on Lightning's built-in distributed strategies and precision plugins to standardize how training jobs are deployed and scaled across shared GPU/TPU infrastructure.

6. Getting Started

Find what you need โ€” browse the official documentation for guides on the Trainer, LightningModule, distributed strategies, and the examples/ directory in the repository for runnable end-to-end scripts.

Install / integrate

pip install pytorch-lightning

Minimal usage pattern:

import lightning.pytorch as pl

model = LitModel()
trainer = pl.Trainer(max_epochs=10, accelerator="auto", devices="auto")
trainer.fit(model, train_dataloaders=train_loader)

Contribute โ€” read CONTRIBUTING.md in the repository for setup and coding-style guidelines, then open a pull request against the master branch. Discuss ideas first via GitHub Issues or the Lightning community Discord.

7. Project Structure

pytorch-lightning/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lightning/            # unified package (pytorch + fabric + app)
โ”‚   โ”œโ”€โ”€ pytorch_lightning/    # Trainer, LightningModule, callbacks, loggers
โ”‚   โ””โ”€โ”€ lightning_fabric/     # Fabric: lightweight scaling layer
โ”œโ”€โ”€ examples/                 # runnable end-to-end training examples
โ”œโ”€โ”€ tests/                    # unit and integration tests
โ”œโ”€โ”€ docs/                     # source for the official documentation
โ””โ”€โ”€ requirements/              # per-component dependency lists

src/pytorch_lightning/trainer/trainer.py implements the core training loop, and src/pytorch_lightning/core/module.py defines the LightningModule base class โ€” these are the two files most users interact with indirectly through subclassing.

8. Related Ecosystem

PyTorch Lightning is built directly on PyTorch and is maintained by Lightning AI, which also develops TorchMetrics (scalable metric computation), Lightning Fabric (included in the same repository), and the Lightning Studio cloud platform for running and deploying training jobs. It integrates with common experiment trackers such as TensorBoard, Weights & Biases, MLflow, Comet, and Neptune, and works alongside distributed-training backends like NVIDIA NCCL and DeepSpeed.

9. License

  • โœ… Commercial and non-commercial use, modification, and redistribution are permitted under the Apache-2.0 License.
  • โœ… Patent grant from contributors is included, offering additional legal protection for users.
  • โ„น๏ธ Modified files must carry a notice stating that changes were made, per Apache-2.0 terms.
  • โŒ The license provides no warranty; the "Lightning" trademark and branding are not covered by the code license.

10. FAQ

Q: What's the difference between the Trainer and Lightning Fabric?
A: The Trainer is a fully managed training loop for standard use cases; Fabric is a thinner layer for teams that want to keep writing their own loop while still getting multi-GPU/multi-node scaling. See the comparison guide.

Q: Do I need to rewrite my existing PyTorch model to use Lightning?
A: No โ€” a LightningModule is a torch.nn.Module with a few added methods (training_step, configure_optimizers, etc.), so existing model code can usually be moved in with minimal changes.

Q: How do I run training on multiple GPUs or nodes?
A: Set the relevant Trainer arguments, e.g. Trainer(accelerator="gpu", devices=4, strategy="ddp", num_nodes=2) โ€” no changes to the LightningModule are required.

Q: Does Lightning support mixed-precision or TPU training?
A: Yes, via Trainer(precision="16-mixed") for mixed precision and Trainer(accelerator="tpu") for TPU training.

Q: How do I report a bug or request a feature?
A: Open an issue on the GitHub repository following the provided templates.

11. Quick Links

12. Summary

PyTorch Lightning gives researchers and engineers a way to write model code once and scale it anywhere โ€” from a laptop CPU to a large multi-node GPU cluster โ€” without touching the underlying logic. It is well suited for individual developers who want to eliminate training-loop boilerplate, and for research teams that need reproducible, large-scale experimentation with minimal engineering overhead.