Skip to content

[distributed] Context parallelism: pad the sequences to a multiple of cp_size - #50508

Merged
3outeille merged 0 commit into
cp-5-docsfrom
cp-7-auto-pad
Nov 9, 2026
Merged

3outeille merged 0 commit into
cp-5-docsfrom
cp-7-auto-pad

Conversation

@3outeille

@3outeille 3outeille commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stack: #50493 → #50496 → #50497 → this PR

With context parallelism, a sequence length that is not a multiple of cp_size used to raise. Now _shard_inputs pads the full sequences at the end before splitting them:

  • input_ids get pad_token_id (or 0), inputs_embeds get zeros. No real token attends to the padding, because it comes after them.
  • position_ids continue those of the last document, so packed sequences and flash attention see no new document.
  • The labels of the padding are -100. shift_labels and num_items_in_batch are built from the real tokens first, so the loss does not change.
  • outputs.logits is this rank's slice of the padded sequence. On the last rank, the padding positions at the end are not meaningful (documented).

Tests

tests/context_parallel: length 7 with cp=2 (unpacked, packed, inputs_embeds) and length 5 with cp=4. The loss, every gradient and the logits at the real positions match an unsharded run on the unpadded sequences.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

Pages changed in this PR:

@3outeille
3outeille added this pull request to stack #50502 November 9, 2026 14:10
@3outeille
3outeille merged commit 4249c6d into main Nov 9, 2026
7 checks passed
@3outeille
3outeille deleted the cp-7-auto-pad branch November 9, 2026 14:25
@3outeille
3outeille restored the cp-7-auto-pad branch November 9, 2026 14:27
@3outeille

Copy link
Copy Markdown
Member Author

This was marked merged into cp-5-docs by the restack (that branch now contains its commits); it was not merged into main. Continued in #50515.

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.

2 participants