CUDA error: invalid argument (getDeviceFromPtr

Hi,

I’m loading a model trained in python via torchscript to C++.
I’m doing it according to the tutorial: https://pytorch.org/tutorials/advanced/cpp_export.html

I have a network operating with CUDA.
I’m transforming the input tensor from a vector via tensor::from_blob (data is a vector containing all values of a matrix) :

torch::Device device(at::kCUDA);
inputs.push_back(torch::from_blob(data.data(), {375,375}, device));

I get this error:

terminate called after throwing an instance of ‘c10::Error’
what(): CUDA error: invalid argument (getDeviceFromPtr at …/…/aten/src/ATen/cuda/CUDADevice.h:13)

i don’t know what to do, if I set the input to torch::ones everything works fine:

torch::Device device(at::kCUDA);
inputs.push_back(torch::ones({375, 375}, device));

(no error)

Thank you

Found the solution and posting it here, in case someone else needs it. i had to call insert a clone to make everything work:

inputs.push_back(torch::from_blob(data.data(), {375,375}).clone().to(device));

1 Like

The clone is not required for me. Just make sure you don’t pass the device in the options argument for torch::from_blob.