This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsubmit.py
398 lines (326 loc) · 13.4 KB
/
submit.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""Federated Learning Cross-Silo basic pipeline.
This script:
1) reads a config file in yaml specifying the number of silos and their parameters,
2) reads the components from a given folder,
3) builds a flexible pipeline depending on the config,
4) configures each step of this pipeline to read/write from the right silo.
"""
import os
import argparse
import random
import string
import datetime
import webbrowser
import time
import json
import sys
# Azure ML sdk v2 imports
import azure
from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential
from azure.ai.ml import MLClient, Input, Output
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml.dsl import pipeline
from azure.ai.ml import load_component
# to handle yaml config easily
from omegaconf import OmegaConf
############################
### CONFIGURE THE SCRIPT ###
############################
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--config",
type=str,
required=False,
default=os.path.join(os.path.dirname(__file__), "config.yaml"),
help="path to a config yaml file",
)
parser.add_argument(
"--offline",
default=False,
action="store_true",
help="Sets flag to not submit the experiment to AzureML",
)
parser.add_argument(
"--example",
required=False,
choices=["MNIST", "HELLOWORLD"],
default="MNIST",
help="dataset name",
)
parser.add_argument(
"--subscription_id",
type=str,
required=False,
help="Subscription ID",
)
parser.add_argument(
"--resource_group",
type=str,
required=False,
help="Resource group name",
)
parser.add_argument(
"--workspace_name",
type=str,
required=False,
help="Workspace name",
)
parser.add_argument(
"--wait",
default=False,
action="store_true",
help="Wait for the pipeline to complete",
)
args = parser.parse_args()
# load the config from a local yaml file
YAML_CONFIG = OmegaConf.load(args.config)
# path to the components
COMPONENTS_FOLDER = os.path.join(
os.path.dirname(__file__), "..", "..", "components", args.example
)
# path to the shared components
SHARED_COMPONENTS_FOLDER = os.path.join(
os.path.dirname(__file__), "..", "..", "components", "utils"
)
###########################
### CONNECT TO AZURE ML ###
###########################
def connect_to_aml():
try:
credential = DefaultAzureCredential()
# Check if given credential can get token successfully.
credential.get_token("https://management.azure.com/.default")
except Exception as ex:
# Fall back to InteractiveBrowserCredential in case DefaultAzureCredential not work
credential = InteractiveBrowserCredential()
# Get a handle to workspace
try:
# tries to connect using cli args if provided else using config.yaml
ML_CLIENT = MLClient(
subscription_id=args.subscription_id or YAML_CONFIG.aml.subscription_id,
resource_group_name=args.resource_group
or YAML_CONFIG.aml.resource_group_name,
workspace_name=args.workspace_name or YAML_CONFIG.aml.workspace_name,
credential=credential,
)
except Exception as ex:
print("Could not find either cli args or config.yaml.")
# tries to connect using local config.json
ML_CLIENT = MLClient.from_config(credential=credential)
return ML_CLIENT
####################################
### LOAD THE PIPELINE COMPONENTS ###
####################################
# Loading the component from their yaml specifications
preprocessing_component = load_component(
source=os.path.join(COMPONENTS_FOLDER, "preprocessing", "spec.yaml")
)
training_component = load_component(
source=os.path.join(COMPONENTS_FOLDER, "traininsilo", "spec.yaml")
)
aggregate_component = load_component(
source=os.path.join(SHARED_COMPONENTS_FOLDER, "aggregatemodelweights", "spec.yaml")
)
########################
### BUILD A PIPELINE ###
########################
def custom_fl_data_path(
datastore_name, output_name, unique_id="${{name}}", iteration_num=None
):
"""Produces a path to store the data during FL training.
Args:
datastore_name (str): name of the Azure ML datastore
output_name (str): a name unique to this output
unique_id (str): a unique id for the run (default: inject run id with ${{name}})
iteration_num (str): an iteration number if relevant
Returns:
data_path (str): direct url to the data path to store the data
"""
data_path = f"azureml://datastores/{datastore_name}/paths/federated_learning/{output_name}/{unique_id}/"
if iteration_num:
data_path += f"iteration_{iteration_num}/"
return data_path
def getUniqueIdentifier(length=8):
"""Generates a random string and concatenates it with today's date
Args:
length (int): length of the random string (default: 8)
"""
str = string.ascii_lowercase
date = datetime.date.today().strftime("%Y_%m_%d_")
return date + "".join(random.choice(str) for i in range(length))
pipeline_identifier = getUniqueIdentifier()
@pipeline(
description=f'FL cross-silo basic pipeline and the unique identifier is "{pipeline_identifier}" that can help you to track files in the storage account.',
)
def fl_cross_silo_internal_basic():
######################
### PRE-PROCESSING ###
######################
# once per silo, we're running a pre-processing step
silo_preprocessed_train_data = (
[]
) # list of preprocessed train datasets for each silo
silo_preprocessed_test_data = [] # list of preprocessed test datasets for each silo
for silo_index, silo_config in enumerate(YAML_CONFIG.federated_learning.silos):
# run the pre-processing component once
silo_pre_processing_step = preprocessing_component(
raw_training_data=Input(
type=silo_config.training_data.type,
mode=silo_config.training_data.mode,
path=silo_config.training_data.path,
),
raw_testing_data=Input(
type=silo_config.testing_data.type,
mode=silo_config.testing_data.mode,
path=silo_config.testing_data.path,
),
metrics_prefix=silo_config.name,
)
# add a readable name to the step
silo_pre_processing_step.name = f"silo_{silo_index}_preprocessing"
# make sure the compute corresponds to the silo
silo_pre_processing_step.compute = silo_config.computes[0]
# assign instance type for AKS, if available
if hasattr(silo_config, "instance_type"):
silo_pre_processing_step.resources = {
"instance_type": silo_config.instance_type
}
# make sure the data is written in the right datastore
silo_pre_processing_step.outputs.processed_train_data = Output(
type=AssetTypes.URI_FOLDER,
mode="mount",
path=custom_fl_data_path(silo_config.datastore, "train_data"),
)
silo_pre_processing_step.outputs.processed_test_data = Output(
type=AssetTypes.URI_FOLDER,
mode="mount",
path=custom_fl_data_path(silo_config.datastore, "test_data"),
)
# store a handle to the train data for this silo
silo_preprocessed_train_data.append(
silo_pre_processing_step.outputs.processed_train_data
)
# store a handle to the test data for this silo
silo_preprocessed_test_data.append(
silo_pre_processing_step.outputs.processed_test_data
)
################
### TRAINING ###
################
running_checkpoint = None # for iteration 1, we have no pre-existing checkpoint
# now for each iteration, run training
for iteration in range(1, YAML_CONFIG.training_parameters.num_of_iterations + 1):
# collect all outputs in a dict to be used for aggregation
silo_weights_outputs = {}
# for each silo, run a distinct training with its own inputs and outputs
for silo_index, silo_config in enumerate(YAML_CONFIG.federated_learning.silos):
# we're using training component here
silo_training_step = training_component(
# with the train_data from the pre_processing step
train_data=silo_preprocessed_train_data[silo_index],
# with the test_data from the pre_processing step
test_data=silo_preprocessed_test_data[silo_index],
# and the checkpoint from previous iteration (or None if iteration == 1)
checkpoint=running_checkpoint,
# Learning rate for local training
lr=YAML_CONFIG.training_parameters.lr,
# Number of epochs
epochs=YAML_CONFIG.training_parameters.epochs,
# Dataloader batch size
batch_size=YAML_CONFIG.training_parameters.batch_size,
# Differential Privacy
dp=YAML_CONFIG.training_parameters.dp,
# DP target epsilon
dp_target_epsilon=YAML_CONFIG.training_parameters.dp_target_epsilon,
# DP target delta
dp_target_delta=YAML_CONFIG.training_parameters.dp_target_delta,
# DP max gradient norm
dp_max_grad_norm=YAML_CONFIG.training_parameters.dp_max_grad_norm,
# Total num of iterations
total_num_of_iterations=YAML_CONFIG.training_parameters.num_of_iterations,
# Silo name/identifier
metrics_prefix=silo_config.name,
# Iteration number
iteration_num=iteration,
)
# add a readable name to the step
silo_training_step.name = f"silo_{silo_index}_training"
# make sure the compute corresponds to the silo
silo_training_step.compute = silo_config.computes[0]
# assign instance type for AKS, if available
if hasattr(silo_config, "instance_type"):
silo_training_step.resources = {
"instance_type": silo_config.instance_type
}
# make sure the data is written in the right datastore
silo_training_step.outputs.model = Output(
type=AssetTypes.URI_FOLDER,
mode="mount",
path=custom_fl_data_path(
# IMPORTANT: writing the output of training into the orchestrator datastore
YAML_CONFIG.federated_learning.orchestrator.datastore,
f"model/silo{silo_index}",
iteration_num=iteration,
),
)
# each output is indexed to be fed into aggregate_component as a distinct input
silo_weights_outputs[
f"input_silo_{silo_index+1}"
] = silo_training_step.outputs.model
# aggregate all silo models into one
aggregate_weights_step = aggregate_component(**silo_weights_outputs)
# this is done in the orchestrator compute
aggregate_weights_step.compute = (
YAML_CONFIG.federated_learning.orchestrator.compute
)
# assign instance type for AKS, if available
if hasattr(silo_config, "instance_type"):
aggregate_weights_step.resources = {
"instance_type": silo_config.instance_type
}
# add a readable name to the step
aggregate_weights_step.name = f"iteration_{iteration}_aggregation"
# make sure the data is written in the right datastore
aggregate_weights_step.outputs.aggregated_output = Output(
type=AssetTypes.URI_FOLDER,
mode="mount",
path=custom_fl_data_path(
YAML_CONFIG.federated_learning.orchestrator.datastore,
"aggregated_output",
unique_id=pipeline_identifier,
iteration_num=iteration,
),
)
# let's keep track of the checkpoint to be used as input for next iteration
running_checkpoint = aggregate_weights_step.outputs.aggregated_output
return {"final_aggregated_model": running_checkpoint}
pipeline_job = fl_cross_silo_internal_basic()
# Inspect built pipeline
print(pipeline_job)
if not args.offline:
print("Submitting the pipeline job to your AzureML workspace...")
ML_CLIENT = connect_to_aml()
pipeline_job = ML_CLIENT.jobs.create_or_update(
pipeline_job, experiment_name="fl_dev"
)
print("The url to see your live job running is returned by the sdk:")
print(pipeline_job.services["Studio"].endpoint)
webbrowser.open(pipeline_job.services["Studio"].endpoint)
if args.wait:
job_name = pipeline_job.name
status = pipeline_job.status
while status not in ["Failed", "Completed", "Canceled"]:
print(f"Job current status is {status}")
# check status after every 100 sec
time.sleep(100)
try:
pipeline_job = ML_CLIENT.jobs.get(name=job_name)
except azure.identity._exceptions.CredentialUnavailableError as e:
print(f"Token expired or Credentials unavailable: {e}")
sys.exit(5)
status = pipeline_job.status
print(f"Job finished with status {status}")
if status in ["Failed", "Canceled"]:
sys.exit(1)
else:
print("The pipeline was NOT submitted, omit --offline to send it to AzureML.")