Lightning-AI/pytorch-lightning

Unable to serialize WandbLogger

cwallenwein opened this issue · 2 comments

Bug description

When using the lightning CLI in combination with WandbLogger, the logger can't be serialized -> "Unable to serialize instance <lightning.pytorch.loggers.wandb.WandbLogger object at 0x102fcfc90>" is added to the config.yaml -> Can't start the CLI with the generated config file.

script.py

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


def cli_main():
    wandb_logger = WandbLogger()

    cli = LightningCLI(
        DemoModel,
        BoringDataModule,
        trainer_defaults={"logger": [wandb_logger]},
    )


if __name__ == "__main__":
    cli_main()

config.yaml

# lightning.pytorch==2.4.0
seed_everything: 0
trainer:
  accelerator: auto
  strategy: auto
  devices: auto
  num_nodes: 1
  precision: null
  logger:
  - Unable to serialize instance <lightning.pytorch.loggers.wandb.WandbLogger object
    at 0x102fcfc90>
  callbacks: null
  fast_dev_run: false
  ...

python script.py fit --config config.yaml

error: Parser key "trainer.logger":
  Does not validate against any of the Union subtypes
  Subtypes: (<class 'lightning.pytorch.loggers.logger.Logger'>, typing.Iterable[lightning.pytorch.loggers.logger.Logger], <class 'bool'>, <class 'NoneType'>)
  Errors:
    - Not a valid subclass of Logger
      Subclass types expect one of:
      - a class path (str)
      - a dict with class_path entry
      - a dict without class_path but with init_args entry (class path given previously)
    - Unexpected import path format: Unable to serialize instance <lightning.pytorch.loggers.wandb.WandbLogger object at 0x10090aa50>
    - Expected a <class 'bool'>
    - Expected a <class 'NoneType'>
  Given value type: <class 'list'>
  Given value: ['Unable to serialize instance <lightning.pytorch.loggers.wandb.WandbLogger object at 0x10090aa50>']

What version are you seeing the problem on?

master

How to reproduce the bug

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


def cli_main():
    wandb_logger = WandbLogger()

    cli = LightningCLI(
        DemoModel,
        BoringDataModule,
        trainer_defaults={"logger": [wandb_logger]},
    )


if __name__ == "__main__":
    cli_main()

Error messages and logs

JsonargparseWarning: 
    Unable to serialize instance <lightning.pytorch.loggers.wandb.WandbLogger object at 0x104a92a50>

Environment

Current environment
* CUDA:
        - GPU:               None
        - available:         False
        - version:           None
* Lightning:
        - lightning:         2.4.0
        - lightning-utilities: 0.11.7
        - pytorch-lightning: 2.4.0
        - torch:             2.4.1
        - torchmetrics:      1.4.2
* Packages:
        - aiohappyeyeballs:  2.4.3
        - aiohttp:           3.10.8
        - aiosignal:         1.3.1
        - antlr4-python3-runtime: 4.9.3
        - attrs:             24.2.0
        - autocommand:       2.2.2
        - backports.tarfile: 1.2.0
        - bert:              0.1
        - bitsandbytes:      0.42.0
        - certifi:           2024.8.30
        - charset-normalizer: 3.3.2
        - click:             8.1.7
        - contourpy:         1.3.0
        - cycler:            0.12.1
        - docker-pycreds:    0.4.0
        - docstring-parser:  0.16
        - filelock:          3.16.1
        - fonttools:         4.54.1
        - frozenlist:        1.4.1
        - fsspec:            2024.9.0
        - gitdb:             4.0.11
        - gitpython:         3.1.43
        - hydra-core:        1.3.2
        - idna:              3.10
        - importlib-metadata: 8.0.0
        - importlib-resources: 6.4.5
        - inflect:           7.3.1
        - jaraco.collections: 5.1.0
        - jaraco.context:    5.3.0
        - jaraco.functools:  4.0.1
        - jaraco.text:       3.12.1
        - jinja2:            3.1.4
        - jsonargparse:      4.33.1
        - kiwisolver:        1.4.7
        - lightning:         2.4.0
        - lightning-utilities: 0.11.7
        - markdown-it-py:    3.0.0
        - markupsafe:        2.1.5
        - matplotlib:        3.9.2
        - mdurl:             0.1.2
        - more-itertools:    10.3.0
        - mpmath:            1.3.0
        - multidict:         6.1.0
        - networkx:          3.3
        - numpy:             2.1.1
        - omegaconf:         2.3.0
        - packaging:         24.1
        - pillow:            10.4.0
        - pip:               24.2
        - platformdirs:      4.3.6
        - protobuf:          5.28.2
        - psutil:            6.0.0
        - pygments:          2.18.0
        - pyparsing:         3.1.4
        - python-dateutil:   2.9.0.post0
        - pytorch-lightning: 2.4.0
        - pyyaml:            6.0.2
        - requests:          2.32.3
        - rich:              13.9.1
        - scipy:             1.14.1
        - sentry-sdk:        2.15.0
        - setproctitle:      1.3.3
        - setuptools:        75.1.0
        - six:               1.16.0
        - smmap:             5.0.1
        - sympy:             1.13.3
        - tensorboardx:      2.6.2.2
        - tomli:             2.0.1
        - torch:             2.4.1
        - torchmetrics:      1.4.2
        - tqdm:              4.66.5
        - typeguard:         4.3.0
        - typeshed-client:   2.7.0
        - typing-extensions: 4.12.2
        - urllib3:           2.2.3
        - wandb:             0.18.3
        - wheel:             0.44.0
        - yarl:              1.13.1
        - zipp:              3.19.2
* System:
        - OS:                Darwin
        - architecture:
                - 64bit
                - 
        - processor:         arm
        - python:            3.11.0
        - release:           23.6.0
        - version:           Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103

Lightning installed with pip

More info

No response

Don't use class instances as default because there is no way for the parser to know how the instance was created, which is why it can't be serialized. The problem is in your code, not in lightning. You should give as default a dict with class_path. See the docs.

Okay, thanks :)