Avoid autograd leaves for energy-only pipelines#107
Conversation
Greptile SummaryThis PR avoids the unnecessary overhead of marking position tensors as autograd leaves when the pipeline is only asked to produce energies, by gating the
Important Files Changed
Reviews (2): Last reviewed commit: "Strengthen energy-only autograd test" | Re-trigger Greptile |
| assert model.scale.grad is not None | ||
| assert model.positions_requires_grad_seen is False |
There was a problem hiding this comment.
The new test checks that
scale.grad is not None but never verifies it is non-zero. The analogous test_training_preserves_force_graph_for_backward test also asserts model.scale.grad.abs() > 0, which catches the case where the backward pass runs but produces a trivially zero gradient (e.g., if the energy output is constant w.r.t. scale). Without this check the regression only proves a gradient object exists, not that the training signal flows correctly.
| assert model.scale.grad is not None | |
| assert model.positions_requires_grad_seen is False | |
| assert model.scale.grad is not None | |
| assert model.scale.grad.abs() > 0 | |
| assert model.positions_requires_grad_seen is False |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| out["energy"].sum().backward() | ||
|
|
||
| assert model.scale.grad is not None | ||
| assert model.positions_requires_grad_seen is False |
There was a problem hiding this comment.
Using
is False for a boolean comparison is unconventional in Python and can surprise readers who associate is with identity checks on objects. tensor.requires_grad always returns a Python bool singleton so both forms are equivalent in practice, but the negation form communicates intent more clearly.
| assert model.positions_requires_grad_seen is False | |
| assert not model.positions_requires_grad_seen |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Updated the regression test to assert a non-zero parameter gradient and use the clearer boolean assertion. Verified with python -m pytest test/models/test_pipeline.py -q. |
Summary
Closes #100.
Testing
python -m pytest test/models/test_pipeline.py::TestPipelineAutogradGroup -qpython -m py_compile nvalchemi\models\pipeline.py test\models\test_pipeline.pygit diff --check