Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorials fix with set_all_parameters #113

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions test/test_crypten.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,8 @@ def forward(self, input):
return out

def set_all_parameters(self, value):
self.fc1.weight.data.fill_(value)
self.fc1.bias.data.fill_(value)
self.fc2.weight.data.fill_(value)
self.fc2.bias.data.fill_(value)
self.fc3.weight.data.fill_(value)
self.fc3.bias.data.fill_(value)
for p in self.parameters():
Copy link
Contributor Author

@gmuraru gmuraru May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Is there any reason why data.fill_ is used?

Copy link
Contributor

@knottb knottb May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I'm aware, torch.nn.init.constant_(param, value) is the same as param.data.fill_(value).

constant_ doc
fill_ doc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should do the same thing but changed it to constant_ because of an issue indicated in this PR

torch.nn.init.constant_(p, value)


class NestedTestModule(nn.Module):
Expand All @@ -302,9 +298,8 @@ def forward(self, input):
out = self.nested(out)

def set_all_parameters(self, value):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Is there any plan to have a method like this added directly in PyTorch for nn.Module?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe there is any plan to do this. This function isn't defined in CrypTen either - it is defined in a unit test as a helper function. In general, I think it is left to the user to initialize (or otherwise set) parameter values.

self.fc1.weight.data.fill_(value)
self.fc1.bias.data.fill_(value)
self.nested.set_all_parameters(value)
for p in self.parameters():
torch.nn.init.constant_(p, value)


# This code only runs when executing the file outside the test harness
Expand Down
2 changes: 1 addition & 1 deletion tutorials/Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
137 changes: 15 additions & 122 deletions tutorials/Tutorial_1_Basics_of_CrypTen_Tensors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -39,18 +39,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([1., 2., 3.])\n",
"tensor([4., 5., 6.])\n"
]
}
],
"outputs": [],
"source": [
"# Create torch tensor\n",
"x = torch.tensor([1.0, 2.0, 3.0])\n",
Expand Down Expand Up @@ -87,28 +78,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Public addition: tensor([3., 4., 5.])\n",
"Private addition: tensor([3., 4., 5.])\n",
"\n",
"Public subtraction: tensor([-1., 0., 1.])\n",
"Private subtraction: tensor([-1., 0., 1.])\n",
"\n",
"Public multiplication: tensor([2., 4., 6.])\n",
"Private multiplication: tensor([2., 4., 6.])\n",
"\n",
"Public division: tensor([0.5000, 1.0000, 1.5000])\n",
"Private division: tensor([0.5000, 1.0000, 1.5000])\n"
]
}
],
"outputs": [],
"source": [
"#Arithmetic operations between CrypTensors and plaintext tensors\n",
"x_enc = crypten.cryptensor([1.0, 2.0, 3.0])\n",
Expand Down Expand Up @@ -153,36 +125,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: tensor([1., 2., 3., 4., 5.])\n",
"y: tensor([5., 4., 3., 2., 1.])\n",
"\n",
"Public (x < y) : tensor([1., 1., 0., 0., 0.])\n",
"Private (x < y) : tensor([1., 1., 0., 0., 0.])\n",
"\n",
"Public (x <= y): tensor([1., 1., 1., 0., 0.])\n",
"Private (x <= y): tensor([1., 1., 1., 0., 0.])\n",
"\n",
"Public (x > y) : tensor([0., 0., 0., 1., 1.])\n",
"Private (x > y) : tensor([0., 0., 0., 1., 1.])\n",
"\n",
"Public (x >= y): tensor([0., 0., 1., 1., 1.])\n",
"Private (x >= y): tensor([0., 0., 1., 1., 1.])\n",
"\n",
"Public (x == y): tensor([0., 0., 1., 0., 0.])\n",
"Private (x == y): tensor([0., 0., 1., 0., 0.])\n",
"\n",
"Public (x != y): tensor([1., 1., 0., 1., 1.])\n",
"Private (x != y): tensor([1., 1., 0., 1., 1.])\n"
]
}
],
"outputs": [],
"source": [
"#Construct two example CrypTensors\n",
"x_enc = crypten.cryptensor([1.0, 2.0, 3.0, 4.0, 5.0])\n",
Expand Down Expand Up @@ -243,31 +188,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Public reciprocal: tensor([10.0000, 3.3333, 2.0000, 1.0000, 0.6667, 0.5000, 0.4000])\n",
"Private reciprocal: tensor([10.0009, 3.3335, 2.0000, 1.0000, 0.6667, 0.5000, 0.4000])\n",
"\n",
"Public logarithm: tensor([-2.3026, -1.2040, -0.6931, 0.0000, 0.4055, 0.6931, 0.9163])\n",
"Private logarithm: tensor([-2.3181, -1.2110, -0.6997, 0.0004, 0.4038, 0.6918, 0.9150])\n",
"\n",
"Public exponential: tensor([ 1.1052, 1.3499, 1.6487, 2.7183, 4.4817, 7.3891, 12.1825])\n",
"Private exponential: tensor([ 1.1021, 1.3440, 1.6468, 2.7121, 4.4574, 7.3280, 12.0188])\n",
"\n",
"Public square root: tensor([0.3162, 0.5477, 0.7071, 1.0000, 1.2247, 1.4142, 1.5811])\n",
"Private square root: tensor([0.3135, 0.5445, 0.7051, 1.0000, 1.2195, 1.4080, 1.5762])\n",
"\n",
"Public tanh: tensor([0.0997, 0.2913, 0.4621, 0.7616, 0.9051, 0.9640, 0.9866])\n",
"Private tanh: tensor([0.1019, 0.2892, 0.4613, 0.7642, 0.9081, 0.9689, 0.9922])\n"
]
}
],
"outputs": [],
"source": [
"torch.set_printoptions(sci_mode=False)\n",
"\n",
Expand Down Expand Up @@ -321,20 +244,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RuntimeError caught: \"Cannot evaluate MPCTensors to boolean values\"\n",
"\n",
"z: tensor(2.)\n",
"z: tensor(2.)\n"
]
}
],
"outputs": [],
"source": [
"x_enc = crypten.cryptensor(2.0)\n",
"y_enc = crypten.cryptensor(4.0)\n",
Expand Down Expand Up @@ -372,28 +284,9 @@
},
{
"cell_type": "code",
"execution_count": 0,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Indexing:\n",
" tensor([1., 2.])\n",
"\n",
"Concatenation:\n",
" tensor([1., 2., 3., 4., 5., 6.])\n",
"\n",
"Stacking:\n",
" tensor([[1., 2., 3.],\n",
" [4., 5., 6.]])\n",
"\n",
"Reshaping:\n",
" tensor([[1., 2., 3., 4., 5., 6.]])\n"
]
}
],
"outputs": [],
"source": [
"x_enc = crypten.cryptensor([1.0, 2.0, 3.0])\n",
"y_enc = crypten.cryptensor([4.0, 5.0, 6.0])\n",
Expand All @@ -411,7 +304,7 @@
"print('\\nStacking:\\n', z_enc.get_plain_text())\n",
"\n",
"# Reshaping\n",
"w_enc = z_enc.reshape(-1, 6)\n",
"w_enc = z_enc.reshape((-1, 6))\n",
"print('\\nReshaping:\\n', w_enc.get_plain_text())\n"
]
},
Expand Down Expand Up @@ -446,7 +339,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
99 changes: 12 additions & 87 deletions tutorials/Tutorial_2_Inside_CrypTensors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -49,18 +49,9 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_enc internal type: ptype.arithmetic\n",
"y_enc internal type: ptype.binary\n"
]
}
],
"outputs": [],
"source": [
"#Constructing CrypTensors with ptype attribute\n",
"\n",
Expand Down Expand Up @@ -90,28 +81,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Rank 0:\n",
" MPCTensor(\n",
"\t_tensor=tensor([1559458991263271971, 464556655590487085, 3264140948701540070])\n",
"\tplain_text=HIDDEN\n",
"\tptype=ptype.arithmetic\n",
")Rank 1:\n",
" MPCTensor(\n",
"\t_tensor=tensor([-1559458991263206435, -464556655590356013, -3264140948701343462])\n",
"\tplain_text=HIDDEN\n",
"\tptype=ptype.arithmetic\n",
")\n",
"\n"
]
}
],
"outputs": [],
"source": [
"import crypten.mpc as mpc\n",
"import crypten.communicator as comm \n",
Expand Down Expand Up @@ -142,28 +114,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Rank 0:\n",
" MPCTensor(\n",
"\t_tensor=tensor([ 125494978816570963, 7944957613546045877])\n",
"\tplain_text=HIDDEN\n",
"\tptype=ptype.binary\n",
")Rank 1:\n",
" MPCTensor(\n",
"\t_tensor=tensor([ 125494978816570961, 7944957613546045878])\n",
"\tplain_text=HIDDEN\n",
"\tptype=ptype.binary\n",
")\n",
"\n"
]
}
],
"outputs": [],
"source": [
"@mpc.run_multiprocess(world_size=2)\n",
"def examine_binary_shares():\n",
Expand All @@ -187,24 +140,9 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"to(crypten.binary):\n",
" ptype: ptype.binary\n",
" plaintext: tensor([1., 2., 3.])\n",
"\n",
"to(crypten.arithmetic):\n",
" ptype: ptype.arithmetic\n",
" plaintext: tensor([1., 2., 3.])\n",
"\n"
]
}
],
"outputs": [],
"source": [
"from crypten.mpc import MPCTensor\n",
"\n",
Expand Down Expand Up @@ -253,22 +191,9 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Rank 0: 0\n",
"Rank 2: 2\n",
"Rank 1: 1\n",
"Source 0: 0.0\n",
"Source 1: 1.0\n",
"Source 2: 2.0\n"
]
}
],
"outputs": [],
"source": [
"@mpc.run_multiprocess(world_size=3)\n",
"def examine_sources():\n",
Expand Down Expand Up @@ -306,7 +231,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
Loading