def test_axon_receptor_forward_works(): def forward(inputs_x: torch.FloatTensor): time.sleep(0.2) return torch.zeros([3, 3, bittensor.__network_dim__]) axon = bittensor.axon( port=8080, ip='0.0.0.0', wallet=wallet, ) axon.attach_forward_callback(forward, modality=bittensor.proto.Modality.TENSOR) axon.start() endpoints = [] for i in range(5): endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=1, hotkey=str(i), ip='0.0.0.0', ip_type=4, port=8080, modality=0, coldkey='') endpoints += [endpoint] dendrite = bittensor.dendrite(max_active_receptors=500) x = torch.rand(3, 3, bittensor.__network_dim__, dtype=torch.float32) tensors, codes, times = dendrite.forward_tensor( endpoints=endpoints, inputs=[x for i in endpoints]) for i in dendrite.receptor_pool.receptors: assert (dendrite.receptor_pool.receptors[i].state() == dendrite.receptor_pool.receptors[i].state().READY) assert codes[0].item() == bittensor.proto.ReturnCode.Success assert list(tensors[0].shape) == [3, 3, bittensor.__network_dim__]
def inspect ( self ): r""" Inspect a cold, hot pair. """ wallet = bittensor.wallet(config = self.config) subtensor = bittensor.subtensor( config = self.config ) dendrite = bittensor.dendrite( wallet = wallet ) with bittensor.__console__.status(":satellite: Looking up account on: [white]{}[/white] ...".format(self.config.subtensor.network)): if self.config.wallet.hotkey == 'None': wallet.coldkeypub cold_balance = wallet.balance bittensor.__console__.print("\n[bold white]{}[/bold white]:\n {}[bold white]{}[/bold white]\n {} {}\n".format( wallet, "coldkey:".ljust(15), wallet.coldkeypub.ss58_address, " balance:".ljust(15), cold_balance.__rich__()), highlight=True) else: wallet.hotkey wallet.coldkeypub neuron = subtensor.neuron_for_pubkey( ss58_hotkey = wallet.hotkey.ss58_address ) endpoint = bittensor.endpoint.from_neuron( neuron ) if neuron.is_null: registered = '[bold white]No[/bold white]' stake = bittensor.Balance.from_tao( 0 ) emission = bittensor.Balance.from_rao( 0 ) latency = 'N/A' else: registered = '[bold white]Yes[/bold white]' stake = bittensor.Balance.from_tao( neuron.stake ) emission = bittensor.Balance.from_rao( neuron.emission * 1000000000 ) _, c, t = dendrite.forward_text( endpoints = endpoint, inputs = 'hello world') latency = "{}".format(t.tolist()[0]) if c.tolist()[0] == 1 else 'N/A' cold_balance = wallet.balance bittensor.__console__.print("\n[bold white]{}[/bold white]:\n [bold grey]{}[bold white]{}[/bold white]\n {}[bold white]{}[/bold white]\n {}{}\n {}{}\n {}{}\n {}{}\n {}{}[/bold grey]".format( wallet, "coldkey:".ljust(15), wallet.coldkeypub.ss58_address, "hotkey:".ljust(15), wallet.hotkey.ss58_address, "registered:".ljust(15), registered, "balance:".ljust(15), cold_balance.__rich__(), "stake:".ljust(15), stake.__rich__(), "emission:".ljust(15), emission.__rich_rao__(), "latency:".ljust(15), latency ), highlight=True)
def test_dendrite_backoff(): _dendrite = bittensor.dendrite(wallet=wallet) _endpoint_obj = bittensor.endpoint( version=bittensor.__version_as_int__, uid=0, ip='0.0.0.0', ip_type=4, port=12345, hotkey=_dendrite.wallet.hotkey.ss58_address, coldkey=_dendrite.wallet.coldkey.ss58_address, modality=0) print(_endpoint_obj) # Normal call. x = torch.rand(3, 3, bittensor.__network_dim__, dtype=torch.float32) out, ops, times = _dendrite.forward_tensor([_endpoint_obj], [x]) assert ops[0].item() == bittensor.proto.ReturnCode.Unavailable assert list(out[0].shape) == [3, 3, bittensor.__network_dim__]
def __init__( self, config: 'bittensor.config', nucleus: 'Nucleus'): r""" Initializes the neuron with the passed config. """ self.config = config self.wallet = bittensor.wallet ( config = self.config ) self.subtensor = bittensor.subtensor ( config = self.config ) self.metagraph = bittensor.metagraph ( config = self.config, subtensor = self.subtensor ) self.dendrite = bittensor.dendrite ( config = self.config, wallet = self.wallet ) self.dataset = bittensor.dataset ( config = self.config ) self.axon = bittensor.axon ( config = self.config, wallet = self.wallet, forward_text = self.forward_text, backward_text = self.backward_text, blacklist = self.blacklist, ) self.device = torch.device( device = self.config.neuron.device ) self.nucleus = nucleus.to(self.device) self.nucleus.metagraph = self.metagraph_callback self.nucleus.dendrite = self.dendrite self.optimizer = torch.optim.SGD( [ {'params': self.nucleus.peer_weights, 'lr': self.config.neuron.learning_rate_chain} ], lr = self.config.neuron.learning_rate, momentum = self.config.neuron.momentum, ) self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size = 1.0, gamma = 0.95 ) self.stats = SimpleNamespace( global_step = 0, last_sync_block = 0, epoch_data_size = 0, epoch_sync_count = 0, local_target_epoch_loss = math.inf, distillation_epoch_loss = math.inf, remote_target_epoch_loss = math.inf, local_epoch_acc = 0, best_epoch_loss = math.inf, scores = torch.nn.Parameter(torch.zeros(0), requires_grad = False).to(self.device), ema_scores = torch.nn.Parameter(torch.zeros(0), requires_grad = False).to(self.device) ) # ---- Decay factor for fisher ema score self.fisher_ema_decay = 0.995
def __init__(self, config: 'bittensor.config' = None): if config == None: config = neuron.config() config = config self.check_config(config) bittensor.logging( config=config, logging_dir=config.neuron.full_path, ) self.config = config print(config) # Load/Create our bittensor wallet. self.wallet = bittensor.wallet(config=config).create().register() # Connect to the chain. self.subtensor = bittensor.subtensor(config=config) # Load/Sync/Save our metagraph. self.metagraph = bittensor.metagraph( subtensor=self.subtensor).load().sync().save() self.uid = self.metagraph.hotkeys.index( self.wallet.hotkey.ss58_address) # Create Dendrite. self.dendrite = bittensor.dendrite(config=config) # Load genesis dataset. self.dataset = bittensor.dataset(config=config) # Build Device. self.device = torch.device(device=config.neuron.device) self.nucleus = Validator(config=config, metagraph=self.metagraph_callback, dendrite=self.dendrite, device=self.device)
from bittensor._endpoint import endpoint import torch import pytest import time import bittensor wallet = bittensor.wallet( path='/tmp/pytest', name='pytest', hotkey='pytest', ) wallet.create_new_coldkey(use_password=False, overwrite=True) wallet.create_new_hotkey(use_password=False, overwrite=True) dendrite = bittensor.dendrite(wallet=wallet) neuron_obj = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='0.0.0.0', ip_type=4, port=12345, hotkey=dendrite.wallet.hotkey.ss58_address, coldkey=dendrite.wallet.coldkey.ss58_address, modality=0) def test_dendrite_forward_text_endpoints_tensor(): endpoints = neuron_obj.to_tensor() x = torch.tensor([[1, 2, 3], [1, 2, 3]]) resp1, _, _ = dendrite.forward_text(endpoints, x) assert list(torch.stack( resp1, dim=0).shape) == [1, 2, 3, bittensor.__network_dim__]
def test_dendrite(): dendrite = bittensor.dendrite() dendrite.to_wandb()
from sys import version from bittensor._endpoint import endpoint import bittensor import torch import pytest from unittest.mock import MagicMock from torch.autograd import Variable import multiprocessing import time dendrite = bittensor.dendrite() dendrite.receptor_pool.forward = MagicMock( return_value=[torch.tensor([]), [1], [0]]) dendrite.receptor_pool.backward = MagicMock( return_value=[torch.tensor([]), [1], [0]]) endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, hotkey='', ip='0.0.0.0', ip_type=4, port=8080, modality=0, coldkey='') def test_dendrite_forward_tensor_shape_error(): x = torch.rand(3, 3, 3) with pytest.raises(ValueError): dendrite.forward_tensor(endpoints=[endpoint], inputs=[x])