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));
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
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());