Skip to content

Commit b28f2c8

Browse files
KAVYANSHTYAGIpre-commit-ci[bot]SkafteNickiBorda
committed
docs: warning don’t use torch.profiler.profile context manager around Trainer methods (#20864)
* Update profiler.rst * move documentation to basic --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Nicki Skafte Detlefsen <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> (cherry picked from commit 04e103b)
1 parent 052836a commit b28f2c8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/source-pytorch/tuning/profiler_basic.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,22 @@ This can be measured with the :class:`~lightning.pytorch.callbacks.device_stats_
121121

122122
CPU metrics will be tracked by default on the CPU accelerator. To enable it for other accelerators set ``DeviceStatsMonitor(cpu_stats=True)``. To disable logging
123123
CPU metrics, you can specify ``DeviceStatsMonitor(cpu_stats=False)``.
124+
125+
.. warning::
126+
127+
**Do not wrap** ``Trainer.fit()``, ``Trainer.validate()``, or other Trainer methods inside a manual
128+
``torch.profiler.profile`` context manager. This will cause unexpected crashes and cryptic errors due to
129+
incompatibility between PyTorch Profiler's context management and Lightning's internal training loop.
130+
Instead, always use the ``profiler`` argument in the ``Trainer`` constructor or the
131+
:class:`~lightning.pytorch.profilers.pytorch.PyTorchProfiler` profiler class if you want to customize the profiling.
132+
133+
Example:
134+
135+
.. code-block:: python
136+
137+
from lightning.pytorch import Trainer
138+
from lightning.pytorch.profilers import PytorchProfiler
139+
140+
trainer = Trainer(profiler="pytorch")
141+
# or
142+
trainer = Trainer(profiler=PytorchProfiler(dirpath=".", filename="perf_logs"))

0 commit comments

Comments
 (0)