Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_execute_command_self(hook):
sy.VirtualWorker.mocked_function = MethodType(
mock.Mock(return_value="bob_mocked_function"), sy.VirtualWorker
)
bob = sy.VirtualWorker(hook, "bob")
x = th.tensor([1, 2, 3]).send(bob)
message = bob.create_message_execute_command(
command_name="mocked_function", command_owner="self"
)
serialized_message = sy.serde.serialize(message)
response = bob._recv_msg(serialized_message)
response = sy.serde.deserialize(response)
assert response == "bob_mocked_function"
bob.mocked_function.assert_called()
def _recv_msg(self, message: bin) -> bin:
"""Forwards a message to the WebsocketIOClientWorker"""
self.socketio.emit("message", message) # Block and wait for the response
# This Event will wait until its `set()` is invoked.
# This will be done when a message from the client is received
# Ideally this should be done with semaphores or events
self.wait_for_client_event = True
while self.wait_for_client_event:
time.sleep(0.1)
if self.response_from_client == "ACK":
return sy.serde.serialize(b"")
return self.response_from_client
Virtual worker filled by stored objects.
"""
worker_mdl = WorkerMDL.query.filter_by(id=worker.id).first()
if worker_mdl:
global last_snapshot_keys
# Recover objects
objs = db.session.query(WorkerObject).filter_by(worker_id=worker.id).all()
obj_dict = {}
for obj in objs:
obj_dict[obj.id] = obj.object
# Recover models
models = TorchModel.query.all()
for result in models:
model = sy.serde.deserialize(result.model)
obj_dict[result.id] = model
worker._objects = obj_dict
last_snapshot_keys = set(obj_dict.keys())
else:
worker_mdl = WorkerMDL(id=worker.id)
db.session.add(worker_mdl)
db.session.commit()
return worker
def simplify(worker: AbstractWorker, state: "State") -> tuple:
"""
Simplify the plan's state when sending a plan
"""
return (
sy.serde.msgpack.serde._simplify(worker, state.state_ids),
sy.serde.msgpack.serde._simplify(worker, state.tensors()),
)
"""
This function reconstructs a LogTensor given it's attributes in form of a tuple.
Args:
worker: the worker doing the deserialization
tensor_tuple: a tuple holding the attributes of the LogTensor
Returns:
PaillierTensor: a LogTensor
Examples:
logtensor = detail(data)
"""
obj_id, chain = tensor_tuple
tensor = PaillierTensor(owner=worker, id=obj_id)
if chain is not None:
chain = sy.serde.msgpack.serde._detail(worker, chain)
tensor.child = chain
return tensor
"""
This function reconstructs a LogTensor given it's attributes in form of a tuple.
Args:
worker: the worker doing the deserialization
tensor_tuple: a tuple holding the attributes of the LogTensor
Returns:
LoggingTensor: a LogTensor
Examples:
logtensor = detail(data)
"""
obj_id, chain = tensor_tuple
tensor = LoggingTensor(owner=worker, id=obj_id)
if chain is not None:
chain = sy.serde._detail(worker, chain)
tensor.child = chain
return tensor
def serialize(self): # check serde.py to see how to provide compression schemes
"""Serializes the tensor on which it's called.
This is the high level convenience function for serializing torch
tensors. It includes three steps, Simplify, Serialize, and Compress as
described in serde.py.
By default serde is compressing using LZ4
Returns:
The serialized form of the tensor.
For example:
x = torch.Tensor([1,2,3,4,5])
x.serialize() # returns a serialized object
"""
return sy.serde.serialize(self)
worker: the worker doing the deserialization
tensor_tuple: a tuple holding the attributes of the CRTPrecisionTensor
Returns:
CRTPrecisionTensor: a CRTPrecisionTensor
"""
tensor_id, tensor_base, tensor_precision_fractional, chain = tensor_tuple
tensor = syft.CRTPrecisionTensor(
base=tensor_base,
precision_fractional=tensor_precision_fractional,
owner=worker,
id=tensor_id,
)
if chain is not None:
chain = syft.serde._detail(worker, chain)
tensor.child = chain
return tensor