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

libtorch location #439

Open
rhobro opened this issue Dec 26, 2021 · 9 comments
Open

libtorch location #439

rhobro opened this issue Dec 26, 2021 · 9 comments

Comments

@rhobro
Copy link

rhobro commented Dec 26, 2021

Hi there,

In the documentation, it says that libtorch must be on the system for the crate to work.

I am creating a program. Does this mean that libtorch must be on every system that I run the program on or will it be packaged with the binary?

Thanks.

@LaurentMazare
Copy link
Owner

Right, libtorch is loaded as a shared library so the library binaries should be deployed on the boxes where you want to run this and in a path so that they could be discovered (i.e. either a system path or you could adjust the LD_LIBRARY_PATH environment variable).

@danieldk
Copy link
Contributor

If you want to distribute libtorch with your binary, you could also put the libtorch libraries in the same directory and use patchelf to patch the libraries and your binary to load libraries from the binary's directory. See e.g.:

https://github.com/tensordot/syntaxdot/blob/519b96491b08c847d91739faff95ea344ed4e870/.github/workflows/release.yml#L39

@rhobro
Copy link
Author

rhobro commented Dec 27, 2021

@danieldk so patchelf would add all of the libtorch libraries into the binary? However, in that example, they seem to be .so files but when I downloaded libtorch, it seemed to be in .dylib files?

@danieldk
Copy link
Contributor

danieldk commented Dec 27, 2021

@danieldk so patchelf would add all of the libtorch libraries into the binary?

No, the libraries are dynamically loaded. If you want to link everything into a single binary, you need a static build of libtorch (I am not sure if libtorch static builds are currently functional, the stuff included in the libtorch archive is incomplete). You probably need to patch tch-rs as well to work with static libtorch libraries.

patchelf makes it possible to hardcode the path of the dynamic libraries in the binary (e.g. in the same directory as the binary), so that you don't need LD_LIBRARY_PATH or a system-wide install.

However, in that example, they seem to be .so files but when I downloaded libtorch, it seemed to be in .dylib files?

Right, this is for Linux, which uses .so as the shared library extension. .dylib files are macOS shared libraries, but since macOS uses Mach-O format, you cannot use patchelf. For macOS binaries, you can use install_name_tool to similar effect.

Another option on macOS is to add the libtorch library path to the DYLD_LIBRARY_PATH variable on macOS.

@rhobro
Copy link
Author

rhobro commented Dec 27, 2021

@danieldk thank you. This is proving to be a harder task.

When programs use the c++ API of libtorch, is it packaged into the binary or is it loaded in a similar fashion?

Would it be possible to take libtorch's code and interact with it using swig or something so that it's in the same binary?

@danieldk
Copy link
Contributor

When programs use the c++ API of libtorch, is it packaged into the binary or is it loaded in a similar fashion?

As long as you build libtorch as a dynamic library, it doesn't matter if you use libtorch directly through the C++ API or tch-rs (which also uses the C++ API). You need to provide the Torch libraries in addition to you program. It's just a property of dynamic libraries and has very little to do with Torch itself. As I said in the earlier comment, you could try to do a static build of libtorch, which would allow you to link libtorch into a binary.

If distributing libtorch is too heavy, you should look at many of the other deployment options, like ONNX.

Though this is probably getting a bit off-topic...

@rhobro
Copy link
Author

rhobro commented Dec 27, 2021

Ok. I'll try a static build.

Haha yes. I thought about onnx but couldn't find any way to train models with it in rust. Hence why I'm thinking about tch-rs.

@mrvollger
Copy link

I have a rust binary that uses tch and I'd also like to be able to statically link libtorch, and I am trying to follow this thread so I can get started.

From what I understand I would start by getting the static libtorch: e.g.:

 wget https://download.pytorch.org/libtorch/cu116/libtorch-cxx11-abi-static-with-deps-1.12.0%2Bcu116.zip

And then the build.rs script won't work because it looks for .so files, so I would need to create a patch that allows it to search for libtorch.a in addition to libtorch.so?

Thanks in advance!

@LaurentMazare
Copy link
Owner

@mrvollger Static linking should be supported in the latest version, some details in the readme.

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

No branches or pull requests

4 participants