Skip to content

stream results in generate.py #571

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

Merged
merged 1 commit into from
Apr 30, 2024
Merged
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
26 changes: 24 additions & 2 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,31 @@ def callback(
# print(, end='', flush=True)

else:
assert not generator_args.chat_mode
buffer = [generator_args.prompt]
period_id = tokenizer.encode(".")[0]
done_generating = False

def callback(x):
return x
def callback(
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have to copy-paste the callback above instead of sharing it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Valid point.

x, buffer=buffer, period_id=period_id, done_generating=done_generating
):
if done_generating:
return
buffer.append(
tokenizer.decode([period_id] + x.tolist())[1:]
) # I think this results in the first output token being dropped from the display which is wrong.
if x.item() == tokenizer.eos_id():
done_generating = True
if (
is_llama3_model
and x.item() == tokenizer.special_tokens["<|eot_id|>"]
):
done_generating = True
buffer = buffer[:-1] # drop the eot_id from the output buffer
if len(buffer) == 4 or done_generating:
print("".join(buffer), end="", flush=True)
buffer.clear()
# print(, end='', flush=True)

t0 = time.perf_counter()
import contextlib
Expand Down