Example #1
0
    def bufferize(worker: AbstractWorker, state: "State") -> StatePB:
        """
        Serialize the State to Protobuf message
        """
        protobuf_state = StatePB()

        protobuf_placeholders = [
            sy.serde.protobuf.serde._bufferize(worker, placeholder)
            for placeholder in state.state_placeholders
        ]
        protobuf_state.placeholders.extend(protobuf_placeholders)

        state_tensors = []
        for tensor in state.tensors():
            protobuf_tensor = sy.serde.protobuf.serde._bufferize(
                worker, tensor)
            state_tensor = StateTensorPB()
            if type(protobuf_tensor) == ParameterPB:
                state_tensor.torch_param.CopyFrom(
                    sy.serde.protobuf.serde._bufferize(worker, tensor))
            else:
                state_tensor.torch_tensor.CopyFrom(
                    sy.serde.protobuf.serde._bufferize(worker, tensor))
            state_tensors.append(state_tensor)

        protobuf_state.tensors.extend(state_tensors)

        return protobuf_state
Example #2
0
    def _object2proto(self) -> StatePB:
        """Returns a protobuf serialization of self.

        As a requirement of all objects which inherit from Serializable,
        this method transforms the current object into the corresponding
        Protobuf object so that it can be further serialized.

        :return: returns a protobuf object
        :rtype: ObjectWithID_PB

        .. note::
            This method is purely an internal method. Please use object.serialize() or one of
            the other public serialization methods if you wish to serialize an
            object.
        """
        proto = StatePB()
        protobuf_placeholders = [
            serialize(placeholder) for placeholder in self.state_placeholders
        ]
        proto.placeholders.extend(protobuf_placeholders)

        state_tensors = []
        for tensor in self.tensors():
            state_tensor = StateTensorPB()
            state_tensor.torch_tensor.CopyFrom(serialize_tensor(tensor))
            state_tensors.append(state_tensor)

        proto.tensors.extend(state_tensors)
        return proto