Why does `is_same` in the C++ PyTorch API fail when comparing with the same tensor that is read from a file?
Why does torch::Tensor::is_same fail the following assertion? A tensor is written to a file using the C++ PyTorch API, then read again into another tensor, and is_same compares both tensors:
torch::Tensor x_sequence = torch::linspace(0, M_PI, 1000);    
torch::save(x_sequence, &quot;x_sequence.dat&quot;);
torch::Tensor x_read;
torch::load(x_read, &quot;x_sequence.dat&quot;);
assert(x_read.is_same(x_sequence));  

This results in:
int main(int, char**): Assertion `x_read.is_same(x_sequence)' failed.

using

python-pytorch, version 1.6.0-2 on Arch Linux
g++ (GCC) 10.1.0


torch::Tensor::is_same(const torch::Tensor&amp; other)is defined here. It is important to notice that a Tensor is actually a pointer on an underlying TensorImpl class (which actually holds the data).
Thus, when you call is_same, what is checked is actually whether or not your pointers are the same, i.e whether your 2 tensors are pointing to the same underlying memory. Here is a very simple example to understand it well :
auto x = torch::randn({4,4});
auto copy = x;
auto clone = x.clone();
std::cout &lt;&lt; x.is_same(copy) &lt;&lt; &quot; &quot; &lt;&lt; x.is_same(clone) &lt;&lt; std::endl;
&gt;&gt;&gt; 0 1

Here, the call to clone forces pytorch to copy the data in another memory location. Consequently, the pointers are different and is_same returns false.
If you want to actually compare the values, you have no choice but to compute the difference between the two tensors and compute how close to 0 this difference is.
