Registry / ai-ml / pytorch-lightning

pytorch-lightning

JSON →
library2.6.5pypypi✓ verified 27d ago

PyTorch Lightning is a lightweight PyTorch wrapper designed to simplify the training and evaluation of deep learning models. It abstracts away common boilerplate code, allowing researchers and engineers to focus on model architecture and experimental logic. The library is actively maintained, currently at version 2.6.1, and follows a release cadence where minor versions may introduce backwards-incompatible changes with deprecations, and major versions may do so without.

pip install pytorch-lightning
INSTALL
IMPORT
SIG · PYTORCH-LIGHTNING
P
pytorch-lightning
ai-mlpythonv2.6.5
Install
71.5s avg
Import
12993ms
Disk
4838MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.6.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 82.65s
py 3.11
✕ build_error
✓ 75.65s
py 3.12
✕ build_error
✓ 66.35s
py 3.13
✕ build_error
✓ 61.15s
py 3.9
✕ build_error
✕ timeout
4838MB installed
● package 4838MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LightningModule
from pytorch_lightning import LightningModule
from lightning import LightningModule
Trainer
from pytorch_lightning import Trainer
LightningDataModule
from pytorch_lightning import LightningDataModule

This quickstart demonstrates a minimal autoencoder training loop using `lightning`. It covers defining a `LightningModule`, setting up data loaders, and training with the `Trainer`. The code shows how Lightning automatically handles the training loop, backward passes, and optimizer steps, reducing boilerplate. A simple inference step is included to show how to use the trained model.

import os from torch import optim, nn, utils, Tensor from torchvision.datasets import MNIST from torchvision.transforms import ToTensor import lightning as L # 1. Define any number of nn.Modules (or use your current ones) encoder = nn.Sequential(nn.Linear(28 * 28, 64), nn.ReLU(), nn.Linear(64, 3)) decoder = nn.Sequential(nn.Linear(3, 64), nn.ReLU(), nn.Linear(64, 28 * 28)) # 2. Define the LightningModule class LitAutoEncoder(L.LightningModule): def __init__(self, encoder, decoder): super().__init__() self.encoder = encoder self.decoder = decoder def training_step(self, batch, batch_idx): x, _ = batch x = x.view(x.size(0), -1) z = self.encoder(x) x_hat = self.decoder(z) loss = nn.functional.mse_loss(x_hat, x) self.log('train_loss', loss) return loss def configure_optimizers(self): optimizer = optim.Adam(self.parameters(), lr=1e-3) return optimizer # 3. Define a dataset dataset = MNIST(os.environ.get('DATASET_PATH', os.getcwd()), download=True, transform=ToTensor()) train_dataloader = utils.data.DataLoader(dataset, batch_size=128) # 4. Train the model model = LitAutoEncoder(encoder, decoder) trainer = L.Trainer(limit_train_batches=100, max_epochs=1) trainer.fit(model, train_dataloader) # 5. Use the model (optional, example prediction step) # For inference, set model to eval mode and disable gradients model.eval() with Tensor.no_grad(): sample_input, _ = dataset[0] sample_input = sample_input.view(1, -1) encoded_output = model.encoder(sample_input) decoded_output = model.decoder(encoded_output) print(f"Original shape: {sample_input.shape}, Encoded shape: {encoded_output.shape}, Decoded shape: {decoded_output.shape}")
Debug
Known issues
breakingMajor API and package renaming in version 2.0. The primary package name for installation changed from `pytorch-lightning` to `lightning`, and imports moved from `pytorch_lightning` (e.g., `pytorch_lightning.Trainer`) to `lightning` (e.g., `lightning.Trainer`). Additionally, many `Trainer` arguments, such as `gpus`, `tpus`, etc., were deprecated in 1.x and removed/refactored in 2.0 in favor of accelerator configurations (e.g., `accelerator='gpu', devices=4`).
fix
Update your `pip install` command to `pip install lightning`, change all `import pytorch_lightning` statements to `import lightning as L`, and migrate `Trainer` arguments to the new unified accelerator API. Consult the official migration guide for a detailed overview.
affects: 2.0.0 and later
deprecatedThe `to_torchscript` method on `LightningModule` was deprecated in version 2.6.1.
fix
Use alternative methods for TorchScript export or refer to the latest Lightning documentation for recommended export patterns.
affects: 2.6.1 and later
gotchaManual device placement (e.g., `.cuda()`, `.to(device)`) is generally not needed within a `LightningModule` and can cause issues. Lightning's `Trainer` handles device management automatically.
fix
Remove explicit `.cuda()` or `.to(device)` calls for your model and tensors that are part of the training loop. Lightning will place them on the correct device. If initializing new tensors, use `new_tensor = torch.Tensor(...).to(existing_tensor)` to ensure correct device placement.
affects: All versions
gotchaFor distributed training, `DistributedSampler` is automatically applied to `DataLoader`s by the `Trainer` when a distributed strategy is used. Manually wrapping your `DataLoader` with `DistributedSampler` can lead to incorrect behavior or errors.
fix
Do not manually instantiate `torch.utils.data.DistributedSampler` for your data loaders when using `lightning.Trainer` with a distributed strategy. Simply pass your standard `DataLoader` to `trainer.fit()`, and Lightning will handle the distributed sampling.
affects: All versions
breakingInstallation of `scikit-learn` (a common dependency for `pytorch-lightning`) and other scientific computing libraries often fails in minimal Docker environments like `python:*-alpine` due to missing C/C++ compilers and other build tools. These libraries have native extensions that need to be compiled during installation.
fix
Install build dependencies in your Dockerfile (e.g., `RUN apk add --no-cache build-base` for Alpine) before `pip install`ing these libraries, or use a more comprehensive Python base image (e.g., `python:3.13` instead of `python:3.13-alpine`).
affects: All versions
Upgrade
Version history
2.6.5latest on PyPI · released May 27, 2026
Audit
Dependencies
torchrequiredCore deep learning framework dependency, usually installed separately to select CUDA version.
pythonrequiredRequires Python 3.10 or higher.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
pytorch-lightning — pip install pytorch-lightning · libregistry