From 7783a6cb0661c286282747a100c1a0e35876108c Mon Sep 17 00:00:00 2001 From: C H Date: Fri, 5 Jan 2024 23:00:17 +0300 Subject: [PATCH] Added subnet weights command, refactoring --- README.md | 2 +- cybertensor/chain_data.py | 8 ---- cybertensor/cli.py | 2 + cybertensor/commands/delegates.py | 2 +- cybertensor/commands/network.py | 65 +++++++++++++++++++++++++++++++ cybertensor/commands/stake.py | 2 +- 6 files changed, 70 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 04486e0..72fbfab 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ cyber keys add validator --recover --home ./home ``` 3. Deploy code and instantiate contract: ```bash -cyber tx wasm store cybernet.wasm --from validator --home ./home --chain-id localbostrom --gas 7000000 --broadcast-mode block -y --keyring-backend test +cyber tx wasm store cybernet.wasm --from validator --home ./home --chain-id localbostrom --gas 8000000 --broadcast-mode block -y --keyring-backend test cyber tx wasm instantiate 1 "{}" --from validator --home ./home --chain-id localbostrom --gas 5000000 --label cybernet1 --admin bostrom1phaxpevm5wecex2jyaqty2a4v02qj7qm5n94ug --broadcast-mode block -y --keyring-backend test ``` 4. Send tokens to contract and activate (with dmn): diff --git a/cybertensor/chain_data.py b/cybertensor/chain_data.py index 18cccf1..df9c58c 100644 --- a/cybertensor/chain_data.py +++ b/cybertensor/chain_data.py @@ -570,14 +570,11 @@ class SubnetInfo: max_allowed_validators: int min_allowed_weights: int max_weight_limit: float - scaling_law_power: float subnetwork_n: int max_n: int blocks_since_epoch: int tempo: int modality: int - # netuid -> topk percentile prunning score requirement (u16:MAX normalized.) - connection_requirements: Dict[str, float] emission_value: float burn: Balance owner: str @@ -608,16 +605,11 @@ def fix_decoded_values(cls, decoded: Dict) -> "SubnetInfo": max_allowed_validators=decoded["max_allowed_validators"], min_allowed_weights=decoded["min_allowed_weights"], max_weight_limit=decoded["max_weights_limit"], - scaling_law_power=decoded["scaling_law_power"], subnetwork_n=decoded["subnetwork_n"], max_n=decoded["max_allowed_uids"], blocks_since_epoch=decoded["blocks_since_last_step"], tempo=decoded["tempo"], modality=decoded["network_modality"], - connection_requirements={ - str(int(netuid)): U16_NORMALIZED_FLOAT(int(req)) - for netuid, req in decoded["network_connect"] - }, emission_value=decoded["emission_values"], burn=Balance.from_boot(decoded["burn"]), owner=decoded["owner"], diff --git a/cybertensor/cli.py b/cybertensor/cli.py index c31dc25..26ab0d3 100644 --- a/cybertensor/cli.py +++ b/cybertensor/cli.py @@ -22,6 +22,7 @@ import cybertensor from .commands import * +from .commands.network import SubnetSetWeightsCommand # Create a console instance for CLI display. console = cybertensor.__console__ @@ -58,6 +59,7 @@ "pow_register": PowRegisterCommand, "register": RegisterCommand, "hyperparameters": SubnetHyperparamsCommand, + "weights": SubnetSetWeightsCommand, }, }, "root": { diff --git a/cybertensor/commands/delegates.py b/cybertensor/commands/delegates.py index 7924852..02eb358 100644 --- a/cybertensor/commands/delegates.py +++ b/cybertensor/commands/delegates.py @@ -748,7 +748,7 @@ def run(cli): if not wallet.coldkeypub_file.exists_on_device(): continue delegates = cwtensor.get_delegated( - coldkey=wallet.coldkeypub.address + delegatee=wallet.coldkeypub.address ) my_delegates = {} # hotkey, amount diff --git a/cybertensor/commands/network.py b/cybertensor/commands/network.py index 1402828..9a3a286 100644 --- a/cybertensor/commands/network.py +++ b/cybertensor/commands/network.py @@ -17,6 +17,7 @@ # DEALINGS IN THE SOFTWARE. import argparse +import re from typing import List, Optional, Dict from rich.prompt import Prompt @@ -25,6 +26,7 @@ import cybertensor from . import defaults from .utils import DelegatesDetails, check_netuid_set +import torch console = cybertensor.__console__ @@ -491,3 +493,66 @@ def add_args(parser: argparse.ArgumentParser): "--netuid", dest="netuid", type=int, required=False, default=False ) cybertensor.cwtensor.add_args(parser) + +class SubnetSetWeightsCommand: + """ + + Optional arguments: + - --uids (str): A comma-separated list of uids for which weights are to be set. + - --weights (str): Corresponding weights for the specified netuids, in comma-separated format. + - --netuid (str): Corresponding subnet for which weights are to be set. + + Example usage: + >>> ctcli subnet weights --uids 1,2,3 --weights 0.3,0.3,0.4 + """ + + @staticmethod + def run(cli): + r"""Set weights for root network.""" + wallet = cybertensor.wallet(config=cli.config) + cwtensor = cybertensor.cwtensor(config=cli.config) + + # Parse from string + uids = torch.tensor( + list(map(int, re.split(r"[ ,]+", cli.config.uids))), dtype=torch.long + ) + weights = torch.tensor( + list(map(float, re.split(r"[ ,]+", cli.config.weights))), + dtype=torch.float32, + ) + + # Run the set weights operation. + cwtensor.set_weights( + wallet=wallet, + netuid=cli.config.netuid, + uids=uids, + weights=weights, + version_key=0, + prompt=not cli.config.no_prompt, + wait_for_finalization=True, + ) + + @staticmethod + def add_args(parser: argparse.ArgumentParser): + parser = parser.add_parser("weights", help="""Set weights for subnet.""") + parser.add_argument("--uids", dest="uids", type=str, required=False) + parser.add_argument("--weights", dest="weights", type=str, required=False) + parser.add_argument( + "--netuid", dest="netuid", type=int, required=False, default=False + ) + + cybertensor.wallet.add_args(parser) + cybertensor.cwtensor.add_args(parser) + + @staticmethod + def check_config(config: "cybertensor.config"): + if not config.is_set("wallet.name") and not config.no_prompt: + wallet_name = Prompt.ask("Enter wallet name", default=defaults.wallet.name) + config.wallet.name = str(wallet_name) + + if not config.is_set("wallet.hotkey") and not config.no_prompt: + hotkey = Prompt.ask("Enter hotkey name", default=defaults.wallet.hotkey) + config.wallet.hotkey = str(hotkey) + + if not config.is_set("netuid") and not config.no_prompt: + check_netuid_set(config, cybertensor.cwtensor(config=config)) diff --git a/cybertensor/commands/stake.py b/cybertensor/commands/stake.py index e40e149..5e8c411 100644 --- a/cybertensor/commands/stake.py +++ b/cybertensor/commands/stake.py @@ -443,7 +443,7 @@ def get_stakes_from_delegates( A dictionary of stakes related to delegates. """ delegates = cwtensor.get_delegated( - coldkey=wallet.coldkeypub.address + delegatee=wallet.coldkeypub.address ) stakes = {} for dele, staked in delegates: