---
title: Profiler
description: 'If you wish to write a custom profiler, you should inherit from this class.'
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.profilers.Profiler
---
# Profiler {#profiler}

### `class lightning.pytorch.profilers.Profiler(dirpath=None, filename=None)` {#lightning.pytorch.profilers.Profiler}

Bases: [`ABC`](https://docs.python.org/3/library/abc.html#abc.ABC "(in Python v3.15)")

If you wish to write a custom profiler, you should inherit from this class.

#### `describe()` {#lightning.pytorch.profilers.Profiler.describe}

Logs a profile report after the conclusion of run.

Return type:

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

#### `profile(action_name)` {#lightning.pytorch.profilers.Profiler.profile}

Yields a context manager to encapsulate the scope of a profiled action.

Example:

```python
with self.profile('load training data'):
    # load training data code
```

The profiler will start once you’ve entered the context and will automatically stop once you exit the code block.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")

#### `setup(stage, local_rank=None, log_dir=None)` {#lightning.pytorch.profilers.Profiler.setup}

Execute arbitrary pre-profiling set-up steps.

Return type:

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

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

Defines how to start recording an action.

Return type:

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

#### `abstract stop(action_name)` {#lightning.pytorch.profilers.Profiler.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.Profiler.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)
