-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathargs.ts
More file actions
190 lines (160 loc) · 6.63 KB
/
args.ts
File metadata and controls
190 lines (160 loc) · 6.63 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { parse } from 'ts-command-line-args'
import { Map, Set } from 'immutable'
import type { DataType, Network, TaskProvider } from "@epfml/discojs";
import { defaultTasks } from '@epfml/discojs'
type AggregationStrategy = "mean" | "byzantine" | "secure";
function parseAggregator(raw: string): AggregationStrategy{
if (raw === "mean" || raw == "byzantine" || raw == "secure")
return raw;
else
throw new Error(`Aggregator ${raw} is not supported.`);
}
export interface BenchmarkArguments {
provider: TaskProvider<DataType, Network>;
testID: string
numberOfUsers: number
epochs: number
roundDuration: number
batchSize: number
validationSplit: number
// DP
epsilon?: number
delta?: number
dpDefaultClippingRadius?: number
// Aggregator
aggregator: AggregationStrategy
// Byzantine aggregator
clippingRadius?: number
maxIterations?: number
beta?: number
// Secure aggregator
maxShareValue?: number
save: boolean
host: URL
}
type BenchmarkUnsafeArguments = Omit<BenchmarkArguments, 'provider'> & {
task: string
help?: boolean
}
const argExample = 'e.g. npm start -- -u 2 -e 3 # runs 2 users for 3 epochs'
const unsafeArgs = parse<BenchmarkUnsafeArguments>(
{
testID: { type: String, alias: 'i', description: 'ID of the testcase' },
task: { type: String, alias: 't', description: 'Task: tinder_dog, titanic, simple_face, cifar10 or lus_covid', defaultValue: 'tinder_dog' },
numberOfUsers: { type: Number, alias: 'u', description: 'Number of users', defaultValue: 2 },
epochs: { type: Number, alias: 'e', description: 'Number of epochs', defaultValue: 10 },
roundDuration: { type: Number, alias: 'r', description: 'Round duration (in epochs)', defaultValue: 2 },
batchSize: { type: Number, alias: 'b', description: 'Training batch size', defaultValue: 10 },
validationSplit : { type: Number, alias: 'v', description: 'Validation dataset ratio', defaultValue: 0.2 },
save: { type: Boolean, alias: 's', description: 'Save logs of benchmark', defaultValue: false },
host: {
type: (raw: string) => new URL(raw),
typeLabel: "URL",
description: "Host to connect to",
defaultValue: new URL("http://localhost:8080"),
},
// Aggregator
aggregator: { type: parseAggregator, description: 'Type of weight aggregator', defaultValue: 'mean' },
// Byzantine aggregator
clippingRadius: { type: Number, description: "Clipping radius for centered clipping", optional: true },
maxIterations: { type: Number, description: "Maximum centered clipping iterations", optional: true },
beta: { type: Number, description: "Momentum coefficient to smooth the aggregation over multiple rounds", optional: true },
// Secure aggregator
maxShareValue: { type: Number, description: "Maximum absolute value over all the weights", optional: true },
// Differential Privacy
epsilon: { type: Number, description: 'Privacy budget', optional: true, defaultValue: undefined},
delta: { type: Number, description: 'Probability of failure, slack parameter', optional: true, defaultValue: undefined},
dpDefaultClippingRadius: {type: Number, description: 'Default clipping radius for DP', optional: true, defaultValue: undefined},
help: { type: Boolean, optional: true, alias: 'h', description: 'Prints this usage guide' }
},
{
helpArg: 'help',
headerContentSections: [{ header: 'DISCO CLI', content: 'npm start -- [Options]\n' + argExample }]
}
)
const supportedTasks = Map(
await Promise.all(
Set.of<TaskProvider<"image" | "tabular", Network>>(
defaultTasks.cifar10,
defaultTasks.lusCovid,
defaultTasks.simpleFace,
defaultTasks.titanic,
defaultTasks.tinderDog,
defaultTasks.mnist,
).map(
async (t) =>
[(await t.getTask()).id, t] as [
string,
TaskProvider<"image" | "tabular", Network>,
],
),
),
);
const provider = supportedTasks.get(unsafeArgs.task);
if (provider === undefined) {
throw Error(`${unsafeArgs.task} not implemented.`)
}
export const args: BenchmarkArguments = {
...unsafeArgs,
provider: {
async getTask() {
const task = await provider.getTask();
// Override training information
task.trainingInformation.batchSize = unsafeArgs.batchSize;
task.trainingInformation.roundDuration = unsafeArgs.roundDuration;
task.trainingInformation.epochs = unsafeArgs.epochs;
task.trainingInformation.validationSplit = unsafeArgs.validationSplit;
const {aggregator, clippingRadius, maxIterations, beta, maxShareValue} = unsafeArgs;
// For aggregators
if (aggregator !== undefined)
task.trainingInformation.aggregationStrategy = aggregator;
// For byzantine aggregator
if (
clippingRadius !== undefined &&
maxIterations !== undefined &&
beta !== undefined
){
if (task.trainingInformation.scheme === "local")
throw new Error("Byzantine aggregator is not supported for local training");
if (task.trainingInformation.aggregationStrategy !== "byzantine")
throw new Error("Byzantine parameters can be set only when aggregationStrategy is byzantine");
task.trainingInformation.privacy = {
...task.trainingInformation.privacy,
byzantineFaultTolerance: {
clippingRadius,
maxIterations,
beta,
},
};
}
// For secure aggregator
if (maxShareValue !== undefined){
if (task.trainingInformation.scheme !== "decentralized")
throw new Error("Secure aggation is only supported for decentralized laerning")
if (task.trainingInformation.aggregationStrategy !== "secure")
throw new Error("maxShareValue can be set when aggregationStrategy is secure");
task.trainingInformation.maxShareValue = maxShareValue;
}
// For DP
const {dpDefaultClippingRadius, epsilon, delta} = unsafeArgs;
if (
// dpDefaultClippingRadius !== undefined &&
epsilon !== undefined &&
delta !== undefined
){
if (task.trainingInformation.scheme === "local")
throw new Error("Can't have differential privacy for local training");
const defaultRadius = dpDefaultClippingRadius ? dpDefaultClippingRadius : 1;
// for the case where privacy parameters are not defined in the default tasks
task.trainingInformation.privacy ??= {};
task.trainingInformation.privacy.differentialPrivacy = {
clippingRadius: defaultRadius,
epsilon: epsilon,
delta: delta,
};
}
return task;
},
getModel: () => provider.getModel(),
},
};