Fix Mask2Former _init_weights to match Meta's original initialization (#50596)
* Fix Mask2Former _init_weights to match Meta's original initialization
Fix three initialization bugs where _init_weights diverged from Meta's
original Mask2Former code:
1. nn.Embedding layers received normal_(std=0.02) from the base
PreTrainedModel._init_weights, but Meta's original uses PyTorch's
default nn.Embedding init (std=1.0). Now explicitly initialized
with normal_(std=1.0).
2. MLP prediction head biases were zeroed by the base _init_weights.
Meta's original _reset_parameters only applies xavier_uniform_ on
tensors where p.dim() > 1, leaving biases at PyTorch's default
Kaiming uniform. Now handled by a dedicated Mask2FormerPredictionBlock
branch.
3. Mask2FormerPixelDecoder.level_embed was initialized with
normal_(std=0), effectively zeroing it. Meta's original does not
explicitly initialize level_embed. Changed to normal_(std=1.0).
Moved super()._init_weights() from a blanket call at the top to an
else branch, so it only runs for modules not handled by
Mask2Former-specific logic. Uses transformers.initialization helpers
throughout.
Added Mask2FormerInitWeightsTest with four regression tests verifying
embedding std, MLP biases, level_embed, and deformable attention init.
Fixes #35877
* Address review: use kaiming_uniform_ for MLP, remove init tests
Per Rocketknight1's review:
- Changed MLP weight init from xavier_uniform_ to
kaiming_uniform_(a=math.sqrt(5)) to match PyTorch's default
nn.Linear init, since Meta's original MLP has no custom
_reset_parameters.
- Removed Mask2FormerInitWeightsTest class as init tests are
not critical and unlikely to regress.
Fixes #35877
* Trim init comments and drop unused padding_idx handling
---------
Co-authored-by: Matt <rocketknight1@gmail.com>