Skip to content

Generate: TextIteratorStreamer (streamer for gradio)#22501

Merged
gante merged 4 commits into
huggingface:mainfrom
gante:streamer_iterator
Apr 3, 2023
Merged

Generate: TextIteratorStreamer (streamer for gradio)#22501
gante merged 4 commits into
huggingface:mainfrom
gante:streamer_iterator

Conversation

@gante

@gante gante commented Mar 31, 2023

Copy link
Copy Markdown
Contributor

What does this PR do?

Following the previous streamer PR (#22449), this PR adds a streamer that can be used as an iterator. If we want to use the iterator while generate is running, they must be on separate threads (naturally).

The interface looks quite simple, as can be seen in the documentation 🤗 The only kink is the need to call generation on a separate thread, but there is no great way around it (at most we can design an if branch inside generate where, if a streamer is used, generate is called in a separate thread... but that seems overkill for now).

A Gradio demo running on this branch can be seen here. There is pretty much no slowdown compared to a non-streamer call.

Inspired by @oobabooga's work (see this comment).

@gante gante requested a review from sgugger March 31, 2023 18:56

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: the logic in put() and end() is a near copy-paste from TextStreamer -- instead of printing, puts things in the queue.

Most of the logic here is to work around model-specific tokenization quirks.

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Mar 31, 2023

Copy link
Copy Markdown

The documentation is not available anymore as the PR was closed or merged.

@sgugger sgugger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Still not a fan of this API that requires a new thread to work, but I guess no one has to use it since it's a separate object. So only those who really like thread will end up using it ;-)

@gante gante changed the title Generate: IteratorStreamer (streamer for gradio) Generate: TextIteratorStreamer (streamer for gradio) Apr 3, 2023
@gante gante force-pushed the streamer_iterator branch from 1fd5766 to 150f813 Compare April 3, 2023 13:48
@gante gante merged commit a55a822 into huggingface:main Apr 3, 2023
@gante gante deleted the streamer_iterator branch April 3, 2023 14:04
raghavanone pushed a commit to raghavanone/transformers that referenced this pull request Apr 5, 2023
novice03 pushed a commit to novice03/transformers that referenced this pull request Jun 23, 2023
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.

3 participants