Skip to content

Commit

Permalink
reapply fix on top of latest master
Browse files Browse the repository at this point in the history
  • Loading branch information
ifrit98 committed Mar 1, 2024
2 parents 7b11ff2 + 1654334 commit 680168a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 32 deletions.
9 changes: 3 additions & 6 deletions bittensor/extrinsics/set_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ def set_weights_extrinsic(
)

if not wait_for_finalization and not wait_for_inclusion:
return (
True,
"Not waiting for finalization or inclusion. Assume successful.",
)
return True, "Not waiting for finalization or inclusion."

if success == True:
bittensor.__console__.print(
Expand All @@ -113,7 +110,7 @@ def set_weights_extrinsic(
prefix="Set weights",
sufix="<green>Finalized: </green>" + str(success),
)
return True, "Success."
return True, "Successfully set weights and Finalized."
else:
bittensor.__console__.print(
":cross_mark: [red]Failed[/red]: error:{}".format(error_message)
Expand All @@ -122,7 +119,7 @@ def set_weights_extrinsic(
prefix="Set weights",
sufix="<red>Failed: </red>" + str(error_message),
)
return False, str(error_message)
return False, error_message

except Exception as e:
# TODO( devs ): lets remove all of the bittensor.__console__ calls and replace with loguru.
Expand Down
99 changes: 73 additions & 26 deletions bittensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ def set_weights(
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
prompt: bool = False,
max_retries: int = 5,
) -> bool:
"""
Sets the inter-neuronal weights for the specified neuron. This process involves specifying the
Expand All @@ -653,25 +654,43 @@ def set_weights(
version_key (int, optional): Version key for compatibility with the network.
wait_for_inclusion (bool, optional): Waits for the transaction to be included in a block.
wait_for_finalization (bool, optional): Waits for the transaction to be finalized on the blockchain.
prompt (bool, optional): If ``True``, prompts for user confirmation before proceeding.
prompt (bool, optional): If True, prompts for user confirmation before proceeding.
max_retries (bool, optional): The maximum number of retries for setting weights.
Returns:
bool: ``True`` if the setting of weights is successful, False otherwise.
This function is crucial in shaping the network's collective intelligence, where each neuron's
learning and contribution are influenced by the weights it sets towards others【81†source】.
"""
return set_weights_extrinsic(
subtensor=self,
wallet=wallet,
netuid=netuid,
uids=uids,
weights=weights,
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
prompt=prompt,
uid = self.get_uid_for_hotkey_on_subnet(
wallet.hotkey.ss58_address, netuid
)
retries = 0
success = False
message = "No attempt made. Perhaps it is too soon to set weights!"
while (
self.blocks_since_last_update(netuid, uid) > self.weights_rate_limit(netuid)
and retries < max_retries
):
try:
success, message = set_weights_extrinsic(
subtensor=self,
wallet=wallet,
netuid=netuid,
uids=uids,
weights=weights,
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
prompt=prompt,
)
except Exception as e:
bittensor.logging.error(f"Error setting weights: {e}")
finally:
retries += 1

return success, message

def _do_set_weights(
self,
Expand Down Expand Up @@ -704,22 +723,37 @@ def _do_set_weights(
trust in other neurons based on observed performance and contributions.
"""

response = self.send_extrinsic(
wallet=wallet,
module="SubtensorModule",
function="set_weights",
params={
"dests": uids,
"weights": vals,
"netuid": netuid,
"version_key": version_key,
},
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
@retry(delay=2, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
with self.substrate as substrate:
call = substrate.compose_call(
call_module="SubtensorModule",
call_function="set_weights",
call_params={
"dests": uids,
"weights": vals,
"netuid": netuid,
"version_key": version_key,
},
)
# Period dictates how long the extrinsic will stay as part of waiting pool
extrinsic = substrate.create_signed_extrinsic(
call=call, keypair=wallet.hotkey, era={"period": 5},
)
response = substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True, "Not waiting for finalziation or inclusion."

if not wait_for_inclusion and not wait_for_finalization:
return True, "Not waiting for inclusion or finalization."
response.process_events()
if response.is_success:
return True, "Successfully set weights."
else:
return False, response.error_message

response.process_events()
if response.is_success:
Expand Down Expand Up @@ -2797,6 +2831,19 @@ def tempo(self, netuid: int, block: Optional[int] = None) -> int:
return None
return self.query_subtensor("Tempo", block, [netuid]).value

def blocks_since_last_update(self, netuid: int, uid: int) -> int:
if not self.subnet_exists(netuid):
return None
return (
self.get_current_block()
- self.query_subtensor("LastUpdate", None, [netuid]).value[uid]
)

def weights_rate_limit(self, netuid: int) -> int:
if not self.subnet_exists(netuid):
return None
return self.query_subtensor("WeightsSetRateLimit", None, [netuid]).value

##########################
#### Account functions ###
##########################
Expand Down

0 comments on commit 680168a

Please sign in to comment.