-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
Bug description
Trying to validate a model every n steps by setting val_check_interval
to an integer and check_val_every_n_epochs
to None is not possible when using the CLI, because the CLI automatically parses all values to floats. The real issue, however, is that importing lightning changes the order of type annotations of modules. More details below.
What version are you seeing the problem on?
master
Reproduced in studio
No response
How to reproduce the bug
The following minimal example should throw the error:
# 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()
python main.py fit --trainer.val_check_interval 1 --trainer.check_val_every_n_epoch null
I've tried to track down the bug and found that importing lightning
changes the order of type annotations. jsonargparse
uses this order to verify the types in the CLI, leading to the error. The following code snippets demonstrate the problem. I was unable to find out exactly where or why importing lightning changes the order of annotations, though.
The following two scripts illustrate the problem
from typing import Optional, Union
class Foo:
def __init__(self, int_or_float: Optional[Union[int, float]] = None):
self.int_or_float = int_or_float
print(Foo.__init__.__annotations__["int_or_float"])
# typing.Union[int, float, NoneType]
from typing import Optional, Union
import lightning
class Foo:
def __init__(self, int_or_float: Optional[Union[int, float]] = None):
self.int_or_float = int_or_float
print(Foo.__init__.__annotations__["int_or_float"])
# typing.Union[float, int, NoneType]
Error messages and logs
Seed set to 0
💡 Tip: For seamless cloud uploads and versioning, try installing [litmodels](https://pypi.org/project/litmodels/) to enable LitModelCheckpoint, which syncs automatically with the Lightning model registry.
Traceback (most recent call last):
File "/mnt/ceph/storage/data-tmp/2025/fschlatt/pruning-col/minimal_2.py", line 11, in <module>
cli_main()
File "/mnt/ceph/storage/data-tmp/2025/fschlatt/pruning-col/minimal_2.py", line 6, in cli_main
cli = LightningCLI(DemoModel, BoringDataModule)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/cli.py", line 410, in __init__
self.instantiate_classes()
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/cli.py", line 604, in instantiate_classes
self.trainer = self.instantiate_trainer()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/cli.py", line 618, in instantiate_trainer
return self._instantiate_trainer(trainer_config, extra_callbacks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/cli.py", line 643, in _instantiate_trainer
return self.trainer_class(**config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/utilities/argparse.py", line 70, in insert_env_defaults
return fn(self, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/trainer/trainer.py", line 445, in __init__
self._data_connector.on_trainer_init(
File "/opt/conda/lib/python3.11/site-packages/lightning/pytorch/trainer/connectors/data_connector.py", line 65, in on_trainer_init
raise MisconfigurationException(
lightning.fabric.utilities.exceptions.MisconfigurationException: `val_check_interval` should be an integer when `check_val_every_n_epoch=None`, found 1.0.```
### Environment
<details>
<summary>Current environment</summary>
Current environment
- CUDA:
- GPU:
- NVIDIA A100-SXM4-40GB MIG 3g.20gb
- available: True
- version: 12.8
- GPU:
- Lightning:
- lightning: 2.5.5
- lightning-ir: 0.0.4
- lightning-utilities: 0.15.2
- pytorch-lightning: 2.5.5
- torch: 2.7.1+cu128
- torchaudio: 2.7.1+cu128
- torchdata: 0.11.0
- torchelastic: 0.2.2
- torchmetrics: 1.8.2
- torchvision: 0.22.1+cu128
- Packages:
- accelerate: 1.10.1
- aiohappyeyeballs: 2.6.1
- aiohttp: 3.12.15
- aiosignal: 1.4.0
- annotated-types: 0.7.0
- antlr4-python3-runtime: 4.9.3
- anyio: 4.10.0
- aquarel: 0.0.7
- archspec: 0.2.5
- argon2-cffi: 25.1.0
- argon2-cffi-bindings: 25.1.0
- arrow: 1.3.0
- asttokens: 3.0.0
- astunparse: 1.6.3
- async-lru: 2.0.5
- attrs: 25.3.0
- autocommand: 2.2.2
- babel: 2.17.0
- backports.tarfile: 1.2.0
- beautifulsoup4: 4.13.4
- beir: 2.2.0
- bitarray: 3.7.1
- black: 25.1.0
- bleach: 6.2.0
- blinker: 1.9.0
- boltons: 24.0.0
- brotli: 1.1.0
- bs4: 0.0.2
- cbor: 1.0.0
- certifi: 2025.8.3
- cffi: 1.17.1
- chardet: 5.2.0
- charset-normalizer: 3.4.2
- chest: 0.2.3
- click: 8.2.1
- cmake: 4.0.2
- colbert-ai: 0.2.19
- colorama: 0.4.6
- coloredlogs: 15.0.1
- comm: 0.2.3
- conda: 25.7.0
- conda-build: 25.5.0
- conda-index: 0.6.1
- conda-libmamba-solver: 25.3.0
- conda-package-handling: 2.4.0
- conda-package-streaming: 0.11.0
- contourpy: 1.3.3
- cycler: 0.12.1
- cython: 3.1.4
- datasets: 4.1.0
- debugpy: 1.8.16
- decorator: 5.2.1
- defusedxml: 0.7.1
- deprecated: 1.2.18
- dill: 0.4.0
- distro: 1.9.0
- dnspython: 2.7.0
- docker: 7.1.0
- docstring-parser: 0.17.0
- einops: 0.8.1
- evalidate: 2.0.5
- exceptiongroup: 1.3.0
- executing: 2.2.0
- expecttest: 0.3.0
- faiss: 1.9.0
- fastapi: 0.116.2
- fastchat: 0.1.0
- fastjsonschema: 2.21.2
- fastkmeans: 0.5.0
- filelock: 3.18.0
- flash-attn: 2.8.3
- flask: 3.1.2
- flatbuffers: 25.2.10
- fonttools: 4.59.2
- fqdn: 1.5.1
- frozendict: 2.4.6
- frozenlist: 1.7.0
- fsspec: 2025.5.1
- ftfy: 6.3.1
- git-python: 1.0.3
- gitdb: 4.0.12
- gitpython: 3.1.45
- h11: 0.16.0
- h2: 4.2.0
- heapdict: 1.0.1
- hf-xet: 1.1.10
- hpack: 4.1.0
- httpcore: 1.0.9
- httpx: 0.28.1
- httpx-sse: 0.4.1
- huggingface-hub: 0.35.0
- humanfriendly: 10.0
- hyperframe: 6.1.0
- hypothesis: 6.135.0
- idna: 3.10
- ijson: 3.4.0
- importlib-metadata: 8.0.0
- importlib-resources: 6.5.2
- inflect: 7.3.1
- iniconfig: 2.1.0
- inscriptis: 2.6.0
- ipykernel: 6.30.1
- ipython: 9.3.0
- ipython-pygments-lexers: 1.1.1
- ipywidgets: 8.1.7
- ir-datasets: 0.5.11
- ir-measures: 0.4.1
- isoduration: 20.11.0
- isort: 6.0.1
- itsdangerous: 2.2.0
- jaraco.collections: 5.1.0
- jaraco.context: 5.3.0
- jaraco.functools: 4.0.1
- jaraco.text: 3.12.1
- jedi: 0.19.2
- jinja2: 3.1.6
- jiter: 0.11.0
- joblib: 1.5.2
- json5: 0.12.1
- jsonargparse: 4.41.0
- jsonlines: 4.0.0
- jsonpatch: 1.33
- jsonpointer: 3.0.0
- jsonschema: 4.24.0
- jsonschema-specifications: 2025.4.1
- jupyter: 1.1.1
- jupyter-client: 8.6.3
- jupyter-console: 6.6.3
- jupyter-core: 5.8.1
- jupyter-events: 0.12.0
- jupyter-lsp: 2.3.0
- jupyter-server: 2.17.0
- jupyter-server-terminals: 0.5.3
- jupyterlab: 4.4.7
- jupyterlab-pygments: 0.3.0
- jupyterlab-server: 2.27.3
- jupyterlab-widgets: 3.0.15
- kiwisolver: 1.4.9
- libarchive-c: 5.3
- libmambapy: 2.1.1
- lief: 0.16.4
- lightning: 2.5.5
- lightning-ir: 0.0.4
- lightning-utilities: 0.15.2
- lintrunner: 0.12.7
- lxml: 6.0.1
- lz4: 4.4.4
- markupsafe: 3.0.2
- matplotlib: 3.10.6
- matplotlib-inline: 0.1.7
- maturin: 1.9.4
- mcp: 1.14.0
- menuinst: 2.2.0
- mistune: 3.1.4
- more-itertools: 10.4.0
- mpmath: 1.3.0
- msgpack: 1.1.0
- multidict: 6.6.4
- multiprocess: 0.70.16
- mypy-extensions: 1.1.0
- nbclient: 0.10.2
- nbconvert: 7.16.6
- nbformat: 5.10.4
- nest-asyncio: 1.6.0
- networkx: 3.5
- ninja: 1.11.1.4
- nltk: 3.9.1
- notebook: 7.4.5
- notebook-shim: 0.2.4
- numpy: 1.26.4
- nvidia-cublas-cu12: 12.8.3.14
- nvidia-cuda-cupti-cu12: 12.8.57
- nvidia-cuda-nvrtc-cu12: 12.8.61
- nvidia-cuda-runtime-cu12: 12.8.57
- nvidia-cudnn-cu12: 9.7.1.26
- nvidia-cufft-cu12: 11.3.3.41
- nvidia-cufile-cu12: 1.13.0.11
- nvidia-curand-cu12: 10.3.9.55
- nvidia-cusolver-cu12: 11.7.2.55
- nvidia-cusparse-cu12: 12.5.7.53
- nvidia-cusparselt-cu12: 0.6.3
- nvidia-nccl-cu12: 2.26.2
- nvidia-nvjitlink-cu12: 12.8.61
- nvidia-nvtx-cu12: 12.8.55
- omegaconf: 2.3.0
- onnxruntime: 1.22.1
- openai: 1.107.3
- optree: 0.16.0
- overrides: 7.7.0
- packaging: 25.0
- pandas: 2.3.2
- pandocfilters: 1.5.1
- parso: 0.8.4
- pathspec: 0.12.1
- patsy: 1.0.1
- pexpect: 4.9.0
- pickleshare: 0.7.5
- pillow: 11.0.0
- pip: 25.1.1
- pkginfo: 1.12.1.2
- pkgutil-resolve-name: 1.3.10
- platformdirs: 4.3.8
- pluggy: 1.5.0
- prometheus-client: 0.22.1
- prompt-toolkit: 3.0.51
- propcache: 0.3.2
- protobuf: 6.32.1
- psutil: 7.0.0
- ptranking: 0.0.5
- ptyprocess: 0.7.0
- pure-eval: 0.2.3
- pyarrow: 21.0.0
- pycosat: 0.6.6
- pycparser: 2.22
- pydantic: 2.11.9
- pydantic-core: 2.33.2
- pydantic-settings: 2.10.1
- pygments: 2.19.1
- pyjnius: 1.7.0
- pyndeval: 0.0.6
- pyparsing: 3.2.4
- pyserini: 1.2.0
- pysocks: 1.7.1
- pytest: 8.4.2
- python-dateutil: 2.9.0.post0
- python-dotenv: 1.1.1
- python-etcd: 0.4.5
- python-json-logger: 3.3.0
- python-multipart: 0.0.20
- python-terrier: 0.13.1
- pytorch-lightning: 2.5.5
- pytrec-eval-terrier: 0.5.9
- pytz: 2025.2
- pyyaml: 6.0.2
- pyzmq: 27.1.0
- referencing: 0.36.2
- regex: 2025.9.1
- requests: 2.32.3
- rfc3339-validator: 0.1.4
- rfc3986-validator: 0.1.1
- rpds-py: 0.25.1
- ruamel-yaml: 0.18.12
- ruamel.yaml.clib: 0.2.8
- safetensors: 0.6.2
- sarge: 0.1.7.post1
- scikit-learn: 1.7.2
- scipy: 1.16.2
- seaborn: 0.13.2
- send2trash: 1.8.3
- sentence-transformers: 5.1.0
- sentencepiece: 0.2.1
- sentry-sdk: 2.38.0
- setuptools: 80.9.0
- six: 1.17.0
- smmap: 5.0.2
- sniffio: 1.3.1
- sortedcontainers: 2.4.0
- soupsieve: 2.7
- sse-starlette: 3.0.2
- stack-data: 0.6.3
- starlette: 0.48.0
- statsmodels: 0.14.5
- sympy: 1.14.0
- terminado: 0.18.1
- threadpoolctl: 3.6.0
- tiktoken: 0.11.0
- tinycss2: 1.4.0
- tira: 0.0.178
- tokenizers: 0.22.0
- tomli: 2.0.1
- torch: 2.7.1+cu128
- torchaudio: 2.7.1+cu128
- torchdata: 0.11.0
- torchelastic: 0.2.2
- torchmetrics: 1.8.2
- torchvision: 0.22.1+cu128
- tornado: 6.5.2
- tqdm: 4.67.1
- traitlets: 5.14.3
- transformers: 4.56.1
- trec-car-tools: 2.6
- trectools: 0.0.50
- triton: 3.3.1
- truststore: 0.10.1
- typeguard: 4.3.0
- types-dataclasses: 0.6.6
- types-python-dateutil: 2.9.0.20250822
- typeshed-client: 2.8.2
- typing-extensions: 4.14.0
- typing-inspection: 0.4.1
- tzdata: 2025.2
- ujson: 5.11.0
- unlzw3: 0.2.3
- uri-template: 1.3.0
- urllib3: 2.4.0
- uvicorn: 0.35.0
- wandb: 0.21.4
- warc3-wet: 0.2.5
- warc3-wet-clueweb09: 0.2.5
- wcwidth: 0.2.13
- webcolors: 24.11.1
- webencodings: 0.5.1
- websocket-client: 1.8.0
- werkzeug: 3.1.3
- wheel: 0.45.1
- widgetsnbextension: 4.0.14
- wrapt: 1.17.3
- xxhash: 3.5.0
- yarl: 1.20.1
- zipp: 3.22.0
- zlib-state: 0.1.10
- zstandard: 0.23.0
- System:
- OS: Linux
- architecture:
- 64bit
- processor: x86_64
- python: 3.11.13
- release: 6.11.0-26-generic
- version: Cannot load saved model. #26~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 17 19:20:47 UTC 2
</details>
cc @mauvilsa