Skip to content

Add a WeightedSum model wrapper - #318

Open
zyxwwxyz wants to merge 2 commits into
metatensor:mainfrom
zyxwwxyz:metatomic_wsum
Open

Add a WeightedSum model wrapper#318
zyxwwxyz wants to merge 2 commits into
metatensor:mainfrom
zyxwwxyz:metatomic_wsum

Conversation

@zyxwwxyz

@zyxwwxyz zyxwwxyz commented Sep 4, 2026

Copy link
Copy Markdown

Adds metatomic.torch.weighted_sum.WeightedSum, a wrapper that combines several
existing outputs of a model into a new output computing their fixed linear
combination (for example, combining several "energy/<head>" outputs, such as
predictions from different DFT functionals, into a single "energy" output).

All the heads entering a given weighted sum are requested from the wrapped
model in a single call and combined by directly operating on the raw
TensorMap values (mts.add/mts.multiply), without detaching or otherwise
interrupting the autograd graph. This means a single backward() call through
the weighted-sum output produces forces and stresses that are exactly the
weighted sum of the forces and stresses of the individual heads, instead of
requiring one evaluation (and one set of gradients) per head.

The wrapper follows the same wrap()-based pattern as HeatFlux and DFTD3.
An optional normalize_coefficients flag on wrap() rescales the given
coefficients to sum to one (this also works with negative coefficients, as
long as they don't sum to exactly zero).

This is motivated by checkpoints exposing several "proper" heads (e.g. one per
training-data functional) that should be combined into physically meaningful
targets - for example an ensemble/consensus energy, without paying for a
separate forward+backward evaluation per combination.

Contributor (creator of pull-request) checklist

  • Tests updated (for new features and bugfixes)?
  • Documentation updated (for new features)?
  • Issue referenced (for PRs that solve an issue)?

Reviewer checklist

  • CHANGELOG updated with public API or any other important changes?

zyxwwxyz and others added 2 commits September 4, 2026 20:28
CI's lint job runs `ruff format --diff`, which flagged a few lines in
tests/weighted_sum.py that were not run through ruff format locally.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@zyxwwxyz
zyxwwxyz marked this pull request as ready for review September 4, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant