{"id":1535,"library":"lightning","title":"Lightning AI Framework","description":"Lightning is a deep learning framework built on PyTorch, simplifying the training, deployment, and scaling of AI models. It abstracts away boilerplate code, allowing researchers and engineers to focus on model logic. The current stable version is 2.6.1, and it maintains a rapid release cadence with minor versions typically released every 1-2 months, alongside frequent patch updates.","status":"active","version":"2.6.1","language":"en","source_language":"en","source_url":"https://github.com/Lightning-AI/lightning","tags":["deep-learning","pytorch","ml-framework","training","ai"],"install":[{"cmd":"pip install lightning","lang":"bash","label":"Stable release"},{"cmd":"pip install 'lightning[pytorch]'  # for PyTorch-specific dependencies","lang":"bash","label":"With PyTorch extras"}],"dependencies":[{"reason":"Core dependency for deep learning models. While not strictly required by 'pip install lightning', it is implicitly required for any practical use of the framework and should be installed separately (e.g., 'pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121').","package":"torch","optional":false}],"imports":[{"note":"The package name for PyTorch-specific components changed from 'pytorch_lightning' to 'lightning.pytorch' in v2.0.","wrong":"from pytorch_lightning import LightningModule","symbol":"LightningModule","correct":"from lightning.pytorch import LightningModule"},{"note":"The package name for PyTorch-specific components changed from 'pytorch_lightning' to 'lightning.pytorch' in v2.0.","wrong":"from pytorch_lightning import Trainer","symbol":"Trainer","correct":"from lightning.pytorch import Trainer"},{"symbol":"ModelCheckpoint","correct":"from lightning.pytorch.callbacks import ModelCheckpoint"},{"symbol":"LightningDataModule","correct":"from lightning.pytorch.utilities.data import LightningDataModule"}],"quickstart":{"code":"import torch\nfrom torch.utils.data import DataLoader, TensorDataset\nfrom lightning.pytorch import LightningModule, Trainer\n\nclass SimpleModel(LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.linear = torch.nn.Linear(10, 1)\n\n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        y_hat = self.linear(x)\n        loss = torch.nn.functional.mse_loss(y_hat, y)\n        self.log('train_loss', loss)\n        return loss\n\n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=0.02)\n        return optimizer\n\n# 1. Prepare dummy data\nx_data = torch.randn(100, 10)\ny_data = torch.randn(100, 1)\ndataset = TensorDataset(x_data, y_data)\ndataloader = DataLoader(dataset, batch_size=32)\n\n# 2. Instantiate model and trainer\nmodel = SimpleModel()\ntrainer = Trainer(max_epochs=5, enable_progress_bar=False, enable_checkpointing=False)\n\n# 3. Train the model\ntrainer.fit(model, dataloader)\n\nprint(\"Training complete for a simple Lightning model.\")","lang":"python","description":"This quickstart defines a simple linear model using `LightningModule`, prepares dummy data with `DataLoader`, and trains it using the `Trainer`. It showcases the minimal setup for defining a model, training step, optimizer, and running a training loop."},"warnings":[{"fix":"Update all import statements, e.g., `from pytorch_lightning import Trainer` should become `from lightning.pytorch import Trainer`.","message":"The primary package name for PyTorch-specific components was renamed from `pytorch_lightning` to `lightning.pytorch` in v2.0. Direct imports from the old name will fail.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Change `return [optimizer]` to `return optimizer` when only one optimizer is configured.","message":"The return signature for `LightningModule.configure_optimizers()` changed. For a single optimizer, it should now return just the optimizer instance directly, not a list containing a single optimizer.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Trust Lightning's device management. If you need to initialize tensors on the correct device, use `self.device` inside your `LightningModule` or access `trainer.device` if available.","message":"Lightning automatically handles device placement for models, data, and optimizers. Manually calling `.to(device)` on models or tensors within `training_step` or similar methods is usually unnecessary and can lead to bugs or redundant operations.","severity":"gotcha","affected_versions":"all"},{"fix":"Refer to the official PyTorch documentation or Lightning's export guide for the recommended way to convert models to TorchScript or other deployment formats.","message":"The `to_torchscript` method on `LightningModule` has been deprecated.","severity":"deprecated","affected_versions":">=2.6.1"},{"fix":"Update your CLI commands to use `lightning run model` followed by your script and arguments. For example, `python train.py --config config.yaml` might become `lightning run model train.py --config config.yaml`.","message":"The `LightningCLI` command-line interface changed its execution pattern. Instead of `python your_script.py fit`, it now uses `lightning run model your_script.py`.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}