[Bug] Mamba2 RMSNormGated uses the global ngroups for group_size under tensor parallelism
Summary
With tensor parallelism, Mamba2 builds its gated RMSNorm with a group size that is world_size times too small (mamba2.py#L144-L145):
self.norm = RMSNormGated(self.d_ssm, eps=1e-5, norm_before_gate=self.norm_before_gate,
group_size=self.d_ssm // ngroups, **factory_kwargs)self.d_ssm is already the per-rank width (L81), but ngroups is the constructor argument, i.e. the global group count. The per-rank count is self.ngroups = ngroups // self.world_size (L83), which is what the rest of the module uses.
From reading the code, the default fused path (use_mem_eff_path=True) is not affected: mamba_split_conv1d_scan_combined computes the norm's group size itself as dim // ngroups with the per-rank values (ssd_combined.py#L872-L873). self.norm is used by the unfused path, by forward when inference_params is passed, and by step(). So with TP > 1, the fused training path and the decoding path normalize over different groups.
The same one-line change was proposed in #908, #916 and #951. All three were closed without being merged, and main still has self.d_ssm // ngroups, so I'm adding a numerical check here in case it helps.
Environment
Commit e9594ce1, PyTorch 2.8.0+cu128, CUDA 12.8, NCCL, FP32, 2× RTX 5090.
Steps to reproduce
Build Mamba2 with TP=1 and TP=2 using d_model=64, expand=2 (so d_inner=128), d_state=16, headdim=16, ngroups=2, use_mem_eff_path=False and sequence_parallel=False. Feed the same global input with the same initial weights (sharded for TP=2), then compare the norm output, the backward pass and one SGD-with-momentum step.
The group size should be 64 in both cases (128 / 2). With TP=2 each rank gets 64 // 2 = 32 instead of 64 // 1 = 64. The inputs to the norm, including the gate, match between TP=1 and TP=2; the outputs start to differ at the norm.
| TP=2 vs. TP=1, normalized L2 | Current code | With self.ngroups |
|---|---|---|
| Norm output | 0.22480341 |
0 |
| Gradient | 0.29062694 |
9.211171e-8 |
| Parameter update | 0.29062671 |
1.5965037e-6 |
Normalized L2 here is ‖a − b‖ / max(‖a‖, ‖b‖). Reproduced on a second machine. I only ran the unfused path without sequence parallelism; the statement about the fused path above is from reading the code.
Expected behavior
group_size should not depend on the TP degree, and the fused and unfused paths should normalize over the same groups.
Proposed fix
Use the per-rank group count:
group_size=self.d_ssm // self.ngroupsThis is unchanged for TP=1.
Source: state-spaces/mamba