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

add truncation in text translator #472

Merged
merged 4 commits into from
May 3, 2024

Conversation

avidale
Copy link
Contributor

@avidale avidale commented Apr 24, 2024

What does this PR do? Please describe:
This PR allows bypassing the problem of TextTranslator failure when the number of source tokens exceeds the maximal length supported by the translation model. As tokenization happens within the TextTranslator, truncation, if implemented, should also happen there. The PR therefore adds a max_src_len argument to it, and enables truncation if this argument is not empty.

Does your PR introduce any breaking changes? If yes, please list them:
None

Check list:

  • Was the content of this PR discussed and approved via a GitHub issue? (no need for typos or documentation improvements)
  • Did you read the contributor guideline?
  • Did you make sure that your PR does only one thing instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (no need for typos, documentation, or minor internal changes)

@avidale avidale requested a review from cbalioglu as a code owner April 24, 2024 14:45
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 24, 2024
Copy link
Contributor

@cbalioglu cbalioglu left a comment

Choose a reason for hiding this comment

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

Just left three nit comments.

src/fairseq2/generation/text.py Outdated Show resolved Hide resolved
src/fairseq2/generation/text.py Outdated Show resolved Hide resolved
@@ -200,6 +205,9 @@ def __call__(self, source_text: str) -> Tuple[str, Seq2SeqGeneratorOutput]:
"""
source_seq = self._source_text_encoder(source_text)

if self.max_src_len and source_seq.shape[0] > self.max_src_len:
Copy link
Contributor

Choose a reason for hiding this comment

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

Although it would be a very edge case; if max_src_len is 0, then the first condition if self.max_src_len will be false, but maybe the user deliberately wants to trim to zero length (although does not make much sense, still expected since our parameter contract says that this parameter is ignored only if it is None). The "right" check would be if self.max_src_len is not None and ...

Copy link
Contributor Author

@avidale avidale Apr 26, 2024

Choose a reason for hiding this comment

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

If the source is trimmed to 0 elements, the translation is going to fail, because the encoder would consume an empty tensor. Therefore, I don't want to apply truncation to zero, and instead will change the parameter contract to explicitly truncate only to a positive number of tokens.

Signed-off-by: David Dale <[email protected]>
@cbalioglu cbalioglu merged commit 80b60c0 into facebookresearch:main May 3, 2024
17 checks passed
@avidale avidale deleted the truncate-in-translator branch May 6, 2024 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants