예제 #1
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
예제 #2
0
파일: serde.py 프로젝트: hobbit19/PySyft
def _serialize_msgpack_binary(
    simple_objects: object,
    worker: AbstractWorker = None,
    simplified: bool = False,
    force_full_simplification: bool = False,
) -> bin:
    # 2) Serialize
    # serialize into a binary
    binary = msgpack_lib.dumps(simple_objects)

    # 3) Compress
    # compress the binary and return the result
    # prepend a 1-byte header '0' or '1' to the output stream
    # to denote whether output stream is compressed or not
    # if compressed stream length is greater than input stream
    # we output the input stream as it is with header set to '0'
    # otherwise we output the compressed stream with header set to '1'
    # even if compressed flag is set to false by the caller we
    # output the input stream as it is with header set to '0'
    return compression._compress(binary)
예제 #3
0
def serialize(
    obj: object,
    worker: AbstractWorker = None,
    simplified: bool = False,
    force_no_compression: bool = False,
    force_no_serialization: bool = False,
    force_full_simplification: bool = False,
) -> bin:
    """This method can serialize any object PySyft needs to send or store.

    This is the high level function for serializing any object or collection
    of objects which PySyft needs to send over the wire. It includes three
    steps, Simplify, Serialize, and Compress as described inline below.

    Args:
        obj (object): the object to be serialized
        simplified (bool): in some cases we want to pass in data which has
            already been simplified - in which case we must skip double
            simplification - which would be bad.... so bad... so... so bad
        force_no_compression (bool): If true, this will override ANY module
            settings and not compress the objects being serialized. The primary
            expected use of this functionality is testing and/or experimentation.
        force_no_serialization (bool): Primarily a testing tool, this will force
            this method to return human-readable Python objects which is very useful
            for testing and debugging (forceably overrides module compression,
            serialization, and the 'force_no_compression' override)). In other words,
            only simplification operations are performed.
        force_full_simplification (bool): Some objects are only partially serialized
            by default. For objects where this is the case, setting this flag to True
            will force the entire object to be serialized. For example, setting this
            flag to True will cause a VirtualWorker to be serialized WITH all of its
            tensors while by default VirtualWorker objects only serialize a small
            amount of metadata.

    Returns:
        binary: the serialized form of the object.
    """

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

    if force_no_serialization:
        # 0) Simplify
        # bufferize difficult-to-serialize objects. See the _bufferize method
        # for unbufferizes on how this works. The general purpose is to handle types
        # which the fast serializer cannot handle
        simple_objects = obj
        if not simplified:
            if force_full_simplification:
                simple_objects = _force_full_bufferize(worker, obj)
            else:
                simple_objects = _bufferize(worker, obj)
        return simple_objects

    # 1) Convert to Protobuf objects
    msg_wrapper = SyftMessagePB()

    protobuf_obj = _bufferize(worker, obj)

    obj_type = type(obj)
    if isinstance(obj_type, None):
        msg_wrapper.contents_empty_msg.CopyFrom(protobuf_obj)
    elif obj_type == ObjectMessage:
        msg_wrapper.contents_object_msg.CopyFrom(protobuf_obj)
    elif obj_type == TensorCommandMessage:
        msg_wrapper.contents_action_msg.CopyFrom(protobuf_obj)

    # 2) Serialize
    # serialize into a binary
    binary = msg_wrapper.SerializeToString()

    # 3) Compress
    # optionally compress the binary and return the result
    # prepend a 1-byte header '0' or '1' to the output stream
    # to denote whether output stream is compressed or not
    # if compressed stream length is greater than input stream
    # we output the input stream as it is with header set to '0'
    # otherwise we output the compressed stream with header set to '1'
    # even if compressed flag is set to false by the caller we
    # output the input stream as it is with header set to '0'
    if force_no_compression:
        return binary
    else:
        return compression._compress(binary)