Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check_nvidia to support running multiple single GPU training / inference at the same time #856

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions unsloth/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,15 +933,25 @@ def patch_llama_rope_scaling(


def check_nvidia():
# Unsloth doesn't work yet on AMD devices - we're working on it!
cuda_visible_devices = os.environ.get('CUDA_VISIBLE_DEVICES')

output = np.array([0,])
try:
output = subprocess.check_output("nvidia-smi --query-gpu=memory.used --format=csv", shell = True)
output = re.findall(rb'([\d]{1,})[\s]{1,}M', output)
output = np.array([int(x.decode('utf-8'))/1024 for x in output])
except:
if cuda_visible_devices is not None:
gpu_ids = cuda_visible_devices.split(',')
query_gpus = ','.join(gpu_ids)
command = f"nvidia-smi --query-gpu=index,memory.used --format=csv -i {query_gpus}"
else:
command = "nvidia-smi --query-gpu=index,memory.used --format=csv"

output = subprocess.check_output(command, shell=True)
output = re.findall(rb'(\d+),\s*(\d+)\s*MiB', output)
output = np.array([int(x[1].decode('utf-8'))/1024 for x in output])
except subprocess.CalledProcessError:
if not torch.cuda.is_available():
raise RuntimeError("Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!")
raise RuntimeError("Unsloth: We do not support AMD / Intel machines yet - it is a work in progress!")
raise

return output
pass
PRE_CHECK = check_nvidia()
Expand Down
16 changes: 8 additions & 8 deletions unsloth/models/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,14 +1410,14 @@ def from_pretrained(

# Patch Trainer
from transformers.trainer import Trainer
try:
if Trainer._inner_training_loop.__name__ != "_fast_inner_training_loop":
inner_training_loop = inspect.getsource(Trainer._inner_training_loop)
Trainer._original_training_loop = inner_training_loop
else:
inner_training_loop = Trainer._original_training_loop
except:
raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')
# try:
if Trainer._inner_training_loop.__name__ != "_fast_inner_training_loop":
inner_training_loop = inspect.getsource(Trainer._inner_training_loop)
Trainer._original_training_loop = inner_training_loop
else:
inner_training_loop = Trainer._original_training_loop
# except:
# raise RuntimeError('Unsloth currently does not support multi GPU setups - but we are working on it!')
pass

if ((post_check - pre_check) >= 1).sum() > 1:
Expand Down