Skip to content

Commit 500a6d3

Browse files
authored
Merge branch 'master' into lr-finder
2 parents e0c8a1e + 6989e15 commit 500a6d3

28 files changed

+787
-62
lines changed

.actions/assistant.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,25 @@ def generate_docker_tags(
459459
tags = [f"{docker_project}:{tag}" for tag in tags]
460460
print(",".join(tags))
461461

462+
@staticmethod
463+
def prune_pytest_as_errors(
464+
pyproject_toml: str = "pyproject.toml", errors: tuple = ("FutureWarning", "DeprecationWarning")
465+
) -> None:
466+
"""Prune pytest warnings as errors from the pyproject.toml file."""
467+
import tomlkit
468+
469+
with open(pyproject_toml, encoding="utf-8") as fopen:
470+
content = fopen.read()
471+
pyproject = tomlkit.parse(content)
472+
filterwarnings = pyproject.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("filterwarnings", [])
473+
if not filterwarnings:
474+
return
475+
filterwarnings = [wrn for wrn in filterwarnings if not any(f"error::{err}" in wrn for err in errors)]
476+
pyproject["tool"]["pytest"]["ini_options"]["filterwarnings"] = filterwarnings
477+
478+
with open(pyproject_toml, "w", encoding="utf-8") as fopen:
479+
fopen.write(tomlkit.dumps(pyproject))
480+
462481

463482
if __name__ == "__main__":
464483
import jsonargparse

.github/workflows/ci-tests-fabric.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ jobs:
9999
- name: Set min. dependencies
100100
if: ${{ matrix.config.requires == 'oldest' }}
101101
run: |
102-
cd requirements/fabric
103102
uv pip install -U "lightning-utilities[cli]"
104-
python -m lightning_utilities.cli requirements set-oldest --req_files "['base.txt', 'strategies.txt', 'test.txt']"
103+
python -m lightning_utilities.cli requirements set-oldest \
104+
--req_files "['requirements/fabric/base.txt', 'requirements/fabric/strategies.txt', 'requirements/fabric/test.txt']"
105105
uv pip install "cython<3.0" wheel
106106
uv pip install "pyyaml==5.4" --no-build-isolation
107+
# This script removes any line containing "error::FutureWarning" from pyproject.toml
108+
uv pip install -r requirements/ci.txt
109+
python .actions/assistant.py prune_pytest_as_errors
107110
108111
- name: Adjust PyTorch versions in requirements files
109112
if: ${{ matrix.config.requires != 'oldest' }}

.github/workflows/ci-tests-pytorch.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ jobs:
9898
- name: Set min. dependencies
9999
if: ${{ matrix.config.requires == 'oldest' }}
100100
run: |
101-
cd requirements/pytorch
102101
uv pip install -U "lightning-utilities[cli]"
103-
python -m lightning_utilities.cli requirements set-oldest --req_files "['base.txt', 'extra.txt', 'strategies.txt', 'examples.txt', 'test.txt']"
102+
python -m lightning_utilities.cli requirements set-oldest \
103+
--req_files "['requirements/pytorch/base.txt', 'requirements/pytorch/extra.txt', 'requirements/pytorch/strategies.txt', 'requirements/pytorch/examples.txt', 'requirements/pytorch/test.txt']"
104104
uv pip install "cython<3.0" wheel
105105
uv pip install "pyyaml==5.4" --no-build-isolation
106+
# This script removes any line containing "error::FutureWarning" from pyproject.toml
107+
uv pip install -r requirements/ci.txt
108+
python .actions/assistant.py prune_pytest_as_errors
106109
107110
- name: Adjust PyTorch versions in requirements files
108111
if: ${{ matrix.config.requires != 'oldest' }}

.github/workflows/code-checks.yml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,32 @@ jobs:
2929
runs-on: ubuntu-22.04
3030
steps:
3131
- uses: actions/checkout@v5
32-
- uses: actions/setup-python@v6
33-
with:
34-
python-version: "3.11"
3532

36-
- name: Mypy cache
37-
uses: actions/cache@v4
33+
- name: Install uv and set Python version
34+
uses: astral-sh/setup-uv@v6
3835
with:
39-
path: .mypy_cache
40-
key: mypy-${{ hashFiles('requirements/typing.txt') }}
36+
python-version: "3.11"
37+
# TODO: Avoid activating environment like this
38+
# see: https://github.com/astral-sh/setup-uv/tree/v6/?tab=readme-ov-file#activate-environment
39+
activate-environment: true
40+
enable-cache: true
4141

4242
- name: Install dependencies
4343
env:
4444
FREEZE_REQUIREMENTS: 1
4545
timeout-minutes: 20
4646
run: |
47-
pip install -e '.[pytorch-all,fabric-all]' -r requirements/typing.txt
48-
pip list
47+
uv pip install '.[pytorch-all,fabric-all]' -r requirements/typing.txt
48+
uv pip list
49+
50+
- name: mypy cache
51+
uses: actions/cache@v4
52+
with:
53+
path: .mypy_cache
54+
key: mypy-${{ hashFiles('requirements/typing.txt') }}
4955

5056
- name: Check typing
5157
run: mypy
58+
59+
- name: Minimize uv cache
60+
run: uv cache prune --ci

docs/source-pytorch/common/early_stopping.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.. testsetup:: *
22

3-
from lightning.pytorch.callbacks.early_stopping import EarlyStopping
3+
from lightning.pytorch.callbacks.early_stopping import EarlyStopping, EarlyStoppingReason
4+
from lightning.pytorch import Trainer, LightningModule
45

56
.. _early_stopping:
67

@@ -71,6 +72,37 @@ Additional parameters that stop training at extreme points:
7172
- ``check_on_train_epoch_end``: When turned on, it checks the metric at the end of a training epoch. Use this only when you are monitoring any metric logged within
7273
training-specific hooks on epoch-level.
7374

75+
After training completes, you can programmatically check why early stopping occurred using the ``stopping_reason``
76+
attribute, which returns an ``EarlyStoppingReason`` enum value.
77+
78+
.. code-block:: python
79+
80+
from lightning.pytorch.callbacks import EarlyStopping
81+
from lightning.pytorch.callbacks.early_stopping import EarlyStoppingReason
82+
83+
early_stopping = EarlyStopping(monitor="val_loss", patience=3)
84+
trainer = Trainer(callbacks=[early_stopping])
85+
trainer.fit(model)
86+
87+
# Check why training stopped
88+
if early_stopping.stopping_reason == EarlyStoppingReason.PATIENCE_EXHAUSTED:
89+
print("Training stopped due to patience exhaustion")
90+
elif early_stopping.stopping_reason == EarlyStoppingReason.STOPPING_THRESHOLD:
91+
print("Training stopped due to reaching stopping threshold")
92+
elif early_stopping.stopping_reason == EarlyStoppingReason.NOT_STOPPED:
93+
print("Training completed normally without early stopping")
94+
95+
# Access human-readable message
96+
if early_stopping.stopping_reason_message:
97+
print(f"Details: {early_stopping.stopping_reason_message}")
98+
99+
The available stopping reasons are:
100+
101+
- ``NOT_STOPPED``: Training completed normally without early stopping
102+
- ``STOPPING_THRESHOLD``: Training stopped because the monitored metric reached the stopping threshold
103+
- ``DIVERGENCE_THRESHOLD``: Training stopped because the monitored metric exceeded the divergence threshold
104+
- ``PATIENCE_EXHAUSTED``: Training stopped because the metric didn't improve for the specified patience
105+
- ``NON_FINITE_METRIC``: Training stopped because the monitored metric became NaN or infinite
74106

75107
In case you need early stopping in a different part of training, subclass :class:`~lightning.pytorch.callbacks.early_stopping.EarlyStopping`
76108
and change where it is called:

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ filterwarnings = [
182182
# "error::DeprecationWarning",
183183
"error::FutureWarning",
184184
"ignore::FutureWarning:onnxscript", # Temporary ignore until onnxscript is updated
185-
"ignore:The pynvml package is deprecated:FutureWarning", # Ignore pynvml deprecation warning, since it is not installed by PL directly
186185
]
187186
xfail_strict = true
188187
junit_duration_report = "call"

requirements/ci.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ importlib-metadata <9.0.0
66
wget
77
pkginfo ==1.12.1.2
88
packaging <25.1
9+
tomlkit

requirements/fabric/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
coverage ==7.10.6
22
numpy >=1.21.0, <1.27.0
33
pytest ==8.4.2
4-
pytest-cov ==6.2.1
4+
pytest-cov ==6.3.0
55
pytest-timeout ==2.4.0
66
pytest-rerunfailures ==16.0.1
77
pytest-random-order ==1.2.0

requirements/pytorch/examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
requests <2.33.0
55
torchvision >=0.16.0, <0.24.0
6-
ipython[all] >=8.0.0, <9.0.0
6+
ipython[all] >=8.0.0, <10.0.0
77
torchmetrics >=0.10.0, <1.9.0

requirements/pytorch/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
coverage ==7.10.6
22
pytest ==8.4.2
3-
pytest-cov ==6.2.1
3+
pytest-cov ==6.3.0
44
pytest-timeout ==2.4.0
55
pytest-rerunfailures ==16.0.1
66
pytest-random-order ==1.2.0

0 commit comments

Comments
 (0)