def __init__(self, input=None, output=None, syft_obj=None, use_torch_init=True): super(Linear, self).__init__() if (syft_obj is None): self.syft_obj = nn.Linear(input, output) if (use_torch_init): self.torch_obj = actual_torch.torch.nn.Linear(input, output) om_weights, om_bias = self.syft_obj.parameters() om_weights *= 0 om_bias *= 0 om_weights += syft.FloatTensor( self.torch_obj.weight.data.t().numpy()) om_bias += syft.FloatTensor( self.torch_obj.bias.data.view(1, -1).numpy()) self.torch_obj = None else: self.syft_obj = syft_obj
def test_add_remote_tensor(self): x = sy.FloatTensor([1, 2, 3, 4]) x.send(bob, ptr_id=1000) x.send(alice, ptr_id=2000) y = sy.FloatTensor([2, 3, 4, 5]) y.send(bob, ptr_id=1001) y.send(alice, ptr_id=2001) z = torch.add(x, y) z.get().get() assert torch.equal(z, torch.FloatTensor([3, 5, 7, 9]))
def test_floattensordata2json2floattensordata(self): # this tests the serialization / deserialization of the data FloatTensor # objects (which is different from those which are merely wrappers). x = sy.FloatTensor([1, 2, 3, 4, 5]) xs = { '__FloatTensor__': { 'type': 'syft.core.frameworks.torch.tensor.FloatTensor', 'torch_type': 'syft.FloatTensor', 'data': [1.0, 2.0, 3.0, 4.0, 5.0], 'child': { '___LocalTensor__': { 'owner': 0, 'id': x.id, 'torch_type': 'syft.FloatTensor' } } } } assert x.ser(private=False) == xs x2 = sy.FloatTensor.deser(xs, worker=me, acquire=True) # ensure values are the same as what was serialized assert x2.tolist() == x.tolist() # assert the objects are the same assert (x == x2).all()
def test_search_obj(self): hook = sy.TorchHook() hook.local_worker.is_client_worker = False x = sy.Var(sy.FloatTensor( [-2, -1, 0, 1, 2, 3])).set_id('#boston_housing #target #dataset') y = sy.Var(sy.FloatTensor( [-2, -1, 0, 1, 2, 3])).set_id('#boston_housing #input #dataset') hook.local_worker.is_client_worker = True assert len(hook.local_worker.search("#boston_housing")) == 2 assert len(hook.local_worker.search(["#boston_housing", "#target"])) == 1
def __init__(self,data=None,syft_obj=None): if(syft_obj is None): self.syft_obj = syft.FloatTensor(data) else: self.syft_obj = syft_obj self.current = 0
def test_localtensor2json2localtensor(self): xs = { '__FloatTensor__': { 'type': 'syft.core.frameworks.torch.tensor.FloatTensor', 'torch_type': 'syft.FloatTensor', 'data': [1.0, 2.0, 3.0, 4.0], 'child': { '___LocalTensor__': { 'owner': 0, 'id': 1000, 'torch_type': 'syft.FloatTensor' } } } } x = sy.FloatTensor([1, 2, 3, 4]) x.child.id = xs['__FloatTensor__']['child']['___LocalTensor__']['id'] # check that serialization is correct assert xs == x.ser(private=False) # reset ID for further testing xs['__FloatTensor__']['child']['___LocalTensor__']['id'] = 54321 x = sy.FloatTensor.deser(xs, worker=me, acquire=True) # correct id assert x.id == xs['__FloatTensor__']['child']['___LocalTensor__']['id'] # correct owner assert x.owner.id == xs['__FloatTensor__']['child'][ '___LocalTensor__']['owner'] # correct type assert type(x).__name__ == xs['__FloatTensor__']['type'].split(".")[-1] # correct size assert len(x) == 4 # correct data assert (x[0:4] == sy.FloatTensor([1, 2, 3, 4])).all() # object shouldn't be in registry yet assert x.id not in me._objects
def test_send_and_get_tensor(self): x = sy.FloatTensor([1, 2, 3, 4, 5]) xid = x.id x.send(bob, ptr_id=1234) # getting tensor back and putting result into x2 # to show that it should have updated x independently x2 = x.get() assert torch.equal(x2, sy.FloatTensor([1, 2, 3, 4, 5])) # make sure x changes id back to what it should assert x.id == xid # make sure x is now registered locally assert xid in me._objects # make sure x is not registered with bob assert xid not in bob._objects # make sure pointer is no longer registered locally assert 1234 not in me._objects
def test_floattensor2json2floattensor(self): xs = { '__FloatTensor__': { 'type': 'syft.core.frameworks.torch.tensor.FloatTensor', 'torch_type': 'syft.FloatTensor', 'data': [1.0, 2.0, 3.0, 4.0, 5.0], 'child': { '___LocalTensor__': { 'owner': 0, 'id': 234152, 'torch_type': 'syft.FloatTensor' } } } } x = sy.FloatTensor([1, 2, 3, 4, 5]) x.child.id = 234152 # test that serialization happens correctly assert x.ser(private=False) == xs # initialize tensor without registering it x2 = sy.FloatTensor.deser(xs, worker=me, acquire=True) # check id and owner are correct assert x2.id == 234152 assert x2.owner.id == 0 # make sure it works (can do operations) y = x2.add(x2) assert (x2 == sy.FloatTensor([1, 2, 3, 4, 5])).all() assert (y == sy.FloatTensor([2, 4, 6, 8, 10])).all() y = torch.add(x2, x2) assert (x2 == sy.FloatTensor([1, 2, 3, 4, 5])).all() assert (y == sy.FloatTensor([2, 4, 6, 8, 10])).all() y = x2 + x2 assert (x2 == sy.FloatTensor([1, 2, 3, 4, 5])).all() assert (y == sy.FloatTensor([2, 4, 6, 8, 10])).all() assert x2.id not in me._objects
def test_set_id(self): init_state = hook.local_worker.is_client_worker hook.local_worker.is_client_worker = False x = torch.FloatTensor([-2, -1, 0, 1, 2, 3]).set_id('bobs tensor') assert x.id == 'bobs tensor' assert x.child.id == 'bobs tensor' assert x.id in hook.local_worker._objects assert list(x.child.old_ids)[0] in hook.local_worker._objects assert list(x.child.old_ids)[0] != x.id x = sy.Var(sy.FloatTensor([-2, -1, 0, 1, 2, 3])).set_id('bobs variable') assert x.id == 'bobs variable' assert x.child.id == 'bobs variable' assert x.id in hook.local_worker._objects assert list(x.child.old_ids)[0] in hook.local_worker._objects assert list(x.child.old_ids)[0] != x.id
def test_tensor2registered_pointer2tensor(self): # Tensor: Local -> Pointer (unregistered) -> Local x = sy.FloatTensor([1, 2, 3, 4]) # make sure it got properly registered assert x.id in me._objects x_ptr = x.create_pointer(register=True) # ensure that it was registered assert x_ptr.id in me._objects ptr_id = x_ptr.id x2 = x_ptr.get() # ensure that it was deregistered assert ptr_id not in me._objects # make sure this returns a pointer that has been properly # wrapped with a FloatTensor since .get() was called on # a FloatTensor assert isinstance(x2, sy.FloatTensor) # make sure the tensor that came back has the correct id assert x2.id == x.id # since we're returning the pointer to an object that is hosted # locally, it should return that oject explicitly x += 2 assert (x2 == torch.FloatTensor([3, 4, 5, 6])).all() x_ptr += 2 # ensure that the pointer wrapper points to the same data location # as x assert (x2 == torch.FloatTensor([5, 6, 7, 8])).all()
def test_operation_with_variable_and_parameter(self): x = sy.Parameter(sy.FloatTensor([1])) y = sy.Variable(sy.FloatTensor([1])) z = x * y assert torch.equal(z, sy.Variable(sy.FloatTensor([1])))
def handle_call(cls, syft_command, owner): """ Execute a forwarded command on the native tensor with native operations. Receive a syft command and an owner, and converts it into command with native torch args. Excute native operations and converts it back into syft response using _LocalTensors. """ tensor_command, torch_type = torch_utils.prepare_child_command( syft_command, replace_tensorvar_with_child=True) torch_utils.assert_has_only_torch_tensorvars(tensor_command) attr = tensor_command['command'] args = tensor_command['args'] kwargs = tensor_command['kwargs'] has_self = tensor_command['has_self'] if has_self: self = tensor_command['self'] attr = torch._command_guard(attr, torch.tensorvar_methods) command = getattr(self, "native_" + attr) else: attr = torch._command_guard(attr, torch.torch_modules) elems = attr.split('.') elems[-1] = 'native_' + elems[-1] native_func_name = '.'.join(elems) command = eval(native_func_name) response = command(*args, **kwargs) # TODO : control registration process if response is None: return response if owner.id != owner.hook.local_worker.id: if isinstance(response, (int, float, bool)): response = sy.zeros(1) + response elif isinstance(response, (np.ndarray, )): response = sy.FloatTensor(response) else: if isinstance(response, (int, float, bool, np.ndarray)): return response # If the command is an in-place method, wrap self and return if has_self and utils.is_in_place_method(attr): # wrap the main element torch_utils.wrap_command_with(response, syft_command['self']) if torch_utils.is_variable(response): # Also wrap the data if it's a variable (don't use wrap_command_with: the chain is not well formed yet) syft_command['self'].child.data = response.data response.data.parent = syft_command['self'].child.data.parent # And wrap the grad if there is one if response.grad is not None: if response.grad.data.dim() > 0: syft_command['self'].child.grad = response.grad else: syft_command['self'].child.grad.native_set_() response.grad.parent = syft_command[ 'self'].child.grad.parent # Finally, fix the links .data and .grad if response.grad is None: torch_utils.link_var_chain_to_data_chain( syft_command['self'], response.data.child) else: torch_utils.link_var_chain_to_data_and_grad_chains( syft_command['self'], response.data.child, response.grad.child) return_response = syft_command['self'] # Else, the response if not self. Iterate over the response(s) and wrap with a syft tensor else: responses = response if isinstance(response, tuple) else (response, ) syft_responses = [] for resp in responses: if resp is None: # Don't wrap None syft_responses.append(resp) continue if isinstance(resp, (int, float, bool)): # if not final worker, convert into Float Tensor, which comes with a _LocalTensor if owner.id != owner.hook.local_worker.id: resp = sy.zeros(1) + resp else: # Else don't wrap it syft_responses.append(resp) continue syft_response = sy._LocalTensor(child=resp, parent=resp, owner=owner, torch_type='syft.' + type(resp).__name__) if torch_utils.is_variable(resp): if resp.grad is None: torch_utils.link_var_chain_to_data_chain( syft_response, resp.data.child) else: torch_utils.link_var_chain_to_data_and_grad_chains( syft_response, resp.data.child, resp.grad.child) syft_responses.append(syft_response) return_response = tuple(syft_responses) if len( syft_responses) > 1 else syft_responses[0] return return_response