Lightning-AI / pytorch-lightning

Pretrain, finetune ANY AI model of ANY size on multiple GPUs, TPUs with zero code changes.
https://lightning.ai
Apache License 2.0
28.54k stars 3.39k forks source link

LightningCLI cannot use strategy and accelerator class instances. #19682

Open ankitgola005 opened 8 months ago

ankitgola005 commented 8 months ago

Bug description

LightningCLI cannot use strategy class instances.

Does not work

python -u main.py fit --trainer.strategy lightning.pytorch.strategies.SingleDeviceStrategy python -u main.py fit --trainer.accelerator lightning.pytorch.accelerators.CPUAccelerator

Works

python -u main.py fit --trainer.strategy single_device python -u main.py fit --trainer.accelerator cpu

What version are you seeing the problem on?

v2.2, master

How to reproduce the bug

# main.py

from lightning.pytorch.cli import LightningCLI
from lightning.pytorch.demos.boring_classes import BoringDataModule, DemoModel

def cli_main():
    cli = LightningCLI(DemoModel, BoringDataModule)

if __name__ == "__main__":
    cli_main()

Error messages and logs

# Error message: python -u main.py fit --trainer.strategy lightning.pytorch.strategies.SingleDeviceStrategy 

Traceback (most recent call last):
  File "/content/main.py", line 8, in <module>
    cli_main()
  File "/content/main.py", line 5, in cli_main
    cli = LightningCLI(DemoModel, BoringDataModule)
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/cli.py", line 385, in __init__
    self.instantiate_classes()
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/cli.py", line 539, in instantiate_classes
    self.trainer = self.instantiate_trainer()
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/cli.py", line 550, in instantiate_trainer
    return self._instantiate_trainer(trainer_config, extra_callbacks)
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/cli.py", line 575, in _instantiate_trainer
    return self.trainer_class(**config)
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/utilities/argparse.py", line 70, in insert_env_defaults
    return fn(self, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/trainer/trainer.py", line 401, in __init__
    self._accelerator_connector = _AcceleratorConnector(
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/trainer/connectors/accelerator_connector.py", line 134, in __init__
    self._check_config_and_set_final_flags(
  File "/usr/local/lib/python3.10/dist-packages/lightning/pytorch/trainer/connectors/accelerator_connector.py", line 203, in _check_config_and_set_final_flags
    raise ValueError(
ValueError: You selected an invalid strategy name: `strategy='lightning.pytorch.strategies.singledevicestrategy'`. It must be either a string or an instance of `lightning.pytorch.strategies.Strategy`. Example choices: auto, ddp, ddp_spawn, deepspeed, ... Find a complete list of options in our documentation at https://lightning.ai/

Environment

Current environment * CUDA: - GPU: None - available: False - version: 12.1 * Lightning: - lightning: 2.2.1 - lightning-utilities: 0.11.0 - pytorch-lightning: 2.2.1 - torch: 2.2.1+cu121 - torchaudio: 2.2.1+cu121 - torchdata: 0.7.1 - torchmetrics: 1.3.2 - torchsummary: 1.5.1 - torchtext: 0.17.1 - torchvision: 0.17.1+cu121 * System: - OS: Linux - architecture: - 64bit - ELF - processor: x86_64 - python: 3.10.12 - release: 6.1.58+ - version: 1 SMP PREEMPT_DYNAMIC Sat Nov 18 15:31:17 UTC 2023

More info

No response

jerome-habana commented 8 months ago

cc @Borda