fix(autotp): supplement replicated_with_grad_allreduce for q/k-norm with transformers < 5.3 (#9156) (#9166)
### Summary
Fixes #9156.
In `transformers < 5.3.0`, `base_model_tp_plan` for models with per-head
Q/K norm (such as Qwen3, Qwen3-MoE, Gemma3, Exaone4) shards
`self_attn.q_proj` / `k_proj` column-wise but omits `self_attn.q_norm`
and `self_attn.k_norm`. Because DeepSpeed allows `transformers>=4.51.3`,
no `grad_allreduce` hooks were registered for those norm weights,
causing each tensor-parallel rank to update its own copy of these
parameters from partial attention gradients and silently drift apart
during AutoTP training.
This PR:
1. Adds `TPPlanConverter.supplement_replicated_qk_norm(hf_tp_plan,
model=None)`:
- For attention layers whose query and key projections are
column-sharded, supplements `replicated_with_grad_allreduce` entries for
`q_norm` and `k_norm` (and `v_norm` when present on the model).
- If a `model` is provided, checks `model.named_parameters()` so that
norm specs are only added when matching norm parameters actually exist
on the model (avoiding adding unnecessary specs to models like LLaMA).
- Preserves existing entries in the plan without modification.
2. Invokes `supplement_replicated_qk_norm` in `_get_hf_tp_plan(model)`
and in `TPPlanConverter.convert(..., model=model)` to ensure Hugging
Face models running on supported `transformers < 5.3` versions
automatically have their per-head Q/K norm weights registered for
tensor-parallel gradient all-reduction.
3. Adds comprehensive unit tests in
`tests/unit/module_inject/test_tp_plan_converter.py` covering plan
supplementation with/without a model, preservation of existing specs,
rejection of unnecessary norms for models without Q/K norm, and mock
Qwen3 `_get_hf_tp_plan` conversion to `PartitionType.SKIP` with
`grad_allreduce=True`.
### Test Plan
```bash
pytest tests/unit/module_inject/test_tp_plan_converter.py
# 18 passed in 0.31s
```
---------
Signed-off-by: shotsan <shotsan@users.noreply.github.com>
Co-authored-by: shotsan <shotsan@users.noreply.github.com>