Skip to content

[SFT] Add seq_lengths to signature columns #3699

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

Merged
merged 3 commits into from
Jul 8, 2025

Conversation

LeonEricsson
Copy link
Collaborator

@LeonEricsson LeonEricsson commented Jul 6, 2025

What does this PR do?

To avoid cross-contamination when training with packed sequences, we pass position_ids to FA2. This was introduced in PR #3526, which calculated position_ids during packing and added them to the dataset. To ensure these are retained, position_ids must be set as a signature column; otherwise, the Trainer will discard them as unused.

https://github.com/huggingface/transformers/blob/ca7e1a3756c022bf31429c452b2f313f043f32de/src/transformers/trainer.py#L1007

While this was implemented correctly, PR #3649 introduced an optimized approach where position_ids are computed within the data collator using a new dataset column, seq_lengths. However, the latter PR failed to update the signature column from position_ids to seq_lengths. As a result, seq_lengths is not passed to the collator, causing it to behave as if the sequences are not packed.

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? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes?
  • 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.

@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.

@LeonEricsson LeonEricsson requested review from lewtun and kashif July 6, 2025 13:11
@LeonEricsson LeonEricsson added 🏋 SFT Related to SFT and removed 🏋 SFT Related to SFT labels Jul 7, 2025
@LeonEricsson
Copy link
Collaborator Author

LeonEricsson commented Jul 8, 2025

waiting to merge until we've confirmed this fixes the issues raised in #3705

Copy link
Member

@qgallouedec qgallouedec left a comment

Choose a reason for hiding this comment

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

Good!

@jiosephlee
Copy link

jiosephlee commented Jul 8, 2025

if args.use_liger_kernel:
    dataset = dataset.select_columns(
        {"input_ids", "position_ids", "completion_mask"}.intersection(dataset.column_names)
    )

This line of code may need to be adjusted as well (edit: nvm just saw the new commit)

@kashif
Copy link
Collaborator

kashif commented Jul 8, 2025

@jiosephlee fixed and tested and works thanks!

@kashif kashif merged commit 0462dd7 into huggingface:main Jul 8, 2025
9 of 10 checks passed
marcandrelarochelle pushed a commit to marcandrelarochelle/trl that referenced this pull request Jul 29, 2025
Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com>
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.

5 participants