Skip to content

Avoid NaN stress gradients for zero-volume molecular cells - #181

Merged
vsimkus merged 2 commits into
orbital-materials:mainfrom
davrens101:fix/zero-volume-stress-gradients
Nov 9, 2026
Merged

vsimkus merged 2 commits into
orbital-materials:mainfrom
davrens101:fix/zero-volume-stress-gradients

Conversation

@davrens101

@davrens101 davrens101 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Stress for a zero-volume molecular cell is divided by abs(det(cell)) before invalid results are masked to zero. The forward result looks valid, but backward through a stress loss still divides by zero and can poison the force-training gradients. Both conservative stress and the direct ZBL stress path have this ordering.

Change

Use a safe denominator for cells without volume, then retain the existing zero-stress output for those cells. Finite positive-volume calculations, cell-handedness handling and checkpoint parameters are unchanged. Document the zero-volume gradient behavior.

The three regression cases are necessary here because forward-only tests miss the defect: all-molecular and mixed-periodicity force-loss parameter gradients, plus the positional derivative of molecular ZBL stress. All three fail before this patch.

Validation

  • A real ASE H2 atom graph with E = k * sum(edge_vectors**2) returns zero molecular stress before and after; the force-plus-stress loss derivative changes from NaN to the analytic 31.36.
  • Real ConservativeForcefieldRegressor.loss(...).loss.backward() on a small nonperiodic H2O graph: upstream has 42 non-finite parameter-gradient tensors on the first step; patched has zero non-finite tensors across four Adam steps, with loss 3.1429 -> 2.3833. No pretrained checkpoint or model-quality claim.
  • Affected utilities/ZBL tests: 9 passed. Conservative/direct regressor suites excluding compilation: 13 passed, 8 deselected.
  • Independent float32/float64 stress/gradient checks cover zero, rank-deficient, small-volume and left/right-handed cells.
  • Repository Ruff check, changed-file Ruff format check, and mypy: pass (85 files checked by mypy).

Upstream Ubuntu CI subsequently completed successfully: Python 3.12, 3.13 and 3.14 test jobs, the aggregate allchecks job, and both Socket Security checks are green.

Local compilation limitation: the broader Windows CPU run fails during Dynamo tracing; a separate untouched upstream checkout also fails with recursion in compute_differentiable_edge_vectors. Compiled-model and GPU behavior are not claimed as verified.

AI-assisted implementation and validation, with an additional review by a second coding agent. I supervised and checked the AI-assisted work as it progressed.

@vsimkus vsimkus 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.

Thanks! I took the liberty to clean it up a bit and also apply the same fix in the D3 correction.

@vsimkus
vsimkus merged commit 441d768 into orbital-materials:main Nov 9, 2026
6 checks passed
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.

2 participants