Skip to content

Detach conservative inference energy outputs - #2136

Open
wtfPrethiv wants to merge 5 commits into
facebookresearch:mainfrom
wtfPrethiv:fix-uma-energy-detach
Open

wtfPrethiv wants to merge 5 commits into
facebookresearch:mainfrom
wtfPrethiv:fix-uma-energy-detach

Conversation

@wtfPrethiv

Copy link
Copy Markdown

Fix conservative UMA inference outputs retaining autograd graphs

Closes #2072.

Problem statement

Conservative UMA inference (direct_forces=False) must keep autograd enabled to derive forces from energy. However, MLIPPredictUnit.predict() returned the energy tensor with its complete forward autograd graph still attached.

Callers that retain completed predictions across multiple calls; for example TorchSim’s batching/reassembly flow therefore retained one full model forward graph per prediction. Allocated GPU memory grew roughly linearly and could not be reclaimed with torch.cuda.empty_cache().

TorchSim worked around this downstream in TorchSim/torch-sim#590 while awaiting the fairchem-side fix.

Root cause

_run_inference() correctly uses nullcontext() for conservative models so autograd.grad() can compute forces. Forces use create_graph=False and do not retain a graph, but energy passed through _process_outputs() unchanged after denormalization/reference restoration.

Fix

Detach returned conservative-evaluation tensors at the inference output boundary, after all post-processing:

if (
    not self.model.training
    and not self.model.module.backbone.regress_config.direct_forces
):
    pred_output = {name: value.detach() for name, value in pred_output.items()}

This:

  • Applies only to eval/inference.
  • Applies only to conservative-force models.
  • Leaves direct-force inference unchanged.
  • Preserves training energy graphs and energy-loss backpropagation.
  • Also detaches conservative stress or other graph-bearing outputs.

Validation

Before the fix, conservative UMA inference returned graph-bearing energy:

pred["energy"].requires_grad  # True
pred["energy"].grad_fn        # non-None

After the fix:

pred["energy"].requires_grad  # False
pred["energy"].grad_fn        # None
pred["forces"].requires_grad  # False
pred["forces"].grad_fn        # None

Added regression coverage for:

  • Conservative UMA predict() outputs: every returned tensor is detached.
  • Direct-force predict() path: remains unaffected.
  • Conservative UMA training: energy retains a valid autograd graph and backward() succeeds.

Test results

  • New regressions: 3 passed
  • Existing conservative CPU stress prediction: 1 passed
  • TorchSim conservative stress integration: 2 passed
  • Ruff and git diff --check: passed

The broader pretrained UMA suite is blocked in this environment by a 401 Unauthorized response from the gated facebook/UMA Hugging Face repository before model execution.

@meta-cla

meta-cla Bot commented Aug 5, 2026

Copy link
Copy Markdown

Hi @wtfPrethiv!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Aug 5, 2026
@meta-cla

meta-cla Bot commented Aug 5, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@frostedoyster frostedoyster added bug Something isn't working patch Patch version release labels Aug 5, 2026

@frostedoyster frostedoyster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rayg1234 do we want to support users of this function differentiating energies/forces on their own? Otherwise this PR looks good

@wtfPrethiv

Copy link
Copy Markdown
Author

@frostedoyster The GPU sweep completed all 44 selected tests successfully (1 expected XFAIL), but the job then segfaulted during NCCL shutdown with exit code 139. Could you rerun test_gpu_sweep (3.14, units, uma-s-1p1) to check whether this was a transient runner issue? Thanks !

@frostedoyster

Copy link
Copy Markdown
Contributor

Hey @wtfPrethiv, thank you very much for the contribution! We're trying to fix some issues with our tests and CI but I think we will be able to merge this PR relatively soon

@wtfPrethiv

Copy link
Copy Markdown
Author

Hey @frostedoyster, just wanted to follow up on this PR since it’s been a few weeks. Happy to make any changes or help with anything needed.

Thanks !

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cla signed patch Patch version release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conservative UMA returns graph-carrying energy from raw predict path — leaks autograd graph in retaining callers

2 participants