Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish model compression for se_r descriptor! #1361

Merged
merged 12 commits into from
Jan 11, 2022

Conversation

liangadam
Copy link
Contributor

Now model compression is available for se_r descriptor.

The compressed result has been checked with lammps , both on CPU and GPU. It's correct and the deviation is within 1e-8 in 1000 steps.

Acceleration performance: CPU 300%, GPU 120% (may be due to the small test system).

@@ -120,8 +120,6 @@ def __init__ (self,
"""
Constructor
"""
if rcut < rcut_smth:
raise RuntimeError("rcut_smth (%f) should be no more than rcut (%f)!" % (rcut_smth, rcut))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you delete these two lines?

Comment on lines 86 to 87
if rcut < rcut_smth:
raise RuntimeError("rcut_smth (%f) should be no more than rcut (%f)!" % (rcut_smth, rcut))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that. This may be because I used the previous version of code when committing mine. I will recall it.

except Exception:
self.sel_a = self.graph.get_operation_by_name('DescrptSeR').get_attr('sel')
self.prod_env_mat_op = self.graph.get_operation_by_name ('DescrptSeR')
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif isinstance(self.descrpt, deepmd.descriptor.DescrptSeA): is recommended.

And the conditions stop when else which capture the case self.descrpt is neither DescrptSeA nor DescrptSeR, and throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it‘s safer. I will change it~

try:
self.sel_a = self.graph.get_operation_by_name('ProdEnvMatR').get_attr('sel')
self.prod_env_mat_op = self.graph.get_operation_by_name ('ProdEnvMatR')
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use KeyError instead of Exception. https://www.tensorflow.org/api_docs/python/tf/Graph#get_operation_by_name gives what get_operation_by_name raises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK~


for ii in range(len(self.filter_neuron) - 1):
if self.filter_neuron[ii] * 2 != self.filter_neuron[ii + 1]:
raise RecursionError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it RecursionError? I think it should be NotImplementedError instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK~

@njzjz
Copy link
Member

njzjz commented Dec 19, 2021

@liangadam
Copy link
Contributor Author

@codecov-commenter
Copy link

codecov-commenter commented Dec 20, 2021

Codecov Report

Merging #1361 (e613be5) into devel (1f4dc3a) will increase coverage by 0.07%.
The diff coverage is 70.29%.

Impacted file tree graph

@@            Coverage Diff             @@
##            devel    #1361      +/-   ##
==========================================
+ Coverage   75.63%   75.70%   +0.07%     
==========================================
  Files          92       92              
  Lines        7531     7628      +97     
==========================================
+ Hits         5696     5775      +79     
- Misses       1835     1853      +18     
Impacted Files Coverage Δ
deepmd/descriptor/se_a.py 94.15% <0.00%> (ø)
deepmd/descriptor/se_t.py 97.35% <0.00%> (ø)
source/op/_tabulate_grad.py 100.00% <ø> (ø)
deepmd/utils/tabulate.py 80.00% <65.38%> (-4.35%) ⬇️
deepmd/descriptor/se_r.py 95.00% <95.23%> (+3.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1f4dc3a...e613be5. Read the comment docs.

@amcadmus amcadmus requested a review from njzjz December 21, 2021 06:23
node = self.embedding_net_nodes[f"filter_type_{ii // self.ntypes}{self.suffix}/matrix_{layer}_{ii % self.ntypes}"]
matrix["layer_" + str(layer)].append(tf.make_ndarray(node))
else:
matrix["layer_" + str(layer)].append(np.array([]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an else to raise the error of un-supported descritpor

node = self.embedding_net_nodes[f"filter_type_{ii // self.ntypes}{self.suffix}/bias_{layer}_{ii % self.ntypes}"]
bias["layer_" + str(layer)].append(tf.make_ndarray(node))
else:
bias["layer_" + str(layer)].append(np.array([]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an else to raise the error of un-supported descritpor

@@ -317,6 +379,9 @@ def _get_env_mat_range(self,
var = np.square(sw / (min_nbor_dist * self.dstd[:, 1:4]))
lower = np.min(-var)
upper = np.max(var)
elif isinstance(self.descrpt, deepmd.descriptor.DescrptSeR):
lower = np.min(-self.davg[:, 0] / self.dstd[:, 0])
upper = np.max(((1 / min_nbor_dist) * sw - self.davg[:, 0]) / self.dstd[:, 0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an else to raise the error of un-supported descritpor

elif isinstance(self.descrpt, deepmd.descriptor.DescrptSeR):
layer_size = len(self.embedding_net_nodes) // ((self.ntypes * self.ntypes - len(self.exclude_types)) * 2)
if self.type_one_side :
layer_size = len(self.embedding_net_nodes) // (self.ntypes * 2)
return layer_size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an else to raise the error of un-supported descritpor

elif isinstance(self.descrpt, deepmd.descriptor.DescrptSeR):
table_size = self.ntypes * self.ntypes
if self.type_one_side :
table_size = self.ntypes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an else to raise the error of un-supported descritpor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK~

Copy link
Member

@amcadmus amcadmus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test is missing. Please add them to check if the compressed OP gives the same result as the original OP.

You may take source/lib/tests/test_tabulate.cc as an example.

except KeyError:
self.sel_a = self.graph.get_operation_by_name('DescrptSeA').get_attr('sel_a')
self.prod_env_mat_op = self.graph.get_operation_by_name ('DescrptSeA')
elif isinstance(self.descrpt, deepmd.descriptor.DescrptSeT):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think DescrptSeA and DescrptSeT are the same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they are the same. But considering the possible changes later, I have explicitly distinguished them.

@liangadam
Copy link
Contributor Author

The unit test is missing. Please add them to check if the compressed OP gives the same result as the original OP.

You may take source/lib/tests/test_tabulate.cc as an example.

OK. I will add an UT for this.

Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very nice to see the new python tests checking the output of the original model is identical to the compressed model.

Could you add the unittest for the new methods tabulate_fusion_se_r_*_cpu and tabulate_fusion_se_r_*_gpu_cuda added in source/lib/include/tabulate.h by this PR?

You may take as examples the tests of tabulate_fusion_se_a_*_cpu and tabulate_fusion_se_a_*_gpu_cuda in source/lib/tests/test_tabulate.cc

@liangadam
Copy link
Contributor Author

OK. I will add them.

@wanghan-iapcm
Copy link
Collaborator

Your UT failed. Please check.

@liangadam
Copy link
Contributor Author

https://github.com/deepmodeling/deepmd-kit/pull/1391/files#r781755472

A mistake in #1391 caused this. It couldn't pass UT but it was merged into devel.

@wanghan-iapcm wanghan-iapcm merged commit 05c785b into deepmodeling:devel Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants