Shrink and Perturb Weight Initialization#1839
Open
melo-gonzo wants to merge 8 commits into
Open
Conversation
Contributor
Greptile SummaryThis PR adds an in-place shrink-and-perturb utility for warm-started models. The main changes are:
Important Files Changed
Reviews (1): Last reviewed commit: "docs: fix merge conflict with changelog" | Re-trigger Greptile |
laserkelvin
requested changes
Jul 20, 2026
laserkelvin
left a comment
Collaborator
There was a problem hiding this comment.
Few minor things to address
|
|
||
|
|
||
| def _resolve_noise( | ||
| noise: Union[ |
Collaborator
There was a problem hiding this comment.
This formatting looks kind of weird to me - is this actually ruff'd?
| if p.numel() < 2: | ||
| # std is undefined for a single element; shrink only, no noise. | ||
| return torch.zeros_like(p) | ||
| z = torch.empty_like(p).normal_(generator=generator) |
Collaborator
There was a problem hiding this comment.
Could you not just do randn_like?
| Literal["scaled_normal", "normal"], Callable[[torch.Tensor], torch.Tensor] | ||
| ] = "scaled_normal", | ||
| include: Optional[Callable[[str, torch.Tensor], bool]] = None, | ||
| generator: Optional[torch.Generator] = None, |
Collaborator
There was a problem hiding this comment.
Do you know how torch.manual_seed propagates to this?
| is updated at most once. A warning is issued if an explicit ``include`` | ||
| matches no parameters. | ||
| generator : torch.Generator, optional | ||
| Generator for the built-in Gaussian noise, for reproducibility. Must be |
Collaborator
There was a problem hiding this comment.
We don't validate the generator being on the same device as module, if it is passed
| ``"scaled_normal"`` is a deliberately model-agnostic variant: it scales the | ||
| noise by the *pretrained tensor's* own standard deviation rather than by a | ||
| freshly re-initialized network's per-layer initializer variance (the form | ||
| in Ash & Adams, §4). It needs no architecture knowledge and matches the |
Collaborator
There was a problem hiding this comment.
Complete reference somewhere?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PhysicsNeMo Pull Request
Description
Adds
physicsnemo.nn.shrink_and_perturb_In-place shrink-and-perturb weight re-initialization, reference paper:
θ ← shrink·θ + perturb·εθ: pretrained model weightsshrink: retention factor in[0, 1), how much of each pretrained weight to keepperturb: noise level, scaled per-layer by the pretrained weight's std (default) or by a fresh init's stdε: random noise (Gaussian by default)Why. Warm-starting from pretrained weights can converge to a worse loss asymptote
than training from scratch (a plasticity loss associated with grown weight norms).
Shrinking each weight toward zero and adding noise is intended to restore the scale
statistics and plasticity of a fresh init while keeping the pretrained direction,
helping recover that warm-start handicap.
Use.
Model-agnostic (any torch.nn.Module, including physicsnemo.Module); in place; buffers
untouched; tied params handled.
Noise (noise=). "scaled_normal" (default): ε = std(θ)·N(0,1), scaled per-tensor
by the pretrained weight's own spread. "normal": unit Gaussian. Or a callable handed
fresh scratch (shape/dtype/device), so torch.randn_like, torch.nn.init.*_, or a
custom sampler all work, e.g. scaling noise by a fresh init's std instead of the
pretrained one.
Applications. Transfer/fine-tuning from a pretrained checkpoint, continual and
active learning (to help re-inject plasticity between rounds), and other warm-start
settings where a fresh baseline might otherwise win.
Limitations. Intended as a repair for a bad warm start rather than a way to add
signal: it tends to interpolate between fine-tune and scratch. scaled_normal adds no noise
to scalar or constant tensors (std=0), and shrink/perturb generally need tuning.
Checklist
Dependencies
Review Process
All PRs are reviewed by the PhysicsNeMo team before merging.
Depending on which files are changed, GitHub may automatically assign a maintainer for review.
We are also testing AI-based code review tools (e.g., Greptile), which may add automated comments with a confidence score.
This score reflects the AI’s assessment of merge readiness and is not a qualitative judgment of your work, nor is
it an indication that the PR will be accepted / rejected.
AI-generated feedback should be reviewed critically for usefulness.
You are not required to respond to every AI comment, but they are intended to help both authors and reviewers.
Please react to Greptile comments with 👍 or 👎 to provide feedback on their accuracy.