In [1]:
import sys
import json
import requests
import random

import torch as th
from torch import nn
import syft as sy

sy.create_sandbox(globals(), verbose=False)


/Users/atrask/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/Users/atrask/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/Users/atrask/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/Users/atrask/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
Setting up Sandbox...
Done!

In [2]:
from syft.codes import MSGTYPE
from syft import serde

class CommandCreator():
    
    def __init__(self, id):
        self.id = id
        
    def send_msg(self, msg_type, message, location):
        # Step 0: combine type and message
        message = (msg_type, message)

        # Step 1: serialize the message to simple python objects
        bin_message = serde.serialize(message)
        
        return bin_message
    
    def send_command(self, recipient, message):
        """
        Send a command through a message to a recipient worker
        :param recipient:
        :param message:
        :return:
        """

        response = self.send_msg(MSGTYPE.CMD, message, location=recipient)

        return response

In [3]:
import msgpack

In [6]:
x = th.tensor([1,2,3,4]).create_pointer()
x.owner = CommandCreator("steve")
y = x + x
msgpack.loads(y[1:])


Out[6]:
[2,
 [1,
  [2,
   [b'__add__',
    [10, [36593675293, 89052606580, b'me', None, [4]]],
    [2, [[10, [36593675293, 89052606580, b'me', None, [4]]]]]]]]]

In [4]:


In [4]:
sy.serde.deserialize(y)


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/workers/base.py in get_obj(self, obj_id)
    333         try:
--> 334             obj = self._objects[obj_id]
    335 

KeyError: 27412296840

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-4-395ad08db0a1> in <module>()
----> 1 sy.serde.deserialize(y)

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in deserialize(binary, worker, compressed, compress_scheme)
    155     # as msgpack's inability to serialize torch tensors or ... or
    156     # python slice objects
--> 157     return _detail(worker, simple_objects)
    158 
    159 

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in _detail(worker, obj)
    925 
    926     if type(obj) == list:
--> 927         return detailers[obj[0]](worker, obj[1])
    928     else:
    929         return obj

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in _detail_collection_tuple(worker, my_tuple)
    485     # Step 1: deserialize each part of the collection
    486     for part in my_tuple:
--> 487         pieces.append(_detail(worker, part))
    488 
    489     return tuple(pieces)

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in _detail(worker, obj)
    925 
    926     if type(obj) == list:
--> 927         return detailers[obj[0]](worker, obj[1])
    928     else:
    929         return obj

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in _detail_collection_tuple(worker, my_tuple)
    485     # Step 1: deserialize each part of the collection
    486     for part in my_tuple:
--> 487         pieces.append(_detail(worker, part))
    488 
    489     return tuple(pieces)

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in _detail(worker, obj)
    925 
    926     if type(obj) == list:
--> 927         return detailers[obj[0]](worker, obj[1])
    928     else:
    929         return obj

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/serde.py in detail(worker, tensor_tuple)
    752     if worker_id == worker.id:
    753 
--> 754         tensor = worker.get_obj(id_at_location)
    755 
    756         if point_to_attr is not None and tensor is not None:

/Users/atrask/anaconda/lib/python3.6/site-packages/syft-0.1.0a1-py3.6.egg/syft/workers/base.py in get_obj(self, obj_id)
    355                     "make sure you haven't already called .get() on this pointer!!!"
    356                 )
--> 357                 raise KeyError(msg)
    358             else:
    359                 raise e

KeyError: 'Tensor "27412296840" not found on worker "me"!!! You just tried to interact with an object ID:27412296840 on worker me which does not exist!!! Use .send() and .get() on all your tensors to make sure they\'reon the same machines. If you think this tensor does exist, check the ._objects dictionaryon the worker and see for yourself!!! The most common reason this error happens is because someone calls.get() on the object\'s pointer without realizing it (which deletes the remote object and sends it to the pointer). Check your code to make sure you haven\'t already called .get() on this pointer!!!'

In [ ]:


In [2]:
sy.local_worker.id = "bob"# + str(random.randint(0,1e10))

In [3]:
url = "http://127.0.0.1:5000"

In [4]:
# request model
res = requests.get(url+"/get_model")
m = sy.serde.deserialize(res.content)
m


Out[4]:
tensor([[0.],
        [0.]])

In [5]:
data = th.ones(4,2)
ptr = data.create_pointer()
ptr = sy.serde.serialize(ptr)

In [ ]:


In [6]:
# request model
res = requests.post(url+"/send_data",data=ptr,headers={'Content-Type': 'application/octet-stream'})
res


Out[6]:
<Response [200]>

In [7]:
m = sy.serde.deserialize(res.content)
m


Out[7]:
tensor([[0.],
        [0.]])

In [ ]:


In [29]:
data.mm(m)


Out[29]:
tensor([[0.],
        [0.],
        [0.],
        [0.]])

In [6]:


In [16]:
# Iniitalize A Toy Model
model = nn.Linear(2, 1, bias=False)
model.send(bob)


Out[16]:
Linear(in_features=2, out_features=1, bias=False)

In [11]:
bob.log_msgs = True

In [ ]:


In [17]:
bob.msg_history


Out[17]:
[b'0\x92\x02\x92\x02\x92\x01\x94\xcf\x00\x00\x00\t\x8c\x8d\xd8\x06\x96\xcf\x00\x00\x00\x05\xadw\xe6*\xda\x01_\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03.\x80\x02}q\x00(X\x10\x00\x00\x00protocol_versionq\x01M\xe9\x03X\r\x00\x00\x00little_endianq\x02\x88X\n\x00\x00\x00type_sizesq\x03}q\x04(X\x05\x00\x00\x00shortq\x05K\x02X\x03\x00\x00\x00intq\x06K\x04X\x04\x00\x00\x00longq\x07K\x04uu.\x80\x02ctorch._utils\n_rebuild_tensor_v2\nq\x00((X\x07\x00\x00\x00storageq\x01ctorch\nFloatStorage\nq\x02X\x0f\x00\x00\x00140431083661328q\x03X\x03\x00\x00\x00cpuq\x04K\x02Ntq\x05QK\x00K\x01K\x02\x86q\x06K\x02K\x01\x86q\x07\x89ccollections\nOrderedDict\nq\x08)Rq\ttq\nRq\x0b.\x80\x02]q\x00X\x0f\x00\x00\x00140431083661328q\x01a.\x02\x00\x00\x00\x00\x00\x00\x00\x83\xed\x1d?\xee&\xd5>\xc0\xc0\xc0\xc0\xc3\xc0',
 b"0\x92\x02\x92\x02\x92\x01\x94\xcf\x00\x00\x00\x10\xc5o\x0b\x1b\x96\xcf\x00\x00\x00\x0b\x91r.\x86\xda\x01_\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03.\x80\x02}q\x00(X\x10\x00\x00\x00protocol_versionq\x01M\xe9\x03X\r\x00\x00\x00little_endianq\x02\x88X\n\x00\x00\x00type_sizesq\x03}q\x04(X\x05\x00\x00\x00shortq\x05K\x02X\x03\x00\x00\x00intq\x06K\x04X\x04\x00\x00\x00longq\x07K\x04uu.\x80\x02ctorch._utils\n_rebuild_tensor_v2\nq\x00((X\x07\x00\x00\x00storageq\x01ctorch\nFloatStorage\nq\x02X\x0f\x00\x00\x00140431140170272q\x03X\x03\x00\x00\x00cpuq\x04K\x02Ntq\x05QK\x00K\x01K\x02\x86q\x06K\x02K\x01\x86q\x07\x89ccollections\nOrderedDict\nq\x08)Rq\ttq\nRq\x0b.\x80\x02]q\x00X\x0f\x00\x00\x00140431140170272q\x01a.\x02\x00\x00\x00\x00\x00\x00\x00\xb0\xcdR>\xc0_'<\xc0\xc0\xc0\xc0\xc3\xc0"]

In [ ]: