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

prevent prompt tensors from accumulating in GPU #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MichaMucha
Copy link

this could help with CUDA OOM errors especially on consumer grade hardware.

prompt and output tensors will be erased from VRAM

Comment on lines +102 to +104
del inputs
output = self._tokenizer.batch_decode(outputs)[0]
del outputs
Copy link
Contributor

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?

Copy link
Author

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

Copy link
Member

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?

Copy link
Contributor

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?

Comment on lines +88 to +109
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaMucha ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants