Create a torch::Tensor from C/C++ array without using "from_blob(...)..."
Asked Answered
B

3

8

Using the C++ libtorch frontend for Pytorch

I want to create a torch::Tensor from a C++ double[] array. Comming from a legacy C/C++ API. I could not find a simple documentation about the subject not in docs nor in the forums.

Something like:

double array[5] = {1, 2, 3, 4, 5};   // or double *array;
auto tharray = torch::Tensor(array, 5, torch::Device(torch::kCUDA));

The only thing I found is to use torch::from_blob but then I would have to clone() and use to(device) if I wanted to use it with CUDA.

double array[] = { 1, 2, 3, 4, 5};. // or double *array;
auto options = torch::TensorOptions().dtype(torch::kFloat64);
torch::Tensor tharray = torch::from_blob(array, {5}, options);

Is there any cleaner way of doing so?

Byelorussian answered 30/10, 2019 at 18:14 Comment(1)
Can you use TensorOptions to set the device at the same time you create your tensor? Something like auto options = torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCUDA, 1)Adallard
A
14

You can read more about tensor creation here: https://pytorch.org/cppdocs/notes/tensor_creation.html

I don't know of any way to create a tensor from an array without using from_blob but you can use TensorOptions to control various things about the tensor including its device.

Based on your example you could create your tensor on the GPU as follows:

double array[] = { 1, 2, 3, 4, 5};
auto options = torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCUDA, 1);
torch::Tensor tharray = torch::from_blob(array, {5}, options);
Adallard answered 30/10, 2019 at 18:37 Comment(5)
no it doesn't work, it still creates on CPU, a to.device is still neededByelorussian
Does it? How can you tell if it's on the CPU or GPU?Butlery
Yes it works! you are right @Butlery I was confused by this issue here github.com/pytorch/pytorch/issues/12506#issuecomment-429573396Byelorussian
This doesn't seem to work for me anymore... I've raise a question about this at: discuss.pytorch.org/t/create-tensor-on-cuda-device-in-c/113232Adallard
from_blob with kCUDA in options raise an error for me, from_blob()clone().to(torch::kCUDA) works. see this: github.com/pytorch/pytorch/issues/15426#issuecomment-451225787Friction
T
8

I usually use:

torch::Tensor tharray = torch::tensor({1, 2, 3, 4, 5}, {torch::kFloat64});
Tjaden answered 23/7, 2020 at 18:34 Comment(1)
well fine people got happy with your answer but the question is regarding a legacy C/C++ array pointer.Byelorussian
M
1

I do not have cleaner way, but I would give you another way to do it.

double array[5] = {1, 2, 3, 4, 5};
auto tharray = torch::zeros(5,torch::kFloat64) //or use kF64
std::memcpy(tharray.data_ptr(),array,sizeof(double)*tharray.numel())

Hope its helpful.

Corrected KF64 to kFloat64 because KF64 does not exist in torch

Mastermind answered 1/4, 2020 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.