I recently started migrating my deep learning research to PyTorch Lightning to reduce boilerplate, but I keep hitting a TypeError: __init__() got an unexpected keyword argument 'gpus' when initializing the Trainer. I’m following an older tutorial. Was this parameter deprecated in the newer 2.x versions, or am I missing a specific import?
3 answers
In the transition from PyTorch Lightning 1.x to 2.x, several arguments in the Trainer class were renamed to provide a more unified interface for different hardware types. The gpus argument was officially deprecated and removed. To fix this, you should now use the accelerator and devices arguments. For example, instead of Trainer(gpus=1), you should use Trainer(accelerator="gpu", devices=1). This change allows PyTorch Lightning to handle CPUs, GPUs, TPUs, and even IPUs using a consistent syntax. If you are on a Mac with M1/M2 chips, you would use accelerator="mps". Always check your version using lightning.__version__ to ensure your code matches the documentation.
Are you also seeing issues with the strategy argument when trying to run distributed training across multiple nodes? I noticed that some older plugins also seem to throw keyword errors in the latest version.
You just need to change gpus=1 to devices=1 and set accelerator='auto' for the easiest fix. It’s a common hurdle when using PyTorch Lightning for the first time.
Steven is right; using accelerator='auto' is a lifesaver because it detects if a GPU is available without crashing your script on a CPU-only machine.
Yes, Michael, the strategy argument replaced several individual flags. If you're trying to use Distributed Data Parallel, you now pass strategy="ddp" directly into the Trainer. This helps PyTorch Lightning manage the environment setup without you needing to manually configure the rank or world_size as you would in raw PyTorch code.