Why hasn't a's ID changed?
a = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
b = torch.tensor([5.0, 5.0, 5.0])
los = sum(a*b)
los.backward()
print(id(a.grad))
a.grad = torch.tensor([1., 2., 3.])
print(id(a.grad))


The output:
2792503915000
2792503915000

Why didn't the ID of a.grad change?

Python's id() simply returns a unique number for the object. IDs of objects with non-overlapping lifetimes can be the same; that is, when an object is destroyed, and then a new object is created, the new object can have the same ID as the last. Python object IDs are often compared to memory addresses in C, and (apparently) in some python implementations, they are just memory addresses.
For instance, consider the following snippet:
class a:
    pass

my_a = a()
print(id(my_a))
my_a = a()
print(id(my_a))
my_a = a()
print(id(my_a))

With my particular python implementation, an example output was:
139647922983888
139647922986768
139647922983888

The first ID was repeated in the third instance; this is permissible by the id() api.
