---
title: ThroughputMonitor
description: Computes and logs throughput with the Throughput
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.callbacks.ThroughputMonitor
---
# ThroughputMonitor {#throughputmonitor}

### `class lightning.pytorch.callbacks.ThroughputMonitor(batch_size_fn, length_fn=None, **kwargs)` {#lightning.pytorch.callbacks.ThroughputMonitor}

Bases: [`Callback`](/docs/pytorch/2.6.6/optional-api/api/lightning.pytorch.callbacks.Callback.md#lightning.pytorch.callbacks.Callback "lightning.pytorch.callbacks.callback.Callback")

Computes and logs throughput with the [`Throughput`](/docs/fabric/stable/api/utilities#lightning.fabric.utilities.throughput.Throughput "(in lightning v2.7.0.dev)")

Example:

```python
class MyModel(LightningModule):
    def setup(self, stage):
        with torch.device("meta"):
            model = MyModel()

            def sample_forward():
                batch = torch.randn(..., device="meta")
                return model(batch)

            self.flops_per_batch = measure_flops(model, sample_forward, loss_fn=torch.Tensor.sum)


logger = ...
throughput = ThroughputMonitor(batch_size_fn=lambda batch: batch.size(0))
trainer = Trainer(max_steps=1000, log_every_n_steps=10, callbacks=throughput, logger=logger)
model = MyModel()
trainer.fit(model)
```

Notes

*   It assumes that the batch size is the same during all iterations.
    
*   It will try to access a `flops_per_batch` attribute on your `LightningModule` on every iteration. We suggest using the [`measure_flops()`](/docs/fabric/stable/api/utilities#lightning.fabric.utilities.throughput.measure_flops "(in lightning v2.7.0.dev)") function for this. You might want to compute it differently each time based on your setup.
    

Parameters:

*   **batch\_size\_fn** ([`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\], [`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\]) – A function to compute the number of samples given a batch.
    
*   **length\_fn** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable "(in Python v3.15)")\[\[[`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")\], [`int`](https://docs.python.org/3/builtins/functions.html#int "(in Python v3.15)")\]\]) – A function to compute the number of items in a sample given a batch.
    
*   **\*\*kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – See available parameters in [`Throughput`](/docs/fabric/stable/api/utilities#lightning.fabric.utilities.throughput.Throughput "(in lightning v2.7.0.dev)")
    

#### `on_predict_batch_end(trainer, pl_module, outputs, batch, *_, **__)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_predict_batch_end}

Called when the predict batch ends.

Return type:

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

#### `on_predict_start(trainer, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_predict_start}

Called when the predict begins.

Return type:

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

#### `on_test_batch_end(trainer, pl_module, outputs, batch, *_, **__)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_test_batch_end}

Called when the test batch ends.

Return type:

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

#### `on_test_start(trainer, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_test_start}

Called when the test begins.

Return type:

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

#### `on_train_batch_end(trainer, pl_module, outputs, batch, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_train_batch_end}

Called when the train batch ends. :rtype: [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")

Note

The value `outputs["loss"]` here will be the normalized value w.r.t `accumulate_grad_batches` of the loss returned from `training_step`.

#### `on_train_start(trainer, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_train_start}

Called when the train begins.

Return type:

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

#### `on_validation_batch_end(trainer, pl_module, outputs, batch, *_, **__)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_validation_batch_end}

Called when the validation batch ends.

Return type:

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

#### `on_validation_end(trainer, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_validation_end}

Called when the validation loop ends.

Return type:

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

#### `on_validation_start(trainer, *_)` {#lightning.pytorch.callbacks.ThroughputMonitor.on_validation_start}

Called when the validation loop begins.

Return type:

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

#### `setup(trainer, pl_module, stage)` {#lightning.pytorch.callbacks.ThroughputMonitor.setup}

Called when fit, validate, test, predict, or tune begins.

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)
