Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libtorch : How to create a gpu tensor base on data_ptr?

Tags:

c++

libtorch

create a gpu tensor base on data_ptr?

  int main{
        auto ten=torch::randn({3,10},torch::kCuda);
        auto p=ten.data_ptr<float>();//I believe "p" is a gpu pointer?
        auto ten2=torch::from_blob(p,{3,10});//ten2's device=cpu,that's the problem
        cout<<ten2;//then I got error:"interrupted by signal 11 SIGSEGV"
    }

Any advice?

like image 365
scirocc Avatar asked Mar 29 '26 23:03

scirocc


1 Answers

I believe you should declare ten2's device to be GPU, like that :

  int main{
    auto ten=torch::randn({3,10},torch::kCUDA);
    auto p=ten.data_ptr<float>();//I believe "p" is a gpu pointer?
    auto options = torch::TensorOptions().device(torch::kCUDA);
    auto ten2=torch::from_blob(p,{3,10}, options);
    cout<<ten2;
}

On a side note, be careful when you use from_blob, the resulting tensor does not take ownership on the underlying memory. So if ten goes out of scope, it will free its memory which is also ten2's underlying data. If you know that ten will go out of scope before ten2, you should make a call to clone :

auto ten2=torch::from_blob(p,{3,10}, options).clone();
like image 67
trialNerror Avatar answered Apr 03 '26 13:04

trialNerror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!