Skip to content

Mask loss at document boundaries - #4075

Open
francesco-bertolotti wants to merge 1 commit into
pytorch:mainfrom
francesco-bertolotti:f14-mask-next-doc-loss
Open

Mask loss at document boundaries#4075
francesco-bertolotti wants to merge 1 commit into
pytorch:mainfrom
francesco-bertolotti:f14-mask-next-doc-loss

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Contributor

This is probably not a significant issue, but it seems like the more correct way to handle document packing.

Currently, when training with document packing, the last token of one document is used to predict the first token of the next document.

Because the tokenizer always appends an EOS token and prepends a BOS token, the model is effectively trained to predict a BOS token after every EOS token. Given this behavior, the current implementation does not introduce an issue. However, without these special tokens, the last token of one document would be trained to predict the first token of an unrelated document, which is clearly undesirable.

Although this is not a problem in the current setup, I believe the correct approach is to ignore the loss for the last token of each document. This avoids learning cross-document transitions and aligns the training objective with document boundaries. Additionally, implementing this requires only a one-line change.


This PR probably breaks some tests. Before fixing those too, I would like to know what do you think about this.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 6, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants