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

Update the codebase for gradient leakage attacks #377

Merged
merged 375 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
375 commits
Select commit Hold shift + click to select a range
d902542
fixed the random seed setting in dlg_server
EthanHugh Aug 1, 2022
576c98c
hardcoded a ground truth plot
EthanHugh Aug 1, 2022
14c3e05
Bug fix branch (#14)
EthanHugh Aug 10, 2022
2f7b49a
GT plot changes (#15)
EthanHugh Aug 11, 2022
2c07959
Merge branch 'main' of https://github.com/TL-System/plato
silviafeiwang Aug 15, 2022
a920671
Update dlg with new changes in plato
silviafeiwang Aug 15, 2022
a0ada45
Added another plot for the final result of the attack that follows th…
EthanHugh Aug 16, 2022
16ce078
Further update dlg with new changes in plato
silviafeiwang Aug 16, 2022
6e73ae0
Fix device and variable bugs
silviafeiwang Aug 16, 2022
ebedb53
Merge branch 'main' of https://github.com/TL-System/plato
silviafeiwang Oct 14, 2022
aac0c96
Refactor DLG trainer
silviafeiwang Oct 14, 2022
40c0ae6
Move files to utils folder and format files
silviafeiwang Oct 14, 2022
48ff068
Remove unnecessary changes to main branch Plato
silviafeiwang Oct 14, 2022
4cf4083
Remove unnecessary changes to main branch Plato
silviafeiwang Oct 14, 2022
fa5cef9
Refactor dlg config files
silviafeiwang Oct 14, 2022
68f75a8
Improve dlg documentation
silviafeiwang Oct 14, 2022
568f22e
Refactor the code using current Plato API
silviafeiwang Oct 15, 2022
d837ad2
Merge branch 'main' into plato-dlg
baochunli Oct 15, 2022
86ee3f2
Added train_step_start() and perform_forward_and_backward_passes() to…
baochunli Oct 15, 2022
12a66bb
Added better function docstrings in the trainer.
baochunli Oct 15, 2022
63eff0d
Fix bugs in trainer's APIs
silviafeiwang Oct 15, 2022
2d35349
Add outputs_generated() to trainer's API
silviafeiwang Oct 15, 2022
ec6b702
Refactor dlg trainer with the latest Plato API
silviafeiwang Oct 16, 2022
0d71665
Rename private functions
silviafeiwang Oct 16, 2022
06fa0cc
Some code cleanup in DLG trainer.
baochunli Oct 16, 2022
287e708
Improved function docstrings.
baochunli Oct 16, 2022
69d7877
Update server.md with new APIs
silviafeiwang Oct 16, 2022
fa1694a
Merge branch 'main' of https://github.com/TL-System/plato
silviafeiwang Sep 14, 2023
dd71cd8
Fix a minor bug referring to config
silviafeiwang Sep 18, 2023
d402f29
Exps on LeNet5 and ResNet18
silviafeiwang Sep 19, 2023
1329fa4
Add more options of reconstruction optimizers
silviafeiwang Sep 26, 2023
911c988
Add lr scheduler, data projection, cost weights
silviafeiwang Sep 26, 2023
9f64426
Project dummy data to corresponding dataset
silviafeiwang Sep 26, 2023
0de5e14
Use csDLG's ResNet and their weights initialization
silviafeiwang Sep 26, 2023
f7a7740
Fix minor bugs
silviafeiwang Sep 28, 2023
8bf5eeb
Update pip requirement
silviafeiwang Sep 28, 2023
d2b4807
Fis minor bugs
silviafeiwang Sep 28, 2023
98cd26f
Fix minor bugs
silviafeiwang Sep 28, 2023
a8bf196
Merge branch 'main' of https://github.com/iQua/plato-dlg
silviafeiwang Sep 29, 2023
a6a961f
Minor changes
silviafeiwang Sep 29, 2023
1bc63d8
Merge branch 'main' of github.com:iQua/plato-dlg into main
silviafeiwang Sep 29, 2023
8183b99
Refactor image plotting with data normalization
silviafeiwang Sep 29, 2023
dcef5b7
Refactor metrics, adding feature MSE
silviafeiwang Sep 29, 2023
8fae227
Correct PSNR computation
silviafeiwang Sep 29, 2023
9e3b108
Refactor image plotting
silviafeiwang Sep 29, 2023
5b00d03
Use original loss function criterion
silviafeiwang Sep 29, 2023
0721449
Add more options to initialize dummy data
silviafeiwang Sep 29, 2023
a1cb844
Update test configs
silviafeiwang Sep 29, 2023
8650cb2
Add all 0.5 tensor as an option to initialize dummy data
silviafeiwang Oct 1, 2023
ea3e450
Use csDLG's way to set random seed for the model
silviafeiwang Oct 1, 2023
88ff8b3
Test rec in .eval() mode
silviafeiwang Oct 1, 2023
b4318b5
Remove unnecessary files
silviafeiwang Oct 7, 2023
4b1a4dd
Port LAMP into dlg_nlp
silviafeiwang Oct 7, 2023
a1c58b2
Fine-tune Llama2 with LoRA on Q&A data
silviafeiwang Oct 12, 2023
d24e15b
Refactor faq.py
silviafeiwang Oct 13, 2023
c9852f7
Add new configs for setting model mode
silviafeiwang Nov 3, 2023
8040874
Remove dlg_nlp/
silviafeiwang Nov 3, 2023
5b073b2
Move model mode setting to the correct location
silviafeiwang Nov 3, 2023
967200e
Correct tensor devices
silviafeiwang Nov 4, 2023
2636b8a
Set torch.no_grad() when evaluating dummy data
silviafeiwang Nov 9, 2023
3e0f4eb
Save parameters only in target deltas
silviafeiwang Nov 10, 2023
8d20fc1
Save parameters only in target weights
silviafeiwang Nov 10, 2023
5197398
Update consts
silviafeiwang Nov 14, 2023
5d577a6
Update .yml and .sh
silviafeiwang Nov 14, 2023
c93c1af
Resume plato's resnet and vgg
silviafeiwang Nov 14, 2023
8779cc4
Rename dlg to gradient_leakage_attacks
silviafeiwang Nov 14, 2023
458d2b5
Merge branch 'main' of https://github.com/TL-System/plato
silviafeiwang Nov 14, 2023
ec18d53
Remove unnecessary files
silviafeiwang Nov 14, 2023
31c6aae
Connect dlg with customized models including LeNet and ResNet
silviafeiwang Nov 14, 2023
de40803
Reformat files
silviafeiwang Nov 14, 2023
2ae4da3
Update README.md
silviafeiwang Nov 14, 2023
5c4a9f6
Update README.md
silviafeiwang Nov 14, 2023
b5a668b
Init deep leakage attack in plato
silviafeiwang May 22, 2022
4df2ff3
Start implementing DLG server
silviafeiwang May 22, 2022
06f1ef9
Further implement gradient matching
silviafeiwang May 24, 2022
fcde933
Replace hard-coded sizes with actual ones from datasets
silviafeiwang May 24, 2022
6e68331
Update config files for testing
silviafeiwang May 24, 2022
22c0be6
Correct data size and plot dummy data
silviafeiwang May 24, 2022
a27a42f
Move dlg related configs to .yml
silviafeiwang May 24, 2022
870895e
Support clients directly uploading gradients
silviafeiwang May 24, 2022
90415e2
Correct dummy label size and refactor code
silviafeiwang May 25, 2022
d3e6be5
Replace hard-coded dummy label size with num of classes
silviafeiwang May 25, 2022
a2d88ce
Add the simpler NN architecture used in DLG paper
silviafeiwang May 25, 2022
c27b407
Let client send ground truth to server for evaluation
silviafeiwang May 26, 2022
f91e7b3
Add MSE metric for evaluation
silviafeiwang May 26, 2022
37c2340
Add LPIPS metric for evaluation
silviafeiwang May 26, 2022
52c5ceb
Support customized NN initialization used in DLG paper
silviafeiwang May 27, 2022
c586bb6
Clean up the code
silviafeiwang May 27, 2022
35b5c53
Make the model compatible with both EMNIST and CIFAR10
silviafeiwang May 27, 2022
262deb0
Enable sharing gradients or weights
silviafeiwang May 31, 2022
b82a2d8
Try fixing data samples in every epochs (doesn't work)
silviafeiwang May 31, 2022
33bbae9
Implement weight matching (debugging)
silviafeiwang Jun 1, 2022
0a6a70b
Correct dummy weights updating (debugging)
silviafeiwang Jun 1, 2022
76417cc
Correct the model that dummy weights are derived
silviafeiwang Jun 1, 2022
1ba660e
Update references and comments
silviafeiwang Jun 1, 2022
8ea6c9f
Init plato-dlg README.md
silviafeiwang Jun 1, 2022
330e52b
Fix typos in README.md
silviafeiwang Jun 1, 2022
8db5ef2
Use cross entropy loss when matching weights
silviafeiwang Jun 2, 2022
279cce7
Hard-coded samples used in multiple epochs (needs to be fixed)
silviafeiwang Jun 2, 2022
c0f3027
Update dlg_trainer: remove differential privacy part
silviafeiwang Jun 3, 2022
3c09c4a
Recover the train loader
silviafeiwang Jun 5, 2022
447165f
Improved the ground truth plots to show the entire batch
Jun 6, 2022
bdd6da7
modified the final plot to contain all dummy data samples for each lo…
Jun 6, 2022
7a56f30
changed all tensor sizes to depend on the partition_size to allow the…
Jun 6, 2022
e9dffed
modified the dlg_trainer to send the full ground truth data and label…
Jun 6, 2022
ebfccbc
removed the duplicate plato folder that was done locally
Jun 6, 2022
6cc1024
Modify the loss steps
silviafeiwang Jun 6, 2022
2b0e7a6
Update dlg_trainer to the latest plato version
silviafeiwang Jun 6, 2022
4332e99
Move dlg_model from plato/ to examples/
silviafeiwang Jun 6, 2022
e4da588
Migrate MetaMonkey and loss steps to plato
silviafeiwang Jun 6, 2022
f32bceb
Pass model instance in the starting program instead
silviafeiwang Jun 6, 2022
dcbd710
Refactor the code
silviafeiwang Jun 7, 2022
a73f523
Modify the logging file names
silviafeiwang Jun 8, 2022
1fe2f97
The client gradients are now only sent once after all local epochs ar…
Jun 8, 2022
ae91c26
added an error check when averaging out the target_grad to see if sha…
Jun 8, 2022
3114aae
Added a plot script that loads the tensors stored in tensor.pt and cr…
Jun 9, 2022
06210d0
Updated the README file to hold instructions on how to use plot.py
Jun 9, 2022
e102403
Implement various cost functions
silviafeiwang Jun 9, 2022
4907d34
Reformat the code
silviafeiwang Jun 9, 2022
6367925
Implement weight updates matching
silviafeiwang Jun 9, 2022
7e20440
Fix the bug of passing customized model
silviafeiwang Jun 10, 2022
c5c0a29
Update the codebase to the public main
silviafeiwang Jun 12, 2022
87b9abf
Migrate GradDefense's gradient perturbation
silviafeiwang Jun 10, 2022
0b3a77b
Test GradDefense's gradient perturbation
silviafeiwang Jun 13, 2022
7cd6496
Port GradDefense's gradient compensation component
silviafeiwang Jun 13, 2022
7004f46
Port GradDefense's clip component
silviafeiwang Jun 13, 2022
f8e888a
Reformat CIFAR10 data
silviafeiwang Jun 13, 2022
ce219d8
Fix a bug when using patchedmodule
silviafeiwang Jun 13, 2022
c459f4d
Evaluations branch (#8)
EthanHugh Jun 14, 2022
c70d8a2
Fix the bug of updating model when sharing gradients
silviafeiwang Jun 15, 2022
120bc19
Replace optimizer with custom gradient descent
silviafeiwang Jun 15, 2022
58d95d3
Should not divide gradients by total steps of local updates
silviafeiwang Jun 15, 2022
5cc65b6
Put back gradient averaging on both sides
silviafeiwang Jun 17, 2022
358163f
Currently only consider learning rate in optimizer
silviafeiwang Jun 17, 2022
060fec9
Multiple trials branch (#9)
EthanHugh Jun 21, 2022
e1972bf
Avoid redundancy when storing GT data
silviafeiwang Jul 1, 2022
8863df3
Soteria branch (#11)
EthanHugh Jul 7, 2022
329362d
Override `server_test` for custom model output
silviafeiwang Jul 7, 2022
6ed5cbb
added try catch statement for model outputs just in case we add other…
EthanHugh Jul 7, 2022
31f229c
applied the defense method directly after finding the gradients, so t…
EthanHugh Jul 11, 2022
cdcd438
Remove comments and unnecessary code
silviafeiwang Jul 12, 2022
b786e74
Refactor dlg_trainer
silviafeiwang Jul 12, 2022
11b8ca7
Implement model compression defense and refactor the code
silviafeiwang Jul 12, 2022
ec9e695
Implement deferential privacy defense
silviafeiwang Jul 12, 2022
552f0b9
Refactor the code
silviafeiwang Jul 15, 2022
3953e89
Add sample config files for convergence tests
silviafeiwang Jul 15, 2022
7175e60
Upload an example sbatch .sh
silviafeiwang Jul 15, 2022
eb6dce0
Update config
silviafeiwang Jul 15, 2022
8f0df99
Comment out img plot and output
silviafeiwang Jul 16, 2022
8c97196
Use the same pruning threshold in Soteria's paper
silviafeiwang Jul 16, 2022
7680741
Update sbatch .sh sample
silviafeiwang Jul 16, 2022
9a8800e
Fix a bug in GradDefense tensor type
silviafeiwang Jul 16, 2022
3c368da
Move self.model param update to every local step
silviafeiwang Jul 17, 2022
6751fdc
Fix a bug in GradDefense tensor type
silviafeiwang Jul 17, 2022
3ec3bb0
Update .yml
silviafeiwang Jul 17, 2022
deb32d7
Fix a bug when using GradDefense clip for larger batch size
silviafeiwang Jul 18, 2022
e7b03ab
Fix a op type bug from GradDefense clip
silviafeiwang Jul 20, 2022
7318960
Remove the use of PatchedModule in dlg_trainer
silviafeiwang Jul 20, 2022
ae56b78
Move random seed setting later in dlg server
silviafeiwang Jul 24, 2022
ebf040b
Pluto (#13)
silviafeiwang Jul 27, 2022
e9b0057
Try applying defense on delta insead in dlg_trainer_delta.py
silviafeiwang Jul 27, 2022
a14ac6c
Rename our defense as "Outpost"
silviafeiwang Jul 27, 2022
755eb38
Modify Outpost with FIM-based noise jittering
silviafeiwang Jul 31, 2022
b3e81b9
Fix a tensor type bug
silviafeiwang Jul 31, 2022
fe72fdd
Adjust variance and FIM
silviafeiwang Jul 31, 2022
4d3b9b3
GT plot changes (#15)
EthanHugh Aug 11, 2022
feffe01
Update dlg with new changes in plato
silviafeiwang Aug 15, 2022
8106682
Further update dlg with new changes in plato
silviafeiwang Aug 16, 2022
a783466
Fix device and variable bugs
silviafeiwang Aug 16, 2022
b51f8b4
Refactor DLG trainer
silviafeiwang Oct 14, 2022
6354e20
Move files to utils folder and format files
silviafeiwang Oct 14, 2022
7cc954e
Remove unnecessary changes to main branch Plato
silviafeiwang Oct 14, 2022
c462425
Remove unnecessary changes to main branch Plato
silviafeiwang Oct 14, 2022
7dcc2f4
Refactor dlg config files
silviafeiwang Oct 14, 2022
c3fad01
Improve dlg documentation
silviafeiwang Oct 14, 2022
925243d
Refactor the code using current Plato API
silviafeiwang Oct 15, 2022
f4281c9
Fix bugs in trainer's APIs
silviafeiwang Oct 15, 2022
3c876b8
Add outputs_generated() to trainer's API
silviafeiwang Oct 15, 2022
6f7c26a
Refactor dlg trainer with the latest Plato API
silviafeiwang Oct 16, 2022
73347c7
Rename private functions
silviafeiwang Oct 16, 2022
2dd13f6
Some code cleanup in DLG trainer.
baochunli Oct 16, 2022
2b51112
Improved function docstrings.
baochunli Oct 16, 2022
8e77472
Update server.md with new APIs
silviafeiwang Oct 16, 2022
4d32f1a
Exps on LeNet5 and ResNet18
silviafeiwang Sep 19, 2023
08a8570
Add more options of reconstruction optimizers
silviafeiwang Sep 26, 2023
145cc4e
Add lr scheduler, data projection, cost weights
silviafeiwang Sep 26, 2023
bda49d3
Project dummy data to corresponding dataset
silviafeiwang Sep 26, 2023
1297319
Use csDLG's ResNet and their weights initialization
silviafeiwang Sep 26, 2023
1fc7067
Fix minor bugs
silviafeiwang Sep 28, 2023
a1bb554
Fix minor bugs
silviafeiwang Sep 28, 2023
3cf051f
Refactor metrics, adding feature MSE
silviafeiwang Sep 29, 2023
6b33a82
Correct PSNR computation
silviafeiwang Sep 29, 2023
b109c1b
Update test configs
silviafeiwang Sep 29, 2023
ce37002
Add all 0.5 tensor as an option to initialize dummy data
silviafeiwang Oct 1, 2023
106842e
Use csDLG's way to set random seed for the model
silviafeiwang Oct 1, 2023
8519207
Remove unnecessary files
silviafeiwang Oct 7, 2023
12ff247
Port LAMP into dlg_nlp
silviafeiwang Oct 7, 2023
f095c15
Fine-tune Llama2 with LoRA on Q&A data
silviafeiwang Oct 12, 2023
ce2dfe2
Refactor faq.py
silviafeiwang Oct 13, 2023
8d5c0d3
Add new configs for setting model mode
silviafeiwang Nov 3, 2023
409db1f
Remove dlg_nlp/
silviafeiwang Nov 3, 2023
e034e01
Move model mode setting to the correct location
silviafeiwang Nov 3, 2023
7798e92
Correct tensor devices
silviafeiwang Nov 4, 2023
130daec
Set torch.no_grad() when evaluating dummy data
silviafeiwang Nov 9, 2023
1098023
Update consts
silviafeiwang Nov 14, 2023
76f57bf
Update .yml and .sh
silviafeiwang Nov 14, 2023
febfe9e
Resume plato's resnet and vgg
silviafeiwang Nov 14, 2023
83de1d9
Rename dlg to gradient_leakage_attacks
silviafeiwang Nov 14, 2023
e9041a0
Remove unnecessary files
silviafeiwang Nov 14, 2023
a08e210
Connect dlg with customized models including LeNet and ResNet
silviafeiwang Nov 14, 2023
24c26ab
Reformat files
silviafeiwang Nov 14, 2023
288b3c2
Update README.md
silviafeiwang Nov 14, 2023
182658f
Update README.md
silviafeiwang Nov 14, 2023
4b7a0b3
Delete unnecessary files
silviafeiwang Nov 14, 2023
0f69a3a
Merge branch 'gradient-leakage-attack' of https://github.com/TL-Syste…
silviafeiwang Nov 14, 2023
94e1bd8
Remove unnecessary changes
silviafeiwang Nov 15, 2023
0a902d0
Correct the optimization loop in delta matching for multiple batches
silviafeiwang Nov 15, 2023
ebff843
Added libraries required by gradient leakage attacks into requirement…
silviafeiwang Nov 15, 2023
40382d6
Merge branch 'main' into gradient-leakage-attack
silviafeiwang Nov 15, 2023
6551eb7
FFixed a bug in early exit when loss becomes NaN
silviafeiwang Nov 17, 2023
69924b3
Implemented feature returning in another forward function.
silviafeiwang Nov 17, 2023
f413f4a
Added a entry program for pretraining with dlg's models.
silviafeiwang Nov 17, 2023
6ec5b3e
Merge branch 'gradient-leakage-attack' of https://github.com/TL-Syste…
silviafeiwang Sep 18, 2024
bba09a5
Merge branch 'main' into gradient-leakage-attack
silviafeiwang Sep 18, 2024
eaa2273
Implemented Geiping's fishing attack (optimization-based class attack).
silviafeiwang Nov 20, 2023
7ea984a
Updated fishing attack configs.
silviafeiwang Nov 21, 2023
cdf0b72
Updated fishing attack configs.
silviafeiwang Nov 21, 2023
5dcf0f2
Implemented binary attack under class collision
silviafeiwang Nov 23, 2023
3c6d98b
Fixed a bug when modifying model weights in server payload.
silviafeiwang Nov 23, 2023
e0e78e2
Loaded all model states besides parameters in model modification.
silviafeiwang Nov 23, 2023
0ef38a6
Correct the start round for attacking a pretrained model
silviafeiwang Nov 23, 2023
a06df34
Plot images of target class in fishing attack
silviafeiwang Nov 27, 2023
afa8728
rPlot the GT of target images one by one
silviafeiwang Nov 27, 2023
792e3c0
Removed the gap between subplots.
silviafeiwang Nov 29, 2023
1d3cd3f
Fixed a bug of incompatible devices.
silviafeiwang Nov 29, 2023
65164ad
Incorporated delta update matching in fishing binary attack.
silviafeiwang Nov 29, 2023
d21bf13
Fixed bugs of incompatible devices
silviafeiwang Dec 4, 2023
1021b7e
Added a random seed arg in .yml for Outpost perturbation.
silviafeiwang Dec 4, 2023
65a8cc1
Incorporated ViT in the models.
silviafeiwang Mar 2, 2024
dc39f48
Incorporated ViT into DLG.
silviafeiwang Mar 4, 2024
5f9e730
Updated configs
silviafeiwang Mar 6, 2024
88060d4
Updated config files
silviafeiwang Mar 6, 2024
cf3fa12
Updated config files.
silviafeiwang Mar 11, 2024
3bb6656
Merged config files.
silviafeiwang Mar 12, 2024
a693128
Fixed a minor bug in dlg server.
silviafeiwang Sep 18, 2024
4e5f684
Added requirement.txt used for dlg.
silviafeiwang Sep 18, 2024
9193b29
Updated config files.
silviafeiwang Sep 18, 2024
a6ae1da
Update README.md
silviafeiwang Sep 18, 2024
7a01ef7
Format the code with Black.
silviafeiwang Sep 18, 2024
b944697
Refactored all the code regarding PyLint.
silviafeiwang Sep 18, 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
9 changes: 4 additions & 5 deletions examples/gradient_leakage_attacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Research on gradient leakage attack and defense using the Plato framework.
- {GC} gradient compression (referred to Soteria's code)
---

# Running Plato with DLG attack
## Run FL with DLG attack
Run the program using the following cmd under the directory `examples/gradient_leakage_attacks` for example, which launches the csDLG attack using delta matching against an untrained ResNet18 model in `.eval()` mode on CIFAR100 data:
```
python dlg.py -c untrained_eval_delta.yml
Expand Down Expand Up @@ -87,6 +87,9 @@ Try tuning the following hyperparameters in `.yml` configuration files.
- (for Outpost) `prune_base: 80` — pruning percentage
- (for Outpost) `noise_base: 0.8` — scale for gaussian noise

### under `trainer`

- `model_type: vit` — needed when using ViTs as the model; specify the ViT model name such as `vit-base-patch16-224-in21k` to `model_name` at the same time

### under `results`

Expand All @@ -95,9 +98,5 @@ Try tuning the following hyperparameters in `.yml` configuration files.
- `trial: [int]` — the trial number to be plotted. Plots the best reconstruction based off MSE if not specified

- `cols/rows: [int]` — specify the number of columns or rows in the plotted figure

## Plot Instructions

Run ```python plot.py -c config_file``` where ```config_file``` is the same one used to run the DLG attack


93 changes: 0 additions & 93 deletions examples/gradient_leakage_attacks/defense/GradDefense/clip.py

This file was deleted.

74 changes: 34 additions & 40 deletions examples/gradient_leakage_attacks/defense/GradDefense/compensate.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
"""
Gradient compensating of GradDefense

Reference:
Wang et al., "Protect Privacy from Gradient Leakage Attack in Federated Learning," INFOCOM 2022.
https://github.com/wangjunxiao/GradDefense
"""

import math

import numpy as np
import torch


def getFactor(num):
def get_factor(num):
"""Calculate factors."""
factors = []
for_times = int(math.sqrt(num))
for i in range(for_times + 1)[1:]:
Expand All @@ -16,10 +25,11 @@ def getFactor(num):
return factors


def getMatrixSize(total_params_num: int, Q: float):
gradients_matrix_v = math.sqrt(Q * total_params_num)
def get_matrix_size(total_params_num: int, q: float):
"""Calculate matrix size."""
gradients_matrix_v = math.sqrt(q * total_params_num)

for factor in sorted(getFactor(total_params_num)):
for factor in sorted(get_factor(total_params_num)):
if factor >= gradients_matrix_v:
gradients_matrix_v = factor
break
Expand All @@ -30,84 +40,71 @@ def getMatrixSize(total_params_num: int, Q: float):
assert isinstance(gradients_matrix_w, int)
assert gradients_matrix_v * gradients_matrix_w == total_params_num

real_Q = gradients_matrix_v / gradients_matrix_w
real_q = gradients_matrix_v / gradients_matrix_w

return gradients_matrix_v, gradients_matrix_w, real_Q
return gradients_matrix_v, gradients_matrix_w, real_q


def getCovarianceMatrix(matrix):
def get_covariance_matrix(matrix):
"""Calculate covariance matrix."""
return np.cov(matrix, rowvar=0)


def denoise(gradients: list, scale: float, Q: float):
def denoise(gradients: list, scale: float, q: float):
"""Denoise gradients."""
layer_dims_pool = []
for layer in gradients:
layer_dims = list((_ for _ in layer.shape))
layer_dims_pool.append(layer_dims)

# print(layer_dims_pool)

layer_params_num_pool = []
for layer_dims in layer_dims_pool:
layer_params_num = 1
for dim in layer_dims:
layer_params_num *= dim
layer_params_num_pool.append(layer_params_num)

# print(layer_params_num_pool)

total_params_num = 0
for layer_params_num in layer_params_num_pool:
total_params_num += layer_params_num

# print(total_params_num)

gradients_matrix_v, gradients_matrix_w, real_Q = getMatrixSize(
total_params_num=total_params_num, Q=Q
gradients_matrix_v, gradients_matrix_w, real_q = get_matrix_size(
total_params_num=total_params_num, q=q
)

# print (gradients_matrix_v, gradients_matrix_w, real_Q)

# Flatten gradients
gradients_flatten = []
for layer in gradients:
layer_flatten = (torch.flatten(layer)).cpu().numpy()
gradients_flatten.extend(layer_flatten)

# print(len(gradients_flatten))

matrix_C = np.array(gradients_flatten).reshape(
matrix_c = np.array(gradients_flatten).reshape(
gradients_matrix_v, gradients_matrix_w
)
covmatrix_CTC = getCovarianceMatrix(matrix_C)

# print(covmatrix_CTC.shape)
covmatrix_ctc = get_covariance_matrix(matrix_c)

lamda_min = ((1 - 1 / math.sqrt(real_Q)) ** 2) * (scale**2)
lamda_max = ((1 + 1 / math.sqrt(real_Q)) ** 2) * (scale**2)
lamda_min = ((1 - 1 / math.sqrt(real_q)) ** 2) * (scale**2)
lamda_max = ((1 + 1 / math.sqrt(real_q)) ** 2) * (scale**2)

eigen_vals, eigen_vecs = np.linalg.eig(covmatrix_CTC)
eigen_vals, eigen_vecs = np.linalg.eig(covmatrix_ctc)

n_index = []
for index in range(len(eigen_vals)):
if eigen_vals[index] <= lamda_min or eigen_vals[index] >= lamda_max:
for index, eigen_val in enumerate(eigen_vals):
if eigen_val <= lamda_min or eigen_val >= lamda_max:
n_index.append(index)
n_eigen_vecs = eigen_vecs[:, n_index]

lowData = np.dot(matrix_C, n_eigen_vecs)
highData = np.dot(lowData, n_eigen_vecs.T)

# print(n_eigen_vecs.shape)
# print(lowData.shape)
# print(highData.shape)
low_data = np.dot(matrix_c, n_eigen_vecs)
high_data = np.dot(low_data, n_eigen_vecs.T)

compensated_gradients_flatten = highData.flatten()
compensated_gradients_flatten = high_data.flatten()

gradients_compensated = []
params_start_indice = 0
params_end_indice = 0

# Recover gradients
for layer_index in range(len(layer_dims_pool)):
for layer_index, _ in enumerate(layer_dims_pool):
if layer_index == 0:
params_start_indice = 0
params_end_indice = layer_params_num_pool[layer_index] - 1
Expand All @@ -117,9 +114,6 @@ def denoise(gradients: list, scale: float, Q: float):
params_start_indice + layer_params_num_pool[layer_index] - 1
)

# print(params_start_indice, params_end_indice)
# print(layer_dims_pool[layer_index])

layer_gradient = (
torch.from_numpy(
np.array(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
"""
Dataloader of GradDefense

Reference:
Wang et al., "Protect Privacy from Gradient Leakage Attack in Federated Learning," INFOCOM 2022.
https://github.com/wangjunxiao/GradDefense
"""

import numpy as np
import torch
from torch.utils.data import Subset
from torch.utils.data.dataloader import DataLoader
from torch.utils.data.dataset import Dataset

DEFAULT_NUM_WORKERS = 8
rootset_per_class = 5
rootset_size = 50

# TODO: total_num_samples is not used
ROOTSET_PER_CLASS = 5
ROOTSET_SIZE = 50


def extract_root_set(
dataset: Dataset,
sample_per_class: int = rootset_per_class,
total_num_samples: int = rootset_size,
sample_per_class: int = ROOTSET_PER_CLASS,
seed: int = None,
):
"""Extract root dataset."""
num_classes = len(dataset.classes)
class2sample = {i: [] for i in range(num_classes)}
select_indices = []
if seed == None:
if seed is None:
index_pool = range(len(dataset))
else:
index_pool = np.random.RandomState(seed=seed).permutation(len(dataset))
Expand All @@ -35,6 +40,7 @@ def extract_root_set(


def get_root_set_loader(trainset):
"""Obtain root dataset loader."""
rootset_indices, __ = extract_root_set(trainset)
root_set = Subset(trainset, rootset_indices)
root_dataloader = DataLoader(
Expand Down
Loading
Loading