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
4 changes: 2 additions & 2 deletions deepmd/fit/dipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def init_variables(self,
suffix : str
suffix to name scope
"""
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def)
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def, suffix=suffix)


def enable_mixed_precision(self, mixed_prec : dict = None) -> None:
Expand All @@ -195,4 +195,4 @@ def enable_mixed_precision(self, mixed_prec : dict = None) -> None:
The mixed precision setting used in the embedding net
"""
self.mixed_prec = mixed_prec
self.fitting_precision = get_precision(mixed_prec['output_prec'])
self.fitting_precision = get_precision(mixed_prec['output_prec'])
2 changes: 1 addition & 1 deletion deepmd/fit/ener.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def init_variables(self,
suffix : str
suffix to name scope
"""
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def)
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def, suffix=suffix)
if self.numb_fparam > 0:
self.fparam_avg = get_tensor_by_name_from_graph(graph, 'fitting_attr%s/t_fparam_avg' % suffix)
self.fparam_inv_std = get_tensor_by_name_from_graph(graph, 'fitting_attr%s/t_fparam_istd' % suffix)
Expand Down
2 changes: 1 addition & 1 deletion deepmd/fit/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def init_variables(self,
suffix : str
suffix to name scope
"""
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def)
self.fitting_net_variables = get_fitting_net_variables_from_graph_def(graph_def, suffix=suffix)


def enable_mixed_precision(self, mixed_prec : dict = None) -> None:
Expand Down
25 changes: 19 additions & 6 deletions deepmd/utils/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,30 @@ def get_embedding_net_variables(model_file : str, suffix: str = "") -> Dict:
return get_embedding_net_variables_from_graph_def(graph_def, suffix=suffix)


def get_fitting_net_nodes_from_graph_def(graph_def: tf.GraphDef) -> Dict:
def get_fitting_net_nodes_from_graph_def(graph_def: tf.GraphDef, suffix: str = "") -> Dict:
"""
Get the fitting net nodes with the given tf.GraphDef object

Parameters
----------
graph_def
The input tf.GraphDef object
suffix
suffix of the scope

Returns
----------
Dict
The fitting net nodes within the given tf.GraphDef object
"""
fitting_net_nodes = get_pattern_nodes_from_graph_def(graph_def, FITTING_NET_PATTERN)
if suffix != "":
fitting_net_pattern = FITTING_NET_PATTERN\
.replace('/idt', suffix + '/idt')\
.replace('/bias', suffix + '/bias')\
.replace('/matrix', suffix + '/matrix')
else:
fitting_net_pattern = FITTING_NET_PATTERN
fitting_net_nodes = get_pattern_nodes_from_graph_def(graph_def, fitting_net_pattern)
for key in fitting_net_nodes.keys():
assert key.find('bias') > 0 or key.find('matrix') > 0 or key.find(
'idt') > 0, "currently, only support weight matrix, bias and idt at the model compression process!"
Expand All @@ -277,22 +286,24 @@ def get_fitting_net_nodes(model_file : str) -> Dict:
return get_fitting_net_nodes_from_graph_def(graph_def)


def get_fitting_net_variables_from_graph_def(graph_def : tf.GraphDef) -> Dict:
def get_fitting_net_variables_from_graph_def(graph_def : tf.GraphDef, suffix: str = "") -> Dict:
"""
Get the fitting net variables with the given tf.GraphDef object

Parameters
----------
graph_def
The input tf.GraphDef object
suffix
suffix of the scope

Returns
----------
Dict
The fitting net variables within the given tf.GraphDef object
"""
fitting_net_variables = {}
fitting_net_nodes = get_fitting_net_nodes_from_graph_def(graph_def)
fitting_net_nodes = get_fitting_net_nodes_from_graph_def(graph_def, suffix=suffix)
for item in fitting_net_nodes:
node = fitting_net_nodes[item]
dtype= tf.as_dtype(node.dtype).as_numpy_dtype
Expand All @@ -304,22 +315,24 @@ def get_fitting_net_variables_from_graph_def(graph_def : tf.GraphDef) -> Dict:
fitting_net_variables[item] = np.reshape(tensor_value, tensor_shape)
return fitting_net_variables

def get_fitting_net_variables(model_file : str) -> Dict:
def get_fitting_net_variables(model_file : str, suffix: str = "") -> Dict:
"""
Get the fitting net variables with the given frozen model(model_file)

Parameters
----------
model_file
The input frozen model path
suffix
suffix of the scope

Returns
----------
Dict
The fitting net variables within the given frozen model
"""
_, graph_def = load_graph_def(model_file)
return get_fitting_net_variables_from_graph_def(graph_def)
return get_fitting_net_variables_from_graph_def(graph_def, suffix=suffix)


def get_type_embedding_net_nodes_from_graph_def(graph_def: tf.GraphDef, suffix: str = "") -> Dict:
Expand Down