Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
88f9177
Add commit-reveal subprocess and related utilities
Oct 16, 2024
3046fa9
Refactor log file paths to constants
Oct 16, 2024
d325288
Refactor weight setting with commit-reveal logic.
Oct 16, 2024
0ca94f1
Tests passing, happy path is done.
Oct 17, 2024
616e0d1
Add commit-reveal subprocess and weight handling improvements
Oct 18, 2024
2e3d79f
Add blocks_until_next_epoch method
Oct 18, 2024
ef0e90b
Add version_key parameter and detailed docstrings
Oct 18, 2024
6c36639
Add batch weight reveal functionality
Oct 18, 2024
96f0af3
Add chain hash consistency check and refactor commit reveal
Oct 21, 2024
baa42bf
Change subprocess initialization flag and refactor socket handling.
Oct 22, 2024
86b07f2
Ruff
Oct 22, 2024
8217c6e
Merge conflict.
Oct 22, 2024
ca653c3
Rename and refactor subprocess utilities and related tests
Oct 22, 2024
65b7e89
prepare for emmit on set weights
Oct 22, 2024
dd9c20d
remove convert to emmit
Oct 22, 2024
ba82e55
if weights is a list of floats
Oct 22, 2024
fb0c769
Refactor weight conversion logic in set_weights.
Oct 22, 2024
0ea65c1
Add test for commit-reveal batch weights over limit
Oct 23, 2024
bb60aa5
Enable commit reveal subprocess control and expiry handling
Oct 24, 2024
686b2cc
Add max_retries parameter and improve commit log details
Oct 25, 2024
77d097f
Change weight commit behavior and add debug prints
Oct 25, 2024
727ea44
Refactor subprocess to only delete old commits + update response from…
Oct 29, 2024
e4abb6f
Refactor commit reveal logic and clean up logging
Oct 29, 2024
1da70d8
Reduce max_retries and refactor substrate calls. Fix local and chain …
Oct 30, 2024
96c4584
Refactor commit reveal periods to interval in codebase, add combinati…
Oct 31, 2024
3c30f19
Fix typo in database commit operation
Oct 31, 2024
8f20eab
Ruff
Oct 31, 2024
c0c454d
Merge branch 'staging' into feat/opendansor/cr2
Oct 31, 2024
af1c0f6
Ruff
Oct 31, 2024
116e200
Fix unit test, add prepare values for emmit on commit.
Nov 1, 2024
70246e0
Dont initialize subprocess for unit test.
Nov 1, 2024
26c988f
Refactor subprocess initialization condition.
Nov 1, 2024
4182475
Lint
Nov 1, 2024
50309bc
Refactor subprocess initialization and update network defaults.
Nov 1, 2024
87a2ab2
Refactor logging and subprocess management
Nov 4, 2024
636ec26
Refactor commit and reveal logic, remove prompts
Nov 5, 2024
cb8b314
Merge conficts
Nov 5, 2024
9705134
Refactor commit and reveal logic, remove prompts
Nov 5, 2024
77087df
Lint
Nov 5, 2024
666ec7e
Remove rich prompt
Nov 5, 2024
91e7043
Merge branch 'staging' into feat/opendansor/cr2
basfroman Nov 5, 2024
dd00498
Merge remote-tracking branch 'origin/staging' into feat/opendansor/cr2
Nov 5, 2024
7d23094
merge staging
Nov 5, 2024
8eb20cb
Merge branch 'staging' into feat/opendansor/cr2
ibraheem-abe Nov 5, 2024
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
2 changes: 1 addition & 1 deletion bittensor/core/chain_data/subnet_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SubnetHyperparameters:
max_validators (int): Maximum number of validators.
adjustment_alpha (int): Alpha value for adjustments.
difficulty (int): Difficulty level.
commit_reveal_weights_interval (int): Interval for commit-reveal weights.
commit_reveal_weights_interval (int): Intervals (tempo intervals) for commit-reveal weights.
commit_reveal_weights_enabled (bool): Flag indicating if commit-reveal weights are enabled.
alpha_high (int): High value of alpha.
alpha_low (int): Low value of alpha.
Expand Down
367 changes: 325 additions & 42 deletions bittensor/core/extrinsics/commit_weights.py

Large diffs are not rendered by default.

178 changes: 115 additions & 63 deletions bittensor/core/extrinsics/set_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# DEALINGS IN THE SOFTWARE.

import logging
import random
from typing import Union, Optional, TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -46,11 +47,13 @@ def do_set_weights(
version_key: int = version_as_int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
period: int = 5,
) -> tuple[bool, Optional[str]]: # (success, error_message)
"""
Internal method to send a transaction to the Bittensor blockchain, setting weights for specified neurons. This method constructs and submits the transaction, handling retries and blockchain communication.

Args:
period (int): Period dictates how long the extrinsic will stay as part of waiting pool
self (bittensor.core.subtensor.Subtensor): Subtensor interface
wallet (bittensor_wallet.Wallet): The wallet associated with the neuron setting the weights.
uids (list[int]): List of neuron UIDs for which weights are being set.
Expand All @@ -67,26 +70,10 @@ def do_set_weights(
"""

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
call = self.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 = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
era={"period": 5},
)
def make_substrate_call_with_retry(extrinsic_):
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
extrinsic=extrinsic_,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
Expand All @@ -102,7 +89,23 @@ def make_substrate_call_with_retry():
response.error_message, substrate=self.substrate
)

return make_substrate_call_with_retry()
call = self.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 = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
era={"period": period},
)
return make_substrate_call_with_retry(extrinsic)


# Community uses this extrinsic directly and via `subtensor.set_weights`
Expand Down Expand Up @@ -131,51 +134,100 @@ def set_weights_extrinsic(
Returns:
tuple[bool, str]: A tuple containing a success flag and an optional response message.
"""
# First convert types.
if use_torch():
if isinstance(uids, list):
uids = torch.tensor(uids, dtype=torch.int64)
if isinstance(weights, list):
weights = torch.tensor(weights, dtype=torch.float32)
else:
if isinstance(uids, list):
uids = np.array(uids, dtype=np.int64)
if isinstance(weights, list):
weights = np.array(weights, dtype=np.float32)

# Reformat and normalize.
weight_uids, weight_vals = weight_utils.convert_weights_and_uids_for_emit(
uids, weights
)
get_subnet_hyperparameters = subtensor.get_subnet_hyperparameters(netuid=netuid)
if (
get_subnet_hyperparameters
and get_subnet_hyperparameters.commit_reveal_weights_enabled
):
# if cr is enabled, commit instead of setting the weights.
salt = [random.randint(0, 350) for _ in range(8)]

logging.info(f":satellite: Committing weights on {subtensor.network}...")
try:
# First convert types.
if use_torch():
if isinstance(uids, list):
uids = torch.tensor(uids, dtype=torch.int64)
if isinstance(weights, list):
weights = torch.tensor(weights, dtype=torch.float32)
else:
if isinstance(uids, list):
uids = np.array(uids, dtype=np.int64)
if isinstance(weights, list):
weights = np.array(weights, dtype=np.float32)

# Reformat and normalize.
weight_uids, weight_vals = weight_utils.convert_weights_and_uids_for_emit(
uids, weights
)

logging.info(
f":satellite: <magenta>Setting weights on </magenta><blue>{subtensor.network}<blue> <magenta>...</magenta>"
)
logging.debug(f"Weights: {[float(v / 65535) for v in weight_vals]}")

try:
success, error_message = do_set_weights(
self=subtensor,
wallet=wallet,
netuid=netuid,
uids=weight_uids,
vals=weight_vals,
version_key=version_key,
wait_for_finalization=wait_for_finalization,
wait_for_inclusion=wait_for_inclusion,
success, message = subtensor.commit_weights(
wallet=wallet,
netuid=netuid,
salt=salt,
uids=weight_uids,
weights=weight_vals,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
if not wait_for_finalization and not wait_for_inclusion:
return True, "Not waiting for finalization or inclusion."

if success is True:
logging.success(
f"<green>Finalized!</green> Committed weights: {str(success)}"
)
return True, "Successfully committed weights and Finalized."
else:
logging.error(message)
return False, message

except Exception as e:
logging.error(f":cross_mark: <red>Failed. Error:</red> {e}")
return False, str(e)
else:
# First convert types.
if use_torch():
if isinstance(uids, list):
uids = torch.tensor(uids, dtype=torch.int64)
if isinstance(weights, list):
weights = torch.tensor(weights, dtype=torch.float32)
else:
if isinstance(uids, list):
uids = np.array(uids, dtype=np.int64)
if isinstance(weights, list):
weights = np.array(weights, dtype=np.float32)

# Reformat and normalize.
weight_uids, weight_vals = weight_utils.convert_weights_and_uids_for_emit(
uids, weights
)

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

if success is True:
logging.success(f"<green>Finalized!</green> Set weights: {str(success)}")
return True, "Successfully set weights and Finalized."
else:
logging.error(error_message)
return False, error_message
logging.info(f":satellite: Setting weights on {subtensor.network}...")
try:
success, error_message = do_set_weights(
self=subtensor,
wallet=wallet,
netuid=netuid,
uids=weight_uids,
vals=weight_vals,
version_key=version_key,
wait_for_finalization=wait_for_finalization,
wait_for_inclusion=wait_for_inclusion,
)

except Exception as e:
logging.error(f":cross_mark: <red>Failed.</red>: Error: {e}")
logging.debug(str(e))
return False, str(e)
if not wait_for_finalization and not wait_for_inclusion:
return True, "Not waiting for finalization or inclusion."

if success is True:
logging.success(
f"<green>Finalized!</green> Set weights: {str(success)}"
)
return True, "Successfully set weights and Finalized."
else:
logging.error(error_message)
return False, error_message

except Exception as e:
logging.error(f":cross_mark: <red>Failed error:</red> {e}")
return False, str(e)
22 changes: 11 additions & 11 deletions bittensor/core/extrinsics/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,10 @@ def do_transfer(
"""

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
call = self.substrate.compose_call(
call_module="Balances",
call_function="transfer_allow_death",
call_params={"dest": dest, "value": transfer_balance.rao},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
)
def make_substrate_call_with_retry(extrinsic_):
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
extrinsic=extrinsic_,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)
Expand All @@ -90,7 +82,15 @@ def make_substrate_call_with_retry():
else:
return False, None, response.error_message

return make_substrate_call_with_retry()
call = self.substrate.compose_call(
call_module="Balances",
call_function="transfer_allow_death",
call_params={"dest": dest, "value": transfer_balance.rao},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
)
return make_substrate_call_with_retry(extrinsic)


# Community uses this extrinsic directly and via `subtensor.transfer`
Expand Down
Loading