-
Notifications
You must be signed in to change notification settings - Fork 608
feat: Implement XAS (X-ray Absorption Spectroscopy) model, fitting, l… #5337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
anyangml
wants to merge
23
commits into
deepmodeling:master
Choose a base branch
from
anyangml:feat/support-xas-spectrum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
85fe716
feat: Implement XAS (X-ray Absorption Spectroscopy) model, fitting, l…
anyangml 9e9c6a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8fd99ad
feat: Reimplement XAS loss with per-atom property fitting, removing p…
anyangml 9352c4f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9bc38d7
feat: Add X-ray Absorption Spectroscopy (XAS) training examples
anyangml c8a4005
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e157ed7
feat: Implement XAS energy normalization in the XAS loss function and…
anyangml 8c21612
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 250168b
fix:device
anyangml 8ab20b2
fix: filter loss-related keys from state dict in inference and ignore…
anyangml 38c3a04
fix: update XAS reference extraction path and ignore tests directory …
anyangml 17ffd5b
feat: add weighted loss and smoothness regularization to XAS training…
anyangml 829048e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f81f2a7
feat: add normalize_fparam option to fitting net and ignore tests dir…
anyangml 3161398
chore: ignore tests directory in git tracking
anyangml ed8a87c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 73398f6
feat: add intensity_norm option to XAS loss for scale-invariant train…
anyangml eaae746
Merge branch 'feat/support-xas-spectrum' of github.com:anyangml/deepm…
anyangml a663c33
feat: add per-type/edge energy standard deviation normalization to XA…
anyangml f2d37ed
refactor: normalize energy predictions using global standard deviatio…
anyangml da895d0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 94d2a5a
fix: change XAS loss reduction from mean to sum for atomic contributions
anyangml 5f15806
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| # Auto-generated stub for development use | ||
| __version__ = "dev" |
This file contains hidden or 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 hidden or 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,81 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from typing import ( | ||
| Any, | ||
| ) | ||
|
|
||
| from deepmd.dpmodel.descriptor.base_descriptor import ( | ||
| BaseDescriptor, | ||
| ) | ||
| from deepmd.dpmodel.fitting.base_fitting import ( | ||
| BaseFitting, | ||
| ) | ||
| from deepmd.dpmodel.fitting.xas_fitting import ( | ||
| XASFittingNet, | ||
| ) | ||
|
|
||
| from .dp_atomic_model import ( | ||
| DPAtomicModel, | ||
| ) | ||
|
|
||
|
|
||
| class DPXASAtomicModel(DPAtomicModel): | ||
| """Atomic model for XAS spectrum fitting. | ||
|
|
||
| Automatically sets ``atom_exclude_types`` to all non-absorbing atom types | ||
| so that the intensive mean reduction in ``fit_output_to_model_output`` | ||
| computes the mean XAS over absorbing atoms only. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| descriptor : BaseDescriptor | ||
| fitting : BaseFitting | ||
| Must be an instance of XASFittingNet. | ||
| type_map : list[str] | ||
| Mapping from type index to element symbol. | ||
| absorbing_type : str | ||
| Element symbol of the absorbing atom type (e.g. "Fe"). | ||
| **kwargs | ||
| Passed to DPAtomicModel. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| descriptor: BaseDescriptor, | ||
| fitting: BaseFitting, | ||
| type_map: list[str], | ||
| absorbing_type: str, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| if not isinstance(fitting, XASFittingNet): | ||
| raise TypeError( | ||
| "fitting must be an instance of XASFittingNet for DPXASAtomicModel" | ||
| ) | ||
| if absorbing_type not in type_map: | ||
| raise ValueError( | ||
| f"absorbing_type '{absorbing_type}' not found in type_map {type_map}" | ||
| ) | ||
| self.absorbing_type = absorbing_type | ||
| absorbing_idx = type_map.index(absorbing_type) | ||
| # Exclude all types except the absorbing type so the intensive mean | ||
| # reduction is computed only over absorbing atoms. | ||
| atom_exclude_types = [i for i in range(len(type_map)) if i != absorbing_idx] | ||
| kwargs["atom_exclude_types"] = atom_exclude_types | ||
| super().__init__(descriptor, fitting, type_map, **kwargs) | ||
|
|
||
| def get_intensive(self) -> bool: | ||
| """XAS is an intensive property (mean over absorbing atoms).""" | ||
| return True | ||
|
|
||
| def serialize(self) -> dict: | ||
| dd = super().serialize() | ||
| dd["absorbing_type"] = self.absorbing_type | ||
| return dd | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> "DPXASAtomicModel": | ||
| data = data.copy() | ||
| absorbing_type = data.pop("absorbing_type") | ||
| # atom_exclude_types is already stored by base; rebuild absorbing_type param | ||
| obj = super().deserialize(data) | ||
| obj.absorbing_type = absorbing_type | ||
| return obj | ||
This file contains hidden or 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 hidden or 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,162 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.dpmodel.array_api import ( | ||
| Array, | ||
| ) | ||
| from deepmd.dpmodel.common import ( | ||
| DEFAULT_PRECISION, | ||
| to_numpy_array, | ||
| ) | ||
| from deepmd.dpmodel.fitting.invar_fitting import ( | ||
| InvarFitting, | ||
| ) | ||
| from deepmd.dpmodel.output_def import ( | ||
| FittingOutputDef, | ||
| OutputVariableDef, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from deepmd.dpmodel.fitting.general_fitting import ( | ||
| GeneralFitting, | ||
| ) | ||
|
|
||
| from deepmd.utils.version import ( | ||
| check_version_compatibility, | ||
| ) | ||
|
|
||
|
|
||
| @InvarFitting.register("xas") | ||
| class XASFittingNet(InvarFitting): | ||
| """Fitting network for X-ray Absorption Spectroscopy (XAS) spectra. | ||
|
|
||
| Predicts per-atom XAS contributions in a relative energy (ΔE) space. | ||
| The global XAS is the mean over all absorbing atoms, handled by the | ||
| XAS model via ``intensive=True`` and type-selective masking. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ntypes : int | ||
| Number of atom types. | ||
| dim_descrpt : int | ||
| Dimension of the descriptor. | ||
| numb_xas : int | ||
| Number of XAS energy grid points. | ||
| neuron : list[int] | ||
| Hidden layer sizes of the fitting network. | ||
| resnet_dt : bool | ||
| Whether to use residual network with time step. | ||
| numb_fparam : int | ||
| Dimension of frame parameters (e.g. edge type encoding). | ||
| numb_aparam : int | ||
| Dimension of atomic parameters. | ||
| dim_case_embd : int | ||
| Dimension of case embedding. | ||
| bias_xas : Array or None | ||
| Initial bias for XAS output, shape (ntypes, numb_xas). | ||
| rcond : float or None | ||
| Cutoff for small singular values. | ||
| trainable : bool or list[bool] | ||
| Whether the fitting parameters are trainable. | ||
| activation_function : str | ||
| Activation function for hidden layers. | ||
| precision : str | ||
| Precision for the fitting parameters. | ||
| mixed_types : bool | ||
| Whether to use a shared network for all atom types. | ||
| exclude_types : list[int] | ||
| Atom types to exclude from fitting (set automatically by XASAtomicModel). | ||
| type_map : list[str] or None | ||
| Mapping from type index to element symbol. | ||
| seed : int, list[int], or None | ||
| Random seed. | ||
| default_fparam : list or None | ||
| Default frame parameter values. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| ntypes: int, | ||
| dim_descrpt: int, | ||
| numb_xas: int = 500, | ||
| neuron: list[int] = [120, 120, 120], | ||
| resnet_dt: bool = True, | ||
| numb_fparam: int = 0, | ||
| numb_aparam: int = 0, | ||
| dim_case_embd: int = 0, | ||
| bias_xas: Array | None = None, | ||
| rcond: float | None = None, | ||
| trainable: bool | list[bool] = True, | ||
| activation_function: str = "tanh", | ||
| precision: str = DEFAULT_PRECISION, | ||
| mixed_types: bool = False, | ||
| exclude_types: list[int] = [], | ||
| type_map: list[str] | None = None, | ||
| seed: int | list[int] | None = None, | ||
| default_fparam: list | None = None, | ||
| ) -> None: | ||
| if bias_xas is not None: | ||
| self.bias_xas = bias_xas | ||
| else: | ||
| self.bias_xas = np.zeros((ntypes, numb_xas), dtype=DEFAULT_PRECISION) | ||
| super().__init__( | ||
| var_name="xas", | ||
| ntypes=ntypes, | ||
| dim_descrpt=dim_descrpt, | ||
| dim_out=numb_xas, | ||
| neuron=neuron, | ||
| resnet_dt=resnet_dt, | ||
| bias_atom=bias_xas, | ||
| numb_fparam=numb_fparam, | ||
| numb_aparam=numb_aparam, | ||
| dim_case_embd=dim_case_embd, | ||
| rcond=rcond, | ||
| trainable=trainable, | ||
| activation_function=activation_function, | ||
| precision=precision, | ||
| mixed_types=mixed_types, | ||
| exclude_types=exclude_types, | ||
| type_map=type_map, | ||
| seed=seed, | ||
| default_fparam=default_fparam, | ||
| ) | ||
anyangml marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def output_def(self) -> FittingOutputDef: | ||
| return FittingOutputDef( | ||
| [ | ||
| OutputVariableDef( | ||
| self.var_name, | ||
| [self.dim_out], | ||
| reducible=True, | ||
| intensive=True, | ||
| r_differentiable=False, | ||
| c_differentiable=False, | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> "GeneralFitting": | ||
| data = data.copy() | ||
| check_version_compatibility(data.pop("@version", 1), 4, 1) | ||
| data["numb_xas"] = data.pop("dim_out") | ||
| data.pop("tot_ener_zero", None) | ||
| data.pop("var_name", None) | ||
| data.pop("layer_name", None) | ||
| data.pop("use_aparam_as_mask", None) | ||
| data.pop("spin", None) | ||
| data.pop("atom_ener", None) | ||
| return super().deserialize(data) | ||
|
|
||
| def serialize(self) -> dict: | ||
| """Serialize the fitting to dict.""" | ||
| dd = { | ||
| **super().serialize(), | ||
| "type": "xas", | ||
| } | ||
| dd["@variables"]["bias_atom_e"] = to_numpy_array(self.bias_atom_e) | ||
| return dd | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.