Skip to content

RPATH/RUNPATH not removed in PyTorch extension projects #924

Open

Description

@Nosamdaman

Note: this issue has been created following a discussion with @rgommers in #903. See the discussion there for the start of the conversation and context

Description

The recent changes to how RPATH/RUNPATHs are handled in meson-python haven't fully addressed the issues with compatibility with meson install. I'm developing a PyTorch extension module that uses meson/meson-python as its build backend (as opposed to the recommended setuptools). The way modern PyTorch extensions work is that you build a standard CPython-compliant extension just like normal, but you make use of PyTorch's custom sub-system. This means that you must link against PyTorch's collection shared objects. However, since PyTorch has its own subsystem for handling the registration of these operators, the expected order of operations is that torch itself is first imported, then the extension modules. Importing torch first means that the shared objects my extension links against are already loaded into the Python address space, so the dynamic linker doesn't need to find them when my extension is loaded. This actually simplifies things for the extension developer, as we don't need to worry about how libtorch will be resolved.

However, meson-python throws a small wrench in this system. Because it skips the meson install step and we still need to find and link against those torch libraries at build time, the torch libraries are detected as non-standard/system libraries and added to the build RPATH automatically. They are then not stripped out when a wheel is built or the package installed into a virtual environment. Truthfully, this doesn't cause any real issues, but it does mean that we're leaking the RPATH from the build environment out into the compiled binaries, which is not ideal.

Reproduction

I've gone ahead and created add-pi, a simple PyTorch extension module using meson and meson-python that demonstrates the issue. Simply build it like you would any other meson-python project on Linux and examine the _native.*.so module. Hear is the abridged output of running readelf -d on that module when it's compiled and installed from pure meson vs meson-python:

Pure Meson

readelf -d ~/venvs/torch-15/lib/python3.15/site-packages/add_pi/_native.abi3-x86_64-linux-gnu.so

Dynamic section at offset 0x30c08 contains 30 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libcudart.so.13]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cpu.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cuda.so]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

Meson-Python

readelf -d ~/venvs/torch-15/lib/python3.15/site-packages/add_pi/_native.abi3-x86_64-linux-gnu.so

Dynamic section at offset 0x30c08 contains 31 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libcudart.so.13]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cpu.so]
 0x0000000000000001 (NEEDED)             Shared library: [libtorch_cuda.so]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [/home/mason/venvs/torch-15/lib/python3.15/site-packages/torch/lib]

Take not of the existence of the RPATH tag when using meson-python vs its absence in pure meson. To re-iterate, the RPATH exists for both versions of the extension module at build time, but standard meson strips it out (which is desirable here) when installing.

Solutions?

In the initial discussion, @rgommers suggested three potential paths forward:

  1. A way to switch the behavior package-wide. Easy to implement; the limitation is that one cannot support a mix of both types of behavior within the same package (that should be rare though).
  2. A more selective override method. That should probably live inside meson.build or be implemented as part of a dependency() object, so dependency('torch') can do the right thing. This is going to be more complicated to implement, and has to live in Meson.
  3. Add a dependency provider for PyTorch in Meson that avoids the absolute paths that trigger this problem in the first place.

My preference would be for option 2, but I'm happy to hear more discussion on what's best for the project as a whole.

Additionally, @rgommers suggestion something along the following code snippet, which I've gone ahead and tried in add-pi (not committed).

torch_dep = declare_dependency(
    include_directories: torch_includes,
    link_args: [
        '-L' + torch_library_path,
        '-ltorch',
        '-lc10',
        '-lc10_cuda',
        '-ltorch_cpu',
        '-ltorch_cuda',
    ]
)

The theory was that meson decided the build rpath was necessary because the shared objects being linked against were resolved as absolute paths internally, and that by obfuscating that fact, I could get around that behavior. In testing this, I found that @rgommers was exactly right. Adding the above snippet in place of the original torch_dep definition produced shared objects without RPATH entries. As mentioned in the original conversation however, I'm not the biggest fan of this solution as more than a quick fix. In order for my project to be portable to platforms outside of GNU gcc/ld, I'd need to either make sure that other compilers/linkers support this syntax or use a separate branch for those platforms (or Linux). This is obviously more of maintenance burden and removes some of the benefits of using a meta-build system at all. Additionally, this change pushes any errors related to missing dependencies downstream to build/link time, which is also not really in the spirit of what meson is here to fix. Usually, I want to see compilation errors at compile time, and configuration/dependency errors at configuration time.

To re-iterate something I mentioned originally, I understand this is a pretty niche issue. This is a very specific use case, and meson-python does in fact work here, it just doesn't work as well as I'd like. I'd love to hear more from the maintainers on what the best course of action here is. Thank you so much!

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions