Id of tensors in pytorch

Hi, everyone.
My colleague found a interesting problem when check the id of some tensors.
Here is the code:

import torch

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x = torch.tensor(x, dtype=torch.float32)

pos_idxes = torch.tensor([1, 2, 3, 4], dtype=torch.int64)

idxes = pos_idxes > 2

n = 10
for i in range(n):
    print(f"[{i}]", "x[pos_idxes][idxes]:", id(x[pos_idxes][idxes]), "x[pos_idxes[idxes]]:", id(x[pos_idxes[idxes]]))

Results:

[0] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[1] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[2] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[3] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[4] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[5] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[6] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[7] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[8] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096
[9] x[pos_idxes][idxes]: 2217284501312 x[pos_idxes[idxes]]: 2217284108096

Pytorch should be return a new tensor when use index, it will have a new id of new tensor, but the code’s output make me confused.

Why it give the same id?

Indexing a tensor will create a new Python object with an id, which can be reused by Python.
You are not checking the id of any underlying tensor, its data, etc. but just from the Python object holding the reference to the indexed tensor.

Hi, ptrblck. This is what I get that I just checked the data id, not the reference object id.
If a reference object point to the tensor data, id will give the address of reference object, right?