Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deepmd/descriptor/se_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def enable_compression(self,
self.compress = True
self.model_file = model_file
self.table_config = [table_extrapolate, table_stride_1, table_stride_2, check_frequency]
self.table = DeepTabulate(self.model_file, self.type_one_side)
self.table = DeepTabulate(self.model_file, self.type_one_side, self.exclude_types)
self.lower, self.upper \
= self.table.build(min_nbor_dist,
table_extrapolate,
Expand Down
4 changes: 3 additions & 1 deletion deepmd/entrypoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .train import train
from .transfer import transfer
from ..infer.model_devi import make_model_devi
from .convert import convert

__all__ = [
"config",
Expand All @@ -18,5 +19,6 @@
"transfer",
"compress",
"doc_train_input",
"make_model_devi"
"make_model_devi",
"convert",
]
16 changes: 8 additions & 8 deletions deepmd/entrypoints/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def compress(
input: str,
output: str,
extrapolate: int,
stride: float,
step: float,
frequency: str,
checkpoint_folder: str,
mpi_log: str,
Expand All @@ -34,9 +34,9 @@ def compress(
"""Compress model.

The table is composed of fifth-order polynomial coefficients and is assembled from
two sub-tables. The first table takes the stride(parameter) as it's uniform stride,
while the second table takes 10 * stride as it's uniform stride. The range of the
first table is automatically detected by deepmd-kit, while the second table ranges
two sub-tables. The first table takes the step parameter as the domain's uniform step size,
while the second table takes 10 * step as it's uniform step size. The range of the
first table is automatically detected by the code, while the second table ranges
from the first table's upper boundary(upper) to the extrapolate(parameter) * upper.

Parameters
Expand All @@ -49,8 +49,8 @@ def compress(
compressed model filename
extrapolate : int
scale of model extrapolation
stride : float
uniform stride of tabulation's first table
step : float
uniform step size of the tabulation's first table
frequency : str
frequency of tabulation overflow check
checkpoint_folder : str
Expand All @@ -71,8 +71,8 @@ def compress(
jdata["model"]["compress"]["model_file"] = input
jdata["model"]["compress"]["table_config"] = [
extrapolate,
stride,
10 * stride,
step,
10 * step,
int(frequency),
]
# be careful here, if one want to refine the model
Expand Down
13 changes: 13 additions & 0 deletions deepmd/entrypoints/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from deepmd.utils.convert import convert_13_to_20

def convert(
*,
FROM: str,
input_model: str,
output_model: str,
**kwargs,
):
if FROM == '1.3':
convert_13_to_20(input_model, output_model)
else:
raise RuntimeError('unsupported model version ' + FROM)
68 changes: 53 additions & 15 deletions deepmd/entrypoints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import logging
from pathlib import Path
from typing import List, Optional
from typing import Dict, List, Optional

from deepmd.entrypoints import (
compress,
Expand All @@ -14,6 +14,7 @@
train,
transfer,
make_model_devi,
convert,
)
from deepmd.loggers import set_log_handles

Expand Down Expand Up @@ -242,8 +243,8 @@ def parse_args(args: Optional[List[str]] = None):
# * compress model *****************************************************************
# Compress a model, which including tabulating the embedding-net.
# The table is composed of fifth-order polynomial coefficients and is assembled
# from two sub-tables. The first table takes the stride(parameter) as it's uniform
# stride, while the second table takes 10 * stride as it\s uniform stride
# from two sub-tables. The first table takes the step(parameter) as it's uniform
# step, while the second table takes 10 * step as it\s uniform step
#  The range of the first table is automatically detected by deepmd-kit, while the
# second table ranges from the first table's upper boundary(upper) to the
# extrapolate(parameter) * upper.
Expand All @@ -263,36 +264,43 @@ def parse_args(args: Optional[List[str]] = None):
"--input",
default="frozen_model.pb",
type=str,
help="The original frozen model, which will be compressed by the deepmd-kit",
help="The original frozen model, which will be compressed by the code",
)
parser_compress.add_argument(
"-o",
"--output",
default="frozen_model_compress.pb",
default="frozen_model_compressed.pb",
type=str,
help="The compressed model",
)
parser_compress.add_argument(
"-s",
"--step",
default=0.01,
type=float,
help="Model compression uses fifth-order polynomials to interpolate the embedding-net. "
"It introduces two tables with different step size to store the parameters of the polynomials. "
"The first table covers the range of the training data, while the second table is an extrapolation of the training data. "
"The domain of each table is uniformly divided by a given step size. "
"And the step(parameter) denotes the step size of the first table and the second table will "
"use 10 * step as it's step size to save the memory. "
"Usually the value ranges from 0.1 to 0.001. "
"Smaller step means higher accuracy and bigger model size",
)
parser_compress.add_argument(
"-e",
"--extrapolate",
default=5,
type=int,
help="The scale of model extrapolation",
)
parser_compress.add_argument(
"-s",
"--stride",
default=0.01,
type=float,
help="The uniform stride of tabulation's first table, the second table will "
"use 10 * stride as it's uniform stride",
help="The domain range of the first table is automatically detected by the code: [d_low, d_up]. "
"While the second table ranges from the first table's upper boundary(d_up) to the extrapolate(parameter) * d_up: [d_up, extrapolate * d_up]",
)
parser_compress.add_argument(
"-f",
"--frequency",
default=-1,
type=int,
help="The frequency of tabulation overflow check(If the input environment "
help="The frequency of tabulation overflow check(Whether the input environment "
"matrix overflow the first or second table range). "
"By default do not check the overflow",
)
Expand Down Expand Up @@ -352,6 +360,34 @@ def parse_args(args: Optional[List[str]] = None):
help="The trajectory frequency of the system"
)

# * convert models
# supported: 1.3->2.0
parser_transform = subparsers.add_parser(
'convert-from',
parents=[parser_log],
help='convert lower model version to supported version',
)
parser_transform.add_argument(
'FROM',
type = str,
choices = ['1.3'],
help="The original model compatibility",
)
parser_transform.add_argument(
'-i',
"--input-model",
default = "frozen_model.pb",
type=str,
help = "the input model",
)
parser_transform.add_argument(
"-o",
"--output-model",
default = "convert_out.pb",
type=str,
help='the output model',
)

parsed_args = parser.parse_args(args=args)
if parsed_args.command is None:
parser.print_help()
Expand Down Expand Up @@ -395,6 +431,8 @@ def main():
doc_train_input()
elif args.command == "model-devi":
make_model_devi(**dict_args)
elif args.command == "convert-from":
convert(**dict_args)
elif args.command is None:
pass
else:
Expand Down
82 changes: 68 additions & 14 deletions deepmd/env.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Module that sets tensorflow working environment and exports inportant constants."""

import os
from pathlib import Path
import logging
import os
import platform
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Any
import numpy as np
from imp import reload
from configparser import ConfigParser
from imp import reload
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple

import numpy as np

if TYPE_CHECKING:
from types import ModuleType
Expand Down Expand Up @@ -37,6 +38,7 @@

SHARED_LIB_MODULE = "op"


def set_env_if_empty(key: str, value: str, verbose: bool = True):
"""Set environment variable only if it is empty.

Expand Down Expand Up @@ -74,7 +76,8 @@ def set_mkl():
"""
if "mkl_rt" in np.__config__.get_info("blas_mkl_info").get("libraries", []):
set_env_if_empty("KMP_BLOCKTIME", "0")
set_env_if_empty("KMP_AFFINITY", "granularity=fine,verbose,compact,1,0")
set_env_if_empty(
"KMP_AFFINITY", "granularity=fine,verbose,compact,1,0")
reload(np)


Expand Down Expand Up @@ -118,8 +121,10 @@ def get_tf_session_config() -> Any:
intra_op_parallelism_threads=intra, inter_op_parallelism_threads=inter
)


default_tf_session_config = get_tf_session_config()


def get_module(module_name: str) -> "ModuleType":
"""Load force module.

Expand Down Expand Up @@ -149,14 +154,59 @@ def get_module(module_name: str) -> "ModuleType":
if not module_file.is_file():
raise FileNotFoundError(f"module {module_name} does not exist")
else:
module = tf.load_op_library(str(module_file))
try:
module = tf.load_op_library(str(module_file))
except tf.errors.NotFoundError as e:
# check CXX11_ABI_FLAG is compatiblity
# see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
# ABI should be the same
if 'CXX11_ABI_FLAG' in tf.__dict__:
tf_cxx11_abi_flag = tf.CXX11_ABI_FLAG
else:
tf_cxx11_abi_flag = tf.sysconfig.CXX11_ABI_FLAG
if TF_CXX11_ABI_FLAG != tf_cxx11_abi_flag:
raise RuntimeError(
"This deepmd-kit package was compiled with "
"CXX11_ABI_FLAG=%d, but TensorFlow runtime was compiled "
"with CXX11_ABI_FLAG=%d. These two library ABIs are "
"incompatible and thus an error is raised when loading %s."
"You need to rebuild deepmd-kit against this TensorFlow "
"runtime." % (
TF_CXX11_ABI_FLAG,
tf_cxx11_abi_flag,
module_name,
)) from e

# different versions may cause incompatibility
# see #406, #447, #557, #774, and #796 for example
# throw a message if versions are different
if TF_VERSION != tf.version.VERSION:
raise RuntimeError(
"The version of TensorFlow used to compile this "
"deepmd-kit package is %s, but the version of TensorFlow "
"runtime you are using is %s. These two versions are "
"incompatible and thus an error is raised when loading %s. "
"You need to install TensorFlow %s, or rebuild deepmd-kit "
"against TensorFlow %s.\nIf you are using a wheel from "
"pypi, you may consider to install deepmd-kit execuating "
"`pip install deepmd-kit --no-binary deepmd-kit` "
"instead." % (
TF_VERSION,
tf.version.VERSION,
module_name,
TF_VERSION,
tf.version.VERSION,
)) from e
raise RuntimeError(
"This deepmd-kit package is inconsitent with TensorFlow"
"Runtime, thus an error is raised when loading %s."
"You need to rebuild deepmd-kit against this TensorFlow"
"runtime." % (
module_name,
)) from e
return module


op_module = get_module("libop_abi")
op_grads_module = get_module("libop_grads")


def _get_package_constants(
config_file: Path = Path(__file__).parent / "pkg_config/run_config.ini",
) -> Dict[str, str]:
Expand All @@ -165,7 +215,7 @@ def _get_package_constants(
Parameters
----------
config_file : str, optional
path to CONFIG file, by default "config/run_config.ini"
path to CONFIG file, by default "pkg_config/run_config.ini"

Returns
-------
Expand All @@ -176,8 +226,14 @@ def _get_package_constants(
config.read(config_file)
return dict(config.items("CONFIG"))


GLOBAL_CONFIG = _get_package_constants()
MODEL_VERSION = GLOBAL_CONFIG["model_version"]
TF_VERSION = GLOBAL_CONFIG["tf_version"]
TF_CXX11_ABI_FLAG = int(GLOBAL_CONFIG["tf_cxx11_abi_flag"])

op_module = get_module("libop_abi")
op_grads_module = get_module("libop_grads")

if GLOBAL_CONFIG["precision"] == "-DHIGH_PREC":
GLOBAL_TF_FLOAT_PRECISION = tf.float64
Expand Down Expand Up @@ -221,5 +277,3 @@ def global_cvt_2_ener_float(xx: tf.Tensor) -> tf.Tensor:
output tensor cast to `GLOBAL_ENER_FLOAT_PRECISION`
"""
return tf.cast(xx, GLOBAL_ENER_FLOAT_PRECISION)


4 changes: 3 additions & 1 deletion deepmd/fit/ener.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def build (self,
initializer = tf.constant_initializer(self.aparam_inv_std))

inputs = tf.cast(tf.reshape(inputs, [-1, self.dim_descrpt * natoms[0]]), self.fitting_precision)
inputs_zero = tf.zeros_like(inputs, dtype=GLOBAL_TF_FLOAT_PRECISION)
if len(self.atom_ener):
# only for atom_ener
inputs_zero = tf.zeros_like(inputs, dtype=GLOBAL_TF_FLOAT_PRECISION)


if bias_atom_e is not None :
Expand Down
Loading