Skip to content

Commit

Permalink
Fix (core/quant/float): use eps to avoid log(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe5 committed May 23, 2024
1 parent eeffd2b commit 8a9196b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/brevitas/core/quant/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ def __init__(
self.scaling_impl = scaling_impl
self.float_clamp_impl = float_clamp_impl

# To avoid log(0), we add small a small value based on the used dtype
self.eps = torch.finfo(dtype).tiny

@brevitas.jit.script_method
def internal_scale(self, x):
internal_scale = floor_ste(torch.log2(torch.abs(x))) - self.mantissa_bit_width()
internal_scale = floor_ste(torch.log2(torch.abs(x) + self.eps)) - self.mantissa_bit_width()
internal_scale = torch.clamp_min(internal_scale, self.fp_internal_scale_min())
internal_scale = torch.exp2(internal_scale)
return internal_scale
Expand Down

0 comments on commit 8a9196b

Please sign in to comment.