Skip to content

Commit

Permalink
Fix corner cases global shift gen
Browse files Browse the repository at this point in the history
In some corner cases, the global shift factor was generated as a
number < 0 (down to -inf...). This makes no sense, so now the global
shift must be 0 at a minimum.
  • Loading branch information
FrancescoConti committed Aug 21, 2024
1 parent 13dd71f commit c20c03c
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/NnxTestClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ def _calculate_global_shift(
"""Calculate global shift so that the output values are in the range of out_type"""
s = tensor.type(torch.float64).std()
target_s = 2 ** (out_type._bits - 1)
return torch.ceil(torch.log2(s / target_s)).type(torch.int32)
shift = torch.ceil(torch.log2(s / target_s)).type(torch.int32)
if shift < 1:
return torch.zeros((1,)).type(torch.int32)
else:
return shift

@staticmethod
def _random_data(_type: IntegerType, shape: Tuple, extremes: Tuple = None):
Expand Down

0 comments on commit c20c03c

Please sign in to comment.