Skip to content

Commit

Permalink
flake8 fixes (#3064)
Browse files Browse the repository at this point in the history
* flake8 fixes

* fix pep8

* fix pep8

Co-authored-by: William Falcon <waf2107@columbia.edu>
  • Loading branch information
yukw777 and williamFalcon authored Aug 20, 2020
1 parent 9a60564 commit cee5eaf
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 227 deletions.
39 changes: 25 additions & 14 deletions pytorch_lightning/callbacks/gpu_usage_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ class GpuUsageLogger(Callback):
"""

def __init__(self, memory_utilisation: bool = True, gpu_utilisation: bool = True,
intra_step_time: bool = False, inter_step_time: bool = False,
fan_speed: bool = False, temperature: bool = False):
def __init__(
self,
memory_utilisation: bool = True,
gpu_utilisation: bool = True,
intra_step_time: bool = False,
inter_step_time: bool = False,
fan_speed: bool = False,
temperature: bool = False,
):
super(GpuUsageLogger).__init__()
self.memory_utilisation = memory_utilisation
self.gpu_utilisation = gpu_utilisation
Expand All @@ -102,9 +108,10 @@ def on_batch_start(self, trainer, pl_module):
if self.inter_step_time:
# First log at beginning of second step
if self.snap_inter_step_time:
trainer.logger.log_metrics({'Batch_Time/inter_step (ms)':
(time.time() - self.snap_inter_step_time) * 1000},
step=trainer.global_step)
trainer.logger.log_metrics(
{'Batch_Time/inter_step (ms)': (time.time() - self.snap_inter_step_time) * 1000},
step=trainer.global_step,
)
if self.intra_step_time:
self.snap_intra_step_time = time.time()

Expand All @@ -125,20 +132,24 @@ def on_batch_end(self, trainer, pl_module):

if self.intra_step_time:
if self.snap_intra_step_time:
trainer.logger.log_metrics({'Batch_Time/intra_step (ms)':
(time.time() - self.snap_intra_step_time) * 1000},
step=trainer.global_step)
trainer.logger.log_metrics(
{'Batch_Time/intra_step (ms)': (time.time() - self.snap_intra_step_time) * 1000},
step=trainer.global_step,
)

def on_train_epoch_start(self, trainer, pl_module):
self.snap_intra_step_time = None
self.snap_inter_step_time = None

@staticmethod
def _get_gpu_stat(pitem: str, unit: str):
result = subprocess.run(["/bin/nvidia-smi", f"--query-gpu={pitem}", "--format=csv,nounits,noheader"],
encoding="utf-8", stdout=subprocess.PIPE,
stderr=subprocess.PIPE, # for backward compatibility with python version 3.6
check=True)
result = subprocess.run(
["/bin/nvidia-smi", f"--query-gpu={pitem}", "--format=csv,nounits,noheader"],
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, # for backward compatibility with python version 3.6
check=True,
)
try:
gpu_usage = [float(x) for x in result.stdout.strip().split(os.linesep)]
except ValueError:
Expand All @@ -152,4 +163,4 @@ def _log_gpu(self, trainer):
def _log_memory(self, trainer):
trainer.logger.log_metrics(self._get_gpu_stat("memory.used", "MB"), step=trainer.global_step)
trainer.logger.log_metrics(self._get_gpu_stat("memory.free", "MB"), step=trainer.global_step)
trainer.logger.log_metrics(self._get_gpu_stat("utilization.memory", "%"), step=trainer.global_step)
trainer.logger.log_metrics(self._get_gpu_stat("utilization.memory", "%"), step=trainer.global_step)
2 changes: 1 addition & 1 deletion pytorch_lightning/core/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _forward_example_input(self) -> None:
input_ = model.transfer_batch_to_device(input_, model.device)

if trainer is not None and trainer.amp_backend == AMPType.NATIVE and not trainer.use_tpu:
model.forward = torch.cuda.amp.autocast()(model.forward)
model.forward = torch.cuda.amp.autocast()(model.forward)

mode = model.training
model.eval()
Expand Down
Loading

0 comments on commit cee5eaf

Please sign in to comment.