[`distributed`] Add a new parallel plan for unit sharding (#48848)
* initial draft
* draft 1.
* update
* up
* this should be better?
* let's introduce the unit dimension?
* inherit
* add rowise
* update
* small fix
* Test unit sharding layouts
Covers replicated, non-dividing, coprime, exact, GQA and MQA head counts.
* Build the unit sharding tests on a real single-layer Llama
Replaces the hand-rolled attention mock with LlamaModel, so the styles read
the config the same way they do in a real plan.
* Move unit mesh construction to TransformersDeviceMesh
Build the (units, unit_replicate) view of the tp mesh once per group count in the
mesh manager instead of creating a new DeviceMesh and process groups per projection.
* no needs config
* Revert "no needs config"
This reverts commit 0106fb111a2f6c2502b7bce9b9e2f3aa2e868bb1.
* Rename single-letter x locals in TP styles, fix unit rowwise comments
* TP: pass config/mesh_manager to the styles instead of setting them on 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.
* update and revert
* [distributed] Ensure unit TP test exercises _ScaleBackward path
* Shorten noisy comment in core_model_loading to pass CI
* Pass config/mesh_manager to every TP style's shard_param
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.
* Pass the TP mesh to every style's shard_param
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.
* Handle per-head attention parameters in the unit TP styles
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.
* Revert per-head unit-sharding hack; keep the unit test to q/k/v/o
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.
---------
Co-authored-by: Ferdinand Mom <47445085+3outeille@users.noreply.github.com>