-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor DeepPot and support AutoBatchSize
Signed-off-by: Jinzhe Zeng <[email protected]>
- Loading branch information
Showing
4 changed files
with
215 additions
and
126 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,26 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import torch | ||
|
||
from deepmd_utils.utils.batch_size import AutoBatchSize as AutoBatchSizeBase | ||
|
||
|
||
class AutoBatchSize(AutoBatchSizeBase): | ||
def is_gpu_available(self) -> bool: | ||
"""Check if GPU is available. | ||
Returns | ||
------- | ||
bool | ||
True if GPU is available | ||
""" | ||
return torch.cuda.is_available() | ||
|
||
def is_oom_error(self, e: Exception) -> bool: | ||
"""Check if the exception is an OOM error. | ||
Parameters | ||
---------- | ||
e : Exception | ||
Exception | ||
""" | ||
return isinstance(e, RuntimeError) and "CUDA out of memory." in e.args[0] |
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,43 @@ | ||
from copy import deepcopy | ||
import json | ||
import unittest | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from deepmd_pt.entrypoints.main import get_trainer | ||
from deepmd_pt.infer.deep_eval import DeepPot | ||
|
||
|
||
class TestDeepPot(unittest.TestCase): | ||
def setUp(self): | ||
input_json = str(Path(__file__).parent / "water/se_atten.json") | ||
with open(input_json, "r") as f: | ||
self.config = json.load(f) | ||
self.config["training"]["numb_steps"] = 1 | ||
self.config["training"]["save_freq"] = 1 | ||
self.config["training"]["training_data"]["systems"] = [str(Path(__file__).parent / "water/data/single")] | ||
self.config["training"]["validation_data"]["systems"] = [str(Path(__file__).parent / "water/data/single")] | ||
self.input_json = "test_dp_test.json" | ||
with open(self.input_json, "w") as fp: | ||
json.dump(self.config, fp, indent=4) | ||
|
||
trainer = get_trainer(deepcopy(self.config)) | ||
trainer.run() | ||
|
||
input_dict, label_dict, _ = trainer.get_data(is_train=False) | ||
trainer.wrapper(**input_dict, label=label_dict, cur_lr=1.0) | ||
self.model = Path(__file__).parent / "model.pt" | ||
|
||
def test_dp_test(self): | ||
dp = DeepPot(str(self.model)) | ||
cell = np.array([ | ||
5.122106549439247480e+00,4.016537340154059388e-01,6.951654033828678081e-01, | ||
4.016537340154059388e-01,6.112136112297989143e+00,8.178091365465004481e-01, | ||
6.951654033828678081e-01,8.178091365465004481e-01,6.159552512682983760e+00, | ||
]).reshape(1, 3, 3) | ||
coord = np.array([ | ||
2.978060152121375648e+00,3.588469695887098077e+00,2.792459820604495491e+00,3.895592322591093115e+00,2.712091020667753760e+00,1.366836847133650501e+00,9.955616170888935690e-01,4.121324820711413039e+00,1.817239061889086571e+00,3.553661462345699906e+00,5.313046969500791583e+00,6.635182659098815883e+00,6.088601018589653080e+00,6.575011420004332585e+00,6.825240650611076099e+00 | ||
]).reshape(1, -1, 3) | ||
atype = np.array([0, 0, 0, 1, 1]).reshape(1, -1) | ||
|
||
e, f, v, ae, av = dp.eval(coord, cell, atype, atomic=True) |