code: def test_device_stats_gpu_from_torch(tmpdir):
      """Test GPU stats are logged using a logger."""
      model = BoringModel()
      device_stats = DeviceStatsMonitor()
  
      class DebugLogger(CSVLogger):
          @rank_zero_only
          def log_metrics(
              self,
              metrics: Dict[str, float],
              step: Optional[int] = None,
          ) -> None:
              fields = [
                  "allocated_bytes.all.freed",
                  "inactive_split.all.peak",
                  "reserved_bytes.large_pool.peak",
              ]
              for f in fields:
                  assert any(f in h for h in metrics)
  
      trainer = Trainer(
          default_root_dir=tmpdir,
          max_epochs=2,
          limit_train_batches=7,
          log_every_n_steps=1,
          accelerator="gpu",
          devices=1,
          callbacks=[device_stats],
          logger=DebugLogger(tmpdir),
          enable_checkpointing=False,
          enable_progress_bar=False,
      )
  
      trainer.fit(model)
  
imports: import csv
  import os
  import re
  from typing import Dict, Optional
  from unittest import mock
  from unittest.mock import Mock
  import pytest
  import torch
  from lightning.pytorch import Trainer
  from lightning.pytorch.accelerators.cpu import _CPU_PERCENT, _CPU_SWAP_PERCENT, _CPU_VM_PERCENT, get_cpu_stats
  from lightning.pytorch.callbacks import DeviceStatsMonitor
  from lightning.pytorch.callbacks.device_stats_monitor import _prefix_metric_keys
  from lightning.pytorch.demos.boring_classes import BoringModel
  from lightning.pytorch.loggers import CSVLogger
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.rank_zero import rank_zero_only
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 1 for calling a global function with example usage: test_device_stats_gpu_from_torch(tmpdir) without return types