I am trying to access PyTorch from c code. This needs creating a tensor in c which I can pass to a function in python. I used to do this with torch like this:
and pass to the lua functions.
But this won’t work anymore since now I need a python object. One workaround would be to use numpy arrays as a interface. I can create a numpy array from c and pass this to python code and create a PyTorch tensor from this numpy object. But this means I have to link my project against numpy as well and this seems to be unnecessarily complicated. Can someone please suggest some better alternative.
Mmm, I guess you can exploit same approach, as I did: create some PyTorch tensors beforehand (using Python’s C API, for example), grab theirs data_ptr’s and use them to pass data between Python and C. You need to know exact dimensions, strides and types of tensors if you plan to use it with C APIs.
@hughperkins Really sorry for the late reply. I started with something and due to deadlines, I scrapped the idea and completely forgot about this post. I was trying to use PyTorch with C++ for inference. I used to do this with Lua Torch by invoking the Lua interpreter from C++ using Lua Torch’s C interface. I was trying to do the same with PyTorch. I will come back to this in a couple of months. I have to do some reading on all the available mechanisms of doing this. Any suggestions are welcome.
Note that pytorch is in part a wrapper around a c++ library called THNN. In theory, you might be able to call into that directly, skipping much of the python bit altogether. You miss out on some of the python-space libraries, so it really depends on what you need/are doing.