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

Fix seq2seq collator padding #30556

Merged
merged 4 commits into from Apr 30, 2024

Conversation

vasqu
Copy link
Contributor

@vasqu vasqu commented Apr 29, 2024

What does this PR do?

DataCollatorForSeq2Seq currently only supports the longest and silently the no_padding strategies. This PR adds some checks to ensure the padding strategy is respected if valid (e.g. max_length without a given max length defaults back to longest).

Furthermore, added tests for the collator in the style of the DataCollatorForTokenClassification for all kinds of supported datatypes (pt, tf, np).

Something to note: The seq2seq collator causes side effects on the features' labels that are passed. I don't think it's a big deal but thought I'd clarify why I produce features twice within a single test.

Fixes #30521

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@Rocketknight1 @amyeroberts

further added tests for the seq2seq data collator in the style of the `data_collator_for_token_classification` (pt, tf, np)
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Code changes LGTM, and I appreciate the extensive testing! cc @amyeroberts for core maintainer review.

The point you made about side-effects on the input is interesting, though - I agree that it's unlikely to be critical (because people rarely reuse the pre-collator inputs after calling the collator), but it's still suboptimal. Maybe we could consider having our collators make copies of the inputs instead so they don't overwrite them, though this would definitely be a task for a separate PR.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for fixing and for all of these tests - beautiful!

@amyeroberts amyeroberts merged commit 9112520 into huggingface:main Apr 30, 2024
21 checks passed
@amyeroberts
Copy link
Collaborator

The point you made about side-effects on the input is interesting, though - I agree that it's unlikely to be critical (because people rarely reuse the pre-collator inputs after calling the collator), but it's still suboptimal. Maybe we could consider having our collators make copies of the inputs instead so they don't overwrite them, though this would definitely be a task for a separate PR.

@Rocketknight1 @vasqu Very much aligned on this - side-effects should definitely be avoided, and it's a bigger piece of work for a follow-up PR.

@vasqu vasqu deleted the fix-seq2seq-collator-padding branch May 1, 2024 15:52
@vasqu
Copy link
Contributor Author

vasqu commented May 1, 2024

@amyeroberts @Rocketknight1 I'm not sure which collators affect which inputs via side-effects so I'd suggest writing a mutability test which assures this wanted behaviour. Afaik, it currently only affects the labels when using the seq2seq collator but maybe I'll catch some others. Got some time now, will be looking into it in a bit.

@amyeroberts
Copy link
Collaborator

@vasqu Awesome ❤️ looking forward to seeing the PR!

@vasqu vasqu mentioned this pull request May 1, 2024
5 tasks
itazap pushed a commit that referenced this pull request May 14, 2024
* fix seq2seq data collator to respect the given padding strategy

further added tests for the seq2seq data collator in the style of the `data_collator_for_token_classification` (pt, tf, np)

* formatting and change bool equals "==" to "is"

* add missed return types in tests

* update numpy test as it can handle unequal shapes, not like pt or tf
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.

[BUG] DataCollatorForSeq2Seq with PaddingStrategy.MAX_LENGTH may not pad labels
4 participants