9

Using the C++ libtorch api for Pytorch

I want to create a torch::Tensor from a C++ double[] array, legacy C/C++ pointer. 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?

1
  • 2
    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) Commented Oct 30, 2019 at 18:30

3 Answers 3

16

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);
Sign up to request clarification or add additional context in comments.

5 Comments

no it doesn't work, it still creates on CPU, a to.device is still needed
Does it? How can you tell if it's on the CPU or GPU?
Yes it works! you are right @JacKeown I was confused by this issue here github.com/pytorch/pytorch/issues/12506#issuecomment-429573396
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/113232
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-451225787
8

I usually use:

torch::Tensor tharray = torch::tensor({1, 2, 3, 4, 5}, {torch::kFloat64});

1 Comment

well fine people got happy with your answer but the question is regarding a legacy C/C++ array pointer.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.