From af009b4831b6db97850008fdaed4eec7bb8ff5d9 Mon Sep 17 00:00:00 2001 From: rasswanth-s <43314053+rasswanth-s@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:01:26 +0530 Subject: [PATCH 1/6] remove old enclave clode - iteration 1 --- notebooks/api/0.8/05-custom-policy.ipynb | 20 +- .../Enclave-single-notebook-DO-DS.ipynb | 735 ------------------ .../syft/src/syft/client/enclave_client.py | 52 -- .../syft/service/enclave/enclave_service.py | 149 ---- .../syft/src/syft/service/policy/policy.py | 13 - 5 files changed, 1 insertion(+), 968 deletions(-) delete mode 100644 notebooks/tutorials/enclaves/Enclave-single-notebook-DO-DS.ipynb diff --git a/notebooks/api/0.8/05-custom-policy.ipynb b/notebooks/api/0.8/05-custom-policy.ipynb index c87f5f84036..663468260b4 100644 --- a/notebooks/api/0.8/05-custom-policy.ipynb +++ b/notebooks/api/0.8/05-custom-policy.ipynb @@ -286,7 +286,7 @@ " root_context = AuthedServiceContext(\n", " node=context.node, credentials=context.node.verify_key\n", " )\n", - " if context.node.node_type == NodeType.DOMAIN:\n", + " if context.node.node_type in NodeType.DOMAIN:\n", " for var_name, arg_id in allowed_inputs.items():\n", " kwarg_value = action_service._get(\n", " context=root_context,\n", @@ -297,14 +297,6 @@ " if kwarg_value.is_err():\n", " return Err(kwarg_value.err())\n", " code_inputs[var_name] = kwarg_value.ok()\n", - "\n", - " elif context.node.node_type == NodeType.ENCLAVE:\n", - " dict_object = action_service.get(context=root_context, uid=code_item_id)\n", - " if dict_object.is_err():\n", - " return Err(dict_object.err())\n", - " for value in dict_object.ok().syft_action_data.values():\n", - " code_inputs.update(value)\n", - "\n", " else:\n", " raise Exception(\n", " f\"Invalid Node Type for Code Submission:{context.node.node_type}\"\n", @@ -328,11 +320,6 @@ " verify_key=context.node.signing_key.verify_key,\n", " )\n", " allowed_inputs = allowed_inputs.get(node_identity, {})\n", - " elif context.node.node_type == NodeType.ENCLAVE:\n", - " base_dict = {}\n", - " for key in allowed_inputs.values():\n", - " base_dict.update(key)\n", - " allowed_inputs = base_dict\n", " else:\n", " raise Exception(\n", " f\"Invalid Node Type for Code Submission:{context.node.node_type}\"\n", @@ -403,11 +390,6 @@ " verify_key=context.node.signing_key.verify_key,\n", " )\n", " allowed_inputs = allowed_inputs.get(node_identity, {})\n", - " elif context.node.node_type == NodeType.ENCLAVE:\n", - " base_dict = {}\n", - " for key in allowed_inputs.values():\n", - " base_dict.update(key)\n", - " allowed_inputs = base_dict\n", " else:\n", " raise Exception(\n", " f\"Invalid Node Type for Code Submission:{context.node.node_type}\"\n", diff --git a/notebooks/tutorials/enclaves/Enclave-single-notebook-DO-DS.ipynb b/notebooks/tutorials/enclaves/Enclave-single-notebook-DO-DS.ipynb deleted file mode 100644 index 62d7fc48c99..00000000000 --- a/notebooks/tutorials/enclaves/Enclave-single-notebook-DO-DS.ipynb +++ /dev/null @@ -1,735 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "0", - "metadata": {}, - "outputs": [], - "source": [ - "# third party\n", - "from recordlinkage.datasets import load_febrl4\n", - "\n", - "# syft absolute\n", - "import syft as sy" - ] - }, - { - "cell_type": "markdown", - "id": "1", - "metadata": {}, - "source": [ - "# Create Nodes and connect to gateway" - ] - }, - { - "cell_type": "markdown", - "id": "2", - "metadata": {}, - "source": [ - "create enclave node" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3", - "metadata": {}, - "outputs": [], - "source": [ - "# Local Python Node\n", - "enclave_node = sy.orchestra.launch(\n", - " name=\"Enclave\",\n", - " node_type=sy.NodeType.ENCLAVE,\n", - " local_db=True,\n", - " dev_mode=True,\n", - " reset=True,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4", - "metadata": {}, - "outputs": [], - "source": [ - "# syft absolute\n", - "from syft.abstract_node import NodeType\n", - "\n", - "assert enclave_node.python_node.node_type == NodeType.ENCLAVE" - ] - }, - { - "cell_type": "markdown", - "id": "5", - "metadata": {}, - "source": [ - "Create canada node & italy node" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6", - "metadata": {}, - "outputs": [], - "source": [ - "ca_node = sy.orchestra.launch(name=\"Canada\", local_db=True, reset=True, dev_mode=True)\n", - "it_node = sy.orchestra.launch(name=\"Italy\", local_db=True, reset=True, dev_mode=True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7", - "metadata": {}, - "outputs": [], - "source": [ - "assert ca_node.python_node.node_type == NodeType.DOMAIN\n", - "assert it_node.python_node.node_type == NodeType.DOMAIN" - ] - }, - { - "cell_type": "markdown", - "id": "8", - "metadata": {}, - "source": [ - "Create gateway Node" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9", - "metadata": {}, - "outputs": [], - "source": [ - "gateway_node = sy.orchestra.launch(\n", - " name=\"gateway\",\n", - " node_type=sy.NodeType.GATEWAY,\n", - " local_db=True,\n", - " reset=True,\n", - " dev_mode=True,\n", - " association_request_auto_approval=True,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "id": "10", - "metadata": {}, - "source": [ - "Connect nodes to gateway" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "11", - "metadata": {}, - "outputs": [], - "source": [ - "enclave_guest_client = enclave_node.client\n", - "ca_guest_client = ca_node.client\n", - "it_guest_client = it_node.client" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "12", - "metadata": {}, - "outputs": [], - "source": [ - "# syft absolute\n", - "from syft.client.domain_client import DomainClient\n", - "from syft.client.enclave_client import EnclaveClient\n", - "from syft.client.gateway_client import GatewayClient\n", - "\n", - "assert isinstance(enclave_guest_client, EnclaveClient)\n", - "assert isinstance(ca_guest_client, DomainClient)\n", - "assert isinstance(it_guest_client, DomainClient)\n", - "assert isinstance(gateway_node.client, GatewayClient)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "13", - "metadata": {}, - "outputs": [], - "source": [ - "# syft absolute\n", - "# Connect enclave to gateway\n", - "from syft.service.response import SyftSuccess\n", - "\n", - "res = enclave_guest_client.connect_to_gateway(handle=gateway_node)\n", - "assert isinstance(res, SyftSuccess)\n", - "res" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "14", - "metadata": {}, - "outputs": [], - "source": [ - "# Connect Canada to gateway\n", - "res = ca_guest_client.connect_to_gateway(handle=gateway_node)\n", - "assert isinstance(res, SyftSuccess)\n", - "res" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "15", - "metadata": {}, - "outputs": [], - "source": [ - "# Connect Italy to gateway\n", - "res = it_guest_client.connect_to_gateway(handle=gateway_node)\n", - "assert isinstance(res, SyftSuccess)\n", - "res" - ] - }, - { - "cell_type": "markdown", - "id": "16", - "metadata": {}, - "source": [ - "# DOs" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "17", - "metadata": {}, - "outputs": [], - "source": [ - "do_ca_client = ca_node.login(email=\"info@openmined.org\", password=\"changethis\")\n", - "do_it_client = it_node.login(email=\"info@openmined.org\", password=\"changethis\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "18", - "metadata": {}, - "outputs": [], - "source": [ - "# syft absolute\n", - "from syft.client.domain_client import DomainClient\n", - "\n", - "assert isinstance(do_ca_client, DomainClient)\n", - "assert isinstance(do_it_client, DomainClient)" - ] - }, - { - "cell_type": "markdown", - "id": "19", - "metadata": {}, - "source": [ - "## Upload dataset" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "20", - "metadata": {}, - "outputs": [], - "source": [ - "# Using public datasets from Freely Extensible Biomedical Record Linkage (Febrl)\n", - "canada_census_data, italy_census_data = load_febrl4()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "21", - "metadata": {}, - "outputs": [], - "source": [ - "for dataset, client, country in zip(\n", - " [canada_census_data, italy_census_data],\n", - " [do_ca_client, do_it_client],\n", - " [\"Canada\", \"Italy\"],\n", - "):\n", - " private_data, mock_data = dataset[:2500], dataset[2500:]\n", - " dataset = sy.Dataset(\n", - " name=f\"{country} - FEBrl Census Data\",\n", - " description=\"abc\",\n", - " asset_list=[\n", - " sy.Asset(\n", - " name=\"census_data\",\n", - " mock=mock_data,\n", - " data=private_data,\n", - " shape=private_data.shape,\n", - " mock_is_real=True,\n", - " )\n", - " ],\n", - " )\n", - " client.upload_dataset(dataset)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "22", - "metadata": {}, - "outputs": [], - "source": [ - "assert len(do_ca_client.datasets.get_all()) == 1\n", - "assert len(do_it_client.datasets.get_all()) == 1" - ] - }, - { - "cell_type": "markdown", - "id": "23", - "metadata": {}, - "source": [ - "## create accounts for DS" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "24", - "metadata": {}, - "outputs": [], - "source": [ - "for client in [do_ca_client, do_it_client]:\n", - " res = client.register(\n", - " name=\"Sheldon\",\n", - " email=\"sheldon@caltech.edu\",\n", - " password=\"changethis\",\n", - " password_verify=\"changethis\",\n", - " )\n", - " assert isinstance(res, SyftSuccess)" - ] - }, - { - "cell_type": "markdown", - "id": "25", - "metadata": {}, - "source": [ - "# DS" - ] - }, - { - "cell_type": "markdown", - "id": "26", - "metadata": {}, - "source": [ - "## Login into gateway as guest" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "27", - "metadata": {}, - "outputs": [], - "source": [ - "ds_gateway_client = gateway_node.client" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "28", - "metadata": {}, - "outputs": [], - "source": [ - "# Explore the domains and enclaves connected to the gateway\n", - "ds_gateway_client.domains" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "29", - "metadata": {}, - "outputs": [], - "source": [ - "# Log into canada as proxy_client\n", - "ds_ca_proxy_client = ds_gateway_client.domains[0]\n", - "ds_ca_proxy_client = ds_ca_proxy_client.login(\n", - " email=\"sheldon@caltech.edu\", password=\"changethis\"\n", - ")\n", - "assert ds_ca_proxy_client.name == \"Canada\"\n", - "assert ds_ca_proxy_client.connection.proxy_target_uid == do_ca_client.id\n", - "assert isinstance(ds_ca_proxy_client, DomainClient)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "30", - "metadata": {}, - "outputs": [], - "source": [ - "# Log into italy as proxy_client\n", - "ds_it_proxy_client = ds_gateway_client.domains[1]\n", - "ds_it_proxy_client = ds_it_proxy_client.login(\n", - " email=\"sheldon@caltech.edu\", password=\"changethis\"\n", - ")\n", - "assert ds_it_proxy_client.name == \"Italy\"\n", - "assert ds_it_proxy_client.connection.proxy_target_uid == do_it_client.id\n", - "assert isinstance(ds_it_proxy_client, DomainClient)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "31", - "metadata": {}, - "outputs": [], - "source": [ - "# Create an account and log into enclave as proxy client\n", - "ds_enclave_proxy_client = ds_gateway_client.enclaves[0]\n", - "ds_enclave_proxy_client = ds_enclave_proxy_client.login(\n", - " email=\"sheldon@caltech.edu\", password=\"changethis\", name=\"Sheldon\", register=True\n", - ")\n", - "assert ds_enclave_proxy_client.name == \"Enclave\"\n", - "assert ds_enclave_proxy_client.connection.proxy_target_uid == enclave_guest_client.id\n", - "assert isinstance(ds_enclave_proxy_client, EnclaveClient)" - ] - }, - { - "cell_type": "markdown", - "id": "32", - "metadata": {}, - "source": [ - "## Find datasets" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "33", - "metadata": {}, - "outputs": [], - "source": [ - "canada_census_data = ds_ca_proxy_client.datasets[-1].assets[0]\n", - "italy_census_data = ds_it_proxy_client.datasets[-1].assets[0]" - ] - }, - { - "cell_type": "markdown", - "id": "34", - "metadata": {}, - "source": [ - "## Create Request" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "35", - "metadata": {}, - "outputs": [], - "source": [ - "@sy.syft_function_single_use(\n", - " canada_census_data=canada_census_data,\n", - " italy_census_data=italy_census_data,\n", - " share_results_with_owners=True,\n", - ")\n", - "def compute_census_matches(canada_census_data, italy_census_data):\n", - " # third party\n", - " import recordlinkage\n", - "\n", - " # Index step\n", - " indexer = recordlinkage.Index()\n", - " indexer.block(\"given_name\")\n", - "\n", - " candidate_links = indexer.index(canada_census_data, italy_census_data)\n", - "\n", - " # Comparison step\n", - " compare_cl = recordlinkage.Compare()\n", - "\n", - " compare_cl.exact(\"given_name\", \"given_name\", label=\"given_name\")\n", - " compare_cl.string(\n", - " \"surname\", \"surname\", method=\"jarowinkler\", threshold=0.85, label=\"surname\"\n", - " )\n", - " compare_cl.exact(\"date_of_birth\", \"date_of_birth\", label=\"date_of_birth\")\n", - " compare_cl.exact(\"suburb\", \"suburb\", label=\"suburb\")\n", - " compare_cl.exact(\"state\", \"state\", label=\"state\")\n", - " compare_cl.string(\"address_1\", \"address_1\", threshold=0.85, label=\"address_1\")\n", - "\n", - " features = compare_cl.compute(\n", - " candidate_links, canada_census_data, italy_census_data\n", - " )\n", - "\n", - " # Classification step\n", - " matches = features[features.sum(axis=1) > 3]\n", - "\n", - " return len(matches)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "36", - "metadata": {}, - "outputs": [], - "source": [ - "# Check result of mock data execution\n", - "mock_result = compute_census_matches(\n", - " canada_census_data=canada_census_data.mock,\n", - " italy_census_data=italy_census_data.mock,\n", - ")\n", - "mock_result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "37", - "metadata": {}, - "outputs": [], - "source": [ - "req = ds_enclave_proxy_client.request_code_execution(compute_census_matches)\n", - "req" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "38", - "metadata": {}, - "outputs": [], - "source": [ - "assert isinstance(req, sy.service.request.request.Request)" - ] - }, - { - "cell_type": "markdown", - "id": "39", - "metadata": {}, - "source": [ - "# DOs" - ] - }, - { - "cell_type": "markdown", - "id": "40", - "metadata": {}, - "source": [ - "## Approve" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "41", - "metadata": {}, - "outputs": [], - "source": [ - "for client in [do_ca_client, do_it_client]:\n", - " res = client.requests[-1].approve()\n", - " assert isinstance(res, SyftSuccess)" - ] - }, - { - "cell_type": "markdown", - "id": "42", - "metadata": {}, - "source": [ - "# DS" - ] - }, - { - "cell_type": "markdown", - "id": "43", - "metadata": {}, - "source": [ - "## Get result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "44", - "metadata": {}, - "outputs": [], - "source": [ - "status = ds_enclave_proxy_client.code.get_all()[-1].status\n", - "status" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "45", - "metadata": {}, - "outputs": [], - "source": [ - "for st, _ in status.status_dict.values():\n", - " assert st == sy.service.request.request.UserCodeStatus.APPROVED" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "46", - "metadata": {}, - "outputs": [], - "source": [ - "ds_enclave_proxy_client.code[-1].output_policy" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "47", - "metadata": {}, - "outputs": [], - "source": [ - "result_pointer = ds_enclave_proxy_client.code.compute_census_matches(\n", - " canada_census_data=canada_census_data, italy_census_data=italy_census_data\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "48", - "metadata": {}, - "outputs": [], - "source": [ - "result_pointer" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "49", - "metadata": {}, - "outputs": [], - "source": [ - "result_pointer.syft_action_data == 858" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "50", - "metadata": {}, - "outputs": [], - "source": [ - "real_result = result_pointer.get()\n", - "real_result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "51", - "metadata": {}, - "outputs": [], - "source": [ - "assert real_result == 813" - ] - }, - { - "cell_type": "markdown", - "id": "52", - "metadata": {}, - "source": [ - "# DO" - ] - }, - { - "cell_type": "markdown", - "id": "53", - "metadata": {}, - "source": [ - "## Can also get the result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "54", - "metadata": {}, - "outputs": [], - "source": [ - "request = do_ca_client.requests[0]\n", - "request" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "55", - "metadata": {}, - "outputs": [], - "source": [ - "result_ptr = request.get_results()\n", - "result_ptr" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "56", - "metadata": {}, - "outputs": [], - "source": [ - "assert result_ptr.syft_action_data == 813" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "57", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.16" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": { - "height": "calc(100% - 180px)", - "left": "10px", - "top": "150px", - "width": "358.398px" - }, - "toc_section_display": true, - "toc_window_display": true - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/packages/syft/src/syft/client/enclave_client.py b/packages/syft/src/syft/client/enclave_client.py index 32eebdf3189..6252817f630 100644 --- a/packages/syft/src/syft/client/enclave_client.py +++ b/packages/syft/src/syft/client/enclave_client.py @@ -2,12 +2,10 @@ from __future__ import annotations # stdlib -from typing import Any from typing import TYPE_CHECKING # relative from ..abstract_node import NodeSideType -from ..client.api import APIRegistry from ..serde.serializable import serializable from ..service.metadata.node_metadata import NodeMetadataJSON from ..service.network.routes import NodeRouteType @@ -15,7 +13,6 @@ from ..service.response import SyftSuccess from ..types.syft_object import SYFT_OBJECT_VERSION_3 from ..types.syft_object import SyftObject -from ..types.uid import UID from ..util.assets import load_png_base64 from ..util.notebook_ui.styles import FONT_CSS from .api import APIModule @@ -27,7 +24,6 @@ if TYPE_CHECKING: # relative from ..orchestra import NodeHandle - from ..service.code.user_code import SubmitUserCode @serializable() @@ -109,54 +105,6 @@ def connect_to_gateway( def get_enclave_metadata(self) -> EnclaveMetadata: return EnclaveMetadata(route=self.connection.route) - def request_code_execution(self, code: SubmitUserCode) -> Any | SyftError: - # relative - from ..service.code.user_code_service import SubmitUserCode - - if not isinstance(code, SubmitUserCode): - raise Exception( - f"The input code should be of type: {SubmitUserCode} got:{type(code)}" - ) - if code.input_policy_init_kwargs is None: - raise ValueError(f"code {code}'s input_policy_init_kwargs is None") - - enclave_metadata = self.get_enclave_metadata() - - code_id = UID() - code.id = code_id - code.enclave_metadata = enclave_metadata - - apis = [] - for k, v in code.input_policy_init_kwargs.items(): - # We would need the verify key of the data scientist to be able to index the correct client - # Since we do not want the data scientist to pass in the clients to the enclave client - # from a UX perspecitve. - # we will use the recent node id to find the correct client - # assuming that it is the correct client - # Warning: This could lead to inconsistent results, when we have multiple clients - # in the same node pointing to the same node. - # One way, by which we could solve this in the long term, - # by forcing the user to pass only assets to the sy.ExactMatch, - # by which we could extract the verify key of the data scientist - # as each object comes with a verify key and node_uid - # the asset object would contain the verify key of the data scientist. - api = APIRegistry.get_by_recent_node_uid(k.node_id) - if api is None: - raise ValueError(f"could not find client for input {v}") - else: - apis += [api] - - for api in apis: - res = api.services.code.request_code_execution(code=code) - if isinstance(res, SyftError): - return res - - # we are using the real method here, see the .code property getter - _ = self.code - res = self._request_code_execution(code=code) - - return res - def _repr_html_(self) -> str: commands = """