Skip to content

[distributed] Add a new parallel plan for unit sharding - #48848

Merged
ArthurZucker merged 30 commits into
mainfrom
replicate-modulo-tp
Nov 26, 2026
Merged

ArthurZucker merged 30 commits into
mainfrom
replicate-modulo-tp

Conversation

@ArthurZucker

@ArthurZucker ArthurZucker commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

CPU CI GPU run-slow

What does this PR do?

This PR introduces unit sharding.
This affects mostly q,k,v, but i figured it will give more freedom in general.

When a layer is expressed as

nn.Linear(hidden_dim, unit_number x unit_dim)

And you want to unsure the last dim is sharded to non empty units, use this TP style.

DTensor does not allow uneven sharding. So the trick is to transform any Shard(n) to Shard(n// units), Replicate().
This is not optimal in terms of memory, but its the only solution for now.

Superseeds #47811
Example:

# 6 heads, 8 RANKs

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

Replaces the hand-rolled attention mock with LlamaModel, so the styles read
the config the same way they do in a real plan.
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 35069698737:1
Result: failure | Jobs: 16 | Tests: 190,462 | Failures: 1 | Duration: 14h 26m

@3outeille 3outeille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did a first review, we need some forward + backward test in test_tensor_parallel_mixin.py to be safe

@kaixuanliu

Copy link
Copy Markdown
Contributor

@ArthurZucker @3outeille Hi, will we adopt this approach? If so, I will close #47811

@ArthurZucker

Copy link
Copy Markdown
Collaborator Author

We will! You can close your PR!
I'll come back to it this month!

… modules

Drops the `needs_config` flag and the `module.config` / `module._mesh_manager`
attributes that `apply_tensor_parallelism` used to attach, so applying a plan no
longer mutates the model's modules. The unit styles take the config and mesh
manager as arguments to `shard_param`, and MLA takes the config in
`transform_output_post_forward`.

Also guards the unit-rowwise inference all-reduce on the unit axis, and drops the
stale "expected shape / prepare the slices" lines from the UnitColwiseParallel
docstring.
)

@is_tensor_parallel_test
def test_tp_unit_plan_forward_backward(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

from my understanding the test never run _ScaleBackward right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It does with the TP Plan that uses the unit colwise etc!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed sorry

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but wait now the test doesnt cover GQA ?

3outeille and others added 6 commits November 26, 2026 06:53
apply_tensor_parallelism passes config= and mesh_manager= to
style.shard_param, but ColwiseParallel, RowwiseParallel, EmbeddingParallel,
PackedColwiseParallel and PackedRowwiseParallel still had the old signature,
so sharding any embedding or linear parameter raised a TypeError.
apply_tensor_parallelism called shard_param without the mesh argument, so
sharding any parameter raised 'EmbeddingParallel.shard_param() missing 1
required positional argument: mesh'. This broke TP, EP and FSDP runs.
Beam keeps a per-head `gate_proj`/`gate_bias` and GraniteSWA a per-head
`sinks`; Glm4v-MoE derives its mrope sections from `head_dim`. The unit-plan
test forced Q heads to 1 without accounting for those, and `UnitColwiseParallel`
assumed every sharded parameter belonged to a Linear weight. Shard bare per-head
parameters on the unit mesh, undo the UnitRowwise input-gradient scaling for
them, and keep the original `head_dim` in the test.
@ArthurZucker

Copy link
Copy Markdown
Collaborator Author

Las commit was shit

Revert the UnitColwise per-head parameter support and its first-forward
gradient hook. Unit sharding is only meant for head-aligned Linear weights
for now; document that extra per-head parameters (Beam gate_bias, GraniteSWA
sinks) are unsupported in a TODO.

Fix the unit-plan test instead: keep the original head_dim so models that
derive mrope sections from it still work, and skip models whose attention has
extra head-aligned parameters.
@ArthurZucker
ArthurZucker merged commit 762cbe1 into main Nov 26, 2026
125 of 126 checks passed
@ArthurZucker
ArthurZucker deleted the replicate-modulo-tp branch November 26, 2026 11:35
qgallouedec added a commit that referenced this pull request Nov 26, 2026
Conflicts: the linear attention conv path became causal_conv1d_forward on main (#49xxx kernelizable conv): the paged
CB branch keeps causal_conv1d_varlen and the rest calls main's causal_conv1d_forward (Qwen3-Next, Qwen3.5, Qwen3.5-MoE,
Qwen4-Exp); transform_output_post_forward takes config (unit sharding, #48848); Gemma3RMSNorm(dim=) -> hidden_size.
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.

4 participants