Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metagraph Improvements #2659

Merged
Changes from all commits
Commits
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
48 changes: 30 additions & 18 deletions bittensor/core/metagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@


Tensor = Union["torch.nn.Parameter", NDArray]
ROOT_TAO_STAKES_WEIGHT = 0.018


METAGRAPH_STATE_DICT_NDARRAY_KEYS = [
Expand Down Expand Up @@ -539,6 +540,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:
"""
Expand Down Expand Up @@ -1047,10 +1057,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,
Expand Down Expand Up @@ -1111,10 +1117,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
)
Expand Down Expand Up @@ -1243,9 +1245,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

Expand Down Expand Up @@ -1347,7 +1346,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,
):
"""
Expand All @@ -1358,8 +1357,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.
Expand Down Expand Up @@ -1394,6 +1394,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 (
Expand Down Expand Up @@ -1612,8 +1615,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 * ROOT_TAO_STAKES_WEIGHT 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"],
Expand Down Expand Up @@ -1645,7 +1654,7 @@ def __init__(
def sync(
self,
block: Optional[int] = None,
lite: bool = True,
lite: Optional[bool] = None,
subtensor: Optional["Subtensor"] = None,
):
"""
Expand All @@ -1656,8 +1665,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.
Expand Down Expand Up @@ -1692,6 +1702,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)
Expand Down Expand Up @@ -1912,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(
Expand Down