How to create a gpu tensor base on data_ptr?

int main(){
    std::cout<<torch::cuda::is_available()<<std::endl;
    auto tensor=torch::randn({3,10},torch::kCUDA);
    auto p=tensor.contiguous().data_ptr<float>();
    torch::Tensor te=torch::from_blob(p,{3,10});
    std::cout<< te<<std::endl;
    return 0;
}

then I got “Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)”
Any advice?

It should work, if you specify the right device in from_blob:

auto t = at::empty({1, 2, 3}, TensorOptions(kCUDA, 0));
auto t_ = from_blob(t.data_ptr(), {1, 2, 3}, kCUDA);