def _get_or_create_receptor_for_endpoint( self, endpoint: 'bittensor.Endpoint' ) -> 'bittensor.Receptor': r""" Finds or creates a receptor TCP connection associated with the passed Neuron Endpoint Returns receptor: (`bittensor.Receptor`): receptor with tcp connection endpoint at endpoint.ip:endpoint.port """ # ---- Find the active receptor for this endpoint ---- if endpoint.hotkey in self.receptors: receptor = self.receptors[ endpoint.hotkey ] # Change receptor address. if receptor.endpoint.ip != endpoint.ip or receptor.endpoint.port != endpoint.port: del receptor bittensor.logging.update_receptor_log( endpoint ) receptor = bittensor.receptor ( endpoint = endpoint, wallet = self.wallet ) self.receptors[ receptor.endpoint.hotkey ] = receptor # ---- Or: Create a new receptor ---- else: bittensor.logging.create_receptor_log( endpoint ) receptor = bittensor.receptor ( endpoint = endpoint, wallet = self.wallet, external_ip = self.external_ip, ) self.receptors[ receptor.endpoint.hotkey ] = receptor return receptor
def test_axon_receptor_connection_backward_timeout(): def backward(inputs_x: torch.FloatTensor, grads): if inputs_x.size() == (1, 1, 1): return None else: raise TimeoutError('Timeout') axon = bittensor.axon( backward_tensor=backward, port=8088, ip='127.0.0.1', wallet=wallet, ) axon.start() endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='127.0.0.1', ip_type=4, port=8088, hotkey=wallet.hotkey.ss58_address, coldkey=wallet.coldkey.ss58_address, modality=2) receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) x = torch.rand(3, 3, bittensor.__network_dim__) out, ops, time = receptor.backward(x, x, bittensor.proto.Modality.TENSOR, timeout=1) assert ops == bittensor.proto.ReturnCode.Timeout axon.stop()
def test_axon_receptor_connection_backward_unauthenticated(): def backward(inputs_x: torch.FloatTensor, grads): return torch.zeros([3, 3, bittensor.__network_dim__]) axon = bittensor.axon( backward_tensor=backward, port=8090, ip='127.0.0.1', wallet=wallet, ) axon.start() endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='127.0.0.1', ip_type=4, port=8090, hotkey=wallet.hotkey.ss58_address, coldkey=wallet.coldkey.ss58_address, modality=2) receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) x = torch.rand(3, 3, bittensor.__network_dim__) receptor.sign = MagicMock(return_value='mock') out, ops, time = receptor.backward(x, x, bittensor.proto.Modality.TENSOR, timeout=1) assert ops == bittensor.proto.ReturnCode.Unauthenticated axon.stop()
def test_axon_receptor_connection_backward_unimplemented(): def backward(inputs_x: torch.FloatTensor, grads): return torch.zeros([3, 3, bittensor.__network_dim__]) axon = bittensor.axon( backward_tensor=backward, port=8086, ip='127.0.0.1', wallet=wallet, ) axon.start() endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='127.0.0.1', ip_type=4, port=8086, hotkey=wallet.hotkey.ss58_address, coldkey=wallet.coldkey.ss58_address, modality=2) receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) x = torch.rand(3, 3) grads = torch.rand(3, 3, bittensor.__network_dim__) out, ops, time = receptor.backward(x, grads, bittensor.proto.Modality.TEXT, timeout=1) assert ops == bittensor.proto.ReturnCode.NotImplemented axon.stop()
def test_axon_receptor_connection_forward_works(): def forward(inputs_x: torch.FloatTensor): return torch.zeros([3, 3, bittensor.__network_dim__]) axon = bittensor.axon( forward_tensor=forward, port=8081, ip='127.0.0.1', wallet=wallet, ) axon.start() endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='127.0.0.1', ip_type=4, port=8081, hotkey=wallet.hotkey.ss58_address, coldkey=wallet.coldkey.ss58_address, modality=2) receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) x = torch.rand(3, 3, bittensor.__network_dim__) out, ops, time = receptor.forward(x, bittensor.proto.Modality.TENSOR, timeout=1) assert ops == bittensor.proto.ReturnCode.Success axon.stop()
def test_dummy_forward(): endpoint = bittensor.endpoint.dummy() dummy_receptor = bittensor.receptor(endpoint=endpoint, wallet=wallet) assert dummy_receptor.endpoint.uid == -1 x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=torch.long) out, ops, time = dummy_receptor.forward(x, bittensor.proto.Modality.TEXT, timeout=1) assert ops == bittensor.proto.ReturnCode.EmptyRequest assert list(out.shape) == [2, 4, bittensor.__network_dim__]
def test_dummy_backward(): endpoint = bittensor.endpoint.dummy() dummy_receptor = bittensor.receptor(endpoint=endpoint, wallet=wallet) assert dummy_receptor.endpoint.uid == -1 x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=torch.long) grads = torch.ones((x.size(0), x.size(1), bittensor.__network_dim__)) out, ops, time = dummy_receptor.backward(x, grads, bittensor.proto.Modality.TEXT, timeout=1) print(out, ops, time) assert ops == bittensor.proto.ReturnCode.EmptyRequest assert list(out.shape) == [2, 4, bittensor.__network_dim__]
def test_receptor_forward_endpoint_exception(): receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) def forward_break(): raise Exception('Mock') with mock.patch.object(bittensor.proto, 'TensorMessage', new=forward_break): x = torch.rand(3, 3, bittensor.__network_dim__) out, ops, time = receptor.forward(x, bittensor.proto.Modality.TENSOR, timeout=1) assert ops == bittensor.proto.ReturnCode.UnknownException
name='pytest', hotkey='pytest', ) wallet.create_new_coldkey(use_password=False, overwrite=True) wallet.create_new_hotkey(use_password=False, overwrite=True) endpoint = bittensor.endpoint(version=bittensor.__version_as_int__, uid=0, ip='0.0.0.0', ip_type=4, port=8060, hotkey=wallet.hotkey.ss58_address, coldkey=wallet.coldkey.ss58_address, modality=0) receptor = bittensor.receptor( endpoint=endpoint, wallet=wallet, ) channel = grpc.insecure_channel('localhost', options=[('grpc.max_send_message_length', -1), ('grpc.max_receive_message_length', -1)]) stub = bittensor.grpc.BittensorStub(channel) def test_print(): print(receptor) print(str(receptor)) #-- dummy testing --