-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
163 lines (139 loc) · 5.19 KB
/
client.py
File metadata and controls
163 lines (139 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from copy import deepcopy
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Subset, DataLoader
from typing import Any, Tuple
from utils.evaluation import FederatedMetrics
class Client(object):
'''
This class simulates a client with its own dataset who represents a terminal user,
furthermore it can be either a training client, validation client or testing client.
'''
def __init__(
self,
args: Any,
name: str,
dataset: Subset,
device: torch.device,
validator: bool = False,
):
'''
Initializes a user client.
Parameters
----------
args: Any
Command line arguments of the simulation
name: str
Identifier name of the client
dataset: Subset
Local dataset used by the client for training or validation or testing
device: torch.device
Device, cuda gpu or cpu, on which computation is performed
validator: bool
Tells if it is a validator (False by default)
'''
self.args = args
self.name = name
self.dataset = dataset
self.validator = validator
self.device = device
self.loader = DataLoader(dataset, batch_size = args.batch_size)
def train(self, algorithm: Any, model: nn.Module) -> Any:
'''
Runs federated `algorithm` for training to optimize central server `model`
on local dataset.
Parameters
----------
algorithm: Any
Algorithm to be run on client at each round, see `FedAlgorithm` descendants
model: nn.Module
Model initialized with central server parameters
at the beginning of the round
Returns
-------
Any
State update collected by central server algorithm
'''
# fails if training is invoked on a validator or tester
assert not self.validator
# local training handled by appropriate algorithm and
# then updated weights are returned
return algorithm.visit(client = self, model = model)
def validate(self, model: nn.Module, metrics: FederatedMetrics):
'''
Evaluates central model performance on local client dataset and updates metrics.
Notes
-----
The local dataset, limited in size, is fed in one pass to the
model to speed up parallelization.
Parameters
----------
model: nn.Module
Model initialized with central server parameters
at the beginning of the round
metrics: FederatedMetrics
Metrics of the central server which are updated
with evaluation results
'''
# note that torch.no_grad is invoked a priori by the server
# so no need here
loss, logits, y = self.evaluate(model)
# performance metrics are updated with outputs and targets
metrics.update(logits, y, loss)
def evaluate(self, model: nn.Module) -> Tuple[float, torch.Tensor, torch.Tensor]:
'''
Evaluates central model performance on local client dataset.
Notes
-----
The local dataset, limited in size, is fed in one pass to the
model to speed up parallelization.
Parameters
----------
model: nn.Module
Model initialized with central server parameters
at the beginning of the round
Returns
-------
Tuple[float, torch.Tensor, torch.Tensor, torch.Tensor, float]
Tuple with loss, logits, y, predictions and accuracy
'''
loader = DataLoader(self.dataset, batch_size = len(self.dataset))
x, y = next(iter(loader))
x = x.to(self.device)
y = y.to(self.device)
# validation mode
model.eval()
# note that torch.no_grad is invoked a priori by the server so no need here
logits, _, loss, _ = model.evaluate(x, y)
## FIXME checks
# assert torch.isfinite(logits).all() and np.isfinite(loss)
# yields outputs
return loss, logits, y
def construct(user_datasets: dict[str, list[tuple[str, Subset]]], device: torch.device, args: Any) -> dict[str, list[Client]]:
'''
Constructs groups of clients from the groups of datasets.
Parameters
----------
user_datasets: dict[str, list[tuple[str, Subset]]]
Dictionary of clients' datasets, with entries such as `training`,
`validation` or `testing`
device: torch.device
Device used by clients for the simulation
args: Any
Command line arguments of the simulation
Returns
-------
dict[str, list[Client]]
Dictionary with groups of clients, like `training`,
`validation` or `testing` clients
'''
return {
group: [
# client is set as validator when he is not assigned to 'training' group
Client(name = name, dataset = data, validator = (group != 'training'), device = device, args = args)
for name, data in datasets
]
# group can either be 'training','validation' or 'testing'
for group, datasets in user_datasets.items()
}