7

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?

imbr
  • 6,226
  • 4
  • 53
  • 65
  • 2
    Can you use [`TensorOptions`](https://github.com/pytorch/pytorch/blob/master/c10/core/TensorOptions.h) 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)` – JoshVarty Oct 30 '19 at 18:30

3 Answers3

13

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);
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
  • no it doesn't work, it still creates on CPU, a `to.device` is still needed – imbr Jan 24 '20 at 12:13
  • 3
    Does it? How can you tell if it's on the CPU or GPU? – JacKeown Feb 06 '20 at 21:47
  • 2
    Yes it works! you are right @JacKeown I was confused by this issue here https://github.com/pytorch/pytorch/issues/12506#issuecomment-429573396 – imbr Feb 14 '20 at 17:03
  • 1
    This doesn't seem to work for me anymore... I've raise a question about this at: https://discuss.pytorch.org/t/create-tensor-on-cuda-device-in-c/113232 – JoshVarty Feb 27 '21 at 21:01
  • 1
    from_blob with kCUDA in options raise an error for me, from_blob()clone().to(torch::kCUDA) works. see this: https://github.com/pytorch/pytorch/issues/15426#issuecomment-451225787 – xiaobing Sep 15 '21 at 04:52
6

I usually use:

torch::Tensor tharray = torch::tensor({1, 2, 3, 4, 5}, {torch::kFloat64});
  • well fine people got happy with your answer but the question is regarding a legacy C/C++ array pointer. – imbr Jan 27 '22 at 13:23
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

abggcv
  • 461
  • 5
  • 20
helloswift123
  • 133
  • 4
  • 13