From bc0a47872d9f7fe47fcb154c6eb9ebe8f6a3d37f Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 11 Feb 2025 08:27:02 -0800 Subject: [PATCH 1/4] add name and symbol fields to metagraph --- bittensor/core/metagraph.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bittensor/core/metagraph.py b/bittensor/core/metagraph.py index a57a9e4507..18813d33f4 100644 --- a/bittensor/core/metagraph.py +++ b/bittensor/core/metagraph.py @@ -258,6 +258,8 @@ class MetagraphMixin(ABC): _dtype_registry = {"int64": np.int64, "float32": np.float32, "bool": bool} # metagraph_info fields + name: str + symbol: str identities: list[Optional["ChainIdentity"]] identity: Optional["SubnetIdentity"] pruning_score: list[float] @@ -937,6 +939,8 @@ def _apply_metagraph_info_mixin(self, metagraph_info: "MetagraphInfo"): metagraph_info (MetagraphInfo): An instance of the MetagraphInfo class containing the data to be applied to the current object. """ + self.name = metagraph_info.name + self.symbol = metagraph_info.symbol self.identities = metagraph_info.identities self.identity = metagraph_info.identity self.pruning_score = metagraph_info.pruning_score From 82ca0107f618da6b64697b8b213c720f92236533 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 11 Feb 2025 19:00:40 +0200 Subject: [PATCH 2/4] Moves shared features to the Mixin, fixes AsyncSubtensor, changes `sync` to coincide with the creation of the subtensor object if not specified. --- bittensor/core/metagraph.py | 45 +++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/bittensor/core/metagraph.py b/bittensor/core/metagraph.py index 18813d33f4..1a66cb973a 100644 --- a/bittensor/core/metagraph.py +++ b/bittensor/core/metagraph.py @@ -539,6 +539,15 @@ def __init__( metagraph = Metagraph(netuid=123, network="finney", lite=True, sync=True) """ + self.lite = lite + self.subtensor = subtensor + self.should_sync = sync + self.netuid = netuid + self.network, self.chain_endpoint = determine_chain_endpoint_and_network( + network + ) + self.neurons = [] + self.axons: list[AxonInfo] = [] def __str__(self) -> str: """ @@ -1047,10 +1056,6 @@ def __init__( """ BaseClass.__init__(self) MetagraphMixin.__init__(self, netuid, network, lite, sync, subtensor) - self.netuid = netuid - self.network, self.chain_endpoint = determine_chain_endpoint_and_network( - network - ) self._dtype_registry = { "int64": torch.int64, "float32": torch.float32, @@ -1111,10 +1116,6 @@ def __init__( self.uids = torch.nn.Parameter( torch.tensor([], dtype=torch.int64), requires_grad=False ) - self.axons: list[AxonInfo] = [] - self.neurons = [] - self.subtensor = subtensor - self.should_sync = sync self.alpha_stake = torch.nn.Parameter( torch.tensor([], dtype=torch.float32), requires_grad=False ) @@ -1243,9 +1244,6 @@ def __init__( self.tao_stake: Tensor = np.array([], dtype=np.int64) self.stake: Tensor = np.array([], dtype=np.int64) self.total_stake: Tensor = np.array([], dtype=np.int64) - - self.axons: list[AxonInfo] = [] - self.neurons = [] self.subtensor = subtensor self.should_sync = sync @@ -1347,7 +1345,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): async def sync( self, block: Optional[int] = None, - lite: bool = True, + lite: Optional[bool] = None, subtensor: Optional["AsyncSubtensor"] = None, ): """ @@ -1358,8 +1356,9 @@ async def sync( Args: block (Optional[int]): A specific block number to synchronize with. If None, the metagraph syncs with the latest block. This allows for historical analysis or specific state examination of the network. - lite (bool): If True, a lite version of the metagraph is used for quicker synchronization. This is + lite (Optional[bool]): If True, a lite version of the metagraph is used for quicker synchronization. This is beneficial when full detail is not necessary, allowing for reduced computational and time overhead. + Defaults to `True`. subtensor (Optional[bittensor.core.subtensor.Subtensor]): An instance of the subtensor class from Bittensor, providing an interface to the underlying blockchain data. If provided, this instance is used for data retrieval during synchronization. @@ -1394,6 +1393,9 @@ async def sync( metagraph.sync(block=history_block, lite=False, subtensor=subtensor) """ + if lite is None: + lite = self.lite + subtensor = await self._initialize_subtensor(subtensor) if ( @@ -1612,8 +1614,14 @@ async def _get_all_stakes_from_chain(self): ) return subnet_state - self.alpha_stake = subnet_state.alpha_stake - self.tao_stake = [b * 0.018 for b in subnet_state.tao_stake] + self.alpha_stake = self._create_tensor( + [b.tao for b in subnet_state.alpha_stake], + dtype=self._dtype_registry["float32"], + ) + self.tao_stake = self._create_tensor( + [b.tao * 0.018 for b in subnet_state.tao_stake], + dtype=self._dtype_registry["float32"], + ) self.total_stake = self.stake = self._create_tensor( [stake.tao for stake in subnet_state.total_stake], dtype=self._dtype_registry["float32"], @@ -1645,7 +1653,7 @@ def __init__( def sync( self, block: Optional[int] = None, - lite: bool = True, + lite: Optional[bool] = None, subtensor: Optional["Subtensor"] = None, ): """ @@ -1656,8 +1664,9 @@ def sync( Args: block (Optional[int]): A specific block number to synchronize with. If None, the metagraph syncs with the latest block. This allows for historical analysis or specific state examination of the network. - lite (bool): If True, a lite version of the metagraph is used for quicker synchronization. This is + lite (Optional[bool]): If True, a lite version of the metagraph is used for quicker synchronization. This is beneficial when full detail is not necessary, allowing for reduced computational and time overhead. + Defaults to `True`. subtensor (Optional[bittensor.core.subtensor.Subtensor]): An instance of the subtensor class from Bittensor, providing an interface to the underlying blockchain data. If provided, this instance is used for data retrieval during synchronization. @@ -1692,6 +1701,8 @@ def sync( metagraph.sync(block=history_block, lite=False, subtensor=subtensor) """ + if lite is None: + lite = self.lite # Initialize subtensor subtensor = self._initialize_subtensor(subtensor=subtensor) From f58aad67640c4be9fd8fc63b2138c58a257d24ec Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 11 Feb 2025 09:16:08 -0800 Subject: [PATCH 3/4] brings all fields from get_metagraph_info fields to the metagraph class instance --- bittensor/core/metagraph.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bittensor/core/metagraph.py b/bittensor/core/metagraph.py index 18813d33f4..f3f3fd6cc8 100644 --- a/bittensor/core/metagraph.py +++ b/bittensor/core/metagraph.py @@ -260,6 +260,9 @@ class MetagraphMixin(ABC): # metagraph_info fields name: str symbol: str + network_registered_at: int + num_uids: int + max_uids: int identities: list[Optional["ChainIdentity"]] identity: Optional["SubnetIdentity"] pruning_score: list[float] @@ -941,6 +944,9 @@ def _apply_metagraph_info_mixin(self, metagraph_info: "MetagraphInfo"): """ self.name = metagraph_info.name self.symbol = metagraph_info.symbol + self.network_registered_at = metagraph_info.network_registered_at + self.num_uids = metagraph_info.num_uids + self.max_uids = metagraph_info.max_uids self.identities = metagraph_info.identities self.identity = metagraph_info.identity self.pruning_score = metagraph_info.pruning_score From 942f2b48eade7bd0ed49baee5acff0ecb9abfe6d Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 11 Feb 2025 19:25:20 +0200 Subject: [PATCH 4/4] Add 0.018 as a constant --- bittensor/core/metagraph.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bittensor/core/metagraph.py b/bittensor/core/metagraph.py index 1a66cb973a..356b997144 100644 --- a/bittensor/core/metagraph.py +++ b/bittensor/core/metagraph.py @@ -42,6 +42,7 @@ Tensor = Union["torch.nn.Parameter", NDArray] +ROOT_TAO_STAKES_WEIGHT = 0.018 METAGRAPH_STATE_DICT_NDARRAY_KEYS = [ @@ -1619,7 +1620,7 @@ async def _get_all_stakes_from_chain(self): dtype=self._dtype_registry["float32"], ) self.tao_stake = self._create_tensor( - [b.tao * 0.018 for b in subnet_state.tao_stake], + [b.tao * ROOT_TAO_STAKES_WEIGHT for b in subnet_state.tao_stake], dtype=self._dtype_registry["float32"], ) self.total_stake = self.stake = self._create_tensor( @@ -1923,7 +1924,7 @@ def _get_all_stakes_from_chain(self): dtype=self._dtype_registry["float32"], ) self.tao_stake = self._create_tensor( - [b.tao * 0.018 for b in subnet_state.tao_stake], + [b.tao * ROOT_TAO_STAKES_WEIGHT for b in subnet_state.tao_stake], dtype=self._dtype_registry["float32"], ) self.total_stake = self.stake = self._create_tensor(