Segfault using data_ptr() after torch::nn::functional::interpolate()

New to PyTorch. Getting a segfault on Jetson NX after call to torch::nn:functional::interpolate() and could use some hints. Code is similar to this:

auto options1 = torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCUDA).requires_grad(false);
auto t = torch::from_blob(ptr, {4, 34, 160, 160}, options1);

// code to populate tensor here ...

// then when it comes time to interpolate, this is what I'm trying to run:
const auto options2 = torch::nn::functional::InterpolateFuncOptions().size(std::vector<int64_t>({320,320})).mode(torch::kBilinear).align_corners(false);
auto resized_output = torch::nn::functional::interpolate(tensors[1], options2);

// now how do we get access to the data without segfaulting?
float * new_tensor_ptr = reinterpret_cast<float*>(resized_output.data_ptr());
std::cout << "first byte of new tensor: " << new_tensor_ptr[0] << std::endl; // <-- this segfaults

Could you post the backtrace via:

gdb --args ./my_app

Also, how did you install PyTorch and which version are you using?

Dug a bit into this code again. The tensor is created like this:

const auto options = torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCUDA).requires_grad(false);
auto t = torch::from_blob(ptr, {4, 34, 160, 160}, options);

This will eventually segfault when we attempt to dereference data_ptr(). But if instead we use these options:

const auto options = torch::TensorOptions().dtype(torch::kFloat32).requires_grad(false);

…then everything works fine. Note the removal of .device(torch::kCUDA).

I’ve looked through the github issues looking for segfault related to data_ptr() and torch::kCUDA, and found some things such as libtorch Segmentation Fault: RHEL 7 - easy to reproduce · Issue #19969 · pytorch/pytorch · GitHub. I did attempt to call cudaDeviceSynchronize() just prior to getting data_ptr() but that did not help.

PyTorch is v1.7, cloned from github in early 2021-02, and built manually on a Jetson NX. It does have the NVIDIA pytorch patch pytorch-1.7-jetpack-4.4.1.patch obtained from the NVIDIA Jetson site along with the instructions on how to build.

Thanks for the update.
Where is ptr coming from and did you make sure that the allocated memory is still alive and on the GPU?
This post shows how to use cv::cuda::GpuMat as the input to torch::from_blob.