Optimization — PyTorch Lightning 2.4.0 documentation
For the majority of research cases, automatic optimization will do the right thing for you and it is what most users should use. For more advanced use cases like multiple optimizers, esoteric optimization schedules or techniques, use manual optimization. For advanced research topics like reinforcement learning, sparse coding, or GAN research, it may be desirable to manually manage the optimization process, especially when dealing with multiple optimizers at the same time. In this mode, Lightning will handle only accelerator, precision and strategy logic. The users are left with optimizer.zero_grad(), gradient accumulation, optimizer toggling, etc.. To manually optimize, do the following: Set self.automatic_optimization=False in your LightningModule’s __init__. Use the following functions and call them manually: self.optimizers() to access your optimizers (one or multiple) optimizer.zero_grad() to clear the gradients from the previous training step self.manual_backward(loss) instead of
Optimization ¶ Lightning offers two modes for managing the optimization process: Manual Optimization Automatic Optimization For the majority of research cases, automatic optimization will do the right thing for you and it is what most users should use. For more advanced use cases like multiple optimizers, esoteric optimization schedules or techniques, use manual optimization . Manual Optimization ¶ For advanced research topics like reinforcement learning, sparse coding, or GAN research, it may be desirable to manually manage the optimization process, especially when dealing with multiple optim
Explore this link on the map →related reading
- LightningModule - PyTorch Lightning 2.6.1 documentationlightning.ai
- Trainer - PyTorch Lightning 2.6.1 documentationlightning.ai
- Redirecting…pytorch.org
- LoRA Without Regret - Thinking Machines Labthinkingmachines.ai
- Learning PyTorch with Examples — PyTorch Tutorials 2.12.0+cu130 documentationpytorch.org
- ♻️ Auto Microbatching - Composerdocs.mosaicml.com
- The Little Book of Deep Learningfleuret.org
- PyTorch internals : ezyang's blogblog.ezyang.com
- ⚡ Migrating from PTL - Composerdocs.mosaicml.com
- Bug Fixes in LLM Training - Gradient Accumulationunsloth.ai
- Transformer Math 101 | EleutherAI Blogblog.eleuther.ai
- the bug that taught me more about PyTorch than years of using it | Elana Simonelanapearl.github.io