Skip to content

Commit 5843c02

Browse files
authored
move AutoBatchSize from dp test to DeepPot.eval (#1173)
* move AutoBatchSize from dp test to dp.eval Bugfix. * Revert "Bug fix of memory overflow when calculating model deviation (#1153)" This reverts commit d8acbb8. * reshape coords before get shape * use the same AutoBatchSize for all models in model deviation
1 parent 4e0002d commit 5843c02

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

deepmd/entrypoints/test.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from deepmd.utils import random as dp_random
1010
from deepmd.utils.data import DeepmdData
1111
from deepmd.utils.weight_avg import weighted_average
12-
from deepmd.utils.batch_size import AutoBatchSize
1312

1413
if TYPE_CHECKING:
1514
from deepmd.infer import DeepDipole, DeepPolar, DeepPot, DeepWFC
@@ -70,7 +69,6 @@ def test(
7069

7170
# init model
7271
dp = DeepPotential(model)
73-
auto_batch_size = AutoBatchSize()
7472

7573
for cc, system in enumerate(all_sys):
7674
log.info("# ---------------output of dp test--------------- ")
@@ -84,7 +82,6 @@ def test(
8482
err = test_ener(
8583
dp,
8684
data,
87-
auto_batch_size,
8885
system,
8986
numb_test,
9087
detail_file,
@@ -162,7 +159,6 @@ def save_txt_file(
162159
def test_ener(
163160
dp: "DeepPot",
164161
data: DeepmdData,
165-
auto_batch_size: AutoBatchSize,
166162
system: str,
167163
numb_test: int,
168164
detail_file: Optional[str],
@@ -230,10 +226,7 @@ def test_ener(
230226
else:
231227
aparam = None
232228

233-
ret = auto_batch_size.execute_all(
234-
dp.eval,
235-
numb_test,
236-
natoms,
229+
ret = dp.eval(
237230
coord,
238231
box,
239232
atype,

deepmd/infer/deep_eval.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
import os
2-
from typing import List, Optional, TYPE_CHECKING
2+
from typing import List, Optional, TYPE_CHECKING, Union
33

44
import numpy as np
55
from deepmd.common import make_default_mesh
66
from deepmd.env import default_tf_session_config, tf, MODEL_VERSION
77
from deepmd.utils.sess import run_sess
8+
from deepmd.utils.batch_size import AutoBatchSize
89

910
if TYPE_CHECKING:
1011
from pathlib import Path
1112

1213

1314
class DeepEval:
14-
"""Common methods for DeepPot, DeepWFC, DeepPolar, ..."""
15+
"""Common methods for DeepPot, DeepWFC, DeepPolar, ...
16+
17+
Parameters
18+
----------
19+
model_file : Path
20+
The name of the frozen model file.
21+
load_prefix: str
22+
The prefix in the load computational graph
23+
default_tf_graph : bool
24+
If uses the default tf graph, otherwise build a new tf graph for evaluation
25+
auto_batch_size : bool or int or AutomaticBatchSize, default: False
26+
If True, automatic batch size will be used. If int, it will be used
27+
as the initial batch size.
28+
"""
1529

1630
_model_type: Optional[str] = None
1731
_model_version: Optional[str] = None
@@ -21,7 +35,8 @@ def __init__(
2135
self,
2236
model_file: "Path",
2337
load_prefix: str = "load",
24-
default_tf_graph: bool = False
38+
default_tf_graph: bool = False,
39+
auto_batch_size: Union[bool, int, AutoBatchSize] = False,
2540
):
2641
self.graph = self._load_graph(
2742
model_file, prefix=load_prefix, default_tf_graph=default_tf_graph
@@ -34,6 +49,19 @@ def __init__(
3449
f"model in graph (version {self.model_version}) is incompatible"
3550
f"with the model (version {MODEL_VERSION}) supported by the current code."
3651
)
52+
53+
# set default to False, as subclasses may not support
54+
if isinstance(auto_batch_size, bool):
55+
if auto_batch_size:
56+
self.auto_batch_size = AutoBatchSize()
57+
else:
58+
self.auto_batch_size = None
59+
elif isinstance(auto_batch_size, int):
60+
self.auto_batch_size = AutoBatchSize(auto_batch_size)
61+
elif isinstance(auto_batch_size, AutoBatchSize):
62+
self.auto_batch_size = auto_batch_size
63+
else:
64+
raise TypeError("auto_batch_size should be bool, int, or AutoBatchSize")
3765

3866
@property
3967
def model_type(self) -> str:

deepmd/infer/deep_pot.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import logging
2-
from typing import TYPE_CHECKING, List, Optional, Tuple
2+
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
33

44
import numpy as np
55
from deepmd.common import make_default_mesh
66
from deepmd.env import default_tf_session_config, tf
77
from deepmd.infer.data_modifier import DipoleChargeModifier
88
from deepmd.infer.deep_eval import DeepEval
99
from deepmd.utils.sess import run_sess
10+
from deepmd.utils.batch_size import AutoBatchSize
1011

1112
if TYPE_CHECKING:
1213
from pathlib import Path
@@ -25,6 +26,9 @@ class DeepPot(DeepEval):
2526
The prefix in the load computational graph
2627
default_tf_graph : bool
2728
If uses the default tf graph, otherwise build a new tf graph for evaluation
29+
auto_batch_size : bool or int or AutomaticBatchSize, default: True
30+
If True, automatic batch size will be used. If int, it will be used
31+
as the initial batch size.
2832
2933
Examples
3034
--------
@@ -49,7 +53,8 @@ def __init__(
4953
self,
5054
model_file: "Path",
5155
load_prefix: str = "load",
52-
default_tf_graph: bool = False
56+
default_tf_graph: bool = False,
57+
auto_batch_size: Union[bool, int, AutoBatchSize] = True,
5358
) -> None:
5459

5560
# add these tensors on top of what is defined by DeepTensor Class
@@ -83,7 +88,8 @@ def __init__(
8388
self,
8489
model_file,
8590
load_prefix=load_prefix,
86-
default_tf_graph=default_tf_graph
91+
default_tf_graph=default_tf_graph,
92+
auto_batch_size=auto_batch_size,
8793
)
8894

8995
# load optional tensors
@@ -224,12 +230,23 @@ def eval(
224230
atom_virial
225231
The atomic virial. Only returned when atomic == True
226232
"""
233+
# reshape coords before getting shape
234+
natoms = len(atom_types)
235+
coords = np.reshape(np.array(coords), [-1, natoms * 3])
236+
numb_test = coords.shape[0]
227237
if atomic:
228238
if self.modifier_type is not None:
229239
raise RuntimeError('modifier does not support atomic modification')
240+
if self.auto_batch_size is not None:
241+
return self.auto_batch_size.execute_all(self._eval_inner, numb_test, natoms,
242+
coords, cells, atom_types, fparam = fparam, aparam = aparam, atomic = atomic, efield = efield)
230243
return self._eval_inner(coords, cells, atom_types, fparam = fparam, aparam = aparam, atomic = atomic, efield = efield)
231244
else :
232-
e, f, v = self._eval_inner(coords, cells, atom_types, fparam = fparam, aparam = aparam, atomic = atomic, efield = efield)
245+
if self.auto_batch_size is not None:
246+
e, f, v = self.auto_batch_size.execute_all(self._eval_inner, numb_test, natoms,
247+
coords, cells, atom_types, fparam = fparam, aparam = aparam, atomic = atomic, efield = efield)
248+
else:
249+
e, f, v = self._eval_inner(coords, cells, atom_types, fparam = fparam, aparam = aparam, atomic = atomic, efield = efield)
233250
if self.modifier_type is not None:
234251
me, mf, mv = self.dm.eval(coords, cells, atom_types)
235252
e += me.reshape(e.shape)

deepmd/infer/model_devi.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from .deep_pot import DeepPot
33
from ..utils.data import DeepmdData
4+
from ..utils.batch_size import AutoBatchSize
45

56

67
def calc_model_devi_f(fs: np.ndarray):
@@ -174,8 +175,9 @@ def make_model_devi(
174175
in a trajectory by a MD engine (such as Gromacs / Lammps).
175176
This paramter is used to determine the index in the output file.
176177
'''
178+
auto_batch_size = AutoBatchSize()
177179
# init models
178-
dp_models = [DeepPot(model) for model in models]
180+
dp_models = [DeepPot(model, auto_batch_size=auto_batch_size) for model in models]
179181

180182
# check type maps
181183
tmaps = [dp.get_type_map() for dp in dp_models]
@@ -195,13 +197,12 @@ def make_model_devi(
195197
nframes_tot = 0
196198
devis = []
197199
for data in data_sets:
198-
coords = data["coord"]
199-
boxs = data["box"]
200-
atypes = data["type"]
201-
for coord, box, atype in zip(coords, boxs, atypes):
202-
devi = calc_model_devi(np.array([coord]), np.array([box]), atype, dp_models, nopbc=nopbc)
203-
nframes_tot += 1
204-
devis.append(devi)
200+
coord = data["coord"]
201+
box = data["box"]
202+
atype = data["type"][0]
203+
devi = calc_model_devi(coord, box, atype, dp_models, nopbc=nopbc)
204+
nframes_tot += coord.shape[0]
205+
devis.append(devi)
205206
devis = np.vstack(devis)
206207
devis[:, 0] = np.arange(nframes_tot) * frequency
207208
write_model_devi_out(devis, output)

deepmd/utils/batch_size.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def execute(self, callable: Callable, start_index: int, natoms: int) -> Tuple[in
7979
n_tot = n_batch * natoms
8080
self.maximum_working_batch_size = max(self.maximum_working_batch_size, n_tot)
8181
# adjust the next batch size
82-
if n_tot >= self.current_batch_size and self.current_batch_size * self.factor < self.minimal_not_working_batch_size:
82+
if n_tot + natoms > self.current_batch_size and self.current_batch_size * self.factor < self.minimal_not_working_batch_size:
8383
self._adjust_batch_size(self.factor)
8484
return n_batch, result
8585

0 commit comments

Comments
 (0)