It is crucial that this isn’t about the “theoretical maths” but about numerical precision.
So this:
a = torch.randn(5,5)
torch.zeros(10,10)
b[:5,:5] = a
a.sum() - b.sum()

will typically give something like tensor(-5.3644e-07).
Even
a.sum() - b[:5,:5].sum()
will usually not give exactly zero.
This is because the PyTorch functions make no guarantee in which order the 25 nonzero elements are summed and the sum actually depends on the order due to limited numerical precision.
Best regards
Thomas