PyTorch tensors from C++ to Python


(Daniele Molinari) #1

Hi everybody,
I need to embed a Python interpreter in my C++ application and I want to pass Tensor s back and forth from C++ and Python.
I know that Boost.Python should fit this purpose, but I’m puzzled on how should I handle the Tensors exchange. My guess is that it should be relatively easy, considering that the C++ and Python frontend probably use the same underlying types. Can somebody point me in the right direction?
Thanks


(Will Feng) #2

THPVariable_Wrap(Variable var) in https://github.com/pytorch/pytorch/blob/63214b572b6a18f7328687ca69420f0be0eaaaa9/torch/csrc/autograd/python_variable.cpp#L64-L76 is how we wrap a Variable (can be cast from torch::Tensor) into a Python object. Using Boost.Python there might be a way to pass the Variable C++ object to Python and construct the Python object.


(Daniele Molinari) #3

Thank you! I will look at it!