Skip to content

Commit 409aa58

Browse files
authored
Merge branch 'master' into 0304_add_chg_spin
2 parents 7a96536 + 73bb1b7 commit 409aa58

9 files changed

Lines changed: 768 additions & 11 deletions

File tree

.github/workflows/build_wheel.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
CUDA_VERSION: ${{ matrix.cuda_version }}
6969
DP_PKG_NAME: ${{ matrix.dp_pkg_name }}
7070
CIBW_BUILD_FRONTEND: "build[uv]"
71-
- uses: actions/upload-artifact@v6
71+
- uses: actions/upload-artifact@v7
7272
with:
7373
name: cibw-cp${{ matrix.python }}-${{ matrix.platform_id }}-cu${{ matrix.cuda_version }}-${{ strategy.job-index }}
7474
path: ./wheelhouse/*.whl
@@ -82,7 +82,7 @@ jobs:
8282
- name: Build sdist
8383
run: pipx run uv tool run --with build[uv] --from build python -m build --installer uv --sdist
8484

85-
- uses: actions/upload-artifact@v6
85+
- uses: actions/upload-artifact@v7
8686
with:
8787
name: cibw-sdist
8888
path: dist/*.tar.gz
@@ -95,7 +95,7 @@ jobs:
9595
id-token: write
9696
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
9797
steps:
98-
- uses: actions/download-artifact@v7
98+
- uses: actions/download-artifact@v8
9999
with:
100100
pattern: cibw-*
101101
path: dist
@@ -124,13 +124,13 @@ jobs:
124124
swap-storage: true
125125
docker-images: true
126126
- uses: actions/checkout@v6
127-
- uses: actions/download-artifact@v7
127+
- uses: actions/download-artifact@v8
128128
with:
129129
path: source/install/docker/dist
130130
pattern: cibw-*-manylinux_x86_64-cu${{ matrix.cuda_version }}*
131131
merge-multiple: true
132132
- name: Log in to the Container registry
133-
uses: docker/login-action@v3
133+
uses: docker/login-action@v4
134134
with:
135135
registry: ghcr.io
136136
username: ${{ github.actor }}
@@ -157,7 +157,7 @@ jobs:
157157
needs: [build_wheels, build_sdist]
158158
runs-on: ubuntu-latest
159159
steps:
160-
- uses: actions/download-artifact@v7
160+
- uses: actions/download-artifact@v8
161161
with:
162162
path: dist/packages
163163
pattern: cibw-*

.github/workflows/package_c.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
if: matrix.filename != 'libdeepmd_c.tar.gz'
4848
# for download and debug
4949
- name: Upload artifact
50-
uses: actions/upload-artifact@v6
50+
uses: actions/upload-artifact@v7
5151
with:
5252
name: libdeepmd_c-${{ strategy.job-index }}-${{ matrix.filename }}
5353
path: ${{ matrix.filename }}
@@ -65,7 +65,7 @@ jobs:
6565
steps:
6666
- uses: actions/checkout@v6
6767
- name: Download artifact
68-
uses: actions/download-artifact@v7
68+
uses: actions/download-artifact@v8
6969
with:
7070
pattern: libdeepmd_c-*
7171
merge-multiple: true

.github/workflows/test_python.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
if: matrix.group == 1
7272
- run: mv .test_durations .test_durations_${{ matrix.group }}
7373
- name: Upload partial durations
74-
uses: actions/upload-artifact@v6
74+
uses: actions/upload-artifact@v7
7575
with:
7676
name: split-${{ matrix.python }}-${{ matrix.group }}
7777
path: .test_durations_${{ matrix.group }}
@@ -100,7 +100,7 @@ jobs:
100100
key: test2-durations-combined-${{ matrix.python }}-${{ github.sha }}
101101
restore-keys: test2-durations-combined-${{ matrix.python }}
102102
- name: Download artifacts
103-
uses: actions/download-artifact@v7
103+
uses: actions/download-artifact@v8
104104
with:
105105
pattern: split-${{ matrix.python }}-*
106106
merge-multiple: true
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
import copy
3+
from typing import (
4+
Any,
5+
)
6+
7+
from deepmd.dpmodel.output_def import (
8+
FittingOutputDef,
9+
)
10+
11+
12+
def make_hessian_model(T_Model: type) -> type:
13+
"""Make a model that can compute Hessian.
14+
15+
With the JAX-mirrored approach, hessian is computed in
16+
``forward_common_atomic`` (in make_model.py) on extended coordinates.
17+
This wrapper only needs to override ``atomic_output_def()`` to set
18+
``r_hessian=True``, and ``communicate_extended_output`` in dpmodel
19+
naturally maps it from nall to nloc.
20+
21+
Parameters
22+
----------
23+
T_Model
24+
The model. Should provide the ``atomic_output_def`` method.
25+
26+
Returns
27+
-------
28+
The model that computes hessian.
29+
30+
"""
31+
32+
class CM(T_Model):
33+
def __init__(
34+
self,
35+
*args: Any,
36+
**kwargs: Any,
37+
) -> None:
38+
super().__init__(
39+
*args,
40+
**kwargs,
41+
)
42+
self.hess_fitting_def = copy.deepcopy(super().atomic_output_def())
43+
44+
def requires_hessian(
45+
self,
46+
keys: str | list[str],
47+
) -> None:
48+
"""Set which output variable(s) requires hessian."""
49+
if isinstance(keys, str):
50+
keys = [keys]
51+
for kk in self.hess_fitting_def.keys():
52+
if kk in keys:
53+
self.hess_fitting_def[kk].r_hessian = True
54+
55+
def atomic_output_def(self) -> FittingOutputDef:
56+
"""Get the fitting output def."""
57+
return self.hess_fitting_def
58+
59+
return CM

deepmd/pt_expt/model/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
from deepmd.dpmodel.model.make_hessian_model import (
3+
make_hessian_model,
4+
)
5+
26
from .dipole_model import (
37
DipoleModel,
48
)
@@ -33,4 +37,5 @@
3337
"PolarModel",
3438
"PropertyModel",
3539
"get_model",
40+
"make_hessian_model",
3641
]

deepmd/pt_expt/model/ener_model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2+
import copy
23
from typing import (
34
Any,
45
)
@@ -14,6 +15,9 @@
1415
from deepmd.dpmodel.model.dp_model import (
1516
DPModelCommon,
1617
)
18+
from deepmd.dpmodel.model.make_hessian_model import (
19+
make_hessian_model,
20+
)
1721

1822
from .make_model import (
1923
make_model,
@@ -34,6 +38,17 @@ def __init__(
3438
) -> None:
3539
DPModelCommon.__init__(self)
3640
DPEnergyModel_.__init__(self, *args, **kwargs)
41+
self._hessian_enabled = False
42+
43+
def enable_hessian(self) -> None:
44+
if self._hessian_enabled:
45+
return
46+
self.__class__ = make_hessian_model(type(self))
47+
self.hess_fitting_def = copy.deepcopy(
48+
super(type(self), self).atomic_output_def()
49+
)
50+
self.requires_hessian("energy")
51+
self._hessian_enabled = True
3752

3853
def forward(
3954
self,
@@ -63,6 +78,8 @@ def forward(
6378
model_predict["atom_virial"] = model_ret["energy_derv_c"].squeeze(-2)
6479
if "mask" in model_ret:
6580
model_predict["mask"] = model_ret["mask"]
81+
if self.atomic_output_def()["energy"].r_hessian:
82+
model_predict["hessian"] = model_ret["energy_derv_r_derv_r"].squeeze(-3)
6683
return model_predict
6784

6885
def forward_lower(
@@ -115,6 +132,8 @@ def translated_output_def(self) -> dict[str, Any]:
115132
output_def["atom_virial"].squeeze(-2)
116133
if "mask" in out_def_data:
117134
output_def["mask"] = out_def_data["mask"]
135+
if self.atomic_output_def()["energy"].r_hessian:
136+
output_def["hessian"] = out_def_data["energy_derv_r_derv_r"]
118137
return output_def
119138

120139
def forward_lower_exportable(

0 commit comments

Comments
 (0)