|  | 
| 4 | 4 | Find bottlenecks in your code | 
| 5 | 5 | ############################# | 
| 6 | 6 | 
 | 
| 7 |  | -.. raw:: html | 
| 8 |  | - | 
| 9 |  | -    <div class="display-card-container"> | 
| 10 |  | -        <div class="row"> | 
| 11 |  | - | 
| 12 |  | -.. Add callout items below this line | 
| 13 |  | -
 | 
| 14 | 7 | .. warning:: | 
| 15 | 8 | 
 | 
| 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): | 
| 19 | 16 | 
 | 
| 20 | 17 |     .. code-block:: python | 
| 21 | 18 | 
 | 
|  | 19 | +        import pytorch_lightning as pl | 
|  | 20 | +
 | 
| 22 | 21 |         trainer = pl.Trainer( | 
| 23 |  | -            profiler="pytorch",  # This is the correct and supported way | 
|  | 22 | +            profiler="pytorch",  # <- This enables built-in profiling safely! | 
| 24 | 23 |             ... | 
| 25 | 24 |         ) | 
|  | 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 | 
| 26 | 37 | 
 | 
| 27 | 38 | .. displayitem:: | 
| 28 | 39 |    :header: Basic | 
|  | 
0 commit comments