Skip to content

Commit

Permalink
Modified and Added tutorial Workflow-Interface_201_Exclusive_GPUs_wit…
Browse files Browse the repository at this point in the history
…h_Ray.ipynb

Signed-off-by: Parth Mandaliya <[email protected]>
  • Loading branch information
ParthM-GitHub committed May 5, 2023
1 parent eadfd1e commit 8b17d7a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 57 deletions.
12 changes: 7 additions & 5 deletions openfl-tutorials/experimental/Privacy_Meter/cifar10_PM.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ def end(self):
)

# Split train, test, and population dataset among collaborators
# this function will be called before executing collaborator steps
# which will return private attributes dictionary for each collaborator
def callable_to_initialize_collaborator_private_attributes(
index, n_collaborators, train_ds, test_ds, population_ds, args):
# construct the training and test and population dataset
Expand Down Expand Up @@ -778,17 +780,17 @@ def callable_to_initialize_collaborator_private_attributes(
Collaborator(
name=collab_name,
private_attributes_callable=callable_to_initialize_collaborator_private_attributes,
# Set `num_gpus=0.5` to `num_gpus=0.0` to run on CPU
num_cpus=0.0, num_gpus=0.5, # Assuming GPU is available in the machine
# If 1 GPU is available in the machine
# Set `num_gpus=0.0` to `num_gpus=0.5` to run on GPU with ray backend with 2 collaborators
num_cpus=0.0, num_gpus=0.0,
index=idx, n_collaborators=len(collaborator_names),
train_ds=train_dataset, test_ds=test_dataset,
population_ds=population_dataset, args=args
)
)

# To activate the ray backend with parallel collaborator tasks run in their own process
# and exclusive GPUs assigned to tasks, set LocalRuntime with backend='ray':
local_runtime = LocalRuntime(aggregator=aggregator, collaborators=collaborators)
# Set backend='ray' to use ray-backend
local_runtime = LocalRuntime(aggregator=aggregator, collaborators=collaborators, backend="single_process")

print(f"Local runtime collaborators = {local_runtime.collaborators}")

Expand Down
48 changes: 42 additions & 6 deletions openfl-tutorials/experimental/Workflow_Interface_101_MNIST.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "14821d97",
"metadata": {},
Expand All @@ -10,6 +11,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bd059520",
"metadata": {},
Expand All @@ -23,6 +25,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "39c3d86a",
"metadata": {},
Expand All @@ -31,6 +34,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a7989e72",
"metadata": {},
Expand All @@ -39,6 +43,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "fc8e35da",
"metadata": {},
Expand All @@ -47,6 +52,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4dbb89b6",
"metadata": {},
Expand All @@ -72,6 +78,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7237eac4",
"metadata": {},
Expand Down Expand Up @@ -165,6 +172,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "cd268911",
"metadata": {},
Expand Down Expand Up @@ -312,9 +320,9 @@
"id": "2aabf61e",
"metadata": {},
"source": [
"You'll notice in the `FederatedFlow` definition above that there were certain attributes that the flow was not initialized with, namely the `train_loader` and `test_loader` for each of the collaborators. These are **private_attributes** that are exposed only through the runtime. Each participant has it's own set of private attributes: a dictionary where the key is the attribute name, and the value is the object that will be made accessible through that participant's task. \n",
"Note that the private attributes are flexible, and you can choose to pass in a completely different type of object to any of the collaborators or aggregator (with an arbitrary name). These private attributes will always be filtered out of the current state when transferring from collaborator to aggregator, or vice versa. \n",
"\n",
"Below, we segment shards of the MNIST dataset for **four collaborators**: Portland, Seattle, Chandler, and Portland. Each has their own slice of the dataset that is accessible through the `train_loader` and `test_loader` attributes, which are set using the `callable_to_initialize_collaborator_private_attributes` callable function. Note that the private attributes are flexible, and you can choose to pass in a completely different type of object to any of the collaborators or aggregator (with an arbitrary name). These private attributes will always be filtered out of the current state when transfering from collaborator to aggregator, or vice versa. "
"Private attributes can be set using callback function while instantiating the participant. Parameters required by the callback function are specified as arguments while instantiating the participant. In this example callback function, `callable_to_initialize_collaborator_private_attributes`, returns the private attributes `train_loader` and `test_loader` of the collaborator. Parameters required by the callback function `index`, `n_collaborators`, `batch_size`, `train_dataset`, `test_dataset` are passed appropriate values with the same names in the Collaborator constructor."
]
},
{
Expand Down Expand Up @@ -356,8 +364,25 @@
"\n",
"local_runtime = LocalRuntime(aggregator=aggregator_, collaborators=collaborators,\n",
" backend=\"ray\")\n",
"print(f'Local runtime collaborators = {local_runtime.collaborators}')\n",
"\n",
"print(f'Local runtime collaborators = {local_runtime.collaborators}')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "278ad46b",
"metadata": {},
"source": [
"Now that we have our flow and runtime defined, let's run the experiment! "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a175b4d6",
"metadata": {},
"outputs": [],
"source": [
"model = None\n",
"best_model = None\n",
"optimizer = None\n",
Expand All @@ -367,11 +392,12 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "278ad46b",
"id": "86b3dd2e",
"metadata": {},
"source": [
"Now that we have our flow and runtime defined, let's run the experiment! "
"Now that the flow has completed, let's get the final model and accuracy"
]
},
{
Expand All @@ -387,6 +413,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5dd1558c",
"metadata": {},
Expand All @@ -409,6 +436,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "a61a876d",
"metadata": {},
Expand Down Expand Up @@ -458,6 +486,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "b55ccb19",
"metadata": {},
Expand Down Expand Up @@ -486,6 +515,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e5efa1ff",
"metadata": {},
Expand All @@ -504,6 +534,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3292b2e0",
"metadata": {},
Expand Down Expand Up @@ -542,6 +573,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "eb1866b7",
"metadata": {},
Expand Down Expand Up @@ -570,6 +602,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ef877a50",
"metadata": {},
Expand Down Expand Up @@ -598,6 +631,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9826c45f",
"metadata": {},
Expand All @@ -616,6 +650,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dd962ddc",
"metadata": {},
Expand All @@ -634,6 +669,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "426f2395",
"metadata": {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "14821d97",
"metadata": {},
Expand All @@ -10,6 +11,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bd059520",
"metadata": {},
Expand All @@ -18,6 +20,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "fc8e35da",
"metadata": {},
Expand All @@ -26,6 +29,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4dbb89b6",
"metadata": {},
Expand All @@ -50,6 +54,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7237eac4",
"metadata": {},
Expand Down Expand Up @@ -133,6 +138,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "cd268911",
"metadata": {},
Expand Down Expand Up @@ -169,6 +175,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "b2e45614",
"metadata": {
Expand Down Expand Up @@ -284,7 +291,9 @@
"source": [
"You'll notice in the `FederatedFlow` definition above that there were certain attributes that the flow was not initialized with, namely the `train_loader` and `test_loader` for each of the collaborators. These are **private_attributes** that are exposed only throught he runtime. Each participant has it's own set of private attributes: a dictionary where the key is the attribute name, and the value is the object that will be made accessible through that participant's task. \n",
"\n",
"Below, we segment shards of the MNIST dataset for **four collaborators**: Portland, Seattle, Chandler, and Portland. Each has their own slice of the dataset that is accessible through the `train_loader` and `test_loader` attributes, which are set using the `callable_to_initialize_collaborator_private_attributes` callable function. Note that the private attributes are flexible, and you can choose to pass in a completely different type of object to any of the collaborators or aggregator (with an arbitrary name). These private attributes will always be filtered out of the current state when transfering from collaborator to aggregator, or vice versa.\n"
"Below, we segment shards of the MNIST dataset for **four collaborators**: `Portland`, `Seattle`, `Chandler`, and `Portland`. Each has their own slice of the dataset that is accessible through the `train_loader` and `test_loader` attributes, which are set using the `callable_to_initialize_collaborator_private_attributes` callable function. Note that the private attributes are flexible, and you can choose to pass in a completely different type of object to any of the collaborators or aggregator (with an arbitrary name). These private attributes will always be filtered out of the current state when transfering from collaborator to aggregator, or vice versa.\n",
"\n",
"Private attributes can be set using callback function while instantiating the participant. Parameters required by the callback function are specified as arguments while instantiating the participant. In this example callback function, `callable_to_initialize_collaborator_private_attributes`, returns the private attributes `train_loader` and `test_loader` of the collaborator. Callback function, `callable_to_initialize_aggregator_private_attributes`, returns the private attribute `test_loader` of the Aggregator."
]
},
{
Expand Down Expand Up @@ -342,6 +351,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0525eaa9",
"metadata": {},
Expand All @@ -365,6 +375,7 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8b9f8d25",
"metadata": {},
Expand Down
Loading

0 comments on commit 8b17d7a

Please sign in to comment.