@@ -34,21 +34,67 @@ class DeviceStatsMonitor(Callback):
34
34
r"""Automatically monitors and logs device stats during training, validation and testing stage.
35
35
``DeviceStatsMonitor`` is a special callback as it requires a ``logger`` to passed as argument to the ``Trainer``.
36
36
37
- Device statistics are logged with keys prefixed as ``DeviceStatsMonitor.{hook_name}/{base_metric_name}`` (e.g.,
38
- ``DeviceStatsMonitor.on_train_batch_start/cpu_percent``).
39
- The source of these metrics depends on the ``cpu_stats`` flag and the active accelerator.
40
37
41
- CPU (via ``psutil``): Logs ``cpu_percent``, ``cpu_vm_percent``, ``cpu_swap_percent``.
42
- All are percentages (%).
43
- CUDA GPU (via :func:`torch.cuda.memory_stats`): Logs detailed memory statistics from
44
- PyTorch's allocator (e.g., ``allocated_bytes.all.current``, ``num_ooms``; all in Bytes).
45
- GPU compute utilization is not logged by default.
46
- Other Accelerators (e.g., TPU, MPS): Logs device-specific stats:
38
+ **Logged Metrics**
47
39
48
- - TPU example: ``avg. free memory (MB)``.
49
- - MPS example: ``mps.current_allocated_bytes``.
40
+ Logs device statistics with keys prefixed as ``DeviceStatsMonitor.{hook_name}/{base_metric_name}``.
50
41
51
- Observe logs or check accelerator documentation for details.
42
+ The actual metrics depend on the active accelerator and the ``cpu_stats`` flag.
43
+
44
+ **CPU (via `psutil`)**
45
+
46
+ - ``cpu_percent``: System-wide CPU utilization (%)
47
+ - ``cpu_vm_percent``: System-wide virtual memory (RAM) utilization (%)
48
+ - ``cpu_swap_percent``: System-wide swap memory utilization (%)
49
+
50
+ **CUDA GPU (via `torch.cuda.memory_stats`)**
51
+
52
+ Logs memory statistics from PyTorch caching allocator (all in Bytes). GPU compute utilization is not logged by default.
53
+
54
+ *General Memory Usage:*
55
+
56
+ - ``allocated_bytes.all.current``: Current allocated GPU memory
57
+ - ``allocated_bytes.all.peak``: Peak allocated GPU memory
58
+ - ``reserved_bytes.all.current``: Current reserved GPU memory (allocated + cached)
59
+ - ``reserved_bytes.all.peak``: Peak reserved GPU memory
60
+ - ``active_bytes.all.current``: Current GPU memory in active use
61
+ - ``active_bytes.all.peak``: Peak GPU memory in active use
62
+ - ``inactive_split_bytes.all.current``: Memory in inactive, splittable blocks
63
+
64
+ *Allocator Pool Statistics* (for ``small_pool`` and ``large_pool``):
65
+
66
+ - ``allocated_bytes.{pool_type}.current`` / ``.peak``
67
+ - ``reserved_bytes.{pool_type}.current`` / ``.peak``
68
+ - ``active_bytes.{pool_type}.current`` / ``.peak``
69
+
70
+ *Allocator Events:*
71
+
72
+ - ``num_ooms``: Cumulative out-of-memory errors
73
+ - ``num_alloc_retries``: Number of allocation retries
74
+ - ``num_device_alloc``: Number of device allocations
75
+ - ``num_device_free``: Number of device deallocations
76
+
77
+ For a full list of CUDA memory stats, see:
78
+ https://pytorch.org/docs/stable/generated/torch.cuda.memory_stats.html
79
+
80
+ **TPU (via `torch_xla`)**
81
+
82
+ *Memory Metrics* (per device, e.g. ``xla:0``):
83
+
84
+ - ``memory.free.xla:0``: Free HBM memory (MB)
85
+ - ``memory.used.xla:0``: Used HBM memory (MB)
86
+ - ``memory.percent.xla:0``: Percentage of HBM memory used (%)
87
+
88
+ *XLA Operation Counters:*
89
+
90
+ - ``CachedCompile.xla``
91
+ - ``CreateXlaTensor.xla``
92
+ - ``DeviceDataCacheMiss.xla``
93
+ - ``UncachedCompile.xla``
94
+ - ``xla::add.xla``, ``xla::addmm.xla``, etc.
95
+
96
+ These counters can be retrieved using:
97
+ ``torch_xla.debug.metrics.counter_names()``
52
98
53
99
Args:
54
100
cpu_stats: if ``None``, it will log CPU stats only if the accelerator is CPU.
0 commit comments