pytorch / cppdocs

PyTorch C++ API Documentation
https://pytorch.org/cppdocs
206 stars 33 forks source link

how to install or use libtorch for pythoners knowing a little about c++ #3

Closed walkacross closed 5 years ago

walkacross commented 5 years ago

hey, thanks for this great framework and the c++ API design philosophy is really user-friendly for old pytorch user using python.

could you please provide some more concrete examples about (1) how to install and use libtorch not only with cmake ( add g++ example?); (2) what is the best practice to use pybind to invoke c++ tensor functions or models in python. considering some pythoners do have the desire to use libtorch and conduct mix programming but with knowing a little about the c++.

@goldsborough @soumith @apaszke

have a nice day.

goldsborough commented 5 years ago

For the first, this is a bit tricky. I can give you the command, which I mostly got by using cmake and then make VERBOSE=1. For CPU:

$ LIBTORCH=/path/to/unzipped/libtorch
$ c++ -I"$LIBTORCH/include" -I"$LIBTORCH/include/torch/csrc/api/include" -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 example-app.cpp -o example-app -Wl,-rpath "$LIBTORCH/lib" "$LIBTORCH/lib/libtorch.so" "$LIBTORCH/lib/libcaffe2.so" "$LIBTORCH/lib/libc10.so"

Where example-app.cpp is the example from https://pytorch.org/cppdocs/installing.html. The reason I say it's tricky is that for us cmake is effectively an abstraction over the exact build steps required. I can tell you with certainty that the names of the libraries to link against will change in the future, for example, or maybe we'll require a new include directory, or the flags will change etc. With cmake, we can do those changes in the background and your cmake will just continue to work in the future with no effort on your side. For this reason, you're free to use the above command, but I'd rather not put this in the documentation website itself.

For the second point of mixing C++ with Python, it depends on what you want to do:

  1. If you want to write a C++ function that uses C++ tensors and bind it into Python, you can use our very well supported C++ extensions: https://pytorch.org/tutorials/advanced/cpp_extension.html
  2. If you want to write a whole model in the C++ frontend and bind into you existing Python models, you will have to patience yourself sightly for this to work 100%. We do provide pybind11 bindings for C++ models, they just don't inter-operate well with Python modules yet. You can find an example of binding this model into Python here. I'm working on the Python inter-op right now.

Let me know if this answers your questions.

walkacross commented 5 years ago

Yep, the answer is clear, I got it. Thanks for your time and kind response.

goldsborough commented 5 years ago

Sure. I will write a tutorial for binding the C++ frontend into Python once that is more mature. Feel free to open more issues with suggestions or questions in the meantime.

sangeeta-yadav-iisc commented 5 years ago

I am having a package with its own cmake. I am trying to like the libraries of libtorch with it . But after successful cmake following error is coming out error: macro "Error" passed 4 arguments, but takes just 1 AT_ASSERT(impl == nullptr || impl->type() == device.type()); ^

eusoubrasileiro commented 4 years ago

@goldsborough your answer totally save my life while trying to compile against libtorch directly on visual studio 2019. Tip use #pragma warning (disable : 4146) to ignore stupid 4146 warning assumed as error by vstudio.