Creating tensor from c

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:

   THFloatTensor* tensor = THFloatTensor_newWithSize3d( 3, rows,cols);

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.

This discussion is doing the exact opposite thing–returning from python to c Constructing PyTorch's CUDA tensor from C++ with image data already on GPU:


Just out of personal curiosity, what are you trying to access in the python code, that is unavailable in the THNN C code?

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.

1 Like

@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.

@TomLiftoff Ya, this seems to be a simple solution. I will try this. Thanks and sorry for very late response.

1 Like

@SelvamArul Glad you solved the issue, using @TomLiftoff’s approach.

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.