Skip to content

Commit 4f2b7f7

Browse files
authored
Merge pull request #2659 from opentensor/fix/thewhaleking/fix-async-torch-metagraph
Metagraph Improvements
2 parents f58aad6 + 942f2b4 commit 4f2b7f7

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

bittensor/core/metagraph.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343

4444
Tensor = Union["torch.nn.Parameter", NDArray]
45+
ROOT_TAO_STAKES_WEIGHT = 0.018
4546

4647

4748
METAGRAPH_STATE_DICT_NDARRAY_KEYS = [
@@ -542,6 +543,15 @@ def __init__(
542543
543544
metagraph = Metagraph(netuid=123, network="finney", lite=True, sync=True)
544545
"""
546+
self.lite = lite
547+
self.subtensor = subtensor
548+
self.should_sync = sync
549+
self.netuid = netuid
550+
self.network, self.chain_endpoint = determine_chain_endpoint_and_network(
551+
network
552+
)
553+
self.neurons = []
554+
self.axons: list[AxonInfo] = []
545555

546556
def __str__(self) -> str:
547557
"""
@@ -1053,10 +1063,6 @@ def __init__(
10531063
"""
10541064
BaseClass.__init__(self)
10551065
MetagraphMixin.__init__(self, netuid, network, lite, sync, subtensor)
1056-
self.netuid = netuid
1057-
self.network, self.chain_endpoint = determine_chain_endpoint_and_network(
1058-
network
1059-
)
10601066
self._dtype_registry = {
10611067
"int64": torch.int64,
10621068
"float32": torch.float32,
@@ -1117,10 +1123,6 @@ def __init__(
11171123
self.uids = torch.nn.Parameter(
11181124
torch.tensor([], dtype=torch.int64), requires_grad=False
11191125
)
1120-
self.axons: list[AxonInfo] = []
1121-
self.neurons = []
1122-
self.subtensor = subtensor
1123-
self.should_sync = sync
11241126
self.alpha_stake = torch.nn.Parameter(
11251127
torch.tensor([], dtype=torch.float32), requires_grad=False
11261128
)
@@ -1249,9 +1251,6 @@ def __init__(
12491251
self.tao_stake: Tensor = np.array([], dtype=np.int64)
12501252
self.stake: Tensor = np.array([], dtype=np.int64)
12511253
self.total_stake: Tensor = np.array([], dtype=np.int64)
1252-
1253-
self.axons: list[AxonInfo] = []
1254-
self.neurons = []
12551254
self.subtensor = subtensor
12561255
self.should_sync = sync
12571256

@@ -1353,7 +1352,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
13531352
async def sync(
13541353
self,
13551354
block: Optional[int] = None,
1356-
lite: bool = True,
1355+
lite: Optional[bool] = None,
13571356
subtensor: Optional["AsyncSubtensor"] = None,
13581357
):
13591358
"""
@@ -1364,8 +1363,9 @@ async def sync(
13641363
Args:
13651364
block (Optional[int]): A specific block number to synchronize with. If None, the metagraph syncs with the
13661365
latest block. This allows for historical analysis or specific state examination of the network.
1367-
lite (bool): If True, a lite version of the metagraph is used for quicker synchronization. This is
1366+
lite (Optional[bool]): If True, a lite version of the metagraph is used for quicker synchronization. This is
13681367
beneficial when full detail is not necessary, allowing for reduced computational and time overhead.
1368+
Defaults to `True`.
13691369
subtensor (Optional[bittensor.core.subtensor.Subtensor]): An instance of the subtensor class from Bittensor,
13701370
providing an interface to the underlying blockchain data. If provided, this instance is used for data
13711371
retrieval during synchronization.
@@ -1400,6 +1400,9 @@ async def sync(
14001400
14011401
metagraph.sync(block=history_block, lite=False, subtensor=subtensor)
14021402
"""
1403+
if lite is None:
1404+
lite = self.lite
1405+
14031406
subtensor = await self._initialize_subtensor(subtensor)
14041407

14051408
if (
@@ -1618,8 +1621,14 @@ async def _get_all_stakes_from_chain(self):
16181621
)
16191622
return subnet_state
16201623

1621-
self.alpha_stake = subnet_state.alpha_stake
1622-
self.tao_stake = [b * 0.018 for b in subnet_state.tao_stake]
1624+
self.alpha_stake = self._create_tensor(
1625+
[b.tao for b in subnet_state.alpha_stake],
1626+
dtype=self._dtype_registry["float32"],
1627+
)
1628+
self.tao_stake = self._create_tensor(
1629+
[b.tao * ROOT_TAO_STAKES_WEIGHT for b in subnet_state.tao_stake],
1630+
dtype=self._dtype_registry["float32"],
1631+
)
16231632
self.total_stake = self.stake = self._create_tensor(
16241633
[stake.tao for stake in subnet_state.total_stake],
16251634
dtype=self._dtype_registry["float32"],
@@ -1651,7 +1660,7 @@ def __init__(
16511660
def sync(
16521661
self,
16531662
block: Optional[int] = None,
1654-
lite: bool = True,
1663+
lite: Optional[bool] = None,
16551664
subtensor: Optional["Subtensor"] = None,
16561665
):
16571666
"""
@@ -1662,8 +1671,9 @@ def sync(
16621671
Args:
16631672
block (Optional[int]): A specific block number to synchronize with. If None, the metagraph syncs with the
16641673
latest block. This allows for historical analysis or specific state examination of the network.
1665-
lite (bool): If True, a lite version of the metagraph is used for quicker synchronization. This is
1674+
lite (Optional[bool]): If True, a lite version of the metagraph is used for quicker synchronization. This is
16661675
beneficial when full detail is not necessary, allowing for reduced computational and time overhead.
1676+
Defaults to `True`.
16671677
subtensor (Optional[bittensor.core.subtensor.Subtensor]): An instance of the subtensor class from Bittensor,
16681678
providing an interface to the underlying blockchain data. If provided, this instance is used for data
16691679
retrieval during synchronization.
@@ -1698,6 +1708,8 @@ def sync(
16981708
16991709
metagraph.sync(block=history_block, lite=False, subtensor=subtensor)
17001710
"""
1711+
if lite is None:
1712+
lite = self.lite
17011713

17021714
# Initialize subtensor
17031715
subtensor = self._initialize_subtensor(subtensor=subtensor)
@@ -1918,7 +1930,7 @@ def _get_all_stakes_from_chain(self):
19181930
dtype=self._dtype_registry["float32"],
19191931
)
19201932
self.tao_stake = self._create_tensor(
1921-
[b.tao * 0.018 for b in subnet_state.tao_stake],
1933+
[b.tao * ROOT_TAO_STAKES_WEIGHT for b in subnet_state.tao_stake],
19221934
dtype=self._dtype_registry["float32"],
19231935
)
19241936
self.total_stake = self.stake = self._create_tensor(

0 commit comments

Comments
 (0)