From 519da9d85f5bd3eebb250df76dd2942114e5e977 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 20 Nov 2024 20:53:49 +0000 Subject: [PATCH] minor changes --- basic.ipynb | 62 ++++++++++++++++++++++++++++++++++++++------------- venv_setup.sh | 2 +- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/basic.ipynb b/basic.ipynb index 5dd1600..e499296 100644 --- a/basic.ipynb +++ b/basic.ipynb @@ -2,12 +2,13 @@ "cells": [ { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "encoding_width = 2**13 # restricted by ckks size\n", - "database_size = 100" + "database_size = 100\n", + "import time" ] }, { @@ -19,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -29,7 +30,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -64,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -78,14 +79,15 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[Client] sending HE_client= and cx=\n" + "[Client] Sending HE_client= and cx=\n", + "[Client] Sent 106.78689 MB\n" ] } ], @@ -99,7 +101,8 @@ "#s_cx = [cx[j].to_bytes() for j in range(len(cx))]\n", "s_cx = cx.to_bytes()\n", "\n", - "print(f\"[Client] sending HE_client={HE_client} and cx={cx}\")" + "print(f\"[Client] Sending HE_client={HE_client} and cx={cx}\")\n", + "print(f\"[Client] Sent {(len(s_context) + len(s_public_key) + len(s_relin_key) + len(s_rotate_key) + len(s_cx)) / (10**6)} MB\")" ] }, { @@ -111,9 +114,21 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'nb' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;129m@nb\u001b[39m\u001b[38;5;241m.\u001b[39mjit\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mhyperbolic_distance_parts\u001b[39m(u, v): \u001b[38;5;66;03m# returns only the numerator and denominator of the hyperbolic distance formula\u001b[39;00m\n\u001b[1;32m 3\u001b[0m diff \u001b[38;5;241m=\u001b[39m u \u001b[38;5;241m-\u001b[39m v\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m#du = -(1 - u @ u) # for some reason we need to negate this\u001b[39;00m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m#dv = -(1 - v @ v) # for some reason we need to negate this\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m#return diff @ diff, du * dv # returns the numerator and denominator\u001b[39;00m\n", + "\u001b[0;31mNameError\u001b[0m: name 'nb' is not defined" + ] + } + ], "source": [ "def hyperbolic_distance_parts(u, v): # returns only the numerator and denominator of the hyperbolic distance formula\n", " diff = u - v\n", @@ -125,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -141,15 +156,27 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[Server] received HE_server= and cx=\n", - "[Server] Distances computed! Responding: res=[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]\n" + "[Server] received HE_server= and cx=\n" + ] + }, + { + "ename": "TypingError", + "evalue": "Failed in nopython mode pipeline (step: nopython frontend)\nnon-precise type pyobject\nDuring: typing of argument at /tmp/ipykernel_5991/1812651176.py (1)\n\nFile \"../../tmp/ipykernel_5991/1812651176.py\", line 1:\n \n\nThis error may have been caused by the following argument(s):\n- argument 0: Cannot determine Numba type of \n- argument 1: Cannot determine Numba type of \n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypingError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[16], line 18\u001b[0m\n\u001b[1;32m 16\u001b[0m cd \u001b[38;5;241m=\u001b[39m HE_server\u001b[38;5;241m.\u001b[39mencrypt(D[i])\n\u001b[1;32m 17\u001b[0m \u001b[38;5;66;03m# Compute distance bewteen recieved query and D[i]\u001b[39;00m\n\u001b[0;32m---> 18\u001b[0m res\u001b[38;5;241m.\u001b[39mappend(\u001b[43mhyperbolic_distance_parts\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcd\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 19\u001b[0m end \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[Server] Compute took \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mend\u001b[38;5;250m \u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;250m \u001b[39mstart\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124ms with bandwidth \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(D)\u001b[38;5;250m \u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;250m \u001b[39m(end\u001b[38;5;241m-\u001b[39mstart)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m documents/s\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m~/.venvs/cs239/lib/python3.9/site-packages/numba/core/dispatcher.py:423\u001b[0m, in \u001b[0;36m_DispatcherBase._compile_for_args\u001b[0;34m(self, *args, **kws)\u001b[0m\n\u001b[1;32m 419\u001b[0m msg \u001b[38;5;241m=\u001b[39m (\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(e)\u001b[38;5;241m.\u001b[39mrstrip()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mThis error may have been caused \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 420\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mby the following argument(s):\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00margs_str\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 421\u001b[0m e\u001b[38;5;241m.\u001b[39mpatch_message(msg)\n\u001b[0;32m--> 423\u001b[0m \u001b[43merror_rewrite\u001b[49m\u001b[43m(\u001b[49m\u001b[43me\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtyping\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 424\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m errors\u001b[38;5;241m.\u001b[39mUnsupportedError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 425\u001b[0m \u001b[38;5;66;03m# Something unsupported is present in the user code, add help info\u001b[39;00m\n\u001b[1;32m 426\u001b[0m error_rewrite(e, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124munsupported_error\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "File \u001b[0;32m~/.venvs/cs239/lib/python3.9/site-packages/numba/core/dispatcher.py:364\u001b[0m, in \u001b[0;36m_DispatcherBase._compile_for_args..error_rewrite\u001b[0;34m(e, issue_type)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(\u001b[38;5;28;01mNone\u001b[39;00m)\n", + "\u001b[0;31mTypingError\u001b[0m: Failed in nopython mode pipeline (step: nopython frontend)\nnon-precise type pyobject\nDuring: typing of argument at /tmp/ipykernel_5991/1812651176.py (1)\n\nFile \"../../tmp/ipykernel_5991/1812651176.py\", line 1:\n \n\nThis error may have been caused by the following argument(s):\n- argument 0: Cannot determine Numba type of \n- argument 1: Cannot determine Numba type of \n" ] } ], @@ -165,17 +192,20 @@ "\n", "# Encode each document weights in plaintext\n", "res = []\n", - "\n", + "start = time.time()\n", "for i in range(len(D)):\n", " #d = np.array(D[i])\n", " #cd = np.array([HE_server.encrypt(d[j]) for j in range(len(d))])\n", " cd = HE_server.encrypt(D[i])\n", " # Compute distance bewteen recieved query and D[i]\n", " res.append(hyperbolic_distance_parts(cx, cd))\n", + "end = time.time()\n", + "print(f\"[Server] Compute took {end - start}s with bandwidth {len(D) / (end-start)} documents/s\")\n", "\n", "s_res = [res[j].to_bytes() for j in range(len(res))]\n", "\n", - "print(f\"[Server] Distances computed! Responding: res={res}\")" + "print(f\"[Server] Distances computed! Responding: res={res}\")\n", + "print(f\"[Server] Sent {(np.sum([len(s_res[i]) for i in range(len(s_res))])) / (10**6)} MB\")" ] }, { diff --git a/venv_setup.sh b/venv_setup.sh index 0d46621..cf9bf01 100755 --- a/venv_setup.sh +++ b/venv_setup.sh @@ -3,4 +3,4 @@ mkdir ~/.venvs rm -rf ~/.venvs/cs239 python3.9 -m venv ~/.venvs/cs239 source ~/.venvs/cs239/bin/activate -pip install pyfhel \ No newline at end of file +pip install numpy pyfhel \ No newline at end of file