Repository navigation
Conversation
|
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. |
ba95ec6 to
a36d4f7
Compare
d0e004f to
16affa7
Compare
6b06ac3 to
4d31656
Compare
distributed] Context parallel implementations, with Ulyssesdistributed] Context parallel module, with Ulysses
|
Thank you for working on this integration, @3outeille I'd recommend to stick to footnote: also rsa can be composed with ulysses => Unified Sequence Parallelism (USP) |
distributed] Context parallel module, with Ulyssesdistributed] Sequence parallel module, with Ulysses
2deaaf4 to
4c1f526
Compare
5b4f074 to
3667b13
Compare
Each rank of a `cp` device mesh holds a contiguous slice of every sequence. The plan pairs two kinds of styles: `cp_split_sequence` cuts the layer inputs to that slice, and an attention style, `cp_ulysses`, runs the attention call of the attention modules over the whole sequence, with the mask and the keyword arguments the model built before the sequence was cut. Ulysses is two all-to-all exchanges around the model's own attention function; another algorithm is a `ContextParallelAttention` subclass registered in `ALL_PARALLEL_STYLES`. The styles share `ContextParallelLayer`, with the `install_forward` contract of the TP and EP styles. `apply_context_parallelism` installs a resolved plan on a model, and `ModelContextParallel` on the model itself: the model checks, a forward without a cache, and the loss of the whole sequences, which every rank reports. Not wired into `DistributedConfig` yet: nothing changes for users.
The all-to-all and its backward, Ulysses against plain sdpa on the whole sequence given the whole mask (unpacked, packed, sliding window, with and without GQA), the split style on a stand-in layer, the routing of the attention call, the Ulysses and model checks, the loss against the full-sequence loss, and a tiny Llama with the plan installed by hand against the same weights on the full sequences. The multi-rank tests use two CPU ranks through `run_distributed`, and carry `is_fsdp_test` so that the sharded CI jobs own them.
The context parallel styles live in their own registry, `ALL_CP_STYLES`, like `ALL_FSDP_STYLES`, instead of `ALL_PARALLEL_STYLES` plus a filter on their class. `apply_context_parallelism` reads it, and another attention algorithm is added to it under its own name. The layer types that `ModelContextParallel.check` accepts are inlined in the check.
…ks in `apply_context_parallelism`, pad the sequences - `context_parallel_loss` is a module-level function that `apply_context_parallelism` sets as `model.loss_function`, the seam transformers has for a model's loss; `ModelContextParallel` is about the forward only. - The model checks open `apply_context_parallelism` instead of a `check` method. - A sequence length that `cp_size` does not divide is padded at its end to a multiple of it, in `ModelContextParallel.transform_inputs_pre_forward`, before the model builds its masks: `input_ids` get `pad_token_id` (or 0), `inputs_embeds` zeros, `attention_mask` zeros, `labels` and `shift_labels` -100, and `position_ids` continue those of the last document. The logits are this rank's slice of the padded sequence; the loss counts the real tokens only. Was #50515.
…he two transforms of a style `ContextParallelLayer` gains `transform_output_post_forward`, so a style can rewrite the output of the forward it wraps, as the tensor parallel styles do. `ModelContextParallel` uses both: its inputs lose the cache, are padded to a multiple of `cp_size`, and carry this rank's slice of the shifted labels with the token count of the whole sequences, which the model's own loss function takes as kwargs; its output reports the loss of the whole sequences on every rank, with the gradient of this rank's slice scaled by `cp_size` for FSDP2's average. `context_parallel_loss` and the replacement of `model.loss_function` are gone.
`ALL_CP_STYLES` lists everything context parallelism installs, the model's own style included, and `apply_context_parallelism` installs the root through the same loop as the plan, under the name `named_modules` gives it.
`apply_context_parallelism` installs the plan it is given, the model itself included under the name `named_modules` gives it, `""`. The tests write it in their plans.
…on install Restores the branch to its pre-merge scope: the tester mixin, CI job and test fetcher changes belong to #50796.
3667b13 to
5eeaf73
Compare
|
Hey, nice stack! I gave it a try with TRL and our SFT and DPO trainers crash on the first step with The reason: we patch transformers/src/transformers/distributed/sequence_parallel.py Lines 59 to 71 in 6ffcf6e So our forward just overwrites yours and the padding, label split and loss reduction never run. I tried installing def install_forward(self, module: nn.Module, sp_mesh: DeviceMesh):
module.register_forward_pre_hook(
lambda module, args, kwargs: self.transform_inputs_pre_forward(module, args, kwargs, sp_mesh),
with_kwargs=True,
)
module.register_forward_hook(
lambda module, args, kwargs, output: self.transform_output_post_forward(module, output, sp_mesh),
with_kwargs=True,
)
return moduleWould you be ok adding it to |
What does this PR do?
TODO: tiled MLP
This is the first PR in a sequence-parallelism stack: this PR → #50496 → #50796 → #50511 → #50514 → #50521 → #50522.
It adds the sequence-parallel module and a Ulysses attention style. Nothing is wired into
DistributedConfigyet: thespmesh axis and config plans come in #50496.Sequence parallelism splits a sequence across ranks so each decoder layer works on fewer tokens. Attention still needs tokens held by other ranks. This PR provides three styles:
sp_modelprepares inputs and combines partial lossessp_split_sequencegives each decoder layer its local tokenssp_ulyssesexchanges sequence slices for head slices so attention can run over the full sequence.For example, split an eight-token sequence across two ranks: