Repository navigation
Fix mliappy libpython extension - #2934
jan-janssen wants to merge 1 commit into
Conversation
|
@lubbersnick any comments on this? |
|
@jan-janssen can you please elaborate on what Python configuration this happens? Can't reproduce the |
|
I tried it on an anaconda environment, it points to libpython3.9.a but the file is libpython3.9.so . |
|
@jan-janssen Please be more specific. Just tried this on a Linux box with Anaconda3 2021.05: |
|
Could finally reproduce it with Linux: MacOS: |
rbberger
left a comment
There was a problem hiding this comment.
Breaks on systems that return INSTSONAME with something like libpython3.9.so.1.0
|
One option would be to use something like https://pypi.org/project/find-libpython/ |
|
Heya, I have had some problems using mliappy with anaconda3 on macos and I wonder if Jan has found the problem in a similar way. The issue is not easy to reproduce because it has something to do with how the python build configuration is set up and from what I can tell there is no transparent way to ask for those configurations in anaconda. I am ok with a solution that doesn't break existing things. Richard pointed out that yes, it will be .dylib sometimes instead of .so. Maybe we should just mimmick/refactor what goes on here to find the lammps shared library? I guess a question is if we could fit a mliappy test into the LAMMPS test suite? I know that testing the link back to python requires a bit more testing but I guess what's preferable is if we can show the new solution works on any machine/python installation, or, at least, on a superset of what works now. |
To add a test we would just need a "dummy" or "mock" machine learning potential implementation. One that does nothing but returns some predefined values, e.g. atom ID as x-direction force, type as y-direction force etc. |
From the examples we can run this: https://github.com/lammps/lammps/blob/master/examples/mliap/convert_mliap_Ta06A.py to convert the Ta potential into a pytorch format. Then this tests mliappy from lammps side: https://github.com/lammps/lammps/blob/master/examples/mliap/in.mliap.pytorch.Ta06A And this tests mliappy from the python side: https://github.com/lammps/lammps/blob/master/examples/mliap/mliap_pytorch_Ta06A.py |
|
@lubbersnick you are missing the point. We want to test the interface only. Without being forced to install and load pytorch. |
|
@lubbersnick FYI, I'm seeing a segfault when trying to run The global static variable of the mliap pyx module storing the Running mliap without external Python interpreter works. |
|
@lubbersnick Turns out that what loader.py does isn't good enough. path = lmp.lib._name
loader = importlib.machinery.ExtensionFileLoader('mliap_model_python_couple', path)That tries to load If that was the reason for the original |
|
@rbberger The Py_IsInitialized is there because some cases were not able to load the python shared library properly. Much as what you found for liblammps, if a different version of the python library gets loaded then the python interpreter lands in a bad state because the calls to python inside lammps do not cooperate with the global variables of the active interpreter. By checking that the loaded library recognizes that python has been initalized one verifies that indeed the shared library calls compiled into lammps will affect the current python interpreter. My hope is that this PR that @jan-janssen has submitted extends the ability to test that lammps will be able to communicate 'back' to python properly. But in any case if that py_isinitialized check fails then nothing will work (at least, I always get segfault) so I don't suggest we remove it. So I think what you've found is the flip side of this, but for the extension to python that is compiled into lammps. It seems to mirror the phenomenon -- if the loader adds a new copy of lammps, then global variables (in the pyx module) aren't necessarily initialized right. I thought that grabbing the _name attribute from the active copy of lammps would dynamically work but apparently not. On my machine the |
|
@lubbersnick I don't think you need any pathname at this moment because the LAMMPS library has already been loaded. Find below a small test case for a simple executable that loads lammps (through the C library interface for simplicity, but C++ would work just as well) and then uses When loading the |
|
@lubbersnick I have to correct myself. The segfault only happens if the importlib machinery in Now, the reason why I've ended up having a symlink to So what ended up happening was that In the end what is needed is a better way of making sure the same @akohlmey Yes, you need to specify the full path. |
|
@rbberger I know that "On my machine" isn't an excuse, but on my machine the lib name points into the python installation path for lammps. Could this actually depend on how the search for the lammps library is being done in lammps.py? in other news I did some digging and the fact that a file name is used to load an extension model is built deep into the c part of CPython, so it doesn't look simple to build a custom loader that takes the CDLL. But I won't say that route is completely impossible. |
|
@lubbersnick I agree. After spending the last days digging into various PEPs and the CPython source code, that's one step too far for this simple extension. Also found this jem: if (strchr(pathname, '/') == NULL) {
/* Prefix bare filename with "./" */
PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
pathname = pathbuf;
}
...
handle = dlopen(pathname, dlopenflags);So it actually forces the lib file to be in the working directory if you don't specify the full path. Anyway, it's probably easier to just re-implement the whole thing in C++, export some extra C symbols, and use ctypes instead. |
|
I am planning to extend on the cython, as such, I'd hope not to obliterate it. I think it should be realistic to obtain the full path for the library -- it is a bit crazy if we do not know what file is being loaded. So I look at it like 1) rewrite the code in a way that maintains compatibility and does bi-directional communication between python and C++ or 2) locate an absolute file path for a file we are already using elsewhere. Am I oversimplifying that too much? |
|
@lubbersnick from what is currently present, it is difficult to see anything that justifies using cython and from what I understand a significant part of what is causing a problem is the way how cython works as such. So finding an alternative that allows to drop using cython is desirable, since it was requiring some effort to integrate it without too much complexity into the build procedure in the first place. The "philosophy" of cython seem to imply that the generated source code would otherwise, by preference, be compiled into a standalone shared object. While that would be easy to support this in CMake, it is a serious complexity to be added to the conventional build system. So to stick with cython there has to be a very good argument showing that something needs to be done that cannot be done otherwise (with reasonable effort). I am opposed to using more band aid to address a problem that is caused by a band aid solution in the first place. I'd also like to remark that on a unix system you cannot "trust" file names at all to remain the same item when they have the same name, only file descriptors (not even file pointers) and even for that there are exceptions. What we currently have seems to be somewhat working. That it breaks with coda packaging is something that we currently will have to live with. |
|
But the solution to Richard's problem isn't a band-aid. The code is compiled into liblammps, not some random file. And liblammps is not going to be a random file descriptor received over network or something like that -- lammps is compiled as a file on disk. Given the location of liblammps, the code loads it as expected and all the examples work. So fundamentally I don't see that this is a cython issue per se -- I think it's just a matter of tracking what library is dynamically loaded. In terms of advantages: One advantage of cython not fully exploited right now is to interact with the C++ objects, without having to treat everything as a C function. Another strong advantage is that code can live where it is used and only interact with the part of lammps it needs to. We aren't having to pass pointers to pairstyles as <void *> through the c library and then hope to reconstruct their types correctly in a completely different part of the code base. We can import and use methods within lammps that aren't exported as part of the external library interface. If at every juncture we decide to export everything we need from C++ as a strict C function, we will end up with non-obvious functions there that require detailed knowledge of lammps internals to use properly. The goal is a tight coupling to python in the MLIAP package so that high-performance ML code in python can be used with lammps easily. This is not the same as 'running lammps from another program', which is (I think) the goal of liblammps & the functions exported there. Richard's issue is not the exact same as what Jan has raised; we can also address that issue, similarly, by just ensuring we have the correct paths. There are more potential alternatives -- but give us some time to construct concrete solutions; I think we'll be able to show that nothing more complicated than the existing compilation is needed. |
|
@lubbersnick Sorry, but I don't buy several of your arguments and most importantly I don't share your vision of what can become of this piece of code. Any piece of code that is dependent on accessing the LAMMPS shared library a second time when it is already loaded (e.g. by launching the LAMMPS executable) is in my opinion fundamentally flawed. While it is not a common occurrence, physical files can change and when you are holding the file open its file descriptor will point to the deleted/replaced file while a newly opened file will point to the new. This is built into the OS (except when you are on Windows). Thus when opening the same path a second time, you must not assume that it is the same. I don't want to start a lengthy argument about details and specifics here and now and just state the thought of turning LAMMPS into the hybrid C++/Python code you describe scares the socks off me when I look at this when I am wearing my maintainer hat. Since we are gearing up to a stable release, any changes in this area are not going to be taken under consideration for the next month or so, anyway. |
|
I found and implemented a simple way to get the embedded python module directly from the CDLL loaded in the lammps.lammps().lib so that there is no need to rely on opening the file more than once. Since you all are gearing up for the current stable release milestone and this PR wasn't meant to solve that problem anyway, I won't make a PR for that just yet. |
Please submit a draft PR anyway so that @rbberger can have a look and compare it to his attempts now that his memory is fresh. Drafts can't be merged anyway and there are already several PRs labeled to be processed in the next "development cycle"... so there is no harm done. |
|
@lubbersnick @jan-janssen @rbberger this has been sitting idle for a while, but since we have concluded the previous development cycle with a stable release a month ago, this may be a good time to revisit this topic. |
Summary
Previously
mliappywas looking forlibpython3.9.arather thanlibpython3.9.soRelated Issue(s)
Author(s)
Jan Janssen, Los Alamos National Lab, [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
Implementation Notes
Post Submission Checklist
Further Information, Files, and Links