Skip to content

Commit

Permalink
Merge pull request #1 from deepmodeling/api
Browse files Browse the repository at this point in the history
api update
  • Loading branch information
iProzd authored Mar 27, 2021
2 parents 8b52a42 + cb8f04f commit d89bfea
Show file tree
Hide file tree
Showing 156 changed files with 1,960 additions and 1,352 deletions.
2 changes: 2 additions & 0 deletions deepmd/entrypoints/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _make_node_names(model_type: str, modifier_type: Optional[str] = None) -> Li
"descrpt_attr/ntypes",
"model_attr/tmap",
"model_attr/model_type",
"model_attr/model_version",
]

if model_type == "ener":
Expand All @@ -59,6 +60,7 @@ def _make_node_names(model_type: str, modifier_type: Optional[str] = None) -> Li
nodes += [
"o_wfc",
"model_attr/sel_type",
"model_attr/output_dim",
]
elif model_type == "dipole":
nodes += [
Expand Down
34 changes: 17 additions & 17 deletions deepmd/entrypoints/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ def test_ener(

# print ("# energies: %s" % energy)
log.info(f"# number of test data : {numb_test:d} ")
log.info(f"Energy L2err : {l2e:e} eV")
log.info(f"Energy L2err/Natoms : {l2ea:e} eV")
log.info(f"Force L2err : {l2f:e} eV/A")
log.info(f"Virial L2err : {l2v:e} eV")
log.info(f"Virial L2err/Natoms : {l2va:e} eV")
log.info(f"Energy RMSE : {l2e:e} eV")
log.info(f"Energy RMSE/Natoms : {l2ea:e} eV")
log.info(f"Force RMSE : {l2f:e} eV/A")
log.info(f"Virial RMSE : {l2v:e} eV")
log.info(f"Virial RMSE/Natoms : {l2va:e} eV")
if has_atom_ener:
log.info(f"Atomic ener L2err : {l2ae:e} eV")
log.info(f"Atomic ener RMSE : {l2ae:e} eV")

if detail_file is not None:
detail_path = Path(detail_file)
Expand Down Expand Up @@ -355,9 +355,9 @@ def print_ener_sys_avg(avg: np.ndarray):
avg : np.ndarray
array with summaries
"""
log.info(f"Energy L2err/Natoms : {avg[0]:e} eV")
log.info(f"Force L2err : {avg[1]:e} eV/A")
log.info(f"Virial L2err/Natoms : {avg[2]:e} eV")
log.info(f"Energy RMSE/Natoms : {avg[0]:e} eV")
log.info(f"Force RMSE : {avg[1]:e} eV/A")
log.info(f"Virial RMSE/Natoms : {avg[2]:e} eV")


def run_test(dp: "DeepTensor", test_data: dict, numb_test: int):
Expand Down Expand Up @@ -420,7 +420,7 @@ def test_wfc(
l2f = l2err(wfc - test_data["wfc"][:numb_test])

log.info("# number of test data : {numb_test:d} ")
log.info("WFC L2err : {l2f:e} eV/A")
log.info("WFC RMSE : {l2f:e} eV/A")

if detail_file is not None:
detail_path = Path(detail_file)
Expand All @@ -447,7 +447,7 @@ def print_wfc_sys_avg(avg):
avg : np.ndarray
array with summaries
"""
log.info(f"WFC L2err : {avg[0]:e} eV/A")
log.info(f"WFC RMSE : {avg[0]:e} eV/A")


def test_polar(
Expand Down Expand Up @@ -509,10 +509,10 @@ def test_polar(
l2fa = l2f / sel_natoms

log.info(f"# number of test data : {numb_test:d} ")
log.info(f"Polarizability L2err : {l2f:e} eV/A")
log.info(f"Polarizability RMSE : {l2f:e} eV/A")
if global_polar:
log.info(f"Polarizability L2err/sqrtN : {l2fs:e} eV/A")
log.info(f"Polarizability L2err/N : {l2fa:e} eV/A")
log.info(f"Polarizability RMSE/sqrtN : {l2fs:e} eV/A")
log.info(f"Polarizability RMSE/N : {l2fa:e} eV/A")

if detail_file is not None:
detail_path = Path(detail_file)
Expand Down Expand Up @@ -542,7 +542,7 @@ def print_polar_sys_avg(avg):
avg : np.ndarray
array with summaries
"""
log.info(f"Polarizability L2err : {avg[0]:e} eV/A")
log.info(f"Polarizability RMSE : {avg[0]:e} eV/A")


def test_dipole(
Expand Down Expand Up @@ -577,7 +577,7 @@ def test_dipole(
l2f = l2err(dipole - test_data["dipole"][:numb_test])

log.info(f"# number of test data : {numb_test:d}")
log.info(f"Dipole L2err : {l2f:e} eV/A")
log.info(f"Dipole RMSE : {l2f:e} eV/A")

if detail_file is not None:
detail_path = Path(detail_file)
Expand Down Expand Up @@ -605,4 +605,4 @@ def print_dipole_sys_avg(avg):
avg : np.ndarray
array with summaries
"""
log.info(f"Dipole L2err : {avg[0]:e} eV/A")
log.info(f"Dipole RMSE : {avg[0]:e} eV/A")
30 changes: 12 additions & 18 deletions deepmd/entrypoints/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def transfer(*, old_model: str, raw_model: str, output: str, **kwargs):
new_graph_def = transform_graph(raw_graph, old_graph)
with tf.gfile.GFile(output, mode="wb") as f:
f.write(new_graph_def.SerializeToString())
log.info("the output model is saved in {output:s}")
log.info("the output model is saved in " + output)


def load_graph(graph_name: str) -> tf.Graph:
Expand Down Expand Up @@ -136,37 +136,31 @@ def transform_graph(raw_graph: tf.Graph, old_graph: tf.Graph) -> tf.Graph:
if raw_graph_dtype == np.float16:
if old_graph_dtype == np.float64 or old_graph_dtype == np.float32:
if (len(tensor_shape) != 1) or (tensor_shape[0] != 1):
tensor = np.frombuffer(old_node.tensor_content, dtype=np.float16)
cp_attr.from_array(tensor, tf.float16, shape=tensor_shape)
tensor = np.frombuffer(old_node.tensor_content).astype(raw_graph_dtype)
cp_attr.from_array(tensor, tf.float16, shape = tensor_shape)
else:
tensor = load_tensor(old_node, old_graph_dtype, np.float16)
tensor = load_tensor(old_node, old_graph_dtype, raw_graph_dtype)
cp_attr.from_array(tensor, tf.float16, [1])

elif old_graph_dtype == np.float16:
tensor = convert_matrix(np.array(old_node.half_val), tensor_shape)
cp_attr.from_array(tensor, tf.float16)
elif old_graph_dtype[1] == "float16":
tensor = convertMatrix(np.array(old_node.half_val), tensor_shape)
cp_attr.from_array(tensor, raw_graph_dtype)

elif raw_graph_dtype == np.float64 or raw_graph_dtype == np.float32:
if old_graph_dtype == np.float64 or old_graph_dtype == np.float32:
if (len(tensor_shape) != 1) or (tensor_shape[0] != 1):
tensor = np.frombuffer(
old_node.tensor_content, dtype=raw_graph_dtype
)
tensor = np.frombuffer(old_node.tensor_content).astype(raw_graph_dtype)
cp_attr.from_str(tensor)
else:
tensor = load_tensor(old_node, old_graph_dtype, raw_graph_dtype)
cp_attr.from_array(tensor, raw_graph_dtype, shape=[1])

elif old_graph_dtype == np.float16:
if (len(tensor_shape) != 1) or (tensor_shape[0] != 1):
tensor = convert_matrix(
np.array(old_node.half_val), tensor_shape, dtype=raw_graph_dtype
)
tensor = convertMatrix(np.array(old_node.half_val), tensor_shape).astype(raw_graph_dtype)
cp_attr.from_str(tensor)
else:
tensor = convert_matrix(
np.array(old_node.half_val), tensor_shape, dtype=raw_graph_dtype
)
tensor = convertMatrix(np.array(old_node.half_val), tensor_shape).astype(raw_graph_dtype)
cp_attr.from_array(tensor, raw_graph_dtype)

return raw_graph_def
Expand All @@ -191,9 +185,9 @@ def from_str(self, tensor: np.ndarray):

def load_tensor(node: tf.Tensor, dtype_old: type, dtype_new: type) -> np.ndarray:
if dtype_old == np.float64:
tensor = np.array(node.double_val, dtype=dtype_new)
tensor = np.array(node.double_val).astype(dtype_new)
elif dtype_old == np.float32:
tensor = np.array(node.float_val, dtype=dtype_new)
tensor = np.array(node.float_val).astype(dtype_new)

return tensor

Expand Down
6 changes: 2 additions & 4 deletions deepmd/infer/deep_dipole.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING
from deepmd.infer.deep_tensor import DeepTensor

from deepmd.infer.deep_eval import DeepTensor
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -33,7 +33,6 @@ def __init__(
# instance namespace
self.tensors = dict(
{
"t_sel_type": "model_attr/sel_type:0",
# output tensor
"t_tensor": "o_dipole:0",
},
Expand All @@ -43,7 +42,6 @@ def __init__(
DeepTensor.__init__(
self,
model_file,
3,
load_prefix=load_prefix,
default_tf_graph=default_tf_graph,
)
Expand Down
Loading

0 comments on commit d89bfea

Please sign in to comment.