I am working on an open-source project called ReLab. It is coded in python (package is named relab) but contains a C++ extension (named librelab.so) to take advantage of multi-threading. The python package relab depends on the python package torch, while the C++ extension depends on the C++ extension of pytorch libtorch.so.
I would like to make the project available on PyPI. How can I achieve that?
I am currently using a combination of poetry, cmake, pybind11, and scikit-build. But while I managed to build and publish the package to PyPI, when installing it with pip, I get linker errors:
I’ve just downloaded torch-2.6.0-cp313-cp313-manylinux1_x86_64.whl, renamed it to zip and searched inside it. I couldn’t find libtorch.so inside (using Windows 11 explorer, which proves nothing), so don’t you have to ship this file anyway?
It’s possible to dynamically link a .so from another one, as long as the relative dir to it is known. I’d love to learn of a more elegant way of manipulating paths in C++, but in a C++ extension based around .dlls or .sos (packaged into wheels for each platform) I used:
Dl_info dlInfo;
dladdr((void *)&a_static_class_function, &dlInfo);
string path(dlInfo.dli_fname);
size_t i = path.find_last_of('/');
string dir = path.substr(0,i);
string geos_dll_path_w = dir + "/libgeos_c.so";
// ...
dlerror();
hDLL = dlopen(geos_dll_path_w.c_str(), RTLD_LAZY);
// Check dlerror() for error a second time, and handle hDLL == null
initGEOS = (initGEOS_t)GetProcAddress(hDLL,"initGEOS");
Depending on the details of how you’re building the extension depending on libtorch, you’ll need something like torch.ops.load_library or torch.library.* to ensure your extension is registered and finds the necessary pieces.
You may also be able to look at a package like torchvision, which has C++/CUDA extensions that depend on libtorch and ships wheels on PyPI.
I thought that too. The search function I used just doesn’t work within zip files (I’ve now unzipped the wheel).
If libtorch.so really is all that’s needed, then that it’s only 192kB in this wheel However it itself probably links to either libtorch_cpu.so (430MB) or libtorch_cuda.sp (880MB).
Thanks so much for your fast reply, it is really helpful! I have been busy with other projects, but a friend of mine will probably try these out shortly, we will keep you posted.
Sorry, for posting here, I wasn’t quite sure where the best place was, and I was hoping that the answer would be general enough to apply to any .so/dll libraries.