File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
1
# The MIT License (MIT)
2
2
# Copyright © 2021 Yuma Rao
3
3
# Copyright © 2023 Opentensor Foundation
4
+ from math import floor
4
5
5
6
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
7
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
@@ -649,4 +650,20 @@ def normalize_children_and_proportions(
649
650
"""
650
651
total = sum (prop for prop , _ in children )
651
652
u64_max = 2 ** 64 - 1
652
- return [(int (prop * u64_max / total ), child ) for prop , child in children ]
653
+ normalized_children = [
654
+ (int (floor (prop * (u64_max - 1 ) / total )), child ) for prop , child in children
655
+ ]
656
+ sum_norm = sum (prop for prop , _ in normalized_children )
657
+
658
+ # if the sum is more, subtract the excess from the first child
659
+ if sum_norm > u64_max :
660
+ if abs (sum_norm - u64_max ) > 10 :
661
+ raise ValueError (
662
+ "The sum of normalized proportions is out of the acceptable range."
663
+ )
664
+ normalized_children [0 ] = (
665
+ normalized_children [0 ][0 ] - (sum_norm - (u64_max - 1 )),
666
+ normalized_children [0 ][1 ],
667
+ )
668
+
669
+ return normalized_children
Original file line number Diff line number Diff line change @@ -62,8 +62,8 @@ def float_to_u64(value: float) -> int:
62
62
if not (0 <= value < 1 ):
63
63
raise ValueError ("Input value must be between 0 and 1" )
64
64
65
- # Convert the float to a u64 value
66
- return int (value * (2 ** 64 - 1 ))
65
+ # Convert the float to a u64 value, take the floor value
66
+ return int (math . floor (( value * (2 ** 64 - 1 )))) - 1
67
67
68
68
69
69
def u64_to_float (value : int ) -> float :
You can’t perform that action at this time.
0 commit comments