class_docstr: Computes and logs throughput with the :class:`~lightning.fabric.utilities.throughput.Throughput`
  
  Example::
  
      class MyModel(LightningModule):
          def setup(self, stage):
              with torch.device("meta"):
                  model = MyModel()
  
                  def sample_forward():
                      batch = torch.randn(...)
                      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 :func:`~lightning.fabric.utilities.throughput.measure_flops` function for this.
        You might want to compute it differently each time based on your setup.
  
  Args:
      batch_size_fn: A function to compute the number of samples given a batch.
      length_fn: A function to compute the number of items in a sample given a batch.
      \**kwargs: See available parameters in
          :class:`~lightning.fabric.utilities.throughput.Throughput`
class_docstr_tok: ["Computes","and","logs","throughput","with","the",":","class",":","`","~lightning.fabric.utilities.throughput.Throughput","`","Example",":",":","class","MyModel","(","LightningModule",")",":","def","setup","(","self",",","stage",")",":","with","torch.device","(","``","meta","''",")",":","model","=","MyModel","(",")","def","sample_forward","(",")",":","batch","=","torch.randn","(","...",")","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",":","func",":","`","~lightning.fabric.utilities.throughput.measure_flops","`","function","for","this",".","You","might","want","to","compute","it","differently","each","time","based","on","your","setup",".","Args",":","batch_size_fn",":","A","function","to","compute","the","number","of","samples","given","a","batch",".","length_fn",":","A","function","to","compute","the","number","of","items","in","a","sample","given","a","batch",".","\\","*","*","kwargs",":","See","available","parameters","in",":","class",":","`","~lightning.fabric.utilities.throughput.Throughput","`"]
code: def _update(
      self,
      trainer: "Trainer",
      pl_module: "LightningModule",
      batch: Any,
      iter_num: int,
  ) -> None:
      stage = trainer.state.stage
      assert stage is not None
      throughput = self._throughputs[stage]
  
      if trainer.strategy.root_device.type == "cuda":
          # required or else perf_counter() won't be correct
          torch.cuda.synchronize()
  
      elapsed = time.perf_counter() - self._t0s[stage]
      if self.length_fn is not None:
          self._lengths[stage] += self.length_fn(batch)
  
      if hasattr(pl_module, "flops_per_batch"):
          flops_per_batch = pl_module.flops_per_batch
      else:
          rank_zero_warn(
              "When using the `ThroughputMonitor`, you need to define a `flops_per_batch` attribute or property"
              f" in {type(pl_module).__name__} to compute the FLOPs."
          )
          flops_per_batch = None
  
      batch_size = self.batch_size_fn(batch)
      throughput.update(
          time=elapsed,
          batches=iter_num,
          # this assumes that all iterations used the same batch size
          samples=iter_num * batch_size,
          lengths=None
          if self.length_fn is None
          else self._lengths[stage],
          flops=flops_per_batch,
      )
  
imports: import time
  from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union
  import torch
  from typing_extensions import override
  from lightning.fabric.plugins import Precision
  from lightning.fabric.utilities.throughput import Throughput, get_available_flops
  from lightning.fabric.utilities.throughput import _plugin_to_compute_dtype
  from lightning.pytorch.callbacks import Callback
  from lightning.pytorch.plugins import BitsandbytesPrecision, DeepSpeedPrecision, DoublePrecision, FSDPPrecision, HalfPrecision, MixedPrecision, Precision, TransformerEnginePrecision, XLAPrecision
  from lightning.pytorch.trainer.states import RunningStage, TrainerFn
  from lightning.pytorch.utilities.rank_zero import rank_zero_only, rank_zero_warn
label_desc: Use image node_id 4 for calling the ThroughputMonitor obj's underlying member method code with example usage: obj._update(trainer, pl_module, batch, iter_num) without return types