I think nn.InstanceNorm2d might use the biased variance, so changing the variance calculation to:
x_var = torch.var(x, unbiased=False, axis=(2,3), keepdims=True)

should work (I haven’t checked the source code of nn.InstanceNorm2d, but batchnorm layers use the biased variance, so I just tested your code quickly  ).