def test_ellipsis_simplify(): """Make sure ellipsis simplifies correctly.""" assert serde.detailers[serde._simplify(Ellipsis) [0]] == native_serde._detail_ellipsis # the simplified ellipsis (empty object) assert serde._simplify(Ellipsis)[1] == b""
def test_ellipsis_simplify(): """Make sure ellipsis simplifies correctly.""" # the id indicating an ellipsis is here assert _simplify(Ellipsis)[0] == 9 # the simplified ellipsis (empty object) assert _simplify(Ellipsis)[1] == b""
def test_torch_device_simplify(): """Test the simplification of torch.device""" device = torch.device("cpu") assert serde.detailers[serde._simplify(device)[0]] == torch_serde._detail_torch_device # the simplified torch.device assert serde._simplify(device)[1] == "cpu"
def test_ellipsis_simplify(workers): """Make sure ellipsis simplifies correctly.""" me = workers["me"] assert serde.detailers[serde._simplify(me, Ellipsis)[0]] == native_serde._detail_ellipsis # the simplified ellipsis (empty object) assert serde._simplify(me, Ellipsis)[1] == (b"",)
def test_torch_device_simplify(): """Test the simplification of torch.device""" device = torch.device("cpu") # the id indicating an torch.device is here assert _simplify(device)[0] == 10 # the simplified torch.device assert _simplify(device)[1] == "cpu"
def test_torch_device_simplify(workers): """Test the simplification of torch.device""" me = workers["me"] device = torch.device("cpu") assert serde.detailers[serde._simplify(me, device)[0]] == torch_serde._detail_torch_device # the simplified torch.device assert serde._simplify(me, device)[1] == "cpu"
def test_set_simplify(): """This tests our ability to simplify set objects. This test is pretty simple since sets just serialize to lists, with a tuple wrapper with the correct ID (3) for sets so that the detailer knows how to interpret it.""" input = set(["hello", "world"]) target = (4, [(18, (b"hello", )), (18, (b"world", ))]) assert _simplify(input)[0] == target[0] assert set(_simplify(input)[1]) == set(target[1])
def test_pointer_tensor_simplify(workers): """Test the simplification of PointerTensor""" alice, me = workers["alice"], workers["me"] input_tensor = PointerTensor(id=1000, location=alice, owner=alice) output = serde._simplify(me, input_tensor) assert output[1][0] == input_tensor.id assert output[1][1] == input_tensor.id_at_location assert output[1][2] == serde._simplify(me, input_tensor.owner.id)
def test_set_simplify(): """This tests our ability to simplify set objects. This test is pretty simple since sets just serialize to lists, with a tuple wrapper with the correct ID (3) for sets so that the detailer knows how to interpret it.""" input = set(["hello", "world"]) set_detail_code = serde.proto_type_info(set).code str_detail_code = serde.proto_type_info(str).code target = (set_detail_code, ((str_detail_code, (b"hello",)), (str_detail_code, (b"world",)))) assert serde._simplify(input)[0] == target[0] assert set(serde._simplify(input)[1]) == set(target[1])
def test_set_simplify(): """This tests our ability to simplify set objects. This test is pretty simple since sets just serialize to lists, with a tuple wrapper with the correct ID (3) for sets so that the detailer knows how to interpret it.""" input = set(["hello", "world"]) set_detail_index = serde.detailers.index( native_serde._detail_collection_set) str_detail_index = serde.detailers.index(native_serde._detail_str) target = (set_detail_index, ((str_detail_index, (b"hello", )), (str_detail_index, (b"world", )))) assert serde._simplify(input)[0] == target[0] assert set(serde._simplify(input)[1]) == set(target[1])
def test_torch_tensor_simplify_generic(workers): """This tests our ability to simplify torch.Tensor objects using "all" serialization strategy """ worker = VirtualWorker(None, id="non-torch") # create a tensor input = Tensor(numpy.random.random((3, 3, 3))) # simplify the tensor output = serde._simplify(worker, input) # make sure outer type is correct assert type(output) == tuple # make sure the object type ID is correct # (0 for torch.Tensor) assert serde.detailers[output[0]] == torch_serde._detail_torch_tensor # make sure inner type is correct assert type(output[1]) == tuple # make sure ID is correctly encoded assert output[1][0] == input.id # make sure tensor data type is correct assert type(output[1][1]) == tuple assert type(output[1][1][1]) == tuple # make sure tensor data matches assert output[1][1][1][0][1] == input.size() assert output[1][1][1][2][1] == tuple(input.flatten().tolist())
def test_torch_tensor_simplify(workers): """This tests our ability to simplify torch.Tensor objects using "torch" serialization strategy. At the time of writing, tensors simplify to a tuple where the first value in the tuple is the tensor's ID and the second value is a serialized version of the Tensor (serialized by PyTorch's torch.save method) """ me = workers["me"] # create a tensor input = Tensor(numpy.random.random((100, 100))) # simplify the tnesor output = serde._simplify(me, input) # make sure outer type is correct assert type(output) == tuple # make sure the object type ID is correct # (0 for torch.Tensor) assert serde.detailers[output[0]] == torch_serde._detail_torch_tensor # make sure inner type is correct assert type(output[1]) == tuple # make sure ID is correctly encoded assert output[1][0] == input.id # make sure tensor data type is correct assert type(output[1][1]) == bytes
def test_torch_tensor_simplify(): """This tests our ability to simplify torch.Tensor objects At the time of writing, tensors simplify to a tuple where the first value in the tuple is the tensor's ID and the second value is a serialized version of the Tensor (serialized by PyTorch's torch.save method) """ # create a tensor input = Tensor(numpy.random.random((100, 100))) # simplify the tnesor output = _simplify(input) # make sure outer type is correct assert type(output) == tuple # make sure the object type ID is correct # (0 for torch.Tensor) assert output[0] == 0 # make sure inner type is correct assert type(output[1]) == tuple # make sure ID is correctly encoded assert output[1][0] == input.id # make sure tensor data type is correct assert type(output[1][1]) == bytes
def test_no_simplifier_found(): """Test that types that can not be simplified are cached.""" # Clean cache. serde.no_simplifiers_found = set() x = 1.3 assert type(x) not in serde.no_simplifiers_found _ = serde._simplify(x) assert type(x) in serde.no_simplifiers_found
def test_float_simplify(): """This tests our ability to simplify float objects. This test is pretty simple since floats just serialize to themselves, with no tuple/id necessary.""" input = 5.6 target = 5.6 assert _simplify(input) == target
def test_numpy_number_simplify(workers): """This tests our ability to simplify numpy.float objects At the time of writing, numpy number simplify to an object inside of a tuple where the first value is a byte representation of the number and the second value is the dtype """ me = workers["me"] input = numpy.float32(2.0) output = serde._simplify(me, input) # make sure simplified type ID is correct assert serde.detailers[output[0]] == native_serde._detail_numpy_number # make sure serialized form is correct assert type(output[1][0]) == bytes assert output[1][1] == serde._simplify(me, input.dtype.name)
def test_string_simplify(): """This tests our ability to simplify string objects. This test is pretty simple since strings just serialize to themselves, with no tuple/id necessary.""" input = "hello" target = (serde.proto_type_info(str).code, (b"hello",)) assert serde._simplify(input) == target
def test_int_simplify(): """This tests our ability to simplify int objects. This test is pretty simple since ints just serialize to themselves, with no tuple/id necessary.""" input = 5 target = 5 assert serde._simplify(input) == target
def test_string_simplify(): """This tests our ability to simplify string objects. This test is pretty simple since strings just serialize to themselves, with no tuple/id necessary.""" input = "hello" target = (serde.detailers.index(native_serde._detail_str), (b"hello", )) assert serde._simplify(input) == target
def test_tuple_simplify(): """This tests our ability to simplify tuple types. This test is pretty simple since tuples just serialize to themselves, with a tuple wrapper with the correct ID (1) for tuples so that the detailer knows how to interpret it.""" input = ("hello", "world") target = (2, ((18, (b"hello", )), (18, (b"world", )))) assert _simplify(input) == target
def test_ndarray_simplify(workers): """This tests our ability to simplify numpy.array objects At the time of writing, arrays simplify to an object inside of a tuple which specifies the ID for the np.array type (6) so that the detailer knows to turn the simplifed form to a np.array """ me = workers["me"] input = numpy.random.random((100, 100)) output = serde._simplify(me, input) # make sure simplified type ID is correct assert serde.detailers[output[0]] == native_serde._detail_ndarray # make sure serialized form is correct assert type(output[1][0]) == bytes assert output[1][1] == serde._simplify(me, input.shape) assert output[1][2] == serde._simplify(me, input.dtype.name)
def test_list_simplify(): """This tests our ability to simplify list types. This test is pretty simple since lists just serialize to themselves, with a tuple wrapper with the correct ID (2) for lists so that the detailer knows how to interpret it.""" input = ["hello", "world"] target = (3, [(18, (b"hello", )), (18, (b"world", ))]) assert _simplify(input) == target
def test_range_simplify(): """This tests our ability to simplify range objects. This test is pretty simple since range objs just serialize to themselves, with a tuple wrapper with the correct ID (5) for dicts so that the detailer knows how to interpret it.""" input = range(1, 3, 4) target = (serde.proto_type_info(range).code, (1, 3, 4)) assert serde._simplify(input) == target
def test_float_simplify(workers): """This tests our ability to simplify float objects. This test is pretty simple since floats just serialize to themselves, with no tuple/id necessary.""" me = workers["me"] input = 5.6 target = 5.6 assert serde._simplify(me, input) == target
def test_dict_simplify(): """This tests our ability to simplify dict objects. This test is pretty simple since dicts just serialize to themselves, with a tuple wrapper with the correct ID (4) for dicts so that the detailer knows how to interpret it.""" input = {"hello": "world"} target = (5, [((18, (b"hello", )), (18, (b"world", )))]) assert _simplify(input) == target
def test_pointer_tensor_simplify(): """Test the simplification of PointerTensor""" alice = syft.VirtualWorker(syft.torch.hook, id="alice") input_tensor = PointerTensor(id=1000, location=alice, owner=alice) output = serde._simplify(input_tensor) assert output[1][0] == input_tensor.id assert output[1][1] == input_tensor.id_at_location assert output[1][2] == input_tensor.owner.id
def test_list_simplify(): """This tests our ability to simplify list types. This test is pretty simple since lists just serialize to themselves, with a tuple wrapper with the correct ID (2) for lists so that the detailer knows how to interpret it.""" input = ["hello", "world"] list_detail_code = serde.proto_type_info(list).code str_detail_code = serde.proto_type_info(str).code target = (list_detail_code, ((str_detail_code, (b"hello",)), (str_detail_code, (b"world",)))) assert serde._simplify(input) == target
def test_dict_simplify(): """This tests our ability to simplify dict objects. This test is pretty simple since dicts just serialize to themselves, with a tuple wrapper with the correct ID for dicts so that the detailer knows how to interpret it.""" input = {"hello": "world"} detail_dict_code = serde.proto_type_info(dict).code detail_str_code = serde.proto_type_info(str).code target = (detail_dict_code, (((detail_str_code, (b"hello",)), (detail_str_code, (b"world",))),)) assert serde._simplify(input) == target
def test_tuple_simplify(workers): """This tests our ability to simplify tuple types. This test is pretty simple since tuples just serialize to themselves, with a tuple wrapper with the correct ID (1) for tuples so that the detailer knows how to interpret it.""" me = workers["me"] input = ("hello", "world") tuple_detail_code = serde.proto_type_info(tuple).code str_detail_code = serde.proto_type_info(str).code target = (tuple_detail_code, ((str_detail_code, (b"hello",)), (str_detail_code, (b"world",)))) assert serde._simplify(me, input) == target
def test_list_simplify(): """This tests our ability to simplify list types. This test is pretty simple since lists just serialize to themselves, with a tuple wrapper with the correct ID (2) for lists so that the detailer knows how to interpret it.""" input = ["hello", "world"] list_detail_index = serde.detailers.index( native_serde._detail_collection_list) str_detail_index = serde.detailers.index(native_serde._detail_str) target = (list_detail_index, ((str_detail_index, (b"hello", )), (str_detail_index, (b"world", )))) assert serde._simplify(input) == target