Esempio n. 1
0
    def detail(worker: BaseWorker, protocol_tuple: tuple) -> "Protocol":
        """This function reconstructs a Protocol object given its attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            protocol_tuple: a tuple holding the attributes of the Protocol
        Returns:
            protocol: a Protocol object
        """

        id, tags, description, plans_reference, workers_resolved = map(
            lambda o: sy.serde._detail(worker, o), protocol_tuple
        )

        plans = []
        for owner_id, plan_id in plans_reference:
            if workers_resolved:
                plan_owner = worker.get_worker(owner_id, fail_hard=True)
                plan_pointer = worker.request_search(plan_id, location=plan_owner)[0]
                worker.register_obj(plan_pointer)
                plans.append((plan_owner, plan_pointer))
            else:
                try:
                    plan_owner = worker.get_worker(owner_id, fail_hard=True)
                    plan_pointer = worker.request_search(plan_id, location=plan_owner)[0]
                    plan = plan_pointer.get()
                except WorkerNotFoundException:
                    plan = worker.get_obj(plan_id)
                plans.append((worker.id, plan))

        protocol = sy.Protocol(plans=plans, id=id, owner=worker, tags=tags, description=description)

        return protocol
Esempio n. 2
0
    def detail(worker: AbstractWorker, protocol_tuple: tuple) -> "Protocol":
        """This function reconstructs a Protocol object given its attributes in the form of a tuple.
        Args:
            worker: the worker doing the deserialization
            protocol_tuple: a tuple holding the attributes of the Protocol
        Returns:
            protocol: a Protocol object
        """
        (id_, name, roles, tags, description) = protocol_tuple

        id_ = sy.serde.msgpack.serde._detail(worker, id_)
        name = sy.serde.msgpack.serde._detail(worker, name)
        roles = sy.serde.msgpack.serde._detail(worker, roles)
        tags = sy.serde.msgpack.serde._detail(worker, tags)
        description = sy.serde.msgpack.serde._detail(worker, description)

        return sy.Protocol(
            id=id_,
            name=name,
            owner=worker,
            roles=roles,
            is_built=True,
            tags=tags,
            description=description,
        )
Esempio n. 3
0
def test_protocol_waiting_promise(hook, workers):
    hook.local_worker.is_client_worker = False

    alice = workers["alice"]
    bob = workers["bob"]

    @syft.func2plan(args_shape=[(1, )])
    def plan_alice1(in_a):
        return in_a + 1

    @syft.func2plan(args_shape=[(1, )])
    def plan_bob1(in_b):
        return in_b + 2

    @syft.func2plan(args_shape=[(1, )])
    def plan_bob2(in_b):
        return in_b + 3

    @syft.func2plan(args_shape=[(1, )])
    def plan_alice2(in_a):
        return in_a + 4

    protocol = syft.Protocol([("alice", plan_alice1), ("bob", plan_bob1),
                              ("bob", plan_bob2), ("alice", plan_alice2)])
    protocol.deploy(alice, bob)

    x = syft.Promise.FloatTensor(shape=torch.Size((1, )))
    in_ptr, res_ptr = protocol(x)

    in_ptr.keep(torch.tensor([1.0]))

    assert res_ptr.value().get() == 11

    hook.local_worker.is_client_worker = True
Esempio n. 4
0
def generateTwoWayProtocol(me):
    @sy.func2plan(args_shape=[(1,)], state=(th.tensor([4.2, 7.3]), ))
    def jasonPlan(x, state):
        bias, = state.read()
        y = x + bias
        z = th.abs(y)
        return z

    @sy.func2plan(args_shape=[(1,)])
    def andyPlan(x):
        y = x + x
        z = th.abs(y)
        return z

    protocol2 = sy.Protocol([("worker1", jasonPlan), ("worker2", andyPlan)])

    # Simplify the protocol
    protocolPb, simplifiedProtocol = serializeToBase64Pb(me, protocol2)

    # Simplify jasonPlan
    jasonPlanPb, simplifiedJasonPlan = serializeToBase64Pb(me, jasonPlan)

    # Simplify andyPlan
    andyPlanPb, simplifiedAndyPlan = serializeToBase64Pb(me, andyPlan)

    return {
        "protocol": str(simplifiedProtocol),
        "_protocol": str(protocolPb),
        "jason": str(simplifiedJasonPlan),
        "_jason": str(jasonPlanPb),
        "andy": str(simplifiedAndyPlan),
        "_andy": str(andyPlanPb),
    }
Esempio n. 5
0
def _create_inc_protocol():
    @sy.func2plan(args_shape=[(1, )])
    def inc1(x):
        return x + 1

    @sy.func2plan(args_shape=[(1, )])
    def inc2(x):
        return x + 1

    @sy.func2plan(args_shape=[(1, )])
    def inc3(x):
        return x + 1

    protocol = sy.Protocol([("worker1", inc1), ("worker2", inc2),
                            ("worker3", inc3)])
    return protocol
Esempio n. 6
0
def generateThreeWayProtocol(me):
    @sy.func2plan(args_shape=[(1,)], state=(th.tensor([4.2, 7.3]), ))
    def bobPlan(x, state):
        bias, = state.read()
        y = x + bias
        z = th.abs(y)
        return z

    @sy.func2plan(args_shape=[(1,)])
    def theoPlan(x):
        y = x + x
        z = th.abs(y)
        return z

    @sy.func2plan(args_shape=[(1,)])
    def alicePlan(x):
        y = x + x
        z = th.abs(y)
        return z

    protocol = sy.Protocol([("worker1", bobPlan), ("worker2", theoPlan), ("worker3", alicePlan)])

    # Simplify the protocol
    protocolPb, simplifiedProtocol = serializeToBase64Pb(me, protocol)

    # Simplify bobPlan
    bobPlanPb, simplifiedBobPlan = serializeToBase64Pb(me, bobPlan)

    # Simplify theoPlan
    theoPlanPb, simplifiedTheoPlan = serializeToBase64Pb(me, theoPlan)

    # Simplify alicePlan
    alicePlanPb, simplifiedAlicePlan = serializeToBase64Pb(me, alicePlan)

    return {
        "protocol": str(simplifiedProtocol),
        "_protocol": str(protocolPb),
        "bob": str(simplifiedBobPlan),
        "_bob": str(bobPlanPb),
        "theo": str(simplifiedTheoPlan),
        "_theo": str(theoPlanPb),
        "alice": str(simplifiedAlicePlan),
        "_alice": str(alicePlanPb),
    }
Esempio n. 7
0
    def create_from_attributes(worker, id, tags, description, workers_resolved,
                               plans_assignments) -> "Protocol":
        """
        This function reconstructs a Protocol object given its attributes.

        Args:
            worker: the worker doing the deserialization
            id: Protocol id
            tags: Protocol tags
            description: Protocol description
            workers_resolved: Flag whether workers are resolved
            plans_assignments: List of workers/plans IDs

        Returns:
            protocol: a Protocol object
        """
        plans = []
        for owner_id, plan_id in plans_assignments:
            if workers_resolved:
                plan_owner = worker.get_worker(owner_id, fail_hard=True)
                plan_pointer = worker.request_search(plan_id,
                                                     location=plan_owner)[0]
                worker.register_obj(plan_pointer)
                plans.append((plan_owner, plan_pointer))
            else:
                try:
                    plan_owner = worker.get_worker(owner_id, fail_hard=True)
                except WorkerNotFoundException:
                    plan = worker.get_obj(plan_id)
                else:
                    plan_pointer = worker.request_search(
                        plan_id, location=plan_owner)[0]
                    plan = plan_pointer.get()
                plans.append((worker.id, plan))

        protocol = sy.Protocol(plans=plans,
                               id=id,
                               owner=worker,
                               tags=tags,
                               description=description)

        return protocol