Skip to content

Commit a4910f8

Browse files
authored
Merge branch 'master' into feature/9580-rich-defaults
2 parents b0d0c9c + 89dbc55 commit a4910f8

File tree

7 files changed

+17
-8
lines changed

7 files changed

+17
-8
lines changed

docs/source-pytorch/common/checkpointing_basic.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The LightningModule also has access to the Hyperparameters
111111
.. code-block:: python
112112
113113
model = MyLightningModule.load_from_checkpoint("/path/to/checkpoint.ckpt")
114-
print(model.learning_rate)
114+
print(model.hparams.learning_rate)
115115
116116
----
117117

examples/fabric/image_classifier/train_fabric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def run(hparams):
158158
# When using distributed training, use `fabric.save`
159159
# to ensure the current process is allowed to save a checkpoint
160160
if hparams.save_model:
161-
fabric.save(model.state_dict(), "mnist_cnn.pt")
161+
fabric.save(path="mnist_cnn.pt", state=model.state_dict())
162162

163163

164164
if __name__ == "__main__":

examples/fabric/kfold_cv/train_fabric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def run(hparams):
161161
# When using distributed training, use `fabric.save`
162162
# to ensure the current process is allowed to save a checkpoint
163163
if hparams.save_model:
164-
fabric.save(model.state_dict(), "mnist_cnn.pt")
164+
fabric.save(path="mnist_cnn.pt", state=model.state_dict())
165165

166166

167167
if __name__ == "__main__":

examples/fabric/tensor_parallel/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def train():
6767
# See `fabric consolidate --help` if you need to convert the checkpoint to a single file
6868
fabric.print("Saving a (distributed) checkpoint ...")
6969
state = {"model": model, "optimizer": optimizer, "iteration": i}
70-
fabric.save("checkpoint.pt", state)
70+
fabric.save(path="checkpoint.pt", state=state)
7171

7272
fabric.print("Training successfully completed!")
7373
fabric.print(f"Peak memory usage: {torch.cuda.max_memory_allocated() / 1e9:.02f} GB")

src/lightning/pytorch/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2828
### Fixed
2929

3030
- fix progress bar console clearing for Rich `14.1+` ([#21016](https://github.com/Lightning-AI/pytorch-lightning/pull/21016))
31+
- fix `AdvancedProfiler` to handle nested profiling actions for Python 3.12+ ([#20809](https://github.com/Lightning-AI/pytorch-lightning/pull/20809))
3132

3233

3334
---

src/lightning/pytorch/profilers/advanced.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import pstats
2121
import tempfile
22+
from collections import defaultdict
2223
from pathlib import Path
2324
from typing import Optional, Union
2425

@@ -66,14 +67,15 @@ def __init__(
6667
If you attempt to stop recording an action which was never started.
6768
"""
6869
super().__init__(dirpath=dirpath, filename=filename)
69-
self.profiled_actions: dict[str, cProfile.Profile] = {}
70+
self.profiled_actions: dict[str, cProfile.Profile] = defaultdict(cProfile.Profile)
7071
self.line_count_restriction = line_count_restriction
7172
self.dump_stats = dump_stats
7273

7374
@override
7475
def start(self, action_name: str) -> None:
75-
if action_name not in self.profiled_actions:
76-
self.profiled_actions[action_name] = cProfile.Profile()
76+
# Disable all profilers before starting a new one
77+
for pr in self.profiled_actions.values():
78+
pr.disable()
7779
self.profiled_actions[action_name].enable()
7880

7981
@override
@@ -114,7 +116,7 @@ def summary(self) -> str:
114116
@override
115117
def teardown(self, stage: Optional[str]) -> None:
116118
super().teardown(stage=stage)
117-
self.profiled_actions = {}
119+
self.profiled_actions.clear()
118120

119121
def __reduce__(self) -> tuple:
120122
# avoids `TypeError: cannot pickle 'cProfile.Profile' object`

tests/tests_pytorch/profilers/test_profiler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ def test_advanced_profiler_deepcopy(advanced_profiler):
336336
assert deepcopy(advanced_profiler)
337337

338338

339+
def test_advanced_profiler_nested(advanced_profiler):
340+
"""Ensure AdvancedProfiler does not raise ValueError for nested profiling actions (Python 3.12+ compatibility)."""
341+
with advanced_profiler.profile("outer"), advanced_profiler.profile("inner"):
342+
pass # Should not raise ValueError
343+
344+
339345
@pytest.fixture
340346
def pytorch_profiler(tmp_path):
341347
return PyTorchProfiler(dirpath=tmp_path, filename="profiler")

0 commit comments

Comments
 (0)