I am trying to sweep some parameters in a very narrow range of values and I use bfloat16 epsilong as the step size to avoid issues such a-b==a for very small b, however I chanced upon this situation:
import torch
test = torch.Tensor([2.8438]).to(torch.bfloat16)
delta=torch.finfo(torch.bfloat16).eps
test - delta == test
Out[1]: tensor([True])
I thought the purpose of the epsilon is to precisely avoid this kind of issues. What am I doing wrong? How can I get the next smallest number? At the moment I am using:
if test - delta == test:
test = test - delta*2
else:
test = test - delta
And this works, but I am concerned my understanding of the epsilon is not correct..?
From Pytorch docs:
As in your example
2.8438>2*1.0, it has exponent greater than the exponent of1.0, so2.8438 + eps!=2.8438doesn't hold.Update. For an arbitrary number, we probably can shift
epsby the exponent of that number as: