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

add weights call to subtensor #1436

Merged
merged 9 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion bittensor/_metagraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def sync ( self, block: Optional[int] = None, lite: bool = True, subtensor: Opti
if lite:
self.neurons = subtensor.neurons_lite( block = block, netuid = self.netuid )
else:
self.neurons = subtensor.neurons(block = block, netuid = self.netuid )
camfairchild marked this conversation as resolved.
Show resolved Hide resolved
self.neurons = subtensor.neurons( block = block, netuid = self.netuid )

self.lite = lite
self.n = torch.nn.Parameter( torch.tensor( len(self.neurons), dtype=torch.int64 ), requires_grad=False )
self.version = torch.nn.Parameter( torch.tensor( [bittensor.__version_as_int__], dtype=torch.int64 ), requires_grad=False )
Expand Down
8 changes: 8 additions & 0 deletions bittensor/_subtensor/chain_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ def _null_neuron() -> 'NeuronInfo':
pruning_score = 0,
)
return neuron

@classmethod
def from_weights_bonds_and_neuron_lite( cls, neuron_lite: 'NeuronInfoLite', weights_as_dict: Dict[int, List[Tuple[int, int]]], bonds_as_dict: Dict[int, List[Tuple[int, int]]] ) -> 'NeuronInfo':
n_dict = neuron_lite.__dict__
n_dict['weights'] = weights_as_dict.get(neuron_lite.uid, [])
n_dict['bonds'] = bonds_as_dict.get(neuron_lite.uid, [])

return cls( **n_dict )

@staticmethod
def _neuron_dict_to_namespace(neuron_dict) -> 'NeuronInfo':
Expand Down
48 changes: 32 additions & 16 deletions bittensor/_subtensor/subtensor_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,25 +1312,23 @@ def neurons(self, netuid: int, block: Optional[int] = None ) -> List[NeuronInfo]
neuron (List[NeuronInfo]):
List of neuron metadata objects.
"""
@retry(delay=2, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
with self.substrate as substrate:
block_hash = None if block == None else substrate.get_block_hash( block )
params = [netuid]
if block_hash:
params = params + [block_hash]
return substrate.rpc_request(
method="neuronInfo_getNeurons", # custom rpc method
params=params
)
neurons_lite = self.neurons_lite( netuid = netuid, block = block )
weights = self.weights( block = block, netuid = netuid )
bonds = self.bonds( block = block, netuid = netuid )

json_body = make_substrate_call_with_retry()
result = json_body['result']
weights_as_dict = {
uid: w for uid, w in weights
}
bonds_as_dict = {
uid: b for uid, b in bonds
}

if result in (None, []):
return []
neurons = [
NeuronInfo.from_weights_bonds_and_neuron_lite( neuron_lite, weights_as_dict, bonds_as_dict ) for neuron_lite in neurons_lite
]

return NeuronInfo.list_from_vec_u8( result )
return neurons


def neuron_for_uid_lite( self, uid: int, netuid: int, block: Optional[int] = None ) -> Optional[NeuronInfoLite]:
r""" Returns a list of neuron lite from the chain.
Expand Down Expand Up @@ -1414,6 +1412,24 @@ def metagraph( self, netuid: int, lite: bool = True, block: Optional[int] = None

return metagraph_

def weights(self, netuid: int, block: Optional[int] = None) -> List[Tuple[int, List[Tuple[int, int]]]]:
w_map = []
w_map_encoded = self.query_map_subtensor(name="Weights", block=block, params = [netuid])
if w_map_encoded.records:
for uid, w in w_map_encoded:
w_map.append((uid.serialize(), w.serialize()))

return w_map

def bonds(self, netuid: int, block: Optional[int] = None) -> List[Tuple[int, List[Tuple[int, int]]]]:
b_map = []
b_map_encoded = self.query_map_subtensor(name="Bonds", block=block, params = [netuid])
if b_map_encoded.records:
for uid, b in b_map_encoded:
b_map.append((uid.serialize(), b.serialize()))

return b_map

################
## Extrinsics ##
################
Expand Down