feat(nn): add configurable norm_layer to Mlp
PhysicsNeMo Pull Request
Description
Adds a norm_layer parameter to physicsnemo.nn.Mlp so normalization type is configurable instead of being limited to BatchNorm1d via use_batchnorm.
Supported options:
norm_layer="batchnorm","layernorm", or"te_layernorm"norm_layer=nn.LayerNormorget_layer_norm_class()for TE-aware LayerNorm- Any callable factory invoked as
norm_layer(out_features)
use_batchnorm=True remains backward compatible and is mutually exclusive with norm_layer.
Closes #1451
Checklist
- [x] I am familiar with the Contributing Guidelines.
- [x] New or existing tests cover these changes.
- [ ] The documentation is up to date with these changes.
- [x] The CHANGELOG.md is up to date with these changes.
- [x] An issue is linked to this pull request.
- [ ] If I am implementing a new model or modifying any existing model, I have followed the Models Implementation Coding Standards.
Dependencies
None.
Test plan
- [x]
pytest test/nn/module/test_mlp_layers.py -v - [x]
pytest --doctest-modules physicsnemo/nn/module/mlp_layers.py - [x] pre-commit hooks on changed files (ruff, interrogate, markdownlint, license, import-linter)
This pull request requires additional validation before any workflows can run on NVIDIA's runners.
Pull request vetters can view their responsibilities here.
Contributors can view more details about this message here.
Greptile Summary
This PR adds a norm_layer parameter to physicsnemo.nn.Mlp, replacing the hard-coded BatchNorm1d path with a flexible factory resolved by the new _resolve_norm_factory helper. The use_batchnorm flag is preserved for backward compatibility and is mutually exclusive with the new parameter.
_resolve_norm_factoryaccepts a string alias (\"batchnorm\",\"layernorm\",\"te_layernorm\"), a class, or any callable factory; unknown strings raiseValueErrorand a pre-instantiatednn.Moduleis explicitly rejected.- Both
\"layernorm\"and\"te_layernorm\"resolve to exactly the sameget_layer_norm_class()factory with no behavioral distinction —\"te_layernorm\"does not enforce TE availability and silently falls back tonn.LayerNorm, making the two strings undifferentiated aliases despite the docstring listing them separately.
Important Files Changed
| Filename | Overview |
|---|---|
| physicsnemo/nn/module/mlp_layers.py | Adds norm_layer parameter and _resolve_norm_factory helper; "layernorm" and "te_layernorm" strings are silent aliases (both call get_layer_norm_class()), which makes the "te_layernorm" option misleading |
| test/nn/module/test_mlp_layers.py | Good coverage of new norm_layer paths; no test exercises the "te_layernorm" string directly to verify it differs from "layernorm" |
| CHANGELOG.md | Entry added under the correct section with accurate description of the new norm_layer parameter and backward-compatibility note |
Reviews (1): Last reviewed commit: "feat(nn): add configurable norm_layer to..." | Re-trigger Greptile
Okay cool, I have addressed the feedback in the latest push:
"layernorm"now maps to PyTorchnn.LayerNorm;"te_layernorm"requirestransformer_engineand CUDA (raises otherwise).get_layer_norm_class()remains available as a user-supplied factory for TE-aware auto selection.self.norm_layeris stored onMlpfor introspection.
Added tests for the TE string path and the stored attribute, too.
Greptile feedback from the first review is addressed in 8896888. Replies are on the inline threads. docs come from the updated Mlp docstrings via Sphinx automodule.
@loliverhennigh gentle ping when you have a moment. Greptile feedback is addressed in 8896888. I will rebase onto main shortly to clear the merge conflict. Thanks!