fix: nan error when all fparam/aparam have equal values#5321
fix: nan error when all fparam/aparam have equal values#5321njzjz merged 2 commits intodeepmodeling:masterfrom
nan error when all fparam/aparam have equal values#5321Conversation
📝 WalkthroughWalkthroughcompute_std now clips the computed variance with Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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 #5321 +/- ##
=======================================
Coverage 82.30% 82.30%
=======================================
Files 775 775
Lines 77628 77626 -2
Branches 3675 3678 +3
=======================================
+ Hits 63888 63889 +1
+ Misses 12568 12565 -3
Partials 1172 1172 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
🧹 Nitpick comments (1)
deepmd/utils/env_mat_stat.py (1)
92-97: Align TensorFlow_compute_stdimplementations with the guard applied indeepmd/utils/env_mat_stat.py.Both
deepmd/tf/fit/ener.py(line 380) anddeepmd/tf/descriptor/se_a.py(line 918) takesqrton unclamped variance without protecting against negative values. Their post-check thresholds do not catchNaNresults that can arise from numerical instability (e.g., when floating-point errors produce negative variance estimates). Apply pre-sqrt variance clamping (a_min=0) to both_compute_stdmethods to match the fix already in place.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@deepmd/utils/env_mat_stat.py` around lines 92 - 97, The two TensorFlow implementations of _compute_std (in deepmd/tf/fit/ener.py and deepmd/tf/descriptor/se_a.py) take tf.sqrt on a variance expression without guarding against small negative values; update each _compute_std to clamp the variance before sqrt (e.g., use tf.clip_by_value or tf.maximum to enforce a_min=0) so you compute tf.sqrt(clamped_variance) instead of tf.sqrt(raw_variance), matching the np.clip fix in deepmd/utils/env_mat_stat.py; ensure you replace the direct variance -> sqrt usage in both _compute_std functions with the clamping step while keeping downstream logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@deepmd/utils/env_mat_stat.py`:
- Around line 92-97: The two TensorFlow implementations of _compute_std (in
deepmd/tf/fit/ener.py and deepmd/tf/descriptor/se_a.py) take tf.sqrt on a
variance expression without guarding against small negative values; update each
_compute_std to clamp the variance before sqrt (e.g., use tf.clip_by_value or
tf.maximum to enforce a_min=0) so you compute tf.sqrt(clamped_variance) instead
of tf.sqrt(raw_variance), matching the np.clip fix in
deepmd/utils/env_mat_stat.py; ensure you replace the direct variance -> sqrt
usage in both _compute_std functions with the clamping step while keeping
downstream logic unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 90ca07ae-2c9b-4464-8b60-6062f9448bdc
📒 Files selected for processing (1)
deepmd/utils/env_mat_stat.py
For standard deviation of$\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_i - \bar{x})^2}=\sqrt{\frac{\sum x_i^2}{N} - \left( \frac{\sum x_i}{N} \right)^2}$ .$\frac{\sum x_i^2}{N} - \left( \frac{\sum x_i}{N} \right)^2$ equals zero.
fparam/aparam,When all
fparam/aparamhave equal values in one dimension,However, it sometimes becomes a very small negative number(for example, 1e-18) due to numerical instability, so$\sqrt{\frac{\sum x_i^2}{N} - \left( \frac{\sum x_i}{N} \right)^2}$ becomes
nan.Summary by CodeRabbit