Skip to content

[distributed] Train a model with sequence parallelism in Trainer - #50521

Open
3outeille wants to merge 5 commits into
cp-9-ep-x-cpfrom
cp-11-trainer
Open

3outeille wants to merge 5 commits into
cp-9-ep-x-cpfrom
cp-11-trainer

Conversation

@3outeille

@3outeille 3outeille commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stack: #50493 → #50496 → #50796 → #50511 → #50514 → this PR → #50522

Adds [Trainer] support for models loaded with DistributedConfig(sp_size=N) (including combinations with FSDP2/HSDP and TP), while keeping Accelerate's DeepSpeed Ulysses SP path intact.

  • Model & Trainer SP lookup: expose model.sp_size (matching tp_size / fsdp_size), unify Trainer.get_sp_size() across DistributedConfig and Accelerate's ParallelismConfig, and reject combining both.
  • Dataloader sharding: pass a (dp_shard, cp, tp) device mesh to Accelerate so ranks in the same (sp, tp) group receive the same batch (using the cp dim name that Accelerate's dataloader expects, without enabling Accelerate's CP).
  • Loss & metrics accounting: deduplicate num_items_in_batch (when average_tokens_across_devices=True), loss scaling, num_input_tokens_seen, and FLOPs across replicated (sp, tp) ranks.
  • Evaluation: allow loss-only evaluation (prediction_loss_only=True) and raise otherwise, since logits remain sequence-sharded on each rank.

Tests

test_sequence_parallel in tests/trainer/distributed/test_trainer_distributed_transformers.py compares step losses, grad norms, eval_loss, and num_input_tokens_seen against a single-process baseline:

setup ranks
sp=2 2
sp=2, grad accum 2 2
fsdp=2 × sp=2, grad accum 2 4
tp=2 × sp=2 4
sp=2, average_tokens_across_devices=False 2

Verified alongside the existing FSDP/HSDP, Accelerate CP (test_cp_equivalence), and DeepSpeed Ulysses SP (test_alst_ulysses_sp) distributed Trainer tests.

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

@3outeille 3outeille changed the title [distributed] Train a model with context parallelism in Trainer [] Train a model with sequence parallelism in Trainer Nov 20, 2026
@3outeille 3outeille changed the title [] Train a model with sequence parallelism in Trainer [distributed] Train a model with sequence parallelism in Trainer Nov 20, 2026
@3outeille
3outeille removed this pull request from stack #50797 November 20, 2026 08:49
@3outeille
3outeille added this pull request to stack #51004 November 20, 2026 08:50
@3outeille
3outeille removed this pull request from stack #51004 November 30, 2026 05:31
@3outeille
3outeille added this pull request to stack #51606 December 2, 2026 05:05
@3outeille
3outeille force-pushed the cp-11-trainer branch 4 times, most recently from 4038af3 to e398b6a Compare December 3, 2026 07:15
`Trainer` trains a model loaded with `DistributedConfig(sp_size=N)` like a model on a single
device. Its dataloader mesh is `(dp_shard, cp, tp)` — the `cp` dim holds the `sp` size, the name
Accelerate's dataloader shards over — so the ranks of an sp group (and of a tp group) get the same
batch and only the data parallel ranks split them. The token count that normalizes the loss counts
each sequence once, the logged loss is the loss of the full sequences, and the per-rank token and
FLOP counts gathered over every rank are divided by the batch replicas.

Evaluation supports the loss only: each rank holds the logits of its own slice of the sequences, so
`predict` refuses to gather them. The refusal added with `DistributedConfig(sp_size=N)` goes away.

The script trains a tiny model through `Trainer` with sp / tp / fsdp sizes and writes the loss, the
gradient norm, the evaluation loss and the token count, so the test compares five configurations
against a single-device run of the same samples.
…ngle device

`sp_distributed_config.py` trains a tiny model through `Trainer` with sp / fsdp / tp sizes and writes the loss, the gradient norm, the evaluation loss and the token count of two steps. Five configurations (sp, sp with gradient accumulation, fsdp x sp, tp x sp, and sp with tokens not averaged across devices) are compared with a single-device run of the same samples, launched with one process so that the reference does not use `DataParallel`.

This branch has not been deployed

No deployments
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