---
title: PyTorchProfiler
description: >-
  This profiler uses PyTorch’s Autograd Profiler and lets you inspect the cost
  of

  different operators inside your model - both on the CPU and GPU.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.profilers.PyTorchProfiler
---
# PyTorchProfiler {#pytorchprofiler}

### `class lightning.pytorch.profilers.PyTorchProfiler(dirpath=None, filename=None, group_by_input_shapes=False, emit_nvtx=False, export_to_chrome=True, row_limit=20, sort_by_key=None, record_module_names=True, table_kwargs=None, **profiler_kwargs)` {#lightning.pytorch.profilers.PyTorchProfiler}

Bases: [`Profiler`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.profilers.Profiler.md#lightning.pytorch.profilers.Profiler "lightning.pytorch.profilers.profiler.Profiler")

This profiler uses PyTorch’s Autograd Profiler and lets you inspect the cost of different operators inside your model - both on the CPU and GPU.

Parameters:

*   **dirpath** (`Union`\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Directory path for the `filename`. If `dirpath` is `None` but `filename` is present, the `trainer.log_dir` (from [`TensorBoardLogger`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.loggers.tensorboard.md#lightning.pytorch.loggers.tensorboard.TensorBoardLogger "lightning.pytorch.loggers.tensorboard.TensorBoardLogger")) will be used.
    
*   **filename** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – If present, filename where the profiler results will be saved instead of printing to stdout. The `.txt` extension will be used automatically.
    
*   **group\_by\_input\_shapes** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Include operator input shapes and group calls by shape.
    
*   **emit\_nvtx** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) –
    
    Context manager that makes every autograd operation emit an NVTX range Run:
    
    ```python
    nvprof --profile-from-start off -o trace_name.prof -- <regular command here>
    ```
    
    To visualize, you can either use:
    
    ```python
    nvvp trace_name.prof
    torch.autograd.profiler.load_nvprof(path)
    ```
    
*   **export\_to\_chrome** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Whether to export the sequence of profiled operators for Chrome. It will generate a `.json` file which can be read by Chrome.
    
*   **row\_limit** ([`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")) – Limit the number of rows in a table, `-1` is a special value that removes the limit completely.
    
*   **sort\_by\_key** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")\]) – Attribute used to sort entries. By default they are printed in the same order as they were registered. Valid keys include: `cpu_time`, `cuda_time`, `cpu_time_total`, `cuda_time_total`, `cpu_memory_usage`, `cuda_memory_usage`, `self_cpu_memory_usage`, `self_cuda_memory_usage`, `count`.
    
*   **record\_module\_names** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Whether to add module names while recording autograd operation.
    
*   **table\_kwargs** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\]\]) – Dictionary with keyword arguments for the summary table.
    
*   **\*\*profiler\_kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Keyword arguments for the [`Pytorch profiler`](https://docs.pytorch.org/docs/stable/profiler.html#torch.profiler.profile "(in PyTorch v2.15)"). The exact available options depend on your PyTorch version.
    

Raises:

**MisconfigurationException** – If arg `sort_by_key` is not present in `AVAILABLE_SORT_KEYS`. If arg `schedule` is not a `Callable`. If arg `schedule` does not return a `torch.profiler.ProfilerAction`.

#### `start(action_name)` {#lightning.pytorch.profilers.PyTorchProfiler.start}

Defines how to start recording an action.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `stop(action_name)` {#lightning.pytorch.profilers.PyTorchProfiler.stop}

Defines how to record the duration once an action is complete.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

#### `teardown(stage)` {#lightning.pytorch.profilers.PyTorchProfiler.teardown}

Execute arbitrary post-profiling tear-down steps.

Closes the currently open file and stream.

Return type:

[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
