-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced new classes: `DPZBLLinearEnergyAtomicModel` and `PairTabAtomicModel`, enhancing atomic model functionalities. - Added `get_zbl_model` function for constructing `DPZBLModel` from input data. - Improved error handling in vector normalization with `safe_for_vector_norm` and `safe_for_sqrt`. - **Bug Fixes** - Enhanced distance calculations in `format_nlist` to prevent NaN errors. - **Documentation** - Updated comments and docstrings for clarity on recent changes. - **Tests** - Enhanced test support for JAX backend in `test_zbl_ener.py`. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Jinzhe Zeng <[email protected]>
- Loading branch information
Showing
11 changed files
with
363 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
"""Safe versions of some functions that have problematic gradients. | ||
Check https://jax.readthedocs.io/en/latest/faq.html#gradients-contain-nan-where-using-where | ||
for more information. | ||
""" | ||
|
||
import array_api_compat | ||
|
||
|
||
def safe_for_sqrt(x): | ||
"""Safe version of sqrt that has a gradient of 0 at x = 0.""" | ||
xp = array_api_compat.array_namespace(x) | ||
mask = x > 0.0 | ||
return xp.where(mask, xp.sqrt(xp.where(mask, x, xp.ones_like(x))), xp.zeros_like(x)) | ||
|
||
|
||
def safe_for_vector_norm(x, /, *, axis=None, keepdims=False, ord=2): | ||
"""Safe version of sqrt that has a gradient of 0 at x = 0.""" | ||
xp = array_api_compat.array_namespace(x) | ||
mask = xp.sum(xp.square(x), axis=axis, keepdims=True) > 0 | ||
if keepdims: | ||
mask_squeezed = mask | ||
else: | ||
mask_squeezed = xp.squeeze(mask, axis=axis) | ||
return xp.where( | ||
mask_squeezed, | ||
xp.linalg.vector_norm( | ||
xp.where(mask, x, xp.ones_like(x)), axis=axis, keepdims=keepdims, ord=ord | ||
), | ||
xp.zeros_like(mask_squeezed, dtype=x.dtype), | ||
) |
Oops, something went wrong.