Commit 9747539
authored
Prepare UMA inference gradients before compilation (#2143)
This standalone PR replaces
[#2126](#2126). The
ghstack PR was marked merged into its temporary `gh/mlazos/4/base`
branch, but its patch did not land on `main`.
UMA computes forces and stress by differentiating energy with respect to
positions and cells. The backbone previously enabled those gradients
with `requires_grad_` inside its compiled forward. Dynamo cannot
represent that tensor metadata mutation, so it stopped and resumed
tracing at each occurrence.
This change prepares position and cell gradient metadata in
`MLIPPredictUnit` after device and dtype conversion but before entering
the model. Guarded fallback calls remain in the backbone for direct
users; predictor-prepared inputs already satisfy those guards, so normal
compiled inference performs no metadata mutation inside the captured
region.
The fix is independent of topology source. It applies with both
`external_graph_gen=True` and the final validation configuration using
`external_graph_gen=False`, `internal_graph_gen_version=3`,
`merge_mole=True`, and `compile_dynamic_shapes=False`. No independent
flag enables it:
```python
settings = InferenceSettings(compile=True)
```
A compiled UMA-S-1p2 run previously reported five `requires_grad_`
graph-break sites and none after this change.
**Test plan**
```bash
PYTHONPATH=$PWD/src:$PYTHONPATH /home/mlazos/.conda/envs/pytorch-3.12/bin/python -m pytest -q tests/core/units/mlip_unit/test_predict.py -k test_prepare_inference_gradients
/home/mlazos/.conda/envs/pytorch-3.12/bin/pre-commit run --files src/fairchem/core/models/uma/escn_md.py src/fairchem/core/units/mlip_unit/predict.py tests/core/units/mlip_unit/test_predict.py
PYTHONPATH=$PWD/src:$PYTHONPATH /home/mlazos/.conda/envs/pytorch-3.12/bin/python -m compileall -q src/fairchem/core/models/uma/escn_md.py src/fairchem/core/units/mlip_unit/predict.py tests/core/units/mlip_unit/test_predict.py
```
Authored with assistance from Codex.1 parent 4ac989d commit 9747539
3 files changed
Lines changed: 47 additions & 3 deletions
File tree
- src/fairchem/core
- models/uma
- units/mlip_unit
- tests/core/units/mlip_unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
780 | 780 | | |
781 | 781 | | |
782 | 782 | | |
783 | | - | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
784 | 786 | | |
785 | | - | |
| 787 | + | |
786 | 788 | | |
787 | 789 | | |
788 | 790 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
| |||
494 | 504 | | |
495 | 505 | | |
496 | 506 | | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
497 | 510 | | |
498 | 511 | | |
499 | 512 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
| |||
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
70 | 99 | | |
71 | 100 | | |
72 | 101 | | |
| |||
0 commit comments