physicsnemo icon indicating copy to clipboard operation
physicsnemo copied to clipboard

feat(nn): add configurable norm_layer to Mlp

Open Thabhelo opened this issue 5 months ago • 3 comments

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.LayerNorm or get_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

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)

Thabhelo avatar Jun 06 '26 02:06 Thabhelo

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.

copy-pr-bot[bot] avatar Jun 06 '26 02:06 copy-pr-bot[bot]

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_factory accepts a string alias (\"batchnorm\", \"layernorm\", \"te_layernorm\"), a class, or any callable factory; unknown strings raise ValueError and a pre-instantiated nn.Module is explicitly rejected.
  • Both \"layernorm\" and \"te_layernorm\" resolve to exactly the same get_layer_norm_class() factory with no behavioral distinction — \"te_layernorm\" does not enforce TE availability and silently falls back to nn.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

greptile-apps avatar Jun 06 '26 03:06 greptile-apps

Okay cool, I have addressed the feedback in the latest push:

  • "layernorm" now maps to PyTorch nn.LayerNorm; "te_layernorm" requires transformer_engine and CUDA (raises otherwise).
  • get_layer_norm_class() remains available as a user-supplied factory for TE-aware auto selection.
  • self.norm_layer is stored on Mlp for introspection.

Added tests for the TE string path and the stored attribute, too.

Thabhelo avatar Jun 06 '26 03:06 Thabhelo

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.

Thabhelo avatar Jun 06 '26 16:06 Thabhelo

@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!

Thabhelo avatar Jun 22 '26 14:06 Thabhelo