Skip to content

Instantly share code, notes, and snippets.

@pfackeldey
Created February 24, 2021 20:34
Show Gist options
  • Save pfackeldey/e3c5edfc3ea81bd1b82bc6e1fd1ab3af to your computer and use it in GitHub Desktop.
Save pfackeldey/e3c5edfc3ea81bd1b82bc6e1fd1ab3af to your computer and use it in GitHub Desktop.
import tensorflow as tf
import nvidia_smi
class GPUStats(tf.keras.callbacks.Callback):
"""
Usage:
model.fit(..., callbacks=[GPUStats()])
requires nvidia_smi package:
conda: conda install -c fastai nvidia-ml-py3
pip : pip install nvidia-ml-py3
"""
def __init__(self, idx=0):
nvidia_smi.nvmlInit()
self.handle = nvidia_smi.nvmlDeviceGetHandleByIndex(idx)
def on_x_end(self, x, logs=None):
self.mem = nvidia_smi.nvmlDeviceGetMemoryInfo(self.handle)
self.res = nvidia_smi.nvmlDeviceGetUtilizationRates(self.handle)
logs["GPU-Usage [%]"] = self.res.gpu
logs["GPU-vRAM [%]"] = 100 * self.mem.used / self.mem.total
logs["GPU-vRAM [MiB]"] = self.mem.used / (1024**2)
on_batch_end = on_x_end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment