-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_protocol.py
318 lines (289 loc) · 11.6 KB
/
run_protocol.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
"""
This script assumes that a subdir with name {n_parties} exists in /models with the model files stored here.
The number of model files should equal the value of {n_parties} + 1.
It kicks off a server for each answering party and a single client who will be requesting queries.
client.py holds the clients training protocol, and server.py the response algorithms.
train_inits.py should be run first to train each model on a separate partition and save them as per the required scheme.
USAGE: call this file with: OMP_NUM_THREADS=24 NGRAPH_HE_VERBOSE_OPS=all NGRAPH_HE_LOG_LEVEL=3 python run_protocol.py
SETUP: create a tmux session with 3 panes, each in /home/dockuser/code/demo/capc
"""
import warnings
from utils.time_utils import get_timestamp
from utils.time_utils import log_timing
warnings.filterwarnings('ignore')
import argparse
import os
import numpy as np
import atexit
from utils.remove_files import remove_files_by_name
import consts
from consts import out_client_name, out_server_name, out_final_name
import getpass
import subprocess
import client
def get_args():
user = getpass.getuser()
"""Initial setup of parameters to be used."""
parser = argparse.ArgumentParser('')
parser.add_argument('--session', type=str, help='session name',
default='capc')
parser.add_argument('--log_timing_file', type=str,
help='name of the global log timing file',
default=f'logs/log-timing-{get_timestamp()}.log')
parser.add_argument('--n_parties', type=int, default=1,
help='number of servers')
parser.add_argument('--start_port', type=int, default=37000,
help='the number of the starting port')
parser.add_argument('--seed', type=int, default=2,
help='seed for top level script')
parser.add_argument('--batch_size', type=int, default=1,
help='batch size')
parser.add_argument('--num_classes', type=int, default=10,
help='Number of classes in the dataset.')
parser.add_argument(
"--rstar_exp",
type=int,
default=10,
help='The exponent for 2 to generate the random r* from.',
)
parser.add_argument(
"--max_logit",
type=float,
default=36.0,
help='The maximum value of a logit.',
)
parser.add_argument('--dp_noise_scale', type=float, default=0.1,
help='The scale of the Gaussian noise for DP privacy.')
parser.add_argument(
"--user",
type=str,
default=user,
help="The name of the OS USER.",
)
parser.add_argument(
"--log_level",
type=int,
default=0,
help='log level for he-transformer',
)
parser.add_argument(
'--round_exp',
type=int,
default=3,
help='Multiply r* and logits by 2^round_exp.'
)
parser.add_argument(
'--num_threads',
type=int,
default=20,
help='Number of threads.',
)
parser.add_argument(
'--qp_id', type=int, default=0, help='which model is the QP?')
parser.add_argument(
"--start_batch",
type=int,
default=0,
help="Test data start index")
parser.add_argument(
"--model_type",
type=str,
default='cryptonets-relu',
help="The type of models used.",
)
parser.add_argument(
"--input_node",
type=str,
default="import/input:0",
help="Tensor name of data input",
)
parser.add_argument(
"--output_node",
type=str,
default="import/output/BiasAdd:0",
help="Tensor name of model output",
)
parser.add_argument(
'--dataset_path', type=str,
default='/home/dockuser/queries',
help='where the queries are.')
parser.add_argument(
'--dataset_name', type=str,
default='mnist',
help='name of dataset where queries came from')
parser.add_argument('--debug', default=False, action='store_true')
parser.add_argument('--n_queries',
type=int,
default=1,
help='total len(queries)')
parser.add_argument('--checkpoint_dir', type=str,
default=f'./models',
help='dir with all checkpoints')
parser.add_argument('--cpu', default=False, action='store_true',
help='set to use cpu and no encryption.')
parser.add_argument('--ignore_parties', default=True, action='store_true',
# False
help='set when using crypto models.')
# parser.add_argument('--',
# default='$HE_TRANSFORMER/configs/he_seal_ckks_config_N13_L5_gc.json')
parser.add_argument('--encryption_params',
default='config/10.json')
args, unparsed = parser.parse_known_args()
if unparsed:
print("Unparsed flags:", unparsed)
exit(1)
return args
def clean_old_files():
"""
Delete old data files.
This function is called before running the protocol.
"""
cur_dir = os.getcwd()
for name in [out_client_name,
out_server_name,
out_final_name,
consts.input_data,
consts.input_labels,
consts.predict_labels,
consts.label_final_name]:
remove_files_by_name(starts_with=name, directory=cur_dir)
def delete_files(port):
"""
Delete files related to this port.
:param port: port number
"""
files_to_delete = [consts.out_client_name + str(port) + 'privacy.txt']
files_to_delete += [
consts.out_final_name + str(port) + '.txt'] # + 'privacy.txt']
files_to_delete += [
consts.out_server_name + str(port) + '.txt'] # + 'privacy.txt']
files_to_delete += [f"{out_final_name}.txt",
f"{out_server_name}.txt"] # aggregates across all parties
files_to_delete += [consts.inference_times_name,
consts.argmax_times_name,
consts.client_csp_times_name,
consts.inference_no_network_times_name]
for f in files_to_delete:
if os.path.exists(f):
print(f'delete file: {f}')
os.remove(f)
def get_models(model_dir, n_parties, ignore_parties):
"""Gets model files from model_dir."""
model_files = [f for f in os.listdir(model_dir) if
os.path.isfile(os.path.join(model_dir, f))]
if len(model_files) != n_parties and not ignore_parties:
raise ValueError(
f'{len(model_files)} models found when {n_parties + 1} parties '
f'requested. Not equal.')
return model_dir, model_files
def run(args):
"""Main pipeline to run the experiment based on the given parameters."""
log_timing_file = args.log_timing_file
log_timing('main: start capc', log_file=log_timing_file)
processes = []
def kill_processes():
for p in processes:
p.kill()
if not args.debug:
atexit.register(kill_processes)
n_parties = args.n_parties
n_queries = args.n_queries
batch_size = args.batch_size
num_classes = args.num_classes
rstar_exp = args.rstar_exp
log_level = args.log_level
round_exp = args.round_exp
num_threads = args.num_threads
input_node = args.input_node
output_node = args.output_node
start_port = args.start_port
index = args.start_batch
# if FLAGS.cpu then use cpu without the encryption.
backend = 'HE_SEAL' if not args.cpu else 'CPU'
models_loc, model_files = get_models(
args.checkpoint_dir, n_parties=n_parties,
ignore_parties=args.ignore_parties)
for port in range(start_port, start_port + n_queries * n_parties):
delete_files(port=port)
# Querying process
for query_num in range(n_queries):
for port, model_file in zip(
[start_port + int(i + query_num * n_parties) for i in
range(n_parties)],
model_files):
print(f"port: {port}")
new_model_file = os.path.join(
"/home/dockuser/models", str(port) + ".pb")
print('Start the servers (answering parties: APs).')
log_timing('start server (AP)', log_file=log_timing_file)
# Command to start server with the relevant parameters.
cmd_string = " ".join(
[
'python -W ignore', 'server.py',
'--backend', backend,
'--n_parties', f'{n_parties}',
'--model_file', new_model_file,
'--dataset_name', args.dataset_name,
'--indext', str(index),
'--encryption_parameters', args.encryption_params,
'--enable_client', 'true',
'--enable_gc', 'true',
'--mask_gc_inputs', 'true',
'--mask_gc_outputs', 'true',
'--from_pytorch', '1',
'--dataset_name', args.dataset_name,
'--dataset_path', args.dataset_path,
'--num_gc_threads', f'{num_threads}',
'--input_node', f'{input_node}',
'--output_node', f'{output_node}',
'--minibatch_id', f'{query_num}',
'--rstar_exp', f'{rstar_exp}',
'--num_classes', f'{num_classes}',
'--round_exp', f'{round_exp}',
'--log_timing_file', log_timing_file,
'--port', f'{port}',
'--checkpoint_dir', args.checkpoint_dir,
])
server_process = subprocess.Popen(cmd_string, shell=True)
print("Start the client (the querying party: QP).")
log_timing('start the client QP', log_file=log_timing_file)
cmd_string = " ".join(
[
# Command to start client server with the relevant parameters.
'python -W ignore client.py',
'--batch_size', f'{batch_size}',
'--encrypt_data_str', 'encrypt',
'--indext', str(index),
'--n_parties', f'{n_parties}',
'--round_exp', f'{round_exp}',
'--from_pytorch', '1',
'--minibatch_id', f'{query_num}',
'--dataset_path', f'{args.dataset_path}',
'--port', f'{port}',
'--dataset_name', args.dataset_name,
'--data_partition', 'test',
'--log_timing_file', log_timing_file,
])
client_process = subprocess.Popen(cmd_string, shell=True)
client_process.wait()
server_process.wait()
log_timing('start privacy guardian', log_file=log_timing_file)
# Command to run Privacy Guardian (Steps 2 & 3).
cmd_string = " ".join(
['python -W ignore', 'pg.py',
'--start_port', f'{start_port + int(query_num * n_parties)}',
'--end_port',
f'{start_port + int(query_num * n_parties) + n_parties}',
'--log_timing_file', log_timing_file,
'--dp_noise_scale', str(args.dp_noise_scale),
])
print(f"start privacy guardian: {cmd_string}")
pg_process = subprocess.Popen(cmd_string, shell=True)
pg_process.wait()
log_timing('finish capc', log_file=log_timing_file)
if __name__ == "__main__":
args = get_args()
np.random.seed(args.seed)
clean_old_files()
run(args=args)
client.print_label()