Beispiel #1
0
def test_backward_response_success_text_priority():
    def priority(pubkey: str, request_type: str, inputs_x):
        return 100

    axon = bittensor.axon(wallet=wallet, priority=priority)

    def backward(inputs_x: torch.FloatTensor, grads_dy: torch.FloatTensor):
        return torch.zeros([1, 1])

    axon.attach_backward_callback(backward,
                                  modality=bittensor.proto.Modality.TEXT)
    inputs_raw = torch.ones((1, 1))
    grads_raw = torch.zeros((1, 1, bittensor.__network_dim__))
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TEXT,
        from_type=bittensor.proto.TensorType.TORCH)
    grads_serialized = serializer.serialize(
        grads_raw,
        modality=bittensor.proto.Modality.TEXT,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized, grads_serialized])
    response, code, call_time, message = axon._backward(request)
    assert code == bittensor.proto.ReturnCode.Success
Beispiel #2
0
def test_receptor_neuron_server_response_with_nans_backward():
    import numpy as np
    y = torch.rand(3, 3, bittensor.__network_dim__)
    y[0][0][0] = np.nan

    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    y_serialized = serializer.serialize(
        y,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    mock_return_val = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=wallet.hotkey.public_key,
        return_code=bittensor.proto.ReturnCode.Success,
        tensors=[y_serialized])

    future = asyncio.Future()
    future.set_result(mock_return_val)
    stub.Backward.future = MagicMock(return_value=future)
    receptor.stub = stub

    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.Success
    assert out[0][0][0] == 0
Beispiel #3
0
def test_receptor_neuron_mock_server_shape_error_backward():
    y = torch.rand(1, 3, bittensor.__network_dim__)

    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    y_serialized = serializer.serialize(
        y,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    mock_return_val = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=wallet.hotkey.public_key,
        return_code=bittensor.proto.ReturnCode.Success,
        tensors=[y_serialized])

    future = asyncio.Future()
    future.set_result(mock_return_val)
    stub.Backward.future = MagicMock(return_value=future)
    receptor.stub = stub

    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.ResponseShapeException
    assert list(out.shape) == [3, 3, bittensor.__network_dim__]
Beispiel #4
0
def test_receptor_backward_exception():
    y = torch.zeros(3, 3, bittensor.__network_dim__)

    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    y_serialized = serializer.serialize(
        y,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    mock_return_val = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=wallet.hotkey.ss58_address,
        return_code=bittensor.proto.ReturnCode.UnknownException,
        tensors=[y_serialized])

    future = asyncio.Future()
    future.set_result(mock_return_val)
    stub.Backward.future = MagicMock(return_value=future)
    receptor.stub = stub

    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.UnknownException
    def test_serialize_deserialize_tensor(self):
        data = torch.rand([12, 23])

        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        serialized_tensor_message = serializer.serialize(
            data,
            modality=bittensor.proto.Modality.TENSOR,
            from_type=bittensor.proto.TensorType.TORCH)

        assert data.requires_grad == serialized_tensor_message.requires_grad
        assert list(data.shape) == serialized_tensor_message.shape
        assert serialized_tensor_message.modality == bittensor.proto.Modality.TENSOR
        assert serialized_tensor_message.dtype == bittensor.proto.DataType.FLOAT32

        deserialized_tensor_message = serializer.deserialize(
            serialized_tensor_message,
            to_type=bittensor.proto.TensorType.TORCH)
        assert serialized_tensor_message.requires_grad == deserialized_tensor_message.requires_grad
        assert serialized_tensor_message.shape == list(
            deserialized_tensor_message.shape)
        assert bittensor.serializer.torch_dtype_to_bittensor_dtype(
            deserialized_tensor_message.dtype
        ) == bittensor.proto.DataType.FLOAT32

        assert torch.all(torch.eq(deserialized_tensor_message, data))
    def test_serialize_deserialize_image(self):
        # Let's grab some image data
        # Let's grab a random image, and give it a crazy type to break the system
        image = torch.ones([1, 28, 28])

        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        serialized_image_tensor_message = serializer.serialize(
            image,
            modality=bittensor.proto.Modality.IMAGE,
            from_type=bittensor.proto.TensorType.TORCH)

        assert image.requires_grad == serialized_image_tensor_message.requires_grad
        assert list(image.shape) == serialized_image_tensor_message.shape
        assert serialized_image_tensor_message.modality == bittensor.proto.Modality.IMAGE
        assert serialized_image_tensor_message.dtype != bittensor.proto.DataType.UNKNOWN

        deserialized_image_tensor_message = serializer.deserialize(
            serialized_image_tensor_message,
            to_type=bittensor.proto.TensorType.TORCH)
        assert serialized_image_tensor_message.requires_grad == deserialized_image_tensor_message.requires_grad
        assert serialized_image_tensor_message.shape == list(
            deserialized_image_tensor_message.shape)
        assert bittensor.serializer.torch_dtype_to_bittensor_dtype(
            deserialized_image_tensor_message.dtype
        ) != bittensor.proto.DataType.UNKNOWN

        assert torch.all(torch.eq(deserialized_image_tensor_message, image))
Beispiel #7
0
def test_backward_response_exception():
    def backward(inputs_x: torch.FloatTensor, grads_dy: torch.FloatTensor):
        if inputs_x.size() == (1, 1, 1):
            return None
        else:
            raise Exception('Timeout')

    axon.attach_backward_callback(backward,
                                  modality=bittensor.proto.Modality.TENSOR)
    inputs_raw = torch.rand(2, 2, 2)
    grads_raw = torch.rand(2, 2, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    grads_serialized = serializer.serialize(
        grads_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized, grads_serialized])
    response, code, call_time, message = axon._backward(request)
    assert code == bittensor.proto.ReturnCode.UnknownException
Beispiel #8
0
def test_receptor_pool_forward_hang():
    endpoints = [neuron_obj, neuron_obj]
    x = torch.ones((2, 2, 2))
    y = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    y_serialized = serializer.serialize(
        y,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    mock_return_val = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=wallet.hotkey.ss58_address,
        return_code=bittensor.proto.ReturnCode.Timeout,
        tensors=[])

    future = asyncio.Future()
    future.set_result(mock_return_val)
    receptor_pool._get_or_create_receptor_for_endpoint(neuron_obj)
    receptor_pool.receptors[neuron_obj.hotkey].stub.Forward.future = MagicMock(
        return_value=future)
    resp1, codes, _ = receptor_pool.forward(endpoints,
                                            x,
                                            bittensor.proto.Modality.TENSOR,
                                            timeout=1)
    assert codes == [
        bittensor.proto.ReturnCode.Timeout, bittensor.proto.ReturnCode.Timeout
    ]
Beispiel #9
0
    def deserialize_backward_response(self, request):
        r"""Deserialization for the backward request.
            The result would update request.output.

            Args:
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.

            Returns:
                success: (:type:`bool`, `required`):
                    True if the deserialization is successful.
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.
        """
        # ---- Post-process request ----
        try:
            outputs = request.response.tensors[0]
            deserializer = bittensor.serializer(outputs.serializer)
            outputs = deserializer.deserialize(
                outputs, to_type=bittensor.proto.TensorType.TORCH)
        except Exception as e:
            request.code = bittensor.proto.ReturnCode.ResponseDeserializationException
            request.message = 'deserialization exception with error:{}'.format(
                e)
            self.request_log(request=request,
                             is_response=True,
                             inputs=list(request.inputs.shape))
            return False, request

        try:
            # ---- Check response shape is same as inputs ----
            if outputs.size() != request.inputs.size():
                request.code = bittensor.proto.ReturnCode.ResponseShapeException
                request.message = 'output shape does not match inputs shape'
                self.request_log(request=request,
                                 is_response=True,
                                 inputs=list(request.inputs.shape))
                return False, request

        except Exception as e:
            request.code = bittensor.proto.ReturnCode.UnknownException
            request.message = 'Size Error: {}'.format(e)
            self.request_log(request=request,
                             is_response=True,
                             inputs=list(request.inputs.shape))
            return False, request

        # ---- Safe catch NaNs and replace with 0.0 ----
        request.outputs = torch.where(torch.isnan(outputs),
                                      torch.zeros_like(outputs), outputs)

        # ---- Return ----
        request.code = bittensor.proto.ReturnCode.Success
        request.message = 'Success'
        self.request_log(request=request,
                         is_response=True,
                         inputs=list(request.inputs.shape))
        self.stats.codes[request.code] += 1
        return False, request
Beispiel #10
0
    def deserialize_forward_response(self, request):
        r"""Deserialization for the forward request.
            The result would update request.output.

            Args:
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.

            Returns:
                success: (:type:`bool`, `required`):
                    True if the deserialization is successful.
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.
        """

        # ---- Deserialize response ----
        try:
            outputs = request.response.tensors[0]
            deserializer = bittensor.serializer(outputs.serializer)
            outputs = deserializer.deserialize(
                outputs, to_type=bittensor.proto.TensorType.TORCH)

        except Exception as e:
            request.code = bittensor.proto.ReturnCode.ResponseDeserializationException
            request.message = 'Deserialziation exception with error:{}'.format(
                str(e))
            self.request_log(request=request,
                             is_response=True,
                             inputs=list(request.inputs.shape))
            return False, request

        # ---- Check response shape ----
        if (outputs.size(0) != request.inputs.size(0)
                or outputs.size(1) != request.inputs.size(1)
                or outputs.size(2) != bittensor.__network_dim__):
            request.code = bittensor.proto.ReturnCode.ResponseShapeException
            request.message = "output.shape:{} does not match inputs:{}".format(
                outputs.shape, request.inputs.shape)
            self.request_log(request=request,
                             is_response=True,
                             inputs=list(request.inputs.shape),
                             outputs=list(outputs.shape))
            return False, request

        # ---- Safe catch NaNs and replace with 0.0 ----
        request.outputs = torch.where(torch.isnan(outputs),
                                      torch.zeros_like(outputs), outputs)

        # ---- Return ----
        request.code = request.response.return_code
        self.request_log(request=request,
                         is_response=True,
                         inputs=list(request.inputs.shape),
                         outputs=list(outputs.shape))
        self.stats.codes[request.code] += 1

        return True, request
    def test_serialize_object_type_exception(self):
        # Let's grab a random image, and try and de-serialize it incorrectly.
        image = torch.ones([1, 28, 28])

        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        with pytest.raises(
                bittensor.serializer.SerializationTypeNotImplementedException):
            serializer.serialize(image,
                                 modality=bittensor.proto.Modality.IMAGE,
                                 from_type=11)
 def test_serialize(self):
     for _ in range(10):
         tensor_a = torch.rand([12, 23])
         serializer = bittensor.serializer(
             serialzer_type=bittensor.proto.Serializer.MSGPACK)
         content = serializer.serialize(
             tensor_a,
             modality=bittensor.proto.Modality.TENSOR,
             from_type=bittensor.proto.TensorType.TORCH)
         tensor_b = serializer.deserialize(
             content, to_type=bittensor.proto.TensorType.TORCH)
         torch.all(torch.eq(tensor_a, tensor_b))
    def test_deserialization_object_type_exception(self):
        data = torch.rand([12, 23])

        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        tensor_message = serializer.serialize(
            data,
            modality=bittensor.proto.Modality.TEXT,
            from_type=bittensor.proto.TensorType.TORCH)

        with pytest.raises(
                bittensor.serializer.SerializationTypeNotImplementedException):
            serializer.deserialize(tensor_message, to_type=11)
Beispiel #14
0
def test_forward_wandb():
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__, tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    axon.update_stats_for_request(request, response, call_time, code)
    print(axon.to_wandb())
Beispiel #15
0
def test_forward_not_implemented():
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__, tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.NotImplemented
Beispiel #16
0
def test_forward_tensor_shape_error():
    inputs_raw = torch.rand(1, 1, 1, 1)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.RequestShapeException
Beispiel #17
0
def test_backward_invalid_request():
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized])
    response, code, call_time, message = axon._backward(request)
    assert code == bittensor.proto.ReturnCode.InvalidRequest
Beispiel #18
0
def test_grpc_backward_works():
    def backward(inputs_x: torch.FloatTensor, grads_dy: torch.FloatTensor):
        return torch.zeros([1, 1, 1])

    axon = bittensor.axon(
        port=7081,
        ip='127.0.0.1',
        wallet=wallet,
    )
    axon.attach_backward_callback(backward,
                                  modality=bittensor.proto.Modality.TENSOR)
    axon.start()

    channel = grpc.insecure_channel('127.0.0.1:7081',
                                    options=[
                                        ('grpc.max_send_message_length', -1),
                                        ('grpc.max_receive_message_length', -1)
                                    ])
    stub = bittensor.grpc.BittensorStub(channel)

    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    grads_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    grads_serialized = serializer.serialize(
        grads_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey='1092310312914',
        tensors=[inputs_serialized, grads_serialized])
    response = stub.Backward(request,
                             metadata=(
                                 ('rpc-auth-header', 'Bittensor'),
                                 ('bittensor-signature', sign(axon.wallet)),
                                 ('bittensor-version',
                                  str(bittensor.__version_as_int__)),
                             ))
    outputs = serializer.deserialize(response.tensors[0],
                                     to_type=bittensor.proto.TensorType.TORCH)
    assert outputs.tolist() == [[[0]]]
    axon.stop()
Beispiel #19
0
def test_forward_tensor_success_image():
    def forward(inputs_x: torch.FloatTensor):
        return torch.zeros(
            [inputs_x.shape[0], inputs_x.shape[1], bittensor.__network_dim__])

    axon.attach_forward_callback(forward, modality=1)
    inputs_raw = torch.rand(1, 1, 1, 1, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.IMAGE,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__, tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.Success
Beispiel #20
0
def test_forward_unknown_error():
    def forward(inputs_x: torch.FloatTensor, modality):
        raise Exception('Unknown')

    with mock.patch.object(axon, '_call_forward', new=forward):
        inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        inputs_serialized = serializer.serialize(
            inputs_raw,
            modality=bittensor.proto.Modality.TENSOR,
            from_type=bittensor.proto.TensorType.TORCH)
        request = bittensor.proto.TensorMessage(
            version=bittensor.__version_as_int__,
            tensors=[inputs_serialized],
            hotkey='123')

        response, code, call_time, message = axon._forward(request)
        assert code == bittensor.proto.ReturnCode.UnknownException
Beispiel #21
0
def test_forward_response_deserialization_error():
    def forward(inputs_x: torch.Tensor):
        return dict()

    axon.attach_forward_callback(forward,
                                 modality=bittensor.proto.Modality.TENSOR)
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)

    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.ResponseDeserializationException
Beispiel #22
0
def test_forward_tensor_exception():
    def forward(inputs_x: torch.FloatTensor):
        if inputs_x.size() == (1, 1, 1):
            return None
        else:
            raise Exception('Mock')

    axon.attach_forward_callback(forward, modality=2)
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        tensors=[inputs_serialized],
        hotkey='123')
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.UnknownException
Beispiel #23
0
def test_grpc_backward_fails():
    def backward(inputs_x: torch.FloatTensor, grads_dy: torch.FloatTensor):
        return torch.zeros([1, 1, 1])

    axon = bittensor.axon(port=7081, ip='127.0.0.1', wallet=wallet)
    axon.attach_backward_callback(backward,
                                  modality=bittensor.proto.Modality.TENSOR)
    axon.start()

    channel = grpc.insecure_channel('127.0.0.1:7081',
                                    options=[
                                        ('grpc.max_send_message_length', -1),
                                        ('grpc.max_receive_message_length', -1)
                                    ])
    stub = bittensor.grpc.BittensorStub(channel)

    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    grads_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    grads_serialized = serializer.serialize(
        grads_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey='1092310312914',
        tensors=[inputs_serialized, grads_serialized])

    try:
        response = stub.Backward(request)
    except grpc.RpcError as rpc_error_call:
        grpc_code = rpc_error_call.code()
        assert grpc_code == grpc.StatusCode.UNAUTHENTICATED

    axon.stop()
Beispiel #24
0
def test_forward_tensor_success_priority():
    def priority(pubkey: str, request_type: str, inputs_x):
        return 100

    axon = bittensor.axon(wallet=wallet, priority=priority)

    def forward(inputs_x: torch.FloatTensor):
        return torch.zeros(
            [inputs_x.shape[0], inputs_x.shape[1], bittensor.__network_dim__])

    axon.attach_forward_callback(forward, modality=2)
    inputs_raw = torch.rand(3, 3, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__, tensors=[inputs_serialized])
    response, code, call_time, message = axon._forward(request)
    assert code == bittensor.proto.ReturnCode.Success
Beispiel #25
0
    def serialization(self, request):
        r""" Does the serialization to the request inputs and grads(backward request only).
            The result would update request.serialized_inputs and request.serialized_grad.

            Args:
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.

            Returns:
                success: (:type:`bool`, `required`):
                    True if the serialization is successful.
                request: (:obj:`Request`, required):
                    The request object holds all specifications and processing of the request.
        """
        try:
            serializer = bittensor.serializer(
                bittensor.proto.Serializer.MSGPACK)
            request.serialized_inputs = serializer.serialize(
                request.inputs,
                modality=request.modality,
                from_type=bittensor.proto.TensorType.TORCH)

            if request.backward:
                request.serialized_grads = serializer.serialize(
                    request.grads_dy,
                    modality=bittensor.proto.Modality.TENSOR,
                    from_type=bittensor.proto.TensorType.TORCH)

        except Exception as e:
            request.code = bittensor.proto.ReturnCode.RequestSerializationException
            request.message = 'Input serialization exception with error:{}'.format(
                str(e))
            self.request_log(request=request,
                             is_response=False,
                             inputs=list(request.inputs.shape))
            return False, request

        return True, request
Beispiel #26
0
def test_backward_response_success():
    def backward(inputs_x: torch.FloatTensor, grads_dy: torch.FloatTensor):
        return torch.zeros([1, 1, 1])

    axon.attach_backward_callback(backward,
                                  modality=bittensor.proto.Modality.TENSOR)
    inputs_raw = torch.rand(1, 1, 1)
    grads_raw = torch.rand(1, 1, bittensor.__network_dim__)
    serializer = bittensor.serializer(
        serialzer_type=bittensor.proto.Serializer.MSGPACK)
    inputs_serialized = serializer.serialize(
        inputs_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    grads_serialized = serializer.serialize(
        grads_raw,
        modality=bittensor.proto.Modality.TENSOR,
        from_type=bittensor.proto.TensorType.TORCH)
    request = bittensor.proto.TensorMessage(
        version=bittensor.__version_as_int__,
        hotkey=axon.wallet.hotkey.ss58_address,
        tensors=[inputs_serialized, grads_serialized])
    response, code, call_time, message = axon._backward(request)
    assert code == bittensor.proto.ReturnCode.Success
    def test_serialize_deserialize_text(self):
        # Let's create some text data
        words = ["This", "is", "a", "word", "list"]
        max_l = 0
        ts_list = []
        for w in words:
            ts_list.append(torch.ByteTensor(list(bytes(w, 'utf8'))))
            max_l = max(ts_list[-1].size()[0], max_l)

        data = torch.zeros((len(ts_list), max_l), dtype=torch.int64)
        for i, ts in enumerate(ts_list):
            data[i, 0:ts.size()[0]] = ts

        serializer = bittensor.serializer(
            serialzer_type=bittensor.proto.Serializer.MSGPACK)
        serialized_data_tensor_message = serializer.serialize(
            data,
            modality=bittensor.proto.Modality.TEXT,
            from_type=bittensor.proto.TensorType.TORCH)

        assert data.requires_grad == serialized_data_tensor_message.requires_grad
        assert list(data.shape) == serialized_data_tensor_message.shape
        assert serialized_data_tensor_message.modality == bittensor.proto.Modality.TEXT
        assert serialized_data_tensor_message.dtype != bittensor.proto.DataType.UNKNOWN

        deserialized_data_tensor_message = serializer.deserialize(
            serialized_data_tensor_message,
            to_type=bittensor.proto.TensorType.TORCH)
        assert serialized_data_tensor_message.requires_grad == deserialized_data_tensor_message.requires_grad
        assert serialized_data_tensor_message.shape == list(
            deserialized_data_tensor_message.shape)
        assert bittensor.serializer.torch_dtype_to_bittensor_dtype(
            deserialized_data_tensor_message.dtype
        ) != bittensor.proto.DataType.UNKNOWN

        assert torch.all(torch.eq(deserialized_data_tensor_message, data))