Example #1
0
    def send_command(
        self,
        recipient: "BaseWorker",
        cmd_name: str,
        target: PointerTensor = None,
        args_: tuple = (),
        kwargs_: dict = {},
        return_ids: str = None,
        return_value: bool = False,
    ) -> Union[List[PointerTensor], PointerTensor]:
        """
        Sends a command through a message to a recipient worker.

        Args:
            recipient: A recipient worker.
            cmd_name: Command number.
            target: Target pointer Tensor.
            args_: additional args for command execution.
            kwargs_: additional kwargs for command execution.
            return_ids: A list of strings indicating the ids of the
                tensors that should be returned as response to the command execution.

        Returns:
            A list of PointerTensors or a single PointerTensor if just one response is expected.
        """
        if return_ids is None:
            return_ids = (sy.ID_PROVIDER.pop(), )

        try:
            message = TensorCommandMessage.computation(cmd_name, target, args_,
                                                       kwargs_, return_ids,
                                                       return_value)
            ret_val = self.send_msg(message, location=recipient)
        except ResponseSignatureError as e:
            ret_val = None
            return_ids = e.ids_generated

        if ret_val is None or type(ret_val) == bytes:
            responses = []
            for return_id in return_ids:
                response = PointerTensor(
                    location=recipient,
                    id_at_location=return_id,
                    owner=self,
                    id=sy.ID_PROVIDER.pop(),
                )
                responses.append(response)

            if return_value:
                responses = [response.get() for response in responses]

            if len(return_ids) == 1:
                responses = responses[0]
            else:
                responses = tuple(responses)
        else:
            responses = ret_val
        return responses
Example #2
0
    def _get_plan_output(self, result_ids, return_ptr=False):
        responses = []
        for return_id in result_ids:
            response = PointerTensor(
                location=self.owner, id_at_location=return_id, owner=self, id=sy.ID_PROVIDER.pop()
            )
            responses.append(response if return_ptr else response.get())

        if len(responses) == 1:
            return responses[0]

        return responses
Example #3
0
def test_build_rule_syft_tensors_and_pointers():
    pointer = PointerTensor(id=1000,
                            location="location",
                            owner="owner",
                            garbage_collect_data=False)
    result = hook_args.build_rule(([torch.tensor([1, 2]), pointer], 42))
    assert result == ([1, 1], 0)
Example #4
0
def test_pointer_tensor(hook, workers):
    serde._apply_compress_scheme = serde.apply_no_compression
    t = PointerTensor(
        id=1000, location=workers["alice"], owner=workers["alice"], id_at_location=12345
    )
    t_serialized = serde.serialize(t)
    t_serialized_deserialized = serde.deserialize(t_serialized)
    assert t.id == t_serialized_deserialized.id
    assert t.location.id == t_serialized_deserialized.location.id
    assert t.id_at_location == t_serialized_deserialized.id_at_location
Example #5
0
def test_pointer_found_exception(workers):
    ptr_id = syft.ID_PROVIDER.pop()
    pointer = PointerTensor(id=ptr_id, location=workers["alice"], owner=workers["me"])

    try:
        raise RemoteObjectFoundError(pointer)
    except RemoteObjectFoundError as err:
        err_pointer = err.pointer
        assert isinstance(err_pointer, PointerTensor)
        assert err_pointer.id == ptr_id
Example #6
0
def test_pointer_tensor_simplify():
    """Test the simplification of PointerTensor"""

    alice = syft.VirtualWorker(syft.torch.hook, id="alice")
    input_tensor = PointerTensor(id=1000, location=alice, owner=alice)

    output = serde._simplify(input_tensor)

    assert output[1][0] == input_tensor.id
    assert output[1][1] == input_tensor.id_at_location
    assert output[1][2] == input_tensor.owner.id
Example #7
0
def test_pointer_tensor_simplify(workers):
    """Test the simplification of PointerTensor"""

    alice, me = workers["alice"], workers["me"]

    input_tensor = PointerTensor(id=1000, location=alice, owner=alice)

    output = msgpack.serde._simplify(me, input_tensor)

    assert output[1][0] == input_tensor.id
    assert output[1][1] == input_tensor.id_at_location
    assert output[1][2] == msgpack.serde._simplify(me, input_tensor.owner.id)
Example #8
0
    async def async_send_command(
        self,
        message: tuple,
        return_ids: str = None,
        return_value: bool = False
    ) -> Union[List[PointerTensor], PointerTensor]:
        """
        Sends a command through a message to the server part attached to the client
        Args:
            message: A tuple representing the message being sent.
            return_ids: A list of strings indicating the ids of the
                tensors that should be returned as response to the command execution.
        Returns:
            A list of PointerTensors or a single PointerTensor if just one response is expected.
        Note: this is the async version of send_command, with the major difference that you
        directly call it on the client worker (so we don't have the recipient kw argument)
        """

        if return_ids is None:
            return_ids = (sy.ID_PROVIDER.pop(), )

        name, target, args_, kwargs_ = message

        # Close the existing websocket connection in order to open a asynchronous connection
        self.close()
        try:
            message = TensorCommandMessage.computation(name, target, args_,
                                                       kwargs_, return_ids,
                                                       return_value)
            ret_val = await self.async_send_msg(message)

        except ResponseSignatureError as e:
            ret_val = None
            return_ids = e.ids_generated
        # Reopen the standard connection
        self.connect()

        if ret_val is None or type(ret_val) == bytes:
            responses = []
            for return_id in return_ids:
                response = PointerTensor(
                    location=self,
                    id_at_location=return_id,
                    owner=sy.local_worker,
                    id=sy.ID_PROVIDER.pop(),
                )
                responses.append(response)

            if len(return_ids) == 1:
                responses = responses[0]
        else:
            responses = ret_val
        return responses
Example #9
0
    def send_command(self,
                     recipient: "BaseWorker",
                     message: tuple,
                     return_ids: str = None
                     ) -> Union[List[PointerTensor], PointerTensor]:
        """
        Sends a command through a message to a recipient worker.

        Args:
            recipient: A recipient worker.
            message: A tuple representing the message being sent.
            return_ids: A list of strings indicating the ids of the
                tensors that should be returned as response to the command execution.

        Returns:
            A list of PointerTensors or a single PointerTensor if just one response is expected.
        """
        if return_ids is None:
            return_ids = tuple([sy.ID_PROVIDER.pop()])

        cmd_name = message[0]
        cmd_owner = message[1]
        cmd_args = message[2]
        cmd_kwargs = message[3]

        try:
            ret_val = self.send_msg(
                OperationMessage(cmd_name, cmd_owner, cmd_args, cmd_kwargs,
                                 return_ids),
                location=recipient,
            )
        except ResponseSignatureError as e:
            ret_val = None
            return_ids = e.ids_generated

        if ret_val is None or type(ret_val) == bytes:
            responses = []
            for return_id in return_ids:
                response = PointerTensor(
                    location=recipient,
                    id_at_location=return_id,
                    owner=self,
                    id=sy.ID_PROVIDER.pop(),
                )
                responses.append(response)

            if len(return_ids) == 1:
                responses = responses[0]
        else:
            responses = ret_val
        return responses
Example #10
0
    def fetch_plan(self,
                   plan_id: Union[str, int],
                   location: "BaseWorker",
                   copy: bool = False) -> "Plan":  # noqa: F821
        """Fetchs a copy of a the plan with the given `plan_id` from the worker registry.

        This method is executed for local execution.

        Args:
            plan_id: A string indicating the plan id.

        Returns:
            A plan if a plan with the given `plan_id` exists. Returns None otherwise.
        """
        message = PlanCommandMessage("fetch_plan", (plan_id, copy))
        plan = self.send_msg(message, location=location)

        plan.replace_worker_ids(location.id, self.id)

        if plan.state_ids:
            state_ids = []
            for state_id in plan.state_ids:
                if copy:
                    state_ptr = PointerTensor(
                        location=location,
                        id_at_location=state_id,
                        owner=self,
                        garbage_collect_data=False,
                    )
                    state_elem = state_ptr.copy().get()
                else:
                    state_elem = self.request_obj(state_id, location)
                self.register_obj(state_elem)
                state_ids.append(state_elem.id)
            plan.replace_ids(plan.state_ids, state_ids)
            plan.state_ids = state_ids

        return plan
Example #11
0
 def callback(grad):
     assert isinstance(grad, torch.Tensor), "Grad in callback should be a Tensor"
     # the grad tensor is created by the torch backprop and might not be registered properly
     self.owner.register_obj(grad)
     pointer = PointerTensor(
         location=self.owner,
         id_at_location=grad.id,
         owner=location,
         id=syft.ID_PROVIDER.pop(),
     )
     # update the message arguments
     message.action.args = (pointer,)
     self.owner.send_msg(message=message, location=location)
     # De-register the grad after the callback has been handled
     self.owner.de_register_obj(grad)
Example #12
0
def test_raising_error_when_item_func_called(workers):
    pointer = PointerTensor(id=1000,
                            location=workers["alice"],
                            owner=workers["me"])
    with pytest.raises(RuntimeError):
        pointer.item()
Example #13
0
def test_init(workers):
    alice, me = workers["alice"], workers["me"]
    pointer = PointerTensor(id=1000, location=alice, owner=me)
    pointer.__str__()
Example #14
0
def test_init(workers):
    pointer = PointerTensor(id=1000,
                            location=workers["alice"],
                            owner=workers["me"])
    pointer.__str__()