Check two tensor is hardware level same

I want to check if two tensors are exactly the same. You need to check that the nature of the tensors is the same, not allclose the same way.
Does a function foo exist that works like this? foo must not change the state of its input tensors.

X = Tensor(...)
X2 = X.copy()
Y = nn.Parameter(X)
foo(X, X) == True
foo(X, Y) == True
foo(Y, Y) == True
foo(X2, X) == False
foo(X2, Y) == False

I guess you want to check the identity of the underlying data, so using .data_ptr() should work:

def foo(a, b):
    if isinstance(a, nn.Parameter):
        a = a.data
    if isinstance(b, nn.Parameter):
        b = b.data
    return a.data_ptr() == b.data_ptr()
    
X = torch.randn(1)
X2 = X.clone()
Y = nn.Parameter(X)

foo(X, X) == True
foo(X, Y) == True
foo(Y, Y) == True
foo(X2, X) == False
foo(X2, Y) == False
1 Like

thanks for the reply!