This tutorial is a 2 notebook tutorial. The partner notebook is the notebook entitled WebsocketServerWorker.ipynb and is in the same folder as this notebook. You should execute this notebook AFTER you have executed the other one.
In that tutorial, we'll demonstrate how to launch a WebsocketWorker server which will listen for PyTorch commands over a socket connection. In this tutorial, the two workers are connected via a socket connection on the localhost network.
If you'd prefer to download this notebook and run it locally, you can do so via the Download .ipynb in the File dropdown field in this Google colab environment.
Setup instructions: https://research.google.com/colaboratory/local-runtimes.html
In [ ]:
# If not using local runtimes, uncomment out the following snippets where necessary.
#! rm -rf /content/PySyft
#! git clone https://github.com/OpenMined/PySyft.git
# http://pytorch.org/
#from os import path
#from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
#platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
#cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
#accelerator = cuda_output[0] if path.exists('/opt/bin/nvidia-smi') else 'cpu'
#!pip3 install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-linux_x86_64.whl
#!pip3 install https://download.pytorch.org/whl/cu100/torchvision-0.3.0-cp36-cp36m-linux_x86_64.whl
import torch
#!cd PySyft; pip3 install -r requirements.txt; pip3 install -r requirements_dev.txt; python3 setup.py install
import os
import sys
#module_path = '/content/PySyft' # You want './PySyft' to be on your sys.path
#if module_path not in sys.path:
# sys.path.append(module_path)
In [0]:
# Check sys.path
#sys.path
In [ ]:
import syft
from syft.workers.websocket_server import WebsocketServerWorker
hook = syft.TorchHook(torch)
local_worker = WebsocketServerWorker(
host='localhost',
hook=hook,
id=0,
port=8182,
log_msgs=True,
verbose=True)
In order to interact with a foreign worker over a socket connection, we need to create a pointer to it containing information on how to contact it. We set the is_pointer=True to signify that this Python object is not in fact a worker in and of itself but that it is merely a pointer to one over the network. We then inform our local worker about this pointer.
In [0]:
hook = syft.TorchHook(torch, local_worker=local_worker)
from syft.workers.websocket_client import WebsocketClientWorker
remote_client = WebsocketClientWorker(
host = 'localhost',
hook=hook,
id=2,
port=8181)
hook.local_worker.add_worker(remote_client)
In [0]:
x = syft.FixedPrecisionTensor([1,3,5,7,9]).share(remote_client)
In [0]:
x2 = syft.FixedPrecisionTensor([2,4,6,8,10]).share(remote_client)
In [0]:
x
Out[0]:
In [0]:
x2
Out[0]:
In [0]:
y = x + x2 + x
In [0]:
y
Out[0]:
In [0]:
y.get()
Out[0]:
In [0]:
y
Out[0]:
In [0]: