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

[Feature] Add callback for Text Generation Pipelines #1204

Merged
merged 6 commits into from
Aug 25, 2023
Merged

Conversation

rahul-tuli
Copy link
Member

@rahul-tuli rahul-tuli commented Aug 25, 2023

This PR adds a callback attribute to TextGenerationInput

callback is a callable that will be invoked on each generated token, and returns a boolean value.

If callback returns false, further generation of tokens is stopped

Test Code:

from deepsparse import Pipeline

start = 0
def dummy_callback(token):
    global start
    start += 1
    return start < 3


if __name__ == "__main__":
    codegen_pipeline = Pipeline.create(
        task="text_generation",
        model_path = "zoo:nlg/text_generation/codegen_mono-350m/pytorch/huggingface/bigpython_bigquery_thepile/base-none"
    )
    
    inputs = {
        "sequences": ["def fib(a, b, accumulator=0)"]*3,
        "callback": dummy_callback,
        "return_logits": True,
    }
    
    outputs = codegen_pipeline(**inputs)
    assert ( shape:=outputs.logits.shape[1]) == 3, f"Expected 3 tokens, got {shape}"

Also added above logic in an automated test (This is not yet active on GHA, but tested locally)


@rahul-tuli rahul-tuli requested review from bfineran, dbogunowicz, mgoin, Satrat and dsikka and removed request for dbogunowicz August 25, 2023 13:59
dbogunowicz
dbogunowicz previously approved these changes Aug 25, 2023
Copy link
Contributor

@dbogunowicz dbogunowicz left a comment

Choose a reason for hiding this comment

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

LGTM, but still not sure how useful this function is. Was this a request from product/QA?

@rahul-tuli rahul-tuli marked this pull request as ready for review August 25, 2023 18:05
Satrat
Satrat previously approved these changes Aug 25, 2023
@rahul-tuli rahul-tuli merged commit 6a70b96 into main Aug 25, 2023
7 checks passed
@rahul-tuli rahul-tuli deleted the add-callback branch August 25, 2023 19:57
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

4 participants