Skip to content

MLIAP Unified Interface - #3458

Merged
akohlmey merged 66 commits into
lammps:developfrom
Boogie3D:mliappy_unified
Sep 27, 2022
Merged

akohlmey merged 66 commits into
lammps:developfrom
Boogie3D:mliappy_unified

Conversation

@Boogie3D

@Boogie3D Boogie3D commented Sep 23, 2022 •

Copy link
Copy Markdown
Contributor

Summary

This PR introduces new features and improvements to the ML-IAP Python component, most prominently including a new "Unified" pair style. It also fixes problems raised in #2934.

Related Issue(s)

Fixes an issue where ML-IAP loads lammps library separately from lammps-python.

A NumPy example for ML-IAP is also included.

Author(s)

Steven Anaya, LANL | [email protected]
Nicholas Lubbers, LANL | [email protected]

Licensing

By submitting this pull request, I agree, that my contribution will be included in LAMMPS and redistributed under either the GNU General Public License version 2 (GPL v2) or the GNU Lesser General Public License version 2.1 (LGPL v2.1).

Backward Compatibility

No known backward compatibility problems are introduced by this PR.

Implementation Notes

The main improvement is the addition of the ML-IAP "Unified" pair style, which allows Python libraries to build ML-IAP components for LAMMPS, taking control of both the descriptor and model protocols. This feature allows, among other possibilities, the ability to use Deep Neural Network models which are end-to-end (taking positions as input, rather than features) and those that view the environment of each atom recursively. A pedagogical example showing how Lennard-Jones can be implemented efficiently in Python is provided. There is a corresponding example, which reproduces the classic lj/melt example using this programming technique. There are also several non-trivial examples which give a notion of more practical usage using the HIP-NN Deep Neural Network via the hippynn python library.

Secondly, this PR introduces improvements to the ML-IAP Python coupling:

  1. It includes a version of the "reverse" Python-driving-LAMMPS code which only depends on NumPy and is thus suitable for automated testing. Also note examples/mliap/mliap_numpy_Ta06.py which tests the reverse python->lammps coupling for mliap model python.
  2. Python-driven codes will extract the embedded ML-IAP modules from LAMMPS directly using the loaded lammps library object, rather than seeking out the lammps library. This prevents any possibility that lammps.lammps() objects and lammps.mliap module.

Lastly, this PR introduces a few extra features for the ML-IAP pair style and data structure. The ML-IAP pair style can be toggled to use neighbors of ghost atoms, which is required for some kinds of potentials.

It also introduces the following members to the MLIAPData class:

  • double **f - pointer to atom->f, updated on each call to generate_neighdata()
  • int ntotal - stores total number of owned and ghost atoms on this proc
  • int natomneigh - current number of atoms and ghosts in atom neighbor arrays
  • int npairs - number of ij neighbor pairs
  • int *pair_i - array storing index of each i atom for each ij pair
  • int *elems - array storing element of each atom in or not in the neighborlist

The following member is added to the MLIAPDescriptor class:

  • double ** cutghost - array storing cutoff for each ghost pair

We used Cython similarly to mliap_model_python.

Post Submission Checklist

  • The feature or features in this pull request is complete
  • Licensing information is complete
  • Corresponding author information is complete
  • The source code follows the LAMMPS formatting guidelines
  • Suitable new documentation files and/or updates to the existing docs are included
  • The added/updated documentation is integrated and tested with the documentation build system
  • The feature has been verified to work with the conventional build system
  • The feature has been verified to work with the CMake based build system
  • Suitable tests have been added to the unittest tree.
  • A package specific README file has been included or updated
  • One or more example input decks are included

Further Information, Files, and Links

https://www.lanl.gov/org/ddste/aldsc/hpc/_assets/docs/posters/22HPC-showcase-poster-anaya.pdf

Boogie3D and others added 30 commits April 15, 2022 14:22
@akohlmey
akohlmey requested a review from rbberger as a code owner September 24, 2022 08:17

@akohlmey akohlmey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Boogie3D @lubbersnick Thanks for your submission. This looks quite nicely done. I've made some - mostly cosmetic - minor changes to improve consistency with the rest of LAMMPS and some (yet undocumented) conventions.
There are a couple notes/questions added in different locations, but neither is a big concern.

Comment thread doc/src/pair_mliap.rst
(0/1) to disable/enable the construction of neighbors lists including
neighbors of ghost atoms. If the filename ends in '.pt', or '.pth', it will be loaded
using pytorch; otherwise, it will be loaded as a pickle file.
If ghostneigh is enabled, it is recommended to set :doc:`comm_modify <comm_modify>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FYI: when I enable the "ghostneigh" flag in the in.mliap.unified.lj.Ar example, I get a segmentation fault. Is this expected behavior? Can this be checked for and an error triggered?
Increasing the communication cutoff as suggested below did not make a difference.

@Boogie3D Boogie3D Sep 24, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't believe this is expected behavior. The error likely comes from neighbor list memory allocation as this segfault occurs while calculating Per MPI Rank memory allocation (see backtrace below). If I reduce the problem size from 4000 atoms to 256 I get a neighbor list overflow instead of a segfault. This error does not happen with the hippynn Al or InP examples.

Core was generated by `lmp -i in.mliap.unified.lj.Ar'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000149cd25883d8 in int_mallinfo () from /lib64/libc.so.6
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-151.el8.x86_64
(gdb) bt
#0  0x0000149cd25883d8 in int_mallinfo () from /lib64/libc.so.6
#1  0x0000149cd258e09c in mallinfo () from /lib64/libc.so.6
#2  0x0000149cd32247c6 in LAMMPS_NS::Info::get_memory_info (this=this@entry=0x7ffc14762a90, meminfo=meminfo@entry=0x7ffc14762a20)
    at /projects/ml4chem/sanaya/lammps/src/info.cpp:1300
#3  0x0000149cd3310b6b in LAMMPS_NS::Output::memory_usage (this=0x744a30) at /projects/ml4chem/sanaya/lammps/src/output.cpp:1018
#4  0x0000149cd33110d1 in LAMMPS_NS::Output::setup (this=0x744a30, memflag=memflag@entry=1) at /projects/ml4chem/sanaya/lammps/src/output.cpp:299
#5  0x0000149cd33f3ae2 in LAMMPS_NS::Verlet::setup (this=0x73ca20, flag=1) at /projects/ml4chem/sanaya/lammps/src/verlet.cpp:160
#6  0x0000149cd33a0c6f in LAMMPS_NS::Run::command (this=0x91bbc0, narg=1, arg=0x749180) at /projects/ml4chem/sanaya/lammps/src/run.cpp:171
#7  0x0000149cd323d1b8 in LAMMPS_NS::Input::execute_command (this=0x72d6f0) at /projects/ml4chem/sanaya/lammps/src/input.cpp:847
#8  0x0000149cd323d58d in LAMMPS_NS::Input::file (this=0x72d6f0) at /projects/ml4chem/sanaya/lammps/src/input.cpp:302
#9  0x0000000000400e85 in main (argc=<optimized out>, argv=<optimized out>) at /projects/ml4chem/sanaya/lammps/src/main.cpp:76

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The gdb trace is bogus. Something has written to an allocated storage block beyond the allocated size and thus messed up the internal malloc data structures.

I have run the code with valgrind and identified the location of the problem at this location:
https://github.com/lammps/lammps/pull/3458/files#diff-cb6b51f0e9ebcb4ecda7e8a34b450fb05f24b7de8a8bfbac614cbfd21af03b6dR256

The index i overflows the dimension of the data->eatoms array which is of size data->nlistatoms.
I am implementing a hack that avoids the crash, but will likely lead to incorrect per-atom energy data.
I would expect that data->eatoms would need to be allocated (and cleared) so it includes ghost atoms and then a reverse communication would be needed to sum the contributions to ghost atoms to their principal owned atoms.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also found and fixed a memory leak, an initialization issue and noticed that destructors of some polymorph classes were not make virtutal and general our conventions for using virtual/override were not fully followed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Boogie3D I think I have sorted this out for the LJ example. I could not test the hippynn examples, since I did not see any of the .pt files.
I also added a test that creates two LAMMPS instances with mliap.unified.lj and lj/cut pair styles, respectively and check some results after a couple of MD steps.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Boogie3D I think I have sorted this out for the LJ example. I could not test the hippynn examples, since I did not see any of the .pt files. I also added a test that creates two LAMMPS instances with mliap.unified.lj and lj/cut pair styles, respectively and check some results after a couple of MD steps.

The scripts to create the hippynn .pt files are in the hippynn repo but unfortunately require HIPNN training to be performed first; the .pt files themselves are not uploaded anywhere.

Thank you for your various improvements and for solving the problem with memory allocation and LJ so quickly. I am beginning graduate school soon and am not currently working at LANL so I may not have had the time to do so myself.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the .pt files themselves are not uploaded anywhere

Can you send me a copy of those files via email? I would very much like to make certain that my changes did not break anything.

Comment thread examples/mliap/README Resolved
Comment thread python/lammps/mliap/__init__.py Resolved
@akohlmey

Copy link
Copy Markdown
Member

@lubbersnick How do propose to continue with this PR?
I cannot confirm that my changes did not break anything (but they prevent out-of-bounds memory accesses, so if they change results, probably the cause needs to be addressed as well) because the necessary software is not yet publicly available.

@athomps

athomps commented Sep 26, 2022

Copy link
Copy Markdown
Contributor

@lubbersnick I think @akohlmey has a point about the unrunnable HIPNN examples. Either we should make them runnable or take them out.

@lubbersnick

Copy link
Copy Markdown
Contributor

@akohlmey @athomps I would like an eventual future where we have an example in the lammps repository because it shows a much more clear reason and use case compared to reimplementing LJ. In some sense the difficulty with including networks in the repository is the number of things to validate in order to prove a network is good/useful for an actual application. Although I don't think it is possible to release a practically useful network from the very limited InP dataset, we can maybe make a standardized one for aluminum.

I am slightly confused at the comment that the code is not public - it is, and will very soon be merged into the main hippynn repository. Building your own network for aluminum is our basic example in hippynn. It is probably not worth the effort to do so for the sake of checking the lammps codebase; it requires going to get the training dataset and so on. I will email you a network so you can before/after test your changes if you like. However I am pretty sure that the change to update_pair_energies shouldn't affect the hippynn library because it does not use that function, as can be verified here .

However, it was good to identify this difficulty of mixing update_pair_energies with ghostneigh. We should probably be clear that ghostneigh is connected to the semantics of the model -- models can give different answers with it on or of and so ghostneigh needs to be set as appropriate for that model. So the fact that ghostneigh gives a segfault for LJ does not surprise me. It is better that we have this check instead of corrupting memory though, and very sharp of you to find it. A good 'future work' thing for us would be to set up the mliap interface class to store the ghostneigh information as needed for that model so that the setting cannot be wrong.

@akohlmey

Copy link
Copy Markdown
Member

@akohlmey @athomps I would like an eventual future where we have an example in the lammps repository because it shows a much more clear reason and use case compared to reimplementing LJ. In some sense the difficulty with including networks in the repository is the number of things to validate in order to prove a network is good/useful for an actual application. Although I don't think it is possible to release a practically useful network from the very limited InP dataset, we can maybe make a standardized one for aluminum.

The examples in the LAMMPS repository are not meant to be blueprints for excellent research. Their purpose is to a) demonstrate the syntax and structure of inputs for a given package and b) to provide a reference that can be checked against in case changes to the code base are made that may (or may not) affect a package. In both cases, the input does not have to produce a high accuracy result. If you are concerned about people misusing the network, just make up a new element/compound and for example call it Bovinexcrementum (element symbol BS) and then there is no danger of somebody using it for real research and blaming bad results on you (or rather the main LAMMPS developers, since most people in first approximation blame us for everything that goes wrong, regardless of who actually authored the code).

I am slightly confused at the comment that the code is not public - it is, and will very soon be merged into the main hippynn repository. Building your own network for aluminum is our basic

I followed the instructions given in the pull request description (and in your private email) and in both cases it points to a repository that at that moment did not contain the necessary changes. You cannot expect people to know that this is not correct.

function, as can be verified here .

However, it was good to identify this difficulty of mixing update_pair_energies with ghostneigh. We should probably be clear that ghostneigh is connected to the semantics of the model -- models can give different answers with it on or of and so ghostneigh needs to be set as appropriate for that model.

Then a check whether this is required or not should be in the model and a proper error should be thrown if the wrong setting is used, or - even better - the C++ code should have a way to query the model for the correct setting and then there need not be a flag. Same as some manybody potentials already contain the pairwise cutoff and set it accordingly. The available documentation suggests (at least the way how it appears to me) that the flag can be chosen rather freely.

So the fact that ghostneigh gives a segfault for LJ does not surprise me. It is better that we have this check instead of corrupting memory though, and very sharp of you to find it. A good 'future work' thing for us would be to set up the mliap interface class to store the ghostneigh information as needed for that model so that the setting cannot be wrong.

Exactly. For the LJ case, it causes a significant slowdown, for obvious reasons. I just enabled it, since there was nothing saying in the docs that it is not supported and can cause problems, and as part of my test procedure for new features, I try to check different flags added to commands and also run with valgrind and other tools to check for memory issues etc (where possible, the python part is next to impossible to debug that way though).

@akohlmey

akohlmey commented Sep 26, 2022 •

Copy link
Copy Markdown
Member

@lubbersnick I think @akohlmey has a point about the unrunnable HIPNN examples. Either we should make them runnable or take them out.

Since there seems to be no mention of hippynn in the manual itself and no documentation for how to obtain the code and the potential files (the only reference is Requires the HIPNN code. in the README), I second the removal of those examples. We cannot expect a user that looks at the examples to figure out where to find the code, how to install it, how to obtain/generate the potential file etc. Beyond that, the PR seems suitable to be merged.

@lubbersnick

Copy link
Copy Markdown
Contributor

Okay, let's just remove those examples for this PR. At some point I will write an example with more complete documentation and a dummy network.

@Boogie3D

Copy link
Copy Markdown
Contributor Author

Okay, let's just remove those examples for this PR. At some point I will write an example with more complete documentation and a dummy network.

I second this for now.

@athomps

athomps commented Sep 26, 2022

Copy link
Copy Markdown
Contributor

I agree that it is sufficient for now to provide only the LJ example (in two different forms).

@athomps athomps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I approve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants