syft.core.common.serde.serializable

Functions

bind_protobuf(cls)

Classes

Serializable()

When we want a custom object to be serializable within the Syft ecosystem (as outline in the tutorial above), the first thing we need to do is have it subclass from this class.

class syft.core.common.serde.serializable.Serializable[source]

Bases: object

When we want a custom object to be serializable within the Syft ecosystem (as outline in the tutorial above), the first thing we need to do is have it subclass from this class. You must then do the following in order for the subclass to be properly implemented:

  • implement a protobuf file in the “PySyft/proto” folder for this custom class.

  • compile the protobuf file by running bash scripts/build_proto

  • find the generated python file in syft.proto

  • import the generated protobuf class into my custom class

  • implement get_protobuf_schema

  • implement <my class>._object2proto() method to serialize the object to protobuf

  • implement <my class>._proto2object() to deserialize the protobuf object

At this point, your class should be ready to serialize and deserialize! Don’t forget to add tests for your object!

If you want to wrap an existing type (like a torch.tensor) to be used in our serialization ecosystem, you should consider wrapping it. Wrapping means that we NEVER use the wrapper further more into our ecosystem, we only need an easy interface to serialize wrappers.

Eg:

class WrapperInt(Serializable)
    def __init__(self, value: int):
       self.int_obj = value

    def _object2proto(self) -> WrapperIntPB:
       ...

    @staticmethod
    def _proto2object(proto) -> int:
       ...

    @staticmethod
    def get_protobuf_schema() -> GeneratedProtocolMessageType:
       ...

    @staticmethod
    def get_wrapped_type() -> type:
       return int

You must implement the following in order for the subclass to be properly implemented to be seen as a wrapper:

  • everything presented in the first tutorial of this docstring.

  • implement get_wrapped_type to return the wrapped type.

Note: A wrapper should NEVER be used in the codebase, these are only for serialization purposes on external objects.

After doing all of the above steps, you can call something like sy.serialize(5) and be serialized using our messaging proto backbone.

static get_protobuf_schema()[source]

Return the type of protobuf object which stores a class of this type

As a part of serialization and deserialization, we need the ability to lookup the protobuf object type directly from the object type. This static method allows us to do this.

Importantly, this method is also used to create the reverse lookup ability within the metaclass of Serializable. In the metaclass, it calls this method and then it takes whatever type is returned from this method and adds an attribute to it with the type of this class attached to it. See the MetaSerializable class for details.

Returns

the type of protobuf object which corresponds to this class.

Return type

GeneratedProtocolMessageType

static get_wrapped_type()[source]

This static method returns the wrapped type, if the current class is a wrapper over an external object.

Returns

the wrapped type

Return type

type