Skip to content

Commit 82d7717

Browse files
Update profiler.rst
1 parent df928db commit 82d7717

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

docs/source-pytorch/tuning/profiler.rst

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,36 @@
44
Find bottlenecks in your code
55
#############################
66

7-
.. raw:: html
8-
9-
<div class="display-card-container">
10-
<div class="row">
11-
12-
.. Add callout items below this line
13-
147
.. warning::
158

16-
Do **not** wrap ``Trainer.fit()``, ``Trainer.validate()``, or similar Trainer methods inside a manual ``torch.profiler.profile`` context manager.
17-
This will cause unexpected crashes and cryptic errors due to incompatibility between PyTorch Profiler's context and Lightning's training loop.
18-
Instead, use the ``profiler`` argument of the ``Trainer``:
9+
**Do not wrap** ``Trainer.fit()``, ``Trainer.validate()``, or other Trainer methods
10+
inside a manual ``torch.profiler.profile`` context manager.
11+
This will cause unexpected crashes and cryptic errors due to incompatibility between
12+
PyTorch Profiler's context management and Lightning's internal training loop.
13+
Instead, always use the ``profiler`` argument in the ``Trainer`` constructor.
14+
15+
Example (correct usage):
1916

2017
.. code-block:: python
2118
19+
import pytorch_lightning as pl
20+
2221
trainer = pl.Trainer(
23-
profiler="pytorch", # This is the correct and supported way
22+
profiler="pytorch", # <- This enables built-in profiling safely!
2423
...
2524
)
25+
trainer.fit(model, train_dataloaders=...)
26+
27+
**References:**
28+
- https://github.com/pytorch/pytorch/issues/88472
29+
- https://github.com/Lightning-AI/lightning/issues/16958
30+
31+
.. raw:: html
32+
33+
<div class="display-card-container">
34+
<div class="row">
35+
36+
.. Add callout items below this line
2637
2738
.. displayitem::
2839
:header: Basic

0 commit comments

Comments
 (0)