-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jax/array-api): energy fitting (#4204)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced a fitting module for energy models using JAX, enhancing compatibility with different array backends. - Added `AtomExcludeMask` class for improved attribute handling in exclusion masks. - **Improvements** - Updated serialization and array handling methods for better integration with array APIs. - Enhanced testing capabilities for energy fitting with support for different backends. - **Documentation** - Added SPDX license identifier to relevant files for licensing clarity. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Jinzhe Zeng <[email protected]>
- Loading branch information
Showing
9 changed files
with
197 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later |
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,39 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Any, | ||
) | ||
|
||
from deepmd.dpmodel.fitting.ener_fitting import EnergyFittingNet as EnergyFittingNetDP | ||
from deepmd.jax.common import ( | ||
flax_module, | ||
to_jax_array, | ||
) | ||
from deepmd.jax.utils.exclude_mask import ( | ||
AtomExcludeMask, | ||
) | ||
from deepmd.jax.utils.network import ( | ||
NetworkCollection, | ||
) | ||
|
||
|
||
def setattr_for_general_fitting(name: str, value: Any) -> Any: | ||
if name in { | ||
"bias_atom_e", | ||
"fparam_avg", | ||
"fparam_inv_std", | ||
"aparam_avg", | ||
"aparam_inv_std", | ||
}: | ||
value = to_jax_array(value) | ||
elif name == "emask": | ||
value = AtomExcludeMask(value.ntypes, value.exclude_types) | ||
elif name == "nets": | ||
value = NetworkCollection.deserialize(value.serialize()) | ||
return value | ||
|
||
|
||
@flax_module | ||
class EnergyFittingNet(EnergyFittingNetDP): | ||
def __setattr__(self, name: str, value: Any) -> None: | ||
value = setattr_for_general_fitting(name, value) | ||
return super().__setattr__(name, value) |
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 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later |
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,38 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Any, | ||
) | ||
|
||
from deepmd.dpmodel.fitting.ener_fitting import EnergyFittingNet as EnergyFittingNetDP | ||
|
||
from ..common import ( | ||
to_array_api_strict_array, | ||
) | ||
from ..utils.exclude_mask import ( | ||
AtomExcludeMask, | ||
) | ||
from ..utils.network import ( | ||
NetworkCollection, | ||
) | ||
|
||
|
||
def setattr_for_general_fitting(name: str, value: Any) -> Any: | ||
if name in { | ||
"bias_atom_e", | ||
"fparam_avg", | ||
"fparam_inv_std", | ||
"aparam_avg", | ||
"aparam_inv_std", | ||
}: | ||
value = to_array_api_strict_array(value) | ||
elif name == "emask": | ||
value = AtomExcludeMask(value.ntypes, value.exclude_types) | ||
elif name == "nets": | ||
value = NetworkCollection.deserialize(value.serialize()) | ||
return value | ||
|
||
|
||
class EnergyFittingNet(EnergyFittingNetDP): | ||
def __setattr__(self, name: str, value: Any) -> None: | ||
value = setattr_for_general_fitting(name, value) | ||
return super().__setattr__(name, value) |
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