Exemplo n.º 1
0
def deserialize(binary: bin, worker: AbstractWorker = None, unbufferizes=True) -> object:
    """ This method can deserialize any object PySyft needs to send or store.

    This is the high level function for deserializing any object or collection
    of objects which PySyft has sent over the wire or stored. It includes three
    steps, Decompress, Deserialize, and Detail as described inline below.

    Args:
        binary (bin): the serialized object to be deserialized.
        worker (AbstractWorker): the worker which is acquiring the message content,
            for example used to specify the owner of a tensor received(not obvious
            for virtual workers)
        unbufferizes (bool): there are some cases where we need to perform the decompression
            and deserialization part, but we don't need to unbufferize all the message.
            This is the case for Plan workers for instance

    Returns:
        object: the deserialized form of the binary input.
    """

    if worker is None:
        # TODO[jvmancuso]: This might be worth a standalone function.
        worker = syft.framework.hook.local_worker

    # 1) Decompress the binary if needed
    binary = compression._decompress(binary)

    # 2) Deserialize
    msg_wrapper = SyftMessagePB()
    msg_wrapper.ParseFromString(binary)

    # 3) Convert back to a Python object
    message_type = msg_wrapper.WhichOneof("contents")
    python_obj = _unbufferize(worker, getattr(msg_wrapper, message_type))
    return python_obj
Exemplo n.º 2
0
def test_compress_decompress(compress_scheme):
    if compress_scheme == compression.LZ4:
        compression._apply_compress_scheme = compression.apply_lz4_compression
    else:
        compression._apply_compress_scheme = compression.apply_no_compression

    original = msgpack_lib.dumps([1, 2, 3])
    compressed = compression._compress(original)
    decompressed = compression._decompress(compressed)
    assert type(compressed) == bytes
    assert original == decompressed
Exemplo n.º 3
0
def _deserialize_msgpack_binary(binary: bin, worker: AbstractWorker = None) -> object:
    if worker is None:
        # TODO[jvmancuso]: This might be worth a standalone function.
        worker = syft.framework.hook.local_worker

    # 1) Decompress the binary if needed
    binary = compression._decompress(binary)

    # 2) Deserialize
    # This function converts the binary into the appropriate python
    # object (or nested dict/collection of python objects)
    simple_objects = msgpack_lib.loads(binary, use_list=False)

    # sometimes we want to skip detailing (such as in Plan)
    return simple_objects