Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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__]
Exemplo n.º 6
0
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__]
Exemplo n.º 7
0
def test_thrash_equality_of_endpoint():
    n_tests = 10000
    for _ in range(n_tests):
        new_endpoint = bittensor.endpoint(
            version = random.randint(0,999),
            uid = random.randint(0,4294967295-1),
            ip = str(random.randint(0,250)) + '.' +  str(random.randint(0,250)) + '.' + str(random.randint(0,250)) + '.' + str(random.randint(0,250)),
            ip_type = random.choice( [4,6] ),
            port = random.randint(0,64000),
            hotkey = test_wallet.hotkey.ss58_address,
            coldkey = test_wallet.coldkey.ss58_address,
            modality = 0
        )
        assert new_endpoint.check_format() == True
        tensor_endpoint = new_endpoint.to_tensor()
        assert list(tensor_endpoint.shape) == [250]
        converted_endpoint = bittensor.endpoint.from_tensor( tensor_endpoint )
        assert converted_endpoint == new_endpoint
        assert torch.equal(tensor_endpoint, converted_endpoint.to_tensor())
Exemplo n.º 8
0
def test_receptor_pool_max_workers_forward():
    neuron_obj2 = bittensor.endpoint(version=bittensor.__version_as_int__,
                                     uid=0,
                                     ip='0.0.0.1',
                                     ip_type=4,
                                     port=12345,
                                     hotkey=wallet2.hotkey.public_key,
                                     coldkey=wallet2.coldkey.public_key,
                                     modality=0)
    receptor_pool = bittensor.receptor_pool(wallet=wallet,
                                            max_active_receptors=1)
    endpoints = [neuron_obj, neuron_obj2]
    x = torch.ones((2, 2, 2))
    resp1, _, _ = receptor_pool.forward(endpoints,
                                        x,
                                        bittensor.proto.Modality.TENSOR,
                                        timeout=1)
    assert list(torch.stack(
        resp1, dim=0).shape) == [2, 2, 2, bittensor.__network_dim__]
Exemplo n.º 9
0
def test_create_endpoint():
    global endpoint
    endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 12345,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert endpoint.check_format() == True
    endpoint.assert_format()
    assert endpoint.version == bittensor.__version_as_int__
    assert endpoint.uid == 0
    assert endpoint.ip == '0.0.0.0'
    assert endpoint.ip_type == 4
    assert endpoint.port == 12345
    assert endpoint.hotkey == test_wallet.hotkey.ss58_address
    assert endpoint.coldkey == test_wallet.coldkey.ss58_address
    assert endpoint.modality == 0
Exemplo n.º 10
0
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__]
    assert dendrite.stats.total_requests == 1
    dendrite.to_wandb()

Exemplo n.º 11
0
    def sync(self, block: int = None, cached: bool = True) -> 'Metagraph':
        r""" Synchronizes this metagraph with the chain state.
        """
        if block == None:
            block = self.subtensor.get_current_block()
            if cached and self.subtensor.network in ("nakamoto", "local"):
                if bittensor.__use_console__:
                    with bittensor.__console__.status(
                            "Synchronizing Metagraph...", spinner="earth"):
                        try:
                            neurons = self.retrieve_cached_neurons()
                        except:
                            # For some reason IPFS cache is down, fallback on regular sync
                            logger.warning(
                                "IPFS cache may be down, falling back to regular sync"
                            )
                            neurons = self.subtensor.neurons()
                        n_total = len(neurons)
                else:
                    try:
                        neurons = self.retrieve_cached_neurons()
                    except:
                        # For some reason IPFS cache is down, fallback on regular sync
                        logger.warning(
                            "IPFS cache may be down, falling back to regular sync"
                        )
                        neurons = self.subtensor.neurons()
                    n_total = len(neurons)
            else:
                neurons = self.subtensor.neurons(block=block)
                n_total = len(neurons)
        else:
            if cached and self.subtensor.network in ("nakamoto", "local"):
                if bittensor.__use_console__:
                    with bittensor.__console__.status(
                            "Synchronizing Metagraph...", spinner="earth"):
                        try:
                            neurons = self.retrieve_cached_neurons(block=block)
                        except:
                            # For some reason IPFS cache is down, fallback on regular sync
                            logger.warning(
                                "IPFS cache may be down, falling back to regular sync to get block {}"
                                .format(block))
                            neurons = self.subtensor.neurons(block=block)
                        n_total = len(neurons)
                else:
                    try:
                        neurons = self.retrieve_cached_neurons(block=block)
                    except:
                        # For some reason IPFS cache is down, fallback on regular sync
                        logger.warning(
                            "IPFS cache may be down, falling back to regular sync to get block {}"
                            .format(block))
                        neurons = self.subtensor.neurons(block=block)
                    n_total = len(neurons)
            else:
                neurons = self.subtensor.neurons(block=block)
                n_total = len(neurons)

        # Fill arrays.
        uids = [i for i in range(n_total)]
        active = [0 for _ in range(n_total)]
        stake = [0 for _ in range(n_total)]
        ranks = [0 for _ in range(n_total)]
        trust = [0 for _ in range(n_total)]
        consensus = [0 for _ in range(n_total)]
        incentive = [0 for _ in range(n_total)]
        emission = [0 for _ in range(n_total)]
        dividends = [0 for _ in range(n_total)]
        last_updates = [-1 for _ in range(n_total)]
        endpoints = [[-1 for _ in range(250)] for _ in range(n_total)]
        weights = [[0 for _ in range(n_total)] for _ in range(n_total)]
        bonds = [[0 for _ in range(n_total)] for _ in range(n_total)]
        self._endpoint_objs = [
            bittensor.endpoint.dummy() for _ in range(n_total)
        ]
        for n in neurons:
            uids[n.uid] = n.uid
            active[n.uid] = n.active
            stake[n.uid] = n.stake
            ranks[n.uid] = n.rank
            trust[n.uid] = n.trust
            consensus[n.uid] = n.consensus
            incentive[n.uid] = n.incentive
            dividends[n.uid] = n.dividends
            emission[n.uid] = n.emission
            last_updates[n.uid] = n.last_update
            endpoint = bittensor.endpoint(version=int(n.version),
                                          uid=int(n.uid),
                                          hotkey=str(n.hotkey),
                                          ip_type=int(n.ip_type),
                                          ip=str(n.ip),
                                          port=int(n.port),
                                          modality=int(n.modality),
                                          coldkey=str(n.coldkey))
            self._endpoint_objs[n.uid] = endpoint
            endpoints[n.uid] = endpoint.to_tensor().tolist()
            if len(n.weights) > 0:
                w_uids, w_weights = zip(*n.weights)
                weights[
                    n.
                    uid] = weight_utils.convert_weight_uids_and_vals_to_tensor(
                        n_total, w_uids, w_weights).tolist()
            else:
                weights[n.uid] = [0] * n_total
            if len(n.bonds) > 0:
                b_uids, b_bonds = zip(*n.bonds)
                bonds[
                    n.uid] = weight_utils.convert_bond_uids_and_vals_to_tensor(
                        n_total, b_uids, b_bonds).tolist()
            else:
                bonds[n.uid] = [0] * n_total

        # Set tensors.
        tn = torch.tensor(n_total, dtype=torch.int64)
        tblock = torch.tensor(block, dtype=torch.int64)
        tuids = torch.tensor(uids, dtype=torch.int64)
        tactive = torch.tensor(active, dtype=torch.int64)
        tstake = torch.tensor(stake, dtype=torch.float32)
        tranks = torch.tensor(ranks, dtype=torch.float32)
        ttrust = torch.tensor(trust, dtype=torch.float32)
        tconsensus = torch.tensor(consensus, dtype=torch.float32)
        tincentive = torch.tensor(incentive, dtype=torch.float32)
        temission = torch.tensor(emission, dtype=torch.float32)
        tdividends = torch.tensor(dividends, dtype=torch.float32)
        tlast_update = torch.tensor(last_updates, dtype=torch.int64)
        tbonds = torch.tensor(bonds, dtype=torch.int64)
        tweights = torch.tensor(weights, dtype=torch.float32)
        tendpoints = torch.tensor(endpoints, dtype=torch.int64)

        # Normalize bond ownership.
        tbonds = torch.nn.functional.normalize(
            tbonds.float(), p=1, dim=0, eps=1e-12) * 0.5 + torch.eye(tn) * 0.5

        # Set params.
        self.n = torch.nn.Parameter(tn, requires_grad=False)
        self.block = torch.nn.Parameter(tblock, requires_grad=False)
        self.uids = torch.nn.Parameter(tuids, requires_grad=False)
        self.stake = torch.nn.Parameter(tstake, requires_grad=False)
        self.ranks = torch.nn.Parameter(tranks, requires_grad=False)
        self.trust = torch.nn.Parameter(ttrust, requires_grad=False)
        self.consensus = torch.nn.Parameter(tconsensus, requires_grad=False)
        self.incentive = torch.nn.Parameter(tincentive, requires_grad=False)
        self.emission = torch.nn.Parameter(temission, requires_grad=False)
        self.dividends = torch.nn.Parameter(tdividends, requires_grad=False)
        self.active = torch.nn.Parameter(tactive, requires_grad=False)
        self.last_update = torch.nn.Parameter(tlast_update,
                                              requires_grad=False)
        self.weights = torch.nn.Parameter(tweights, requires_grad=False)
        self.bonds = torch.nn.Parameter(tbonds, requires_grad=False)
        self.endpoints = torch.nn.Parameter(tendpoints, requires_grad=False)

        # For contructor.
        return self
Exemplo n.º 12
0
def test_endpoint_fails_checks():
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = -1,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 12345,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 4294967296,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 12345,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 5,
        port = 12345,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 12345222,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 2142,
        hotkey = test_wallet.hotkey.ss58_address + "sssd",
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 2142,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address + "sssd",
        modality = 0
    )
    assert test_endpoint.check_format() == False
    test_endpoint = bittensor.endpoint(
        version = bittensor.__version_as_int__,
        uid = 0,
        ip = '0.0.0.0',
        ip_type = 4,
        port = 2142,
        hotkey = test_wallet.hotkey.ss58_address,
        coldkey = test_wallet.coldkey.ss58_address,
        modality = 2
    )
    assert test_endpoint.check_format() == False
Exemplo n.º 13
0
import asyncio

logging = bittensor.logging()

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)

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():
Exemplo n.º 14
0
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])


def test_dendrite_forward_image_shape_error():
    x = torch.rand(3, 3, 3)
    with pytest.raises(ValueError):
        dendrite.forward_image(endpoints=[endpoint], inputs=[x])
Exemplo n.º 15
0
)
wallet.create_new_coldkey(use_password=False, overwrite=True)
wallet.create_new_hotkey(use_password=False, overwrite=True)

wallet2 = bittensor.wallet(
    path='/tmp/pytest',
    name='pytest',
    hotkey='pytest2',
)
wallet2.create_new_coldkey(use_password=False, overwrite=True)
wallet2.create_new_hotkey(use_password=False, overwrite=True)

neuron_obj = bittensor.endpoint(version=bittensor.__version_as_int__,
                                uid=0,
                                ip='0.0.0.0',
                                ip_type=4,
                                port=12345,
                                hotkey=wallet.hotkey.public_key,
                                coldkey=wallet.coldkey.public_key,
                                modality=0)

receptor_pool = bittensor.receptor_pool(wallet=wallet)


def test_receptor_pool_forward():
    endpoints = [neuron_obj]
    x = torch.ones((1, 2, 2))
    resp1, _, _ = receptor_pool.forward(endpoints,
                                        x,
                                        bittensor.proto.Modality.TENSOR,
                                        timeout=1)
    assert list(torch.stack(