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
Open
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
39 changes: 22 additions & 17 deletions inference/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,28 @@ def __init__(self, model_name, gpu_id, max_memory):

def do_inference(self, prompt, max_new_tokens, do_sample, temperature, top_k, stream_callback=None):
stop_criteria = StopWordsCriteria(self._tokenizer, [self.human_id], stream_callback)
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):]
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
Comment on lines +102 to +104
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?


# remove the context from the output
output = output[len(prompt):]
finally:
torch.cuda.empty_cache()
Comment on lines +88 to +109
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 ☝️


return output

Expand Down