yichong96:

do you mind explaining what the phrase “without taking ownership of the original data.” means? from the torch::from_blob docmentation.


This is what I meant with

using from_blob requires you to keep the pointer array alive for the lifetime of the returned tensor.

So you pass in a pointer to from_blob but PyTorch “not taking ownership” means that PyTorch doesn’t know where it comes from or tries to do anything with it. This means that you as the caller are responsible for the lifetime of the memory blob:

making sure the memory is alive while PyTorch has references to the tensor (e.g. if you have a local std::vector<float> v and you use from_blob on its v.data(), you run into trouble if v goes out of scope while the tensor is still in use. (On the other hand, if you do copying with .clone() or computation, like /255., and don’t use the original anymore, you are OK.
deallocating the memory eventually. PyTorch can help you by signalling when it is done with the tensor returned by from_blob if you pass in a deleter .

Best regards
Thomas