-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
191 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,68 @@ | ||
from typing import Any | ||
|
||
from lightning.pytorch import Callback, Trainer, LightningModule | ||
from lightning.pytorch.utilities.types import STEP_OUTPUT | ||
|
||
|
||
class GpuStatsLogger(Callback): | ||
"""Log GPU stats for each training epoch""" | ||
|
||
def __init__(self, delay: float = 0.5): | ||
def __init__(self, delay: float = 0.5, on_epoch: bool = True, on_step: bool = False): | ||
try: | ||
from gpumonitor.monitor import GPUStatMonitor | ||
except ImportError: | ||
raise ImportError("gpumonitor is required to use GPUStatsLogger") | ||
|
||
self.monitor = GPUStatMonitor(delay=delay) | ||
self.monitor_epoch = GPUStatMonitor(delay=delay) if on_epoch else None | ||
self.monitor_step = GPUStatMonitor(delay=delay) if on_step else None | ||
self.on_epoch = on_epoch | ||
self.on_step = on_step | ||
assert self.on_epoch or self.on_step, "on_epoch and on_step cannot be both False" | ||
|
||
def on_train_epoch_start(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
self.monitor.reset() | ||
if not self.on_epoch: | ||
return | ||
self.monitor_epoch.reset() | ||
|
||
def on_train_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
for gpu in self.monitor.average_stats.gpus: | ||
if not self.on_epoch: | ||
return | ||
for gpu in self.monitor_epoch.average_stats.gpus: | ||
logged_info = { | ||
f"gpu{gpu.index}_memory_used_epoch": gpu.memory_used / 1024, | ||
f"gpu{gpu.index}_memory_total_epoch": gpu.memory_total / 1024, | ||
f"gpu{gpu.index}_memory_util_epoch": gpu.memory_used / gpu.memory_total, | ||
f"gpu{gpu.index}_temperature_epoch": float(gpu.temperature), | ||
f"gpu{gpu.index}_utilization_epoch": gpu.utilization / 100, | ||
f"gpu{gpu.index}_power_draw_epoch": float(gpu.power_draw), | ||
f"gpu{gpu.index}_power_percentage_epoch": gpu.power_draw / gpu.power_limit, | ||
f"gpu{gpu.index}_fan_speed_epoch": float(gpu.fan_speed) if gpu.fan_speed is not None else 0., | ||
} | ||
pl_module.logger.log_metrics(logged_info, step=trainer.global_step) | ||
pl_module.log_dict(logged_info, logger=False, sync_dist=pl_module.distributed) | ||
|
||
def on_train_batch_start( | ||
self, trainer: Trainer, pl_module: LightningModule, batch: Any, batch_idx: int | ||
) -> None: | ||
if not self.on_step: | ||
return | ||
self.monitor_step.reset() | ||
|
||
def on_train_batch_end( | ||
self, trainer: Trainer, pl_module: LightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int | ||
) -> None: | ||
if not self.on_step: | ||
return | ||
for gpu in self.monitor_step.average_stats.gpus: | ||
logged_info = { | ||
f"gpu{gpu.index}_memory_used": gpu.memory_used / 1024, | ||
f"gpu{gpu.index}_memory_total": gpu.memory_total / 1024, | ||
f"gpu{gpu.index}_memory_util": gpu.memory_used / gpu.memory_total, | ||
f"gpu{gpu.index}_temperature": float(gpu.temperature), | ||
f"gpu{gpu.index}_utilization": gpu.utilization / 100, | ||
f"gpu{gpu.index}_power_draw": float(gpu.power_draw), | ||
f"gpu{gpu.index}_power_percentage": gpu.power_draw / gpu.power_limit, | ||
f"gpu{gpu.index}_fan_speed": float(gpu.fan_speed) if gpu.fan_speed is not None else 0., | ||
f"gpu{gpu.index}_memory_used_step": gpu.memory_used / 1024, | ||
f"gpu{gpu.index}_memory_total_step": gpu.memory_total / 1024, | ||
f"gpu{gpu.index}_memory_util_step": gpu.memory_used / gpu.memory_total, | ||
f"gpu{gpu.index}_temperature_step": float(gpu.temperature), | ||
f"gpu{gpu.index}_utilization_step": gpu.utilization / 100, | ||
f"gpu{gpu.index}_power_draw_step": float(gpu.power_draw), | ||
f"gpu{gpu.index}_power_percentage_step": gpu.power_draw / gpu.power_limit, | ||
f"gpu{gpu.index}_fan_speed_step": float(gpu.fan_speed) if gpu.fan_speed is not None else 0., | ||
} | ||
pl_module.logger.log_metrics(logged_info, step=trainer.global_step) | ||
pl_module.log_dict(logged_info, logger=False, sync_dist=pl_module.distributed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,44 @@ | ||
from typing import Any | ||
|
||
from lightning.pytorch import Callback, Trainer, LightningModule | ||
from lightning.pytorch.utilities.types import STEP_OUTPUT | ||
|
||
|
||
class SystemStatsLogger(Callback): | ||
"""Log system stats for each training epoch""" | ||
|
||
def __init__(self): | ||
def __init__(self, on_epoch: bool = True, on_step: bool = False): | ||
try: | ||
import psutil | ||
except ImportError: | ||
raise ImportError("psutil is required to use SystemStatsLogger") | ||
self.psutil = psutil | ||
self.on_epoch = on_epoch | ||
self.on_step = on_step | ||
assert self.on_epoch or self.on_step, "on_epoch and on_step cannot be both False" | ||
|
||
def on_train_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
if not self.on_epoch: | ||
return | ||
cpu_usage = self.psutil.cpu_percent() | ||
memory_usage = self.psutil.virtual_memory().percent | ||
logged_info = { | ||
"cpu_usage_epoch": cpu_usage, | ||
"memory_usage_epoch": memory_usage | ||
} | ||
pl_module.logger.log_metrics(logged_info, step=trainer.global_step) | ||
pl_module.log_dict(logged_info, logger=False, sync_dist=pl_module.distributed) | ||
|
||
def on_train_batch_end( | ||
self, trainer: Trainer, pl_module: LightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int | ||
) -> None: | ||
if not self.on_step: | ||
return | ||
cpu_usage = self.psutil.cpu_percent() | ||
memory_usage = self.psutil.virtual_memory().percent | ||
logged_info = { | ||
"cpu_usage": cpu_usage, | ||
"memory_usage": memory_usage | ||
"cpu_usage_step": cpu_usage, | ||
"memory_usage_step": memory_usage | ||
} | ||
pl_module.logger.log_metrics(logged_info, step=trainer.global_step) | ||
pl_module.log_dict(logged_info, logger=False, sync_dist=pl_module.distributed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from enum import Enum | ||
from typing import Callable, Union, List, Tuple, TypeVar | ||
|
||
import numpy as np | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.