-
Notifications
You must be signed in to change notification settings - Fork 32k
Enable TruncationStrategy override for pipelines #9432
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
Changes from all commits
1e7489b
3836835
64cf667
ce40c3f
9c6676c
1ec452c
e4dc04a
2e81658
b5585de
0975eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| from typing import List, Optional, Union | ||
|
|
||
| from ..file_utils import add_end_docstrings, is_tf_available, is_torch_available | ||
| from ..tokenization_utils import TruncationStrategy | ||
| from ..utils import logging | ||
| from .base import PIPELINE_INIT_ARGS, Pipeline | ||
|
|
||
|
|
@@ -317,12 +318,14 @@ def __call__( | |
| else: | ||
| return output | ||
|
|
||
| def _parse_and_tokenize(self, inputs, **kwargs): | ||
| def _parse_and_tokenize( | ||
| self, inputs, add_special_tokens=False, padding=False, truncation=TruncationStrategy.DO_NOT_TRUNCATE, **kwargs | ||
|
||
| ): | ||
| """ | ||
| Parse arguments and tokenize, adding an EOS token at the end of the user input | ||
| """ | ||
| # Parse arguments | ||
| inputs = self.tokenizer(inputs, add_special_tokens=False, padding=False).get("input_ids", []) | ||
| inputs = self.tokenizer(inputs, add_special_tokens=add_special_tokens, padding=padding).get("input_ids", []) | ||
| for input in inputs: | ||
| input.append(self.tokenizer.eos_token_id) | ||
| return inputs | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from ..file_utils import add_end_docstrings, is_tf_available, is_torch_available | ||
| from ..tokenization_utils import TruncationStrategy | ||
| from ..utils import logging | ||
| from .base import PIPELINE_INIT_ARGS, Pipeline | ||
|
|
||
|
|
@@ -50,7 +51,13 @@ def check_inputs(self, input_length: int, min_length: int, max_length: int): | |
| return True | ||
|
|
||
| def __call__( | ||
| self, *args, return_tensors=False, return_text=True, clean_up_tokenization_spaces=False, **generate_kwargs | ||
| self, | ||
| *args, | ||
| return_tensors=False, | ||
| return_text=True, | ||
| clean_up_tokenization_spaces=False, | ||
| truncation=TruncationStrategy.DO_NOT_TRUNCATE, | ||
| **generate_kwargs | ||
| ): | ||
| r""" | ||
| Generate the output text(s) using text(s) given as inputs. | ||
|
|
@@ -64,6 +71,10 @@ def __call__( | |
| Whether or not to include the decoded texts in the outputs. | ||
| clean_up_tokenization_spaces (:obj:`bool`, `optional`, defaults to :obj:`False`): | ||
| Whether or not to clean up the potential extra spaces in the text output. | ||
| truncation (:obj:`TruncationStrategy`, `optional`, defaults to :obj:`TruncationStrategy.DO_NOT_TRUNCATE`): | ||
| The truncation strategy for the tokenization within the pipeline. | ||
| :obj:`TruncationStrategy.DO_NOT_TRUNCATE` (default) will never truncate, but it is sometimes desirable | ||
| to truncate the input to fit the model's max_length instead of throwing an error down the line. | ||
| generate_kwargs: | ||
| Additional keyword arguments to pass along to the generate method of the model (see the generate method | ||
| corresponding to your framework `here <./model.html#generative-models>`__). | ||
|
|
@@ -96,7 +107,7 @@ def __call__( | |
| ) | ||
|
|
||
| with self.device_placement(): | ||
| inputs = self._parse_and_tokenize(*args, padding=padding, **generate_kwargs) | ||
| inputs = self._parse_and_tokenize(*args, padding=padding, truncation=truncation) | ||
|
Contributor
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. Awesome |
||
|
|
||
| if self.framework == "pt": | ||
| inputs = self.ensure_tensor_on_device(**inputs) | ||
|
|
@@ -108,9 +119,6 @@ def __call__( | |
| max_length = generate_kwargs.get("max_length", self.model.config.max_length) | ||
| self.check_inputs(input_length, min_length, max_length) | ||
|
|
||
| # truncation should be used by _parse_and_tokenize | ||
| generate_kwargs.pop("truncation", None) | ||
|
|
||
| generations = self.model.generate( | ||
| inputs["input_ids"], | ||
| attention_mask=inputs["attention_mask"], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,25 +50,15 @@ def __init__(self, *args, **kwargs): | |
| self.check_model_type(self.ALLOWED_MODELS) | ||
|
|
||
| # overriding _parse_and_tokenize to allow for unusual language-modeling tokenizer arguments | ||
|
|
||
| def _parse_and_tokenize(self, inputs, padding=True, add_special_tokens=True, **kwargs): | ||
| def _parse_and_tokenize(self, *args, **kwargs): | ||
| """ | ||
| Parse arguments and tokenize | ||
| """ | ||
| # Parse arguments | ||
| if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: | ||
| tokenizer_kwargs = {"add_space_before_punct_symbol": True} | ||
| else: | ||
| tokenizer_kwargs = {} | ||
| inputs = self.tokenizer( | ||
| inputs, | ||
| add_special_tokens=add_special_tokens, | ||
| return_tensors=self.framework, | ||
| padding=padding, | ||
| **tokenizer_kwargs, | ||
| ) | ||
|
|
||
| return inputs | ||
| kwargs.update({"add_space_before_punct_symbol": True}) | ||
|
||
|
|
||
| return super()._parse_and_tokenize(*args, **kwargs) | ||
|
|
||
| def __call__( | ||
| self, | ||
|
|
||
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.
Is "kwargs" just used to catch unused params here?
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.
yes