Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BIT-636] Change u16 weight normalization to max-upscaling #1241

Merged
merged 5 commits into from
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions bittensor/utils/weight_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def convert_weight_uids_and_vals_to_tensor( n: int, uids: List[int], weights: Li
"""
row_weights = torch.zeros( [ n ], dtype=torch.float32 )
for uid_j, wij in list(zip( uids, weights )):
row_weights[ uid_j ] = float( wij ) / float(U16_MAX)
row_weights[ uid_j ] = float( wij ) # assumes max-upscaled values (w_max = U16_MAX).
row_sum = row_weights.sum()
if row_sum > 0:
row_weights /= row_sum # normalize
return row_weights

def convert_bond_uids_and_vals_to_tensor( n: int, uids: List[int], bonds: List[int] ) -> 'torch.LongTensor':
Expand Down Expand Up @@ -126,12 +129,13 @@ def convert_weights_and_uids_for_emit( uids: torch.LongTensor, weights: torch.Fl
if sum(weights) == 0:
return [],[] # Nothing to set on chain.
else:
weights = [ float(value) / sum(weights) for value in weights] # Initial normalization.
max_weight = float(max(weights))
weights = [float(value) / max_weight for value in weights] # max-upscale values (max_weight = 1).

weight_vals = []
weight_uids = []
for i, (weight_i, uid_i) in enumerate(list(zip(weights, uids))):
uint16_val = int(float(weight_i) * int(U16_MAX)) # convert to int representation.
uint16_val = round(float(weight_i) * int(U16_MAX)) # convert to int representation.

# Filter zeros
if uint16_val != 0: # Filter zeros
Expand Down