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

stopping criteria for TextGenerationPipeline #26280

Closed
geronimi73 opened this issue Sep 20, 2023 · 6 comments
Closed

stopping criteria for TextGenerationPipeline #26280

geronimi73 opened this issue Sep 20, 2023 · 6 comments

Comments

@geronimi73
Copy link

Feature request

pass stopping criteria or string to TextGenerationPipeline

Motivation

it does not exist, have not found any way to do it at least, but would be very useful

Your contribution

none

@ArthurZucker
Copy link
Collaborator

Hey! You can pass generation_kwargs to the pipeline, which are usually used for stopping criteria such as max_length, max_new_tokens, max_time . You can also pass some stopping_criteria argument to the generate function using `generation_kwargs = {stopping_criteria = StoppingCriteriaList: [MaxTimeCriteria(32)] }.

from transformers import pipeline, StoppingCriteriaList, MaxTimeCriteria

# Initialize the text generation pipeline
generator = pipeline("text-generation")

# Define the stopping criteria using MaxTimeCriteria
stopping_criteria = StoppingCriteriaList([MaxTimeCriteria(32)])

# Define the generation_kwargs with stopping criteria
generation_kwargs = {
    "max_length": 100,  # Maximum length of the generated text
    "max_new_tokens": 10,  # Maximum number of new tokens to generate
    "generation_kwargs": {"stopping_criteria": stopping_criteria}  # Add stopping criteria to generation_kwargs
}

# Pass the generation_kwargs to the pipeline
generated_text = generator(
    "Hey!  How are you able.",
    **generation_kwargs
)

# Print the generated text
print(generated_text[0]["generated_text"])
>>> Hey!  How are you able.  Do you have a job or do you have

@ArthurZucker
Copy link
Collaborator

This should probably be added to the documentation!

@LysandreJik
Copy link
Member

cc @MKhalusova who is currently working on the generate docs!

@MKhalusova
Copy link
Contributor

MKhalusova commented Sep 28, 2023

The max_length and max_new_tokens are mentioned in nearly every doc on text generation:

However, I see a couple of issues:

  • the MaxTimeCriteria is much less discoverable, as it is only mentioned in the Utilities for Generation.
  • A more critical issue from my point of view is the lack of connection between the pipeline documentation and the text generation docs. The TextGenerationPipeline API doc lists parameters that control how pipeline is instantiated, but doesn't link to text generation docs.

I can address these.

@Tanman2001
Copy link

Is this a duplicate of #17562?

If so, it seems the only reason that older issue is still open is because of the missing documentation. If the documentation is fixed then both can be closed.

@peng-yiwen
Copy link

According to my experiments, setting

generation_kwargs = {
    "max_new_tokens": 1000,  # Maximum number of new tokens to generate
    "generation_kwargs": {"stopping_criteria": stopping_criteria}  # Add stopping criteria to generation_kwargs
}

is not feasible for passing a stopping criteria into a pipeline. Instead, if we let "stopping_criteria" out of "generation_kwargs", it then works well. As below:

generation_kwargs = {
    "max_new_tokens": 1000,  # Maximum number of new tokens to generate
    "stopping_criteria": stopping_criteria,
}

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

No branches or pull requests

6 participants