Double* to tensor and back

I have a model that excepts an input of shape [1, 1, 1000]. I am creating a real-time application in which I would like to feed in a buffer (of type double*) into the model, and then write the output back into the buffer. I am having trouble with the double* to torch::Tensor conversion and back.

Example Python:

input = torch.rand(1, 1, 1000)
input = model(input)

C++:

double* input; // written to by some other function
int len = 1000;
torch::jit::script::Module model; // loaded in from torchscript
/* 
Now how do I convert input into a tensor of shape [1, 1, len],
pass it through the model, and convert it back into an array of
type double*
*/

// EDIT: figured out conversion to tensor
std::vector<torch::jit::IValue> input_tensors;
input_tensors.push_back(torch::from_blob(input, {1, 1, len}, torch::kFloat64));
torch::Tensor output_tensor = model.forward(input_tensors).toTensor();
// now how do copy back into double*

Thanks in advance!

I think you can use torch::from_blob.
Take a look at the comment in this old issue
https://github.com/pytorch/pytorch/issues/37201
You need to specify torch::kFloat64 as the input type in torch::from_blob in your case.

Thanks! Using from_blob I can create a tensor from double*, now how would I get it back into double* after passing through the model?

I think I figured it out: I can use std::memcpy():

double* input;  // address of input
double* output; // address of output
int len = 1000; // length of input and output buffers
torch::jit::script::Module model; // model loaded in from torchscript

// Convert input to tensor
std::vector<torch::jit::IValue> input_tensors;
input_tensors.push_back(torch::from_blob(input, {1, 1, len}, torch::kFloat64));

// Forward pass
torch::Tensor output_tensor = model.forward(input_tensors).toTensor();

// Copy into output
std::memcpy(output_tensor.data_ptr(), output, sizeof(double)*len);