Skip to content

Commit

Permalink
skip the gpu memory checks if the device is set to 'auto'
Browse files Browse the repository at this point in the history
  • Loading branch information
winglian committed Sep 19, 2023
1 parent faecff9 commit 972bd42
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/axolotl/utils/bench.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
"""Benchmarking and measurement utilities"""
import functools

import pynvml
import torch
from pynvml.nvml import NVMLError


def check_cuda_device(default_value):
"""
wraps a function and returns the default value instead of running the
wrapped function if cuda isn't available or the device is auto
:param default_value:
:return:
"""

def actual_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
device = kwargs.get("device", args[0] if args else None)

if not torch.cuda.is_available() or device == "auto":
return default_value

return func(*args, **kwargs)

return wrapper

return actual_decorator


@check_cuda_device(0.0)
def gpu_memory_usage(device=0):
return torch.cuda.memory_allocated(device) / 1024.0**3


@check_cuda_device((0.0, 0.0, 0.0))
def gpu_memory_usage_all(device=0):
usage = torch.cuda.memory_allocated(device) / 1024.0**3
reserved = torch.cuda.memory_reserved(device) / 1024.0**3
smi = gpu_memory_usage_smi(device)
return usage, reserved - usage, max(0, smi - reserved)


@check_cuda_device(0.0)
def gpu_memory_usage_smi(device=0):
if isinstance(device, torch.device):
device = device.index
Expand Down

0 comments on commit 972bd42

Please sign in to comment.