Skip to content

Commit

Permalink
chore: demonstrate larger calibration datasets in nb (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto authored Dec 19, 2023
1 parent cdb6473 commit 5c52909
Show file tree
Hide file tree
Showing 26 changed files with 499 additions and 144 deletions.
23 changes: 21 additions & 2 deletions examples/notebooks/decision_tree.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"pk_path = os.path.join('test.pk')\n",
"vk_path = os.path.join('test.vk')\n",
"settings_path = os.path.join('settings.json')\n",
"",
"\n",
"witness_path = os.path.join('witness.json')\n",
"data_path = os.path.join('input.json')"
]
Expand Down Expand Up @@ -154,6 +154,25 @@
"assert res == True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# generate a bunch of dummy calibration data\n",
"cal_data = {\n",
" \"input_data\": [(torch.rand(20, *shape)).flatten().tolist()],\n",
"}\n",
"\n",
"cal_path = os.path.join('val_data.json')\n",
"# save as json file\n",
"with open(cal_path, \"w\") as f:\n",
" json.dump(cal_data, f)\n",
"\n",
"res = ezkl.calibrate_settings(cal_path, model_path, settings_path, \"resources\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -284,4 +303,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
24 changes: 17 additions & 7 deletions examples/notebooks/ezkl_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
"pk_path = os.path.join('test.pk')\n",
"vk_path = os.path.join('test.vk')\n",
"settings_path = os.path.join('settings.json')\n",
"",
"\n",
"witness_path = os.path.join('witness.json')\n",
"data_path = os.path.join('input.json')\n",
"cal_data_path = os.path.join('cal_data.json')"
Expand Down Expand Up @@ -402,11 +402,7 @@
"json.dump(data, open(data_path, 'w'))\n",
"\n",
"\n",
"# use the test set to calibrate the circuit\n",
"cal_data = dict(input_data = test_X.flatten().tolist())\n",
"\n",
"# Serialize calibration data into file:\n",
"json.dump(data, open(cal_data_path, 'w'))\n"
"\n"
]
},
{
Expand All @@ -430,6 +426,20 @@
"# TODO: Dictionary outputs\n",
"res = ezkl.gen_settings(model_path, settings_path)\n",
"assert res == True\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# use the test set to calibrate the circuit\n",
"cal_data = dict(input_data = test_X.flatten().tolist())\n",
"\n",
"# Serialize calibration data into file:\n",
"json.dump(data, open(cal_data_path, 'w'))\n",
"\n",
"res = ezkl.calibrate_settings(cal_data_path, model_path, settings_path, \"resources\") # Optimize for resources"
]
Expand Down Expand Up @@ -751,4 +761,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
9 changes: 9 additions & 0 deletions examples/notebooks/gcn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@
"# TODO: Dictionary outputs\n",
"res = ezkl.gen_settings(model_path, settings_path, py_run_args=run_args)\n",
"assert res == True\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"res = ezkl.calibrate_settings(data_path, model_path, settings_path, \"resources\")\n",
"assert res == True"
Expand Down
29 changes: 24 additions & 5 deletions examples/notebooks/generalized_inverse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"pk_path = os.path.join('test.pk')\n",
"vk_path = os.path.join('test.vk')\n",
"settings_path = os.path.join('settings.json')\n",
"",
"\n",
"witness_path = os.path.join('witness.json')\n",
"data_path = os.path.join('input.json')"
]
Expand All @@ -111,7 +111,9 @@
"outputs": [],
"source": [
"# After training, export to onnx (network.onnx) and create a data file (input.json)\n",
"A = 0.1*torch.rand(1,*[10, 10], requires_grad=True)\n",
"shape = [10, 10]\n",
"\n",
"A = 0.1*torch.rand(1,*shape, requires_grad=True)\n",
"B = A.inverse()\n",
"\n",
"# Flips the neural net into inference mode\n",
Expand Down Expand Up @@ -174,10 +176,27 @@
"\n",
"res = ezkl.gen_settings(model_path, settings_path, py_run_args=gip_run_args)\n",
"\n",
"assert res == True\n",
"assert res == True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cal_path = os.path.join(\"calibration.json\")\n",
"\n",
"data_array = (0.1*torch.rand(20,*shape).detach().numpy()).reshape([-1]).tolist()\n",
"\n",
"data = dict(input_data = [data_array])\n",
"\n",
"# Serialize data into file:\n",
"json.dump(data, open(cal_path, 'w'))\n",
"\n",
"\n",
"res = ezkl.calibrate_settings(data_path, model_path, settings_path, \"resources\")\n",
"assert res == True"
"assert res == True\n"
]
},
{
Expand Down Expand Up @@ -321,4 +340,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
63 changes: 31 additions & 32 deletions examples/notebooks/gradient_boosted_trees.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "95613ee9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"num diff: [0]\n"
]
}
],
"outputs": [],
"source": [
"# check if notebook is in colab\n",
"try:\n",
Expand Down Expand Up @@ -102,7 +94,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "b37637c4",
"metadata": {},
"outputs": [],
Expand All @@ -112,28 +104,17 @@
"pk_path = os.path.join('test.pk')\n",
"vk_path = os.path.join('test.vk')\n",
"settings_path = os.path.join('settings.json')\n",
"",
"\n",
"witness_path = os.path.join('witness.json')\n",
"data_path = os.path.join('input.json')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "82db373a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================ Diagnostic Run torch.onnx.export version 2.0.1 ================\n",
"verbose: False, log level: Level.ERROR\n",
"======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================\n",
"\n"
]
}
],
"outputs": [],
"source": [
"# !!!!!!!!!!!!!!!!! This cell will flash a warning about onnx runtime compat but it is fine !!!!!!!!!!!!!!!!!!!!!\n",
"\n",
Expand Down Expand Up @@ -168,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "d5e374a2",
"metadata": {},
"outputs": [],
Expand All @@ -179,14 +160,32 @@
"# TODO: Dictionary outputs\n",
"res = ezkl.gen_settings(model_path, settings_path, py_run_args=run_args)\n",
"assert res == True\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cal_path = os.path.join(\"calibration.json\")\n",
"\n",
"data_array = (torch.rand(20,*shape).detach().numpy()).reshape([-1]).tolist()\n",
"\n",
"data = dict(input_data = [data_array])\n",
"\n",
"# Serialize data into file:\n",
"json.dump(data, open(cal_path, 'w'))\n",
"\n",
"\n",
"res = ezkl.calibrate_settings(data_path, model_path, settings_path, \"resources\")\n",
"assert res == True"
"assert res == True\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "3aa4f090",
"metadata": {},
"outputs": [],
Expand All @@ -197,7 +196,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "8b74dcee",
"metadata": {},
"outputs": [],
Expand All @@ -208,7 +207,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "18c8b7c7",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -249,7 +248,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "c384cbc8",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -314,4 +313,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
4 changes: 3 additions & 1 deletion examples/notebooks/hashed_vis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
"metadata": {},
"outputs": [],
"source": [
"x = torch.rand(1,*[3, 8, 8], requires_grad=True)\n",
"\n",
"shape = [3, 8, 8]\n",
"x = torch.rand(1,*shape, requires_grad=True)\n",
"\n",
"# Flips the neural net into inference mode\n",
"circuit.eval()\n",
Expand Down
24 changes: 21 additions & 3 deletions examples/notebooks/keras_simple_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@
"import tf2onnx\n",
"import tensorflow as tf\n",
"\n",
"\n",
"shape = [1, 28, 28]\n",
"# After training, export to onnx (network.onnx) and create a data file (input.json)\n",
"x = 0.1*np.random.rand(1,*[1, 28, 28])\n",
"x = 0.1*np.random.rand(1,*shape)\n",
"\n",
"spec = tf.TensorSpec([1, 28, 28, 1], tf.float32, name='input_0')\n",
"\n",
Expand All @@ -130,11 +132,27 @@
"\n",
"# TODO: Dictionary outputs\n",
"res = ezkl.gen_settings(model_path, settings_path)\n",
"assert res == True\n",
"assert res == True\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cal_path = os.path.join(\"calibration.json\")\n",
"\n",
"data_array = (0.1*np.random.rand(20,*shape)).reshape([-1]).tolist()\n",
"\n",
"data = dict(input_data = [data_array])\n",
"\n",
"# Serialize data into file:\n",
"json.dump(data, open(cal_path, 'w'))\n",
"\n",
"\n",
"res = ezkl.calibrate_settings(data_path, model_path, settings_path, \"resources\")\n",
"assert res == True"
"assert res == True\n"
]
},
{
Expand Down
Loading

0 comments on commit 5c52909

Please sign in to comment.