fix(dpmodel): guard empty magnetic loss masks#5798
Conversation
Use Array API-safe nonzero denominators for magnetic-force loss reductions so an all-empty mask contributes finite zero instead of NaN. Add backend-neutral NumPy and Torch namespace regressions across loss functions, precisions, and label-presence flags. Coding-Agent: Codex Codex-Version: codex-cli 0.144.1 Model: gpt-5.6-sol Reasoning-Effort: xhigh
|
Warning Review limit reached
Next review available in: 25 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5798 +/- ##
==========================================
- Coverage 79.85% 78.30% -1.55%
==========================================
Files 1022 1029 +7
Lines 117351 117576 +225
Branches 4313 4312 -1
==========================================
- Hits 93706 92064 -1642
- Misses 22101 23956 +1855
- Partials 1544 1556 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Root cause and fix
EnergySpinLossmasks non-magnetic atoms out of the force-magnitude residual, then divides its reductions byn_valid * 3. When a batch contains no magnetic atoms, both numerator and denominator are zero, producing NaN. Multiplying that value by a zero prefactor or a missing-label flag does not recover it because0 * NaNis still NaN.The implementation now constructs:
and uses it for every global magnetic-force MSE/MAE reduction. The masked numerator is already zero for an empty set, so the contribution becomes exactly zero. Guarding the denominator itself is important: array backends may evaluate both branches of a later
where, and an unselected divide-by-zero can still produce invalid values or gradients. The expression is compatible with Array API namespaces and JAX tracing; non-empty batches retain the exact previous formula.This PR intentionally does not change the native PyTorch loss or redefine its missing-label display metrics. When
find_force_mag == 0, the total loss is finite zero whiledisplay_if_existcontinues to report NaN for the unavailable magnetic metric.Why existing tests missed this
Existing spin-loss mask tests always selected at least one magnetic atom in each batch. They covered partial masks and all-magnetic batches, but never the global zero denominator.
The new backend-neutral regression parameterizes:
mseandmae;find_force_magpresent and absent.It verifies a finite zero total loss in all eight combinations, zero magnetic metrics for a present-but-empty label, and the existing NaN display behavior for an absent label. A separate pt_expt test exercises the same shared dpmodel implementation through the Torch Array API namespace.
On the previous implementation, all eight backend-neutral cases fail with NaN. With the fix, the combined new and related targeted tests pass.
Validation
array_api_strictMSE/MAE empty-mask scenarios passedruff format --check .ruff check .git diff --checkCloses #5637.
Coding agent: Codex
Codex version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning effort: xhigh