Example #1
0
    def bufferize(worker: AbstractWorker,
                  operation: "OperationMessage") -> "OperationMessagePB":
        """
        This function takes the attributes of a OperationMessage and saves them in Protobuf
        Args:
            worker (AbstractWorker): a reference to the worker doing the serialization
            ptr (OperationMessage): an OperationMessage
        Returns:
            protobuf_obj: a Protobuf message holding the unique attributes of the message
        Examples:
            data = bufferize(message)
        """
        protobuf_op_msg = OperationMessagePB()
        protobuf_op = OperationPB()
        protobuf_op.command = operation.cmd_name

        if type(operation.cmd_owner
                ) == sy.generic.pointers.pointer_tensor.PointerTensor:
            protobuf_owner = protobuf_op.owner_pointer
        elif (type(operation.cmd_owner) == sy.frameworks.torch.tensors.
              interpreters.placeholder.PlaceHolder):
            protobuf_owner = protobuf_op.owner_placeholder
        else:
            protobuf_owner = protobuf_op.owner_tensor

        if operation.cmd_owner is not None:
            protobuf_owner.CopyFrom(
                sy.serde.protobuf.serde._bufferize(worker,
                                                   operation.cmd_owner))

        if operation.cmd_args:
            protobuf_op.args.extend(
                OperationMessage._bufferize_args(worker, operation.cmd_args))

        if operation.cmd_kwargs:
            for key, value in operation.cmd_kwargs.items():
                protobuf_op.kwargs.get_or_create(key).CopyFrom(
                    OperationMessage._bufferize_arg(worker, value))

        if operation.return_ids is not None:
            if type(operation.return_ids) == PlaceHolder:
                return_ids = list((operation.return_ids, ))
            else:
                return_ids = operation.return_ids

            for return_id in return_ids:
                if type(return_id) == PlaceHolder:
                    protobuf_op.return_placeholders.extend([
                        sy.serde.protobuf.serde._bufferize(worker, return_id)
                    ])
                else:
                    sy.serde.protobuf.proto.set_protobuf_id(
                        protobuf_op.return_ids.add(), return_id)

        protobuf_op_msg.operation.CopyFrom(protobuf_op)
        return protobuf_op_msg
Example #2
0
    def unbufferize(worker: AbstractWorker, protobuf_plan: PlanPB) -> "Plan":
        """This function reconstructs a Plan object given its attributes in the form of a Protobuf message
        Args:
            worker: the worker doing the deserialization
            protobuf_plan: a Protobuf message holding the attributes of the Plan
        Returns:
            plan: a Plan object
        """

        worker._tmp_placeholders = {}
        id = sy.serde.protobuf.proto.get_protobuf_id(protobuf_plan.id)

        operations = []
        for operation in protobuf_plan.operations:
            op_msg = OperationMessagePB()
            op_msg.operation.CopyFrom(operation)
            operations.append(op_msg)

        operations = [
            sy.serde.protobuf.serde._unbufferize(worker, operation)
            for operation in operations
        ]
        state = sy.serde.protobuf.serde._unbufferize(worker,
                                                     protobuf_plan.state)

        placeholders = [
            sy.serde.protobuf.serde._unbufferize(worker, placeholder)
            for placeholder in protobuf_plan.placeholders
        ]
        placeholders = dict([(placeholder.id, placeholder)
                             for placeholder in placeholders])

        plan = sy.Plan(
            include_state=protobuf_plan.include_state,
            is_built=protobuf_plan.is_built,
            operations=operations,
            placeholders=placeholders,
            id=id,
            owner=worker,
        )
        del worker._tmp_placeholders

        plan.state = state
        state.plan = plan

        plan.name = protobuf_plan.name
        if protobuf_plan.tags:
            plan.tags = set(protobuf_plan.tags)
        if protobuf_plan.description:
            plan.description = protobuf_plan.description

        return plan