Skip to content

Instantly share code, notes, and snippets.

@hereismari
Created August 27, 2019 01:22
Show Gist options
  • Save hereismari/32c893b47297a333c942470f42c9289d to your computer and use it in GitHub Desktop.
Save hereismari/32c893b47297a333c942470f42c9289d to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Host model on a grid node"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Import dependencies</h2>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"import torch\n",
"from torchvision import datasets, transforms\n",
"import torch.nn as nn\n",
"import grid as gr\n",
"import torch.nn.functional as F\n",
"import torch.optim as optim\n",
"import syft as sy\n",
"from torch.utils.data import TensorDataset, DataLoader\n",
"import time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Setup config</h2>\n",
"Define Config parameters, init hook, etc..."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"hook = sy.TorchHook(torch)\n",
"class Parser:\n",
" \"\"\"Parameters for training\"\"\"\n",
" def __init__(self):\n",
" self.epochs = 1\n",
" self.lr = 0.001\n",
" self.test_batch_size = 8\n",
" self.batch_size = 8\n",
" self.log_interval = 10\n",
" self.seed = 1\n",
" \n",
"args = Parser()\n",
"\n",
"torch.manual_seed(args.seed)\n",
"kwargs = {}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Load dataset</h2>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"test_loader = torch.utils.data.DataLoader(\n",
" datasets.MNIST(\n",
" \"./data\",\n",
" train=False,\n",
" download=True,\n",
" transform=transforms.Compose(\n",
" [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]\n",
" ),\n",
" ),\n",
" batch_size=args.test_batch_size,\n",
" shuffle=False,\n",
" drop_last=False,\n",
" )\n",
"(data, target) = test_loader.__iter__().next()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Define Model</h2>"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"class Net(nn.Module):\n",
" def __init__(self):\n",
" super(Net, self).__init__()\n",
" self.conv1 = nn.Conv2d(1, 20, 5, 1)\n",
" self.conv2 = nn.Conv2d(20, 50, 5, 1)\n",
" self.fc1 = nn.Linear(4 * 4 * 50, 500)\n",
" self.fc2 = nn.Linear(500, 10)\n",
"\n",
" @sy.messaging.method2plan\n",
" def forward(self, x):\n",
" x = F.relu(self.conv1(x))\n",
" x = F.max_pool2d(x, 2, 2)\n",
" x = F.relu(self.conv2(x))\n",
" x = F.max_pool2d(x, 2, 2)\n",
" x = x.view(-1, 4 * 4 * 50)\n",
" x = F.relu(self.fc1(x))\n",
" x = self.fc2(x)\n",
" return F.log_softmax(x, dim=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Connect with remote workers</h2>\n",
"<strong><me>Before this step, it is necessary to initialize the workers separately<me></strong>"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING: Grid nodes publish datasets online and are for EXPERIMENTAL use only.Deploy nodes at your own risk. Do not use OpenGrid with any data/models you wish to keep private.\n",
"\n"
]
}
],
"source": [
"bob = gr.WebsocketGridClient(hook, \"http://localhost:3000\", id=\"Bob\")\n",
"bob.connect()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Net(\n",
" (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n",
" (conv2): Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=800, out_features=500, bias=True)\n",
" (fc2): Linear(in_features=500, out_features=10, bias=True)\n",
"), tensor([[[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" ...,\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]]])]\n",
"[Net(\n",
" (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n",
" (conv2): Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=800, out_features=500, bias=True)\n",
" (fc2): Linear(in_features=500, out_features=10, bias=True)\n",
"), tensor([[[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" ...,\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]]])]\n",
"[Net(\n",
" (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n",
" (conv2): Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=800, out_features=500, bias=True)\n",
" (fc2): Linear(in_features=500, out_features=10, bias=True)\n",
"), tensor([[[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" ...,\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]],\n",
"\n",
"\n",
" [[[-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" ...,\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242],\n",
" [-0.4242, -0.4242, -0.4242, ..., -0.4242, -0.4242, -0.4242]]]])]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/marianne/PySyft/syft/frameworks/torch/hook/hook.py:676: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
" response = method(*args, **kwargs)\n",
"/home/marianne/PySyft/syft/frameworks/torch/hook/hook.py:676: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
" response = method(*args, **kwargs)\n",
"/home/marianne/anaconda3/envs/syft/lib/python3.6/site-packages/torch/tensor.py:427: RuntimeWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).\n",
" 'incorrect results).', category=RuntimeWarning)\n",
"/home/marianne/PySyft/syft/frameworks/torch/hook/hook.py:676: TracerWarning: Converting a tensor to a Python number might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
" response = method(*args, **kwargs)\n",
"/home/marianne/PySyft/syft/frameworks/torch/hook/hook.py:676: TracerWarning: Converting a tensor to a Python list might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
" response = method(*args, **kwargs)\n"
]
}
],
"source": [
"model = Net()\n",
"traced_model = torch.jit.trace(model, data)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[-2.4033, -2.2002, -2.1950, -2.3501, -2.2825, -2.3332, -2.3243, -2.2884,\n",
" -2.3752, -2.2948],\n",
" [-2.4595, -2.1257, -2.1943, -2.4151, -2.3403, -2.3070, -2.4238, -2.2729,\n",
" -2.3599, -2.1849],\n",
" [-2.3713, -2.1804, -2.2160, -2.3804, -2.3242, -2.3135, -2.3160, -2.3359,\n",
" -2.4036, -2.2115],\n",
" [-2.4065, -2.2372, -2.0636, -2.3684, -2.3596, -2.3088, -2.4875, -2.3626,\n",
" -2.3131, -2.1857],\n",
" [-2.3550, -2.1882, -2.2375, -2.2963, -2.3522, -2.3449, -2.4698, -2.2223,\n",
" -2.3328, -2.2573],\n",
" [-2.4224, -2.1799, -2.2057, -2.3915, -2.3079, -2.3048, -2.3100, -2.3404,\n",
" -2.4045, -2.1940],\n",
" [-2.4813, -2.1791, -2.2374, -2.2706, -2.3577, -2.3272, -2.3404, -2.2758,\n",
" -2.3741, -2.2170],\n",
" [-2.4745, -2.1344, -2.1988, -2.2492, -2.3500, -2.3217, -2.4395, -2.3424,\n",
" -2.3797, -2.1919]], grad_fn=<DifferentiableGraphBackward>)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"traced_model(data)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def make_plan(method, model):\n",
" \"\"\"Creates a plan from a function.\n",
"\n",
" For folks who would prefer to not use a decorator, they can use this function\n",
" to create a plan.\n",
" \"\"\"\n",
" plan = sy.messaging.Plan(\n",
" owner=sy.local_worker,\n",
" id=sy.ID_PROVIDER.pop(),\n",
" name=method.__name__,\n",
" is_method=True,\n",
" blueprint=method,\n",
" verbose=False,\n",
" )\n",
" \n",
" plan._self = model\n",
" \n",
" return plan"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"plan = make_plan(model.forward, model)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"model.forward.build(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([8, 1, 28, 28])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "forward() takes 2 positional arguments but 3 were given",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-33-e1dff39cdb04>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mplan\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/PySyft/syft/messaging/plan.py\u001b[0m in \u001b[0;36mbuild\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 217\u001b[0m \"\"\"\n\u001b[1;32m 218\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_prepare_for_running_local_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 219\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_build\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 220\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_after_running_local_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 221\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/PySyft/syft/messaging/plan.py\u001b[0m in \u001b[0;36m_build\u001b[0;34m(self, args)\u001b[0m\n\u001b[1;32m 238\u001b[0m \u001b[0mlocal_args\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 239\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 240\u001b[0;31m \u001b[0mres_ptr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mblueprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mlocal_args\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 241\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 242\u001b[0m \u001b[0mres_ptr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchild\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgarbage_collect_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/anaconda3/envs/syft/lib/python3.6/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 487\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 488\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 489\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 490\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 491\u001b[0m \u001b[0mhook_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: forward() takes 2 positional arguments but 3 were given"
]
}
],
"source": [
"plan.build(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Serve model"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['first_model', '2']"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bob.models"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"Error\": \"Model ID should be unique. There is already a model being hosted with this id.\"}'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bob.serve_model(model.forward, model_id=\"3\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['first_model', '2', '3']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bob.models"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'prediction': [[-2.322812080383301,\n",
" -2.303769111633301,\n",
" -2.293975830078125,\n",
" -2.3327364921569824,\n",
" -2.306673049926758,\n",
" -2.274453639984131,\n",
" -2.29142165184021,\n",
" -2.3145298957824707,\n",
" -2.3104403018951416,\n",
" -2.2766435146331787]]}"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bob.run_inference(model_id=\"first_model\", data=torch.zeros(1, 1, 28, 28))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "JSONDecodeError",
"evalue": "Expecting value: line 1 column 1 (char 0)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mJSONDecodeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-22-11d7ca84b5a2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbob\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_inference\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel_id\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"3\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m28\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m28\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/Grid/grid/client.py\u001b[0m in \u001b[0;36mrun_inference\u001b[0;34m(self, model_id, data, N)\u001b[0m\n\u001b[1;32m 87\u001b[0m \u001b[0;34m\"models/{}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m\"data\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mserialized_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"ISO-8859-1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 89\u001b[0;31m \u001b[0mN\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 90\u001b[0m )\n\u001b[1;32m 91\u001b[0m )\n",
"\u001b[0;32m~/anaconda3/envs/syft/lib/python3.6/json/__init__.py\u001b[0m in \u001b[0;36mloads\u001b[0;34m(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[1;32m 352\u001b[0m \u001b[0mparse_int\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mparse_float\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 353\u001b[0m parse_constant is None and object_pairs_hook is None and not kw):\n\u001b[0;32m--> 354\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_default_decoder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 355\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 356\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mJSONDecoder\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/anaconda3/envs/syft/lib/python3.6/json/decoder.py\u001b[0m in \u001b[0;36mdecode\u001b[0;34m(self, s, _w)\u001b[0m\n\u001b[1;32m 337\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 338\u001b[0m \"\"\"\n\u001b[0;32m--> 339\u001b[0;31m \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraw_decode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0m_w\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 340\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_w\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 341\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/anaconda3/envs/syft/lib/python3.6/json/decoder.py\u001b[0m in \u001b[0;36mraw_decode\u001b[0;34m(self, s, idx)\u001b[0m\n\u001b[1;32m 355\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscan_once\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 356\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 357\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mJSONDecodeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Expecting value\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 358\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)"
]
}
],
"source": [
"bob.run_inference(model_id=\"3\", data=torch.zeros(1, 1, 28, 28))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment