-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Pipeline: use tokenizer pad token at generation time if the model pad token is unset. #29614
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f3ca17
fix warning
gante 0acda97
set pad_token_id in the generalist forward; remove test
gante 658f3d0
extra condition
gante 30c3b2e
remove unused arg; remove unwanted diff
gante 399da6d
move to init
gante cef3ae0
standardize interface
gante File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,9 +196,7 @@ def new_user_input(self): | |
build_pipeline_init_args(has_tokenizer=True), | ||
r""" | ||
min_length_for_response (`int`, *optional*, defaults to 32): | ||
The minimum length (in number of tokens) for a response. | ||
minimum_tokens (`int`, *optional*, defaults to 10): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Initially, I removed it from the signature of the private |
||
The minimum length of tokens to leave for a response.""", | ||
The minimum length (in number of tokens) for a response.""", | ||
) | ||
class ConversationalPipeline(Pipeline): | ||
""" | ||
|
@@ -241,17 +239,13 @@ def __init__(self, *args, **kwargs): | |
if self.tokenizer.pad_token_id is None: | ||
self.tokenizer.pad_token = self.tokenizer.eos_token | ||
|
||
def _sanitize_parameters( | ||
self, min_length_for_response=None, minimum_tokens=None, clean_up_tokenization_spaces=None, **generate_kwargs | ||
): | ||
def _sanitize_parameters(self, min_length_for_response=None, clean_up_tokenization_spaces=None, **generate_kwargs): | ||
preprocess_params = {} | ||
forward_params = {} | ||
postprocess_params = {} | ||
|
||
if min_length_for_response is not None: | ||
preprocess_params["min_length_for_response"] = min_length_for_response | ||
if minimum_tokens is not None: | ||
forward_params["minimum_tokens"] = minimum_tokens | ||
|
||
if "max_length" in generate_kwargs: | ||
forward_params["max_length"] = generate_kwargs["max_length"] | ||
|
@@ -304,7 +298,7 @@ def preprocess(self, conversation: Conversation, min_length_for_response=32) -> | |
input_ids = tf.constant([input_ids]) | ||
return {"input_ids": input_ids, "conversation": conversation} | ||
|
||
def _forward(self, model_inputs, minimum_tokens=10, **generate_kwargs): | ||
def _forward(self, model_inputs, **generate_kwargs): | ||
n = model_inputs["input_ids"].shape[1] | ||
conversation = model_inputs.pop("conversation") | ||
if "max_length" not in generate_kwargs and "max_new_tokens" not in generate_kwargs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note regarding this file's diff, also applicable to the diff in
src/transformers/pipelines/image_to_text.py
:The conventional strategy to pass kwargs to
generate
is through**forward_params
. Previously in this file, the generation kwargs were held asforward_params["generate_kwargs"]
, which prevented the use of the conventional strategy. There isn't really a reason to hold these kwargs separately,generate
is the only sink for kwargs in models that can generate. Models that can't generatewillshould throw an exception regardless of the container for kwargs. As such, this diff aims at minimizing the difference forgenerate
parameterization across pipelines :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of this - it's far cleaner to clearly outline what are generate kwargs and what are not. In the current pipelines the models might be the only sink, but that's not guaranteed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually - I realise what I've said about the forward kwargs is wrong here - we can just assume they're passed to the model. In this case, my preference is to still have "generate_kwargs" explicitly in the forward_kwargs, but I don't feel strongly and don't mind if you leave as-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can agree that regardless of the pattern we choose here, it should be applied to all pipelines with generative capabilities for consistency. Based on this premise, enforcing a separation of
generate_kwargs
this exact way will break backward compatibility, i.e. the following would not be possibleNevertheless, I am aligned with you -- we should separate them! We can do it through
generation_config.update(**kwargs)
, and perform the required validation with the aid ofgeneration_config.validate()
. One of the requirements to do so is to have a single big blob of keyword arguments to untangle, and thus these changes go in this direction.Let me know if you agree, in which case I'll merge the PR and prepare this follow-up. [My instinct was to merge this PR now, but I've held it back -- I've merged too many not-100%-approved PRs recently 😉 ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's merge atm so this is unblocked and then we can iterate on something different :)