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

Add pFedHN baseline #2377

Open
wants to merge 101 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
1b54aa5
Adding the initial things
achiverram28 Sep 14, 2023
56c8e5f
Adding the conf
achiverram28 Sep 14, 2023
1fceaaa
Adding the models
achiverram28 Sep 14, 2023
2f3afd8
Adding the data
achiverram28 Sep 14, 2023
a21f94d
Adding the main
achiverram28 Sep 14, 2023
0d4f7b4
Making a user friendly approach
achiverram28 Sep 15, 2023
458a5af
Modifying main
achiverram28 Sep 15, 2023
bd95e07
Modyfing th dataset.py
achiverram28 Sep 15, 2023
10f8080
Modifying the models
achiverram28 Sep 15, 2023
57e7816
will update these later
achiverram28 Sep 15, 2023
d2d792d
Adding the trainer
achiverram28 Sep 15, 2023
2c52399
Adding the strategy
achiverram28 Sep 15, 2023
daaf571
Adding the client.py
achiverram28 Sep 15, 2023
09fbdaa
Modifying the conf
achiverram28 Sep 15, 2023
b0ade10
Updating the readme
achiverram28 Sep 15, 2023
1f4752c
Updating the README
achiverram28 Sep 15, 2023
7372b8d
Code formatting
achiverram28 Sep 15, 2023
19a3821
Revert "Code formatting"
achiverram28 Sep 15, 2023
71da489
Code formatting
achiverram28 Sep 15, 2023
e032d33
Docstring formatting
achiverram28 Sep 15, 2023
e6c71e0
Modifying the pyproject.toml
achiverram28 Sep 15, 2023
170b336
Formatting and modifying the imports
achiverram28 Sep 15, 2023
585cb2d
Modifying the conf
achiverram28 Sep 15, 2023
d2df7d2
Updating the README.md
achiverram28 Sep 15, 2023
15a681a
Updating the README.md
achiverram28 Sep 15, 2023
0ffccae
Updating the README.md
achiverram28 Sep 15, 2023
ab1ae3b
Updating the README.md
achiverram28 Sep 15, 2023
246edf2
updating the dataset.py
Sep 15, 2023
00cf814
Updating the README.md
achiverram28 Sep 15, 2023
ea0bb4b
Modifying the docstring formatter
achiverram28 Sep 15, 2023
64a7b16
Updating dataset.py
Sep 15, 2023
aaeded3
Optimising the client.py
Sep 15, 2023
d21179f
evert "Optimising the client.py"
Sep 15, 2023
baaf5b0
Revert "Updating dataset.py"
Sep 15, 2023
158a2db
Testing everything in test-baseline.sh except pylint
achiverram28 Sep 15, 2023
1d8b94e
Merge branch 'main' of https://github.com/achiverram28/flower into HEAD
Sep 15, 2023
18a0550
Pylint check done
Sep 16, 2023
6b7382a
Testing done
Sep 16, 2023
6ab3f4e
Pushing the .pylintrc
Sep 16, 2023
e1104fd
removing the .pylint
achiverram28 Sep 16, 2023
168ee86
Solving all checks
achiverram28 Sep 16, 2023
8f039a7
Modifying the strategy
achiverram28 Sep 16, 2023
45f4255
Merge branch 'main' into testing
achiverram28 Sep 16, 2023
d6c1642
Merge pull request #2 from achiverram28/testing
achiverram28 Sep 16, 2023
8ccf4a2
Delete baselines/pFedHN/outputs/2023-09-15 directory
achiverram28 Sep 16, 2023
d792299
Delete baselines/pFedHN/pFedHN/outputs/2023-09-15 directory
achiverram28 Sep 16, 2023
9a9312a
Removed outputs
achiverram28 Sep 16, 2023
3c8af8b
Merge pull request #3 from achiverram28/testing
achiverram28 Sep 16, 2023
945aa97
Updating the toml file
Sep 16, 2023
5bc8d8d
Merge branch 'main' into main
kishan-droid Sep 16, 2023
aa50b1e
Update README.md
kishan-droid Sep 23, 2023
7e7cbb8
Adding the modified changes for a new approach
achiverram28 Oct 17, 2023
fc0b5a0
Conducting all the tests
achiverram28 Oct 17, 2023
dc6ebc7
Modifying the docstring of client
achiverram28 Oct 17, 2023
f515962
modifying some aspects in model
achiverram28 Oct 17, 2023
48be2c3
Modifying the given files
achiverram28 Oct 23, 2023
edab990
modify the base conf
achiverram28 Oct 23, 2023
f600012
Merge pull request #4 from achiverram28/new-changes
achiverram28 Oct 23, 2023
679cb22
Modify the import manner
achiverram28 Oct 23, 2023
99ae12a
Merge pull request #5 from achiverram28/new-changes
achiverram28 Oct 23, 2023
c349718
Modifying the toml for matplotlib
achiverram28 Oct 23, 2023
c25671d
Modifying the README.md
achiverram28 Oct 23, 2023
fdb22f9
Adding the comparison algorithms
achiverram28 Oct 23, 2023
4600005
Merge pull request #6 from achiverram28/addexp
achiverram28 Oct 23, 2023
cf9cda8
Updating the README.md
achiverram28 Oct 23, 2023
23c8e33
Adding the results
achiverram28 Oct 24, 2023
ff05cf5
Adding more results
achiverram28 Oct 24, 2023
64d8fed
Adding more results
achiverram28 Oct 24, 2023
c50f205
modifying conf
achiverram28 Oct 24, 2023
ee16be3
Adding more results
achiverram28 Oct 24, 2023
daaca2e
Adding the resources
achiverram28 Oct 24, 2023
6379113
Modifying the client
achiverram28 Oct 24, 2023
029166b
Modifying the comaprison experiment's flexibility and README.md updation
achiverram28 Oct 25, 2023
3508378
Adding more results
achiverram28 Oct 25, 2023
a871cea
Adding the resource allocations
achiverram28 Oct 25, 2023
445c781
Adding the formatting
achiverram28 Oct 25, 2023
e4d54cb
Adding more results
achiverram28 Oct 25, 2023
5de13b7
Adding more results
achiverram28 Oct 25, 2023
c900476
Adding more results
achiverram28 Oct 26, 2023
27835e7
Modifying the hyp-param values
achiverram28 Oct 26, 2023
76beea6
Adding more results
achiverram28 Oct 28, 2023
a548dbd
updates here and there
jafermarq Oct 31, 2023
03b8ee4
Modifying changes
achiverram28 Nov 2, 2023
7b8044a
Modifying the toml
achiverram28 Nov 2, 2023
58cad5c
Modifying in comparison_algorithms
achiverram28 Nov 2, 2023
919164e
Making device related changes
achiverram28 Nov 2, 2023
57ca494
Device related changes
achiverram28 Nov 2, 2023
7f19d7c
Modifying few more things
achiverram28 Nov 2, 2023
ba54c7f
Adding results
achiverram28 Nov 2, 2023
96df338
Adding more results
achiverram28 Nov 2, 2023
1e4fbc1
Adding more results
achiverram28 Nov 3, 2023
36a8800
Modified commmit to run proper results
achiverram28 Nov 11, 2023
1e199b2
Small change
achiverram28 Nov 11, 2023
0b565a3
formatting;evaluate_every; other minor changes
jafermarq Nov 14, 2023
6c55df4
Merge branch 'adap:main' into main
kishan-droid Dec 6, 2023
e79c6eb
Modified changes
achiverram28 Dec 19, 2023
377702a
Modifying the config
achiverram28 Dec 19, 2023
fb57216
Modifying the README.md
achiverram28 Dec 19, 2023
60262a7
Merge branch 'main' of https://github.com/achiverram28/flower
achiverram28 Dec 19, 2023
bca8124
Modifying changes
achiverram28 Dec 19, 2023
8b657c0
Modified changes
achiverram28 Jan 5, 2024
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
202 changes: 202 additions & 0 deletions baselines/pFedHN/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
140 changes: 140 additions & 0 deletions baselines/pFedHN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
Title: Personalized Federated Learning using Hypernetworks
Url: https://arxiv.org/abs/2103.04628
Labels: [data heterogenity, hypernetworks, personalised FL,]
Dataset: [MNIST, CIFAR-10, CIFAR-100]
---

# Personalized Federated Learning using Hypernetworks

> Note: If you use this baseline in your work, please remember to cite the original authors of the paper as well as the Flower paper.

**Paper:** [arxiv.org/abs/2103.04628](https://arxiv.org/abs/2103.04628)

**Authors:** Aviv Shamsian, Aviv Navon, Ethan Fetaya, Gal Chechik

**Abstract:** Personalized federated learning is tasked with training machine learning models for multiple clients, each with its own data distribution. The goal is to train personalized models in a collaborative way while accounting for data disparities across clients and reducing communication costs. We propose a novel approach to this problem using hypernetworks, termed pFedHN for personalized Federated HyperNetworks. In this approach, a central hypernetwork model is trained to generate a set of models, one model for each client. This architecture provides effective parameter sharing across clients, while maintaining the capacity to generate unique and diverse personal models. Furthermore, since hypernetwork parameters are never transmitted, this approach decouples the communication cost from the trainable model size. We test pFedHN empirically in several personalized federated learning challenges and find that it outperforms previous methods. Finally, since hypernetworks share information across clients we show that pFedHN can generalize better to new clients whose distributions differ from any client observed during training.

## About this baseline

**What’s implemented:** The code in the repository reproduces the paper by implementing the concept of hypernetworks which create weights for that target network thus resolving the problems of dataheterogenity. The hypernetworks lies in the server and the clients have the target model. Initially hypernetwork sends the weights from the server which is loaded into the target net. After the targetnet is trained we pass the delta_theta to the client, which inturn updated the phi_gradients for the hypernetwork and the flow goes on.

**Datasets:** MNIST, CIFAR-10, CIFAR-100 from torchvision

**Hardware Setup:** The experiments were conducted on a 12-core CPU MacBook Pro M2 Pro with 32GB of RAM, as well as on an HPC Cluster equipped with NVIDIA A100-PCIE-40GB GPU, alternately.

**Contributors:**
---
| Names | Profiles |
| ----------- | ----------- |
| Ram Samarth B B | [achiverram28](https://github.com/achiverram28) |
| Kishan Gurumurthy | [kishan-droid](https://github.com/kishan-droid) |
| Sachin DN | [sachugowda](https://github.com/sachugowda) |
---

## Experimental Setup

**Task:** Image Classification

**Model:** CNNHyper for the HyperNetwork , CNNTarget For the TargetNetwork

**Dataset:** This baseline includes the MNIST, CIFAR-10 , CIFAR-100 datasets. By default it will be partitioned into 50 clients following Non-IID distribution. The settings are as follow:

| Dataset | #classes | partitioning method | classes per client |
| :------ | :---: | :---: | :---: |
| MNIST | 10 | Non-IID | 2 |
| CIFAR10 | 10<br>100 | Non-IID | 2<br>10 |


**Training Hyperparameters:** The following table shows the main hyperparameters for this baseline with their default value (i.e. the value used if you run `python3 -m pFedHN.main` directly)

| Description | Default Value |
| ----------- | ----- |
| Data Partition | Non - IID |
| Dataset | CIFAR-10 |
| Batch size | 64 |
| Classes per client | 2|
| Total clients | 50 |
| Tocal epoch(client-side) | 50 |
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's worth clarifying that this actually means "number of batches". If I see your train() function in trainer.py, each "epoch" uses a single batch of the trainloader. This seems to be exactly what Algorithm 1 in the paper indicates. Maybe it would be better if you rename it to "local step"? (and make that change in the code+config as well)

| Number of rounds | 5000 |
| HyperNetwork hidden units for CIFAR-10/CIFAR-100 | 3 |
| HyperNetwork hidden units for MNIST | 1 |
| HyperNetwork learning rate | 1e-2 |
| HyperNetwork momentum | 0.9 |
| HyperNetwork weight decay | 1e-3 |
| HyperNetwork Optimizer | SGD with momentum and weight decay |
| TargetNetwork learning rate | 5e-3 |
| TargetNetwork momentum | 0.9 |
| TargetNetwork weight decay | 5e-5 |
| TargetNetwork Optimizer | SGD with momentum and weight decay |
**Target Model Variations**
| **Dataset** | CIFAR-10 |
| Number of input channels | 3 |
| Input Image Dimension | 32x32 |
| Number of classes | 10 |
| Kernels | 16 |
| **Dataset** | CIFAR-100 |
| Number of input channels | 3 |
| Input Image Dimension | 32x32 |
| Number of classes | 100 |
| Kernels | 16 |
| **Dataset** | MNIST |
| Number of input channels | 1 |
| Input Image Dimension | 28x28 |
| Number of classes | 10 |
| Kernels | 7 |
| Local Layer for pFedHN | False |
| Local Layer for pFedHNPC | True |
| Learning rate for pFedHNPC | 5e-2 |




## Environment Setup


To construct the Python environment follow these steps:

```bash
# Set Python 3.10
pyenv local 3.10.11

# Tell poetry to use python 3.10
poetry env use 3.10.11

# Install the base Poetry environment
poetry install

# Activate the environment
poetry shell
```

## Running the Experiments

To run this pFedHN, first ensure you have activated your Poetry environment (execute `poetry shell` from this directory), then:

```bash
## These instructions are for the pFedHN Experiments
python -m pFedHN.main # this will run using the default settings in the `conf/config.yaml` that is for the CIFAR-10 dataset

python -m pFedHN.main dataset.data="cifar100" model.out_dim=100 client.num_classes_per_node=10 client.batch_size=128 # this will run for the CIFAR-100 dataset where we give each client 10 classes and number of classes is 100

python -m pFedHN.main dataset.data="mnist" model.n_kernels=7 model.in_channels=1 model.n_hidden=1 # this will run for the MNIST dataset where the number of input channels is 1 , the number of hidden layers in hypernetwork is 1 and the number of kernels used in the CNNTarget is 7

## For conducting pFedHNPC Experiments follow the commands given below

# For MNIST we are not conducting experiments as the paper has not done it.

python -m pFedHN.main model.local=True model.variant=1 server.lr=5e-2 # this will run the pFedHNPC for CIFAR-10 dataset where local=True is for using LocalLayer and variant=1 for setting pFedHNPC . Learning rate is modified to 5e-2

python -m pFedHN.main dataset.data="cifar100" model.out_dim=100 client.num_classes_per_node=10 model.local=True model.variant=1 server.lr=5e-2 client.batch_size=128 # this will run the pFedHNPC for CIFAR-100 dataset where local=True is for using LocalLayer and variant=1 for setting pFedHNPC . Learning rate is modified to 5e-2

```

## Expected Results

| Algorithm | Dataset | Num_Clients | Paper_Accuracy | Experimented_Accuracy | Experimented_Loss | Hardware | Time-Taken |
| ----------- | ----- | ----- | ----- | ----- | ----- | ----- | ----- |
| pFedHN | MNIST | 50 | 99.28 ± 0.11 | 99.26 | 0.0262 | NVIDIA A100-PCIE-40GB, num_gpus=1 | 11245.3352 seconds |
| pFedHN | CIFAR-10 | 50 | 88.38 ± 0.29 | 88.01 | 0.81694 | MacBook Pro M2 Pro, 12 Core CPU | 15227.9250 seconds |
| pFedHN | CIFAR-100 | 50 | 59.48 ± 0.67 | 57.21 | 3.6549 | MacBook Pro M2 Pro, 12 Core CPU | 20808.95235 seconds |
2 changes: 2 additions & 0 deletions baselines/pFedHN/pFedHN/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pylint: disable=invalid-name
"""Template baseline package."""
Loading
Loading