[Ulysses] Carry the KV head count per DistributedAttention (#8316)
Fixes #8291.
## What breaks
`single_all_to_all` decides whether a model does uneven-head sequence
parallelism by asking whether the process-wide `tp_shard` head count is
set:
```python
if get_num_kv_heads() is not None or (num_heads % seq_world_size != 0 and not scatter_idx < 2):
```
AutoTP writes that same slot on every injection
(`replace_module.py:306`, `engine.py:770`), so it is not only a second
Ulysses model that clobbers the first — a single AutoTP model anywhere
in the process is enough. `uneven_heads_all2all` then splits against
that value, and `get_shard_size_list` falls back to the same global
internally, so threading a value without also passing `num_kv_heads=`
explicitly would not have been enough.
A 12-head model on sp=4 should hand every rank 3 heads. With a leftover
`num_kv_heads=3`:
```
12 heads, sp=4
clean process -> [3, 3, 3, 3]
after AutoTP set kv=3 -> [4, 4, 4, 0]
```
Driving it through `DistributedAttention` on 4 gloo ranks, upstream
`6e3bd087f`:
```
clean process
rank0: local_attn got 3 heads ids=[0, 1, 2] backward OK
rank1: local_attn got 3 heads ids=[3, 4, 5] backward OK
rank2: local_attn got 3 heads ids=[6, 7, 8] backward OK
rank3: local_attn got 3 heads ids=[9, 10, 11] backward OK
after an AutoTP model ran first
(no output — rank 3 gets an empty tensor and the forward hangs in the output all-to-all)
```
Same script with this branch applied, both cases:
```
rank0: local_attn got 3 heads ids=[0, 1, 2] backward OK
rank1: local_attn got 3 heads ids=[3, 4, 5] backward OK
rank2: local_attn got 3 heads ids=[6, 7, 8] backward OK
rank3: local_attn got 3 heads ids=[9, 10, 11] backward OK
```
## The change
`DistributedAttention` holds the count, either from a new `num_kv_heads`
argument or read off its own key tensor, and only when it does not
divide evenly — claiming one otherwise would push every model onto the
uneven kernels. It threads the value through `_SeqAllToAll` into
`single_all_to_all` and `uneven_heads_all2all`, and `backward` restores
it from `ctx`, which is the piece the gather direction cannot recover
from tensor shapes.
The value is the **KV** head count, taken from the key tensor rather
than the query tensor. That is what the `tp_shard` slot held, and it is
what the partition has to follow: under GQA a query head must land on
the rank holding its KV head. Q=6 / KV=3 over sp=2 divides evenly on the
query side, so reading the query would give [3, 3] and split group 1
across two ranks; the KV side gives [2, 1] and therefore [4, 2]. Thanks
to @FU-max-boop for catching that on the issue - my first revision had
exactly this bug.
`get_shard_size_list` is called with `num_kv_heads` set explicitly so
its module-level default cannot reintroduce the leak.
Callers that reach `single_all_to_all` directly, Megatron-DeepSpeed
among them, pass nothing and keep the old behaviour including the
`set_num_kv_heads` publish on first use. A sentinel separates "not
supplied" from a deliberate `None`, which now means "splits evenly, take
the fast path".
This is independent of #8241 — it touches only
`deepspeed/sequence/layer.py` and does not depend on `AutoTPMeta` — but
the two overlap in intent, and I am happy to rebase on it if that lands
first.
## Tests
New `tests/unit/sequence_parallelism/test_ulysses_multiple_models.py`,
shaped after the multi-model AutoTP test in #8241:
- two `DistributedAttention` instances at 3 and 5 heads over sp=2 — the
first still splits `[2, 1]` after the second has run forward and
backward
- a 6-head model over sp=2 with a leftover `num_kv_heads=3` — this is
the case above, and it hangs on master
- GQA, Q=6 / KV=3 over sp=2 - splits [4, 2], matching master
- the explicit `num_kv_heads` argument
```
$ LOCAL_SIZE=2 pytest unit/sequence_parallelism/test_ulysses_multiple_models.py
4 passed in 21.65s
$ LOCAL_SIZE=2 pytest unit/sequence_parallelism/ unit/module_inject/test_tp_shard.py
31 passed in 97.39s
```
Run on CPU, so `cpu-torch-latest` picks them up rather than only the GPU
workflows.
---------
Signed-off-by: alanhuangyoo <alanhuangyoo@gmail.com>
Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>