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

Implement streamer for text-generation and add context arg to Pipeline.engine_forward #1140

Merged
merged 4 commits into from
Aug 2, 2023

Conversation

mgoin
Copy link
Member

@mgoin mgoin commented Jul 23, 2023

Allows usage of the Transformers TextStreamer and TextIteratorStreamer classes with DeepSparse

from deepsparse import Pipeline
from transformers import TextStreamer

pipe = Pipeline.create(
    task="codegen",
    model_path="zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base-none",
    max_generated_tokens=64,
    prompt_processing_sequence_length=1,
    use_deepsparse_cache=False,
)

streamer = TextStreamer(pipe.tokenizer)

# While also returning the usual output, the streamer will also print the generated text to stdout
_ = pipe(sequences="def fib():", streamer=streamer)
Screen.Recording.2023-07-23.at.4.52.34.PM.mov

===

This also extends beyond printing to stdout by using TextIteratorStreamer

from deepsparse import Pipeline
from transformers import TextIteratorStreamer
from threading import Thread

pipe = Pipeline.create(
    task="codegen",
    model_path="zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base-none",
    max_generated_tokens=64,
    prompt_processing_sequence_length=1,
    use_deepsparse_cache=False,
)

streamer = TextIteratorStreamer(pipe.tokenizer)

generation_kwargs = dict(sequences="def fib():", streamer=streamer)
thread = Thread(target=pipe, kwargs=generation_kwargs)
thread.start()
generated_text = ""
for new_text in streamer:
    generated_text += new_text
print(generated_text)

"""
def fib():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

def fib2(n):
    a, b = 0, 1
    while a < n:
        yield a
        a, b
"""

dbogunowicz
dbogunowicz previously approved these changes Jul 24, 2023
bfineran
bfineran previously approved these changes Aug 2, 2023
Copy link
Member

@bfineran bfineran left a comment

Choose a reason for hiding this comment

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

LGTM let's also update the PR title to include that context is added to engine forward - in terms of what's added there I think it looks good as a first step, we might want to think through mechanism/rules for updating the context dictionary in engine forward, if we want to allow that at all

src/deepsparse/pipeline.py Outdated Show resolved Hide resolved
@mgoin mgoin changed the title Implement streamer for text-generation Implement streamer for text-generation and add context arg to Pipeline.engine_forward Aug 2, 2023
@mgoin mgoin merged commit b7726ed into main Aug 2, 2023
7 checks passed
@mgoin mgoin deleted the text-gen-streamer branch August 2, 2023 18:40
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.

None yet

3 participants