Skip to content

Commit

Permalink
Fix (core/quant/float): use eps to avoid log(0) (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe5 authored May 28, 2024
1 parent b535615 commit 486a6df
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/brevitas/core/quant/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ 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
if dtype is None:
dtype = torch.get_default_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 486a6df

Please sign in to comment.