pytorch-lightning
PyTorch Lightning transforms raw PyTorch code into clean, organized training workflows by handling device management, distributed strategies (DDP, FSDP, DeepSpeed), and logging automatically. Define your model as a LightningModule, pass it to Trainer with your data, and let the framework manage GPU/TPU switching, mixed precision, checkpointing, and callbacks—reducing typical training code from 40+ lines to 15.
PyTorch Lightning provides automatic GPU/TPU training with minimal boilerplate through a high-level Trainer API.
AI-generated summary based on this skill's SKILL.md
Install
NousResearch/hermes-agent/pytorch-lightning · repository language: Python
git clone https://github.com/NousResearch/hermes-agent
cp -r hermes-agent/optional-skills/mlops/pytorch-lightning ~/.claude/skills/pytorch-lightningnpx skillfed install NousResearch/hermes-agent/pytorch-lightningFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to use pytorch lightning for distributed training?
PyTorch Lightning handles distributed training through its Trainer class. Set the `strategy` parameter to 'ddp' (Distributed Data Parallel), 'fsdp' (Fully Sharded Data Parallel), or 'deepspeed' and specify `devices='auto'` or a device count. Lightning automatically manages process spawning, gradient synchronization, and communication across GPUs or nodes—no code changes needed to your LightningModule.
What does PyTorch Lightning do compared to raw PyTorch?
PyTorch Lightning eliminates boilerplate by automating device management, mixed precision, checkpointing, and logging. Instead of writing 40+ lines of training loop code with manual GPU handling and callback orchestration, you define a LightningModule, configure a Trainer with your desired settings (GPU count, precision, strategy), and call `trainer.fit()`. Lightning handles the rest.
Does PyTorch Lightning support GPU, CPU, and TPU?
Yes, PyTorch Lightning supports GPU, CPU, and TPU through unified device abstraction. Set `accelerator='gpu'`, `accelerator='cpu'`, or `accelerator='tpu'` in the Trainer, and optionally specify `devices='auto'` to use all available hardware. Your LightningModule code remains unchanged regardless of target hardware.
How do PyTorch Lightning callbacks enable early stopping?
PyTorch Lightning provides built-in callbacks like `EarlyStopping` that monitor validation metrics and halt training when no improvement occurs for a specified patience window. Pass callbacks to the Trainer: `trainer = Trainer(callbacks=[EarlyStopping(monitor='val_loss', patience=3)])`. Custom callbacks can also log metrics, save checkpoints, or adjust learning rates automatically.
Can PyTorch Lightning optimize memory and precision for large models?
PyTorch Lightning supports mixed precision training (fp16, bf16) via the `precision` parameter in Trainer, reducing memory footprint and accelerating computation. For very large models, use `strategy='fsdp'` (Fully Sharded Data Parallel) to shard parameters across devices, or integrate DeepSpeed for advanced memory optimization and gradient checkpointing.
What is a LightningModule and how do I structure training?
A LightningModule is a PyTorch nn.Module subclass that organizes your model, optimizer, and training logic into standardized methods: `forward()`, `training_step()`, `validation_step()`, and `configure_optimizers()`. Pass it to a Trainer with your DataLoader, call `trainer.fit()`, and Lightning orchestrates the training loop, validation, checkpointing, and logging automatically.
SKILL.md
rendered from the published skill — quoted content, verbatim
PyTorch Lightning - High-Level Training Framework
Quick start
PyTorch Lightning organizes PyTorch code to eliminate boilerplate while maintaining flexibility.
Installation:
pip install lightning
Convert PyTorch to Lightning (3 steps):
```python import lightning as L import torch from torch import nn from torch.utils.data import DataLoader, Dataset
Step 1: Define LightningModule (organize your PyTorch code)
class LitModel(L.LightningModule): def init(self, hidden_size=128): super().init() self.model = nn.Sequential( nn.Linear(28 * 28, hidden_size), nn.ReLU(), nn.Linear(hidden_size, 10) )
def training_step(self, batch, batch_idx):
x, y = batch
y_hat =
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 4 files
optional-skills/mlops/pytorch-lightning/SKILL.md
optional-skills/mlops/pytorch-lightning/references/callbacks.md
optional-skills/mlops/pytorch-lightning/references/distributed.md
optional-skills/mlops/pytorch-lightning/references/hyperparameter-tuning.md