-
Notifications
You must be signed in to change notification settings - Fork 1k
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
prevent prompt tensors from accumulating in GPU #84
base: main
Are you sure you want to change the base?
Conversation
del inputs | ||
output = self._tokenizer.batch_decode(outputs)[0] | ||
del outputs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @MichaMucha. Should del inputs
and del outputs
be in the finally block instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay-
I suppose in case outputs failed to generate for some reason, they wouldn't be set, therefore del outputs
in the finally block would throw an exception..
Not sure if a similar case can be made for inputs, but I wanted to avoid the risk of an unhandled exceptio
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if a similar case can be made for inputs, but I wanted to avoid the risk of an unhandled exceptio
Makes sense, 'outputs' isn't defined if it errors out before then. Maybe just move inputs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check if inputs
and outputs
are set before del inputs
and del outputs
in the finally block?
try: | ||
inputs = ( | ||
self._tokenizer(prompt, return_tensors='pt') | ||
.to(self._model.device) | ||
) | ||
outputs = self._model.generate( | ||
**inputs, | ||
max_new_tokens=max_new_tokens, | ||
do_sample=do_sample, | ||
temperature=temperature, | ||
top_k=top_k, | ||
pad_token_id=self._tokenizer.eos_token_id, | ||
stopping_criteria=StoppingCriteriaList([stop_criteria]), | ||
) | ||
del inputs | ||
output = self._tokenizer.batch_decode(outputs)[0] | ||
del outputs | ||
|
||
# remove the context from the output | ||
output = output[len(prompt):] | ||
finally: | ||
torch.cuda.empty_cache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try: | |
inputs = ( | |
self._tokenizer(prompt, return_tensors='pt') | |
.to(self._model.device) | |
) | |
outputs = self._model.generate( | |
**inputs, | |
max_new_tokens=max_new_tokens, | |
do_sample=do_sample, | |
temperature=temperature, | |
top_k=top_k, | |
pad_token_id=self._tokenizer.eos_token_id, | |
stopping_criteria=StoppingCriteriaList([stop_criteria]), | |
) | |
del inputs | |
output = self._tokenizer.batch_decode(outputs)[0] | |
del outputs | |
# remove the context from the output | |
output = output[len(prompt):] | |
finally: | |
torch.cuda.empty_cache() | |
inputs = None | |
outputs = None | |
try: | |
inputs = ( | |
self._tokenizer(prompt, return_tensors='pt') | |
.to(self._model.device) | |
) | |
outputs = self._model.generate( | |
**inputs, | |
max_new_tokens=max_new_tokens, | |
do_sample=do_sample, | |
temperature=temperature, | |
top_k=top_k, | |
pad_token_id=self._tokenizer.eos_token_id, | |
stopping_criteria=StoppingCriteriaList([stop_criteria]), | |
) | |
output = self._tokenizer.batch_decode(outputs)[0] | |
# remove the context from the output | |
output = output[len(prompt):] | |
finally: | |
if inputs is not None: | |
del inputs | |
if outputs is not None: | |
del outputs | |
torch.cuda.empty_cache() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaMucha ☝️
this could help with CUDA OOM errors especially on consumer grade hardware.
prompt and output tensors will be erased from VRAM