Convert Torch Tensor to flattened C++ array

How do I convert a Torch tensor to a C++ array without looping over all the dimensions ( I can do this, but is terribly slow) and std::memcpy has undefined behavior.

Using memcpy like this.

std::array<float, 327600> output_data;
at::Tensor output = *from some instructions*
std::memcpy(output_data.data(),output.data_ptr(),327600*sizeof(float));
1 Like

I didn’t get the same undefine behavior using g++. But I guess

Maybe you missed the usage of template version of data_ptr:

output.data_ptr<float>()

The output.data_ptr() returns a void pointer, and sometimes memcpy btwn pointers with different types has undefined behavior

Thanks for the reply.

The problem was the non-contiguous memory distribution for the tensor. So the raw memory copy failed. Using the contiguous method on the tensor and returning the data pointer worked

2 Likes

Could you help me to understand in details ?

I use these code, but it do not work:

result is a torch::Tensor object, it is on cpu.

result = result.contiguous();
std::memcpy(ret_array,_result.data_ptr(),sizeof(float)*_result.numel());

Like python, torch::Tensor has the view method to
You could do the following -

#include<torch/torch.h>

auto tensor = torch::rand({1, 2, 3 ,5});
tensor = tensor.view(tensor.size(0), -1); //Here the shape of the data is batch, num_tensors
tensor = tensor.contiguous();
std::vector<float> vector(tensor.data_ptr<float>(), tensor.data_ptr<float>()+tensor.numel());