Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import time
import torch
import syft as sy
from grid.workers import WebsocketIOServerWorker
def _payload(location):
x = torch.tensor([10, 20, 30, 40, 50.0])
x.send(location)
hook = sy.TorchHook(torch)
server_worker = WebsocketIOServerWorker(
hook, "localhost", 5000, log_msgs=True, payload=_payload
)
def test_client_id():
android = server_worker.socketio.test_client(server_worker.app)
android.emit("client_id", "android")
assert len(server_worker.clients) == 1
android.disconnect()
server_worker.terminate()
def test_payload_execution():
android = server_worker.socketio.test_client(server_worker.app)
android.emit("client_id", "android")
def test___init__():
hook = sy.TorchHook(torch)
tensor = torch.tensor([1, 2, 3, 4])
worker_id = sy.ID_PROVIDER.pop()
alice_id = f"alice{worker_id}"
alice = VirtualWorker(hook, id=alice_id)
worker_id = sy.ID_PROVIDER.pop()
bob = VirtualWorker(hook, id=f"bob{worker_id}")
worker_id = sy.ID_PROVIDER.pop()
charlie = VirtualWorker(hook, id=f"charlie{worker_id}")
worker_id = sy.ID_PROVIDER.pop()
dawson = VirtualWorker(hook, id=f"dawson{worker_id}", data=[tensor])
# Ensure adding data on signup functionality works as expected
assert tensor.owner == dawson
parser.add_argument(
"--testing",
action="store_true",
help="if set, websocket server worker will load the test dataset instead of the training dataset",
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="if set, websocket server worker will be started in verbose mode",
)
args = parser.parse_args()
# Hook and start server
hook = sy.TorchHook(torch)
server = start_websocket_server_worker(
id=args.id,
host=args.host,
port=args.port,
hook=hook,
verbose=args.verbose,
keep_labels=KEEP_LABELS_DICT[args.id] if args.id in KEEP_LABELS_DICT else list(range(10)),
training=not args.testing,
)
def init(self, input_array, id, owner):
# add the new attribute to the created instance
if id is None:
id = random.randint(0, 1e10)
self.id = id
if owner is None:
# cache the local_worker object locally which we will
# use for all outgoing communications
if not hasattr(sy, "local_worker"):
sy.TorchHook()
owner = sy.local_worker
self.owner = owner
self.owner.register(self)
self.initialized = True
# Finally, we must return the newly created object:
return self
import torch as th
import syft as sy
import grid as gr
import requests
import time
addr = "http://localhost:5000"
hook = sy.TorchHook(th)
worker = gr.GridClient(addr=addr)
def main():
x = th.tensor([1, 2, 3, 4]).send(worker)
y = x + x
print("Y = ", y.get())
if __name__ == "__main__":
main()
# del x
import binascii
import json
import os
from flask import Flask, session, request
import redis
import syft as sy
import torch as th
hook = sy.TorchHook(th)
app = Flask(__name__)
app.secret_key = b'keepmesecret'
try:
db = redis.from_url(os.environ['REDISCLOUD_URL'])
except:
db = redis.from_url('redis://localhost:6379')
def _maybe_create_worker(worker_name: str = 'worker', virtual_worker_id: str = 'grid'):
worker = db.get(worker_name)
if worker is None:
worker = sy.VirtualWorker(hook, virtual_worker_id, auto_add=False)
print("\t \nCREATING NEW WORKER!!")
else:
parser.add_argument(
"--id", type=str, help="name (id) of the websocket server worker, e.g. --id alice"
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="if set, websocket server worker will be started in verbose mode",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
hook = sy.TorchHook(torch)
args = get_args()
kwargs = {
"id": args.id,
"host": args.host,
"port": args.port,
"hook": hook,
"verbose": args.verbose,
}
server = WebsocketServerWorker(**kwargs)
server.start()
import torch as th
import torch.nn.functional as F
from torch import nn
import syft as sy
from syft import workers
from syft.frameworks.torch import pointers
use_cuda = th.cuda.is_available()
th.manual_seed(1)
device = th.device("cuda" if use_cuda else "cpu")
hook = sy.TorchHook(th)
me = hook.local_worker
kwargs_websocket = {"host": "localhost", "hook": hook, "verbose": False}
alice = workers.WebsocketClientWorker(id="alice", port=8777, **kwargs_websocket)
# Loss function
@th.jit.script
def loss_fn(real, pred):
return ((real - pred) ** 2).mean()
# Model
class Net(th.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(2, 3)
def main():
args = define_and_get_arguments()
hook = sy.TorchHook(torch)
if args.use_virtual:
alice = VirtualWorker(id="alice", hook=hook, verbose=args.verbose)
bob = VirtualWorker(id="bob", hook=hook, verbose=args.verbose)
charlie = VirtualWorker(id="charlie", hook=hook, verbose=args.verbose)
else:
kwargs_websocket = {"host": "localhost", "hook": hook, "verbose": args.verbose}
alice = WebsocketClientWorker(id="alice", port=8777, **kwargs_websocket)
bob = WebsocketClientWorker(id="bob", port=8778, **kwargs_websocket)
charlie = WebsocketClientWorker(id="charlie", port=8779, **kwargs_websocket)
workers = [alice, bob, charlie]
use_cuda = args.cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
from multiprocessing import Process
import syft as sy
from syft.workers import WebsocketServerWorker
import torch
import argparse
hook = sy.TorchHook(torch)
def start_proc(participant, kwargs): # pragma: no cover
""" helper function for spinning up a websocket participant """
def target():
server = participant(**kwargs)
server.start()
p = Process(target=target)
p.start()
return p
parser = argparse.ArgumentParser(description="Run websocket server worker.")
parser.add_argument(