What do words “exposes” and “without taking ownership” in from_blob
Exposes the given
data
as aTensor
without taking ownership of the original data.
actually mean? Are internally copy perations done on data
by
at::[Tensor](https://pytorch.org/cppdocs/api/classat_1_1_tensor.html#_CPPv4N2at6TensorE)
torch::
from_blob (void **data* , at::IntArrayRef *sizes* , at::IntArrayRef *strides* , *const* at::TensorOptions &*options* = at::TensorOptions())
?
Is it possible to have a huge C array in memory and use from_blob
to “view” it as a Tensor
and also modify the arrays data? Example:
#include <torch/torch.h>
#include <iostream>
#include <array>
int main ()
{
float array[] = {1.23, 2.34, 3.45, 4.56, 5.67};
auto options = torch::TensorOptions().dtype(torch::kFloat32);
torch::Tensor t1 = torch::from_blob(array, {5}, options);
std::cout << t1 << std::endl;
t1 = torch::ones_like(t1);
std::cout << t1 << std::endl;
for (auto el : array)
std::cout << el << std::endl;
return 0;
}
I expected this code to overwrite the array elements, but the output shows they are not overwritten,
./example-app
1.2300
2.3400
3.4500
4.5600
5.6700
[ CPUFloatType{5} ]
1
1
1
1
1
[ CPUFloatType{5} ]
1.23
2.34
3.45
4.56
5.67
Are all Tensor
constructors deep-copy operations, or is there one that imposes a “write” view on an array?
If it is not possible to construct a Tensor
without copying data
, what would be the the best way to assign values of t1
(in this simple example, the ones_like
) to array
? Is there a way to get a pointer to the data from Tensor
?