def test_recv_msg(): """Tests the recv_msg command with 2 tests The first test uses recv_msg to send an object to alice. The second test uses recv_msg to request the object previously sent to alice.""" # TEST 1: send tensor to alice # create a worker to send data to worker_id = int(10e10 * random.random()) alice = VirtualWorker(sy.torch.hook, id=f"alice{worker_id}") # create object to send obj = torch.Tensor([100, 100]) # create/serialize message msg = (MSGTYPE.OBJ, obj) bin_msg = serde.serialize(msg) # have alice receive message alice.recv_msg(bin_msg) # ensure that object is now in alice's registry assert obj.id in alice._objects # Test 2: get tensor back from alice # Create message: Get tensor from alice msg = (MSGTYPE.OBJ_REQ, obj.id) # serialize message bin_msg = serde.serialize(msg) # call receive message on alice resp = alice.recv_msg(bin_msg) obj_2 = serde.deserialize(resp) # assert that response is correct type assert type(resp) == bytes # ensure that the object we receive is correct assert obj_2.id == obj.id
def test_plan_serde(hook): hook.local_worker.is_client_worker = False @sy.func2plan def my_plan(data): x = data * 2 y = (x - 2) * 10 return x + y # TODO: remove this line when issue #2062 is fixed # Force to build plan my_plan(th.tensor([1, 2, 3])) serialized_plan = serialize(my_plan) deserialized_plan = deserialize(serialized_plan) x = th.tensor([-1, 2, 3]) assert (deserialized_plan(x) == th.tensor([-42, 24, 46])).all()
def recv_msg(self, bin_message): """Implements the logic to receive messages. The binary message is deserialized and routed to the appropriate function. And, the response serialized the returned back. Every message uses this method. Args: bin_message: A binary serialized message. Returns: A binary message response. """ # Step -1: save message if log_msgs == True if self.log_msgs: self.msg_history.append(bin_message) # Step 0: deserialize message (msg_type, contents) = serde.deserialize(bin_message, worker=self) if self.verbose: print(f"worker {self} received {msg_type} {contents}") # Step 1: route message to appropriate function response = self._message_router[msg_type](contents) # # Step 2: If response in none, set default # TODO: not sure if someone needed this - if this comment # is still here after Feb 15, 2018, please delete these # two lines of (commented out) code. # if response is None: # response = None # Step 3: Serialize the message to simple python objects bin_response = serde.serialize(response) return bin_response
def test_torch_Tensor(compress): t = Tensor(numpy.random.random((100, 100))) t_serialized = serialize(t, compress=compress) t_serialized_deserialized = deserialize(t_serialized, compressed=compress) assert (t == t_serialized_deserialized).all()