-
Notifications
You must be signed in to change notification settings - Fork 1
/
privacy_workload.py
344 lines (305 loc) · 12.7 KB
/
privacy_workload.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
import math
import os
from collections import defaultdict
from typing import Optional, Union
import modin.pandas as pd
import numpy as np
from alibaba_privacy_workload.utils import (
compute_gaussian_demands,
compute_laplace_demands,
compute_noise_and_rdp_from_target_epsilon,
map_to_range,
sample_from_frequencies_dict,
)
from loguru import logger
from omegaconf import DictConfig, OmegaConf
from ray.util.multiprocessing import Pool
class PrivacyWorkload:
"""
csv-based privacy workload.
Reads and processes the alibaba csv files; exposes features to be used as proxies and calls a deterministic
function for the creation of the privacy alibaba workload
"""
def __init__(self, alibaba_trace, cfg: DictConfig):
self.cfg = cfg
self.tasks = None
self.alibaba_trace = alibaba_trace
def create_dp_task(self, task_trace: Union[dict, pd.series.Series]) -> dict:
inst_num = task_trace["inst_num"]
start_execution_time = task_trace["start_execution_time"]
end_execution_time = task_trace["end_execution_time"]
plan_cpu = task_trace["plan_cpu"]
plan_mem = task_trace["plan_mem"]
plan_gpu = task_trace["plan_gpu"]
gpu_type = task_trace["gpu_type"]
submit_time = task_trace["submit_time"]
cpu_usage = task_trace["cpu_usage"]
gpu_wrk_util = task_trace["gpu_wrk_util"]
avg_mem = task_trace["avg_mem"]
max_mem = task_trace["max_mem"]
avg_gpu_wrk_mem = task_trace["avg_gpu_wrk_mem"]
max_gpu_wrk_mem = task_trace["max_gpu_wrk_mem"]
read = task_trace["read"]
write = task_trace["write"]
read_count = task_trace["read_count"]
write_count = task_trace["write_count"]
machine_cpu_iowait = task_trace["machine_cpu_iowait"]
machine_cpu_kernel = task_trace["machine_cpu_kernel"]
machine_cpu_usr = task_trace["machine_cpu_usr"]
machine_gpu = task_trace["machine_gpu"]
machine_load_1 = task_trace["machine_load_1"]
machine_net_receive = task_trace["machine_net_receive"]
machine_num_worker = task_trace["machine_num_worker"]
machine_cpu = task_trace["machine_cpu"]
gpu_type_spec = task_trace["gpu_type_spec"]
workload = task_trace["workload"]
cap_cpu = task_trace["cap_cpu"]
cap_mem = task_trace["cap_mem"]
cap_gpu = task_trace["cap_gpu"]
wait_time = start_execution_time - submit_time
runtime = task_trace["runtime"]
relative_submit_time = task_trace["relative_submit_time"]
alphas = self.cfg.alphas
cpu_based_curve = task_trace["cpu_based_curve"]
n_blocks = self.compute_num_blocks(cpu_based_curve, inst_num, runtime, read)
rdp_epsilons, epsilon, delta, task_name = self.compute_budget(
cpu_task=cpu_based_curve,
workload=workload,
runtime=runtime,
avg_mem=avg_mem,
plan_gpu=plan_gpu,
alphas=alphas,
n_blocks=n_blocks,
avg_gpu_wrk_mem=avg_gpu_wrk_mem,
)
task = {
"epsilon": epsilon,
"delta": delta,
"n_blocks": n_blocks,
"profit": self.compute_profit(gpu_type_spec),
"block_selection_policy": self.compute_block_selection_policy(),
"task_name": task_name,
"alphas": alphas,
"rdp_epsilons": rdp_epsilons,
"relative_submit_time": relative_submit_time,
"submit_time": submit_time,
"workload": workload,
}
post_processed_task = self.post_process_task(task)
return post_processed_task
def post_process_task(self, task: dict) -> Optional[dict]:
"""
Returns an error string if the task is too small or unsuitable for any other reason.
Also clips the demands to avoid NaN.
"""
task["rdp_epsilons"] = [
1_000_000 if x == math.inf else x for x in task["rdp_epsilons"]
]
# We normalize by a fictional block
epsilon, delta = float(self.cfg.normalizing_epsilon), float(
self.cfg.normalizing_delta
)
task["normalized_rdp_epsilons"] = []
for alpha, rdp_epsilon in zip(task["alphas"], task["rdp_epsilons"]):
block_epsilon = epsilon + np.log(delta) / (alpha - 1)
if block_epsilon <= 0 or (rdp_epsilon / block_epsilon) == np.inf:
task["normalized_rdp_epsilons"].append(10)
else:
task["normalized_rdp_epsilons"].append(rdp_epsilon / block_epsilon)
epsilon_min = min(task["normalized_rdp_epsilons"])
if epsilon_min < self.cfg.rdp_epsilon_min:
return "epsilon_min_too_small"
if epsilon_min > 1:
return "epsilon_min_too_big"
if task["n_blocks"] > self.cfg.n_blocks_cutoff:
return "n_blocks_too_big"
return task
def generate(self):
logger.info("Mapping to a privacy workload...")
tasks_info = self.alibaba_trace.tasks_info.head(self.cfg.max_number_of_tasks)
logger.info(tasks_info.head())
logger.info(f"Enumerating tasks...")
tasks_list = [index_and_row[1] for index_and_row in tasks_info.iterrows()]
logger.info(f"Starting pool mapping on {len(tasks_list)} tasks...")
with Pool(processes=int(os.cpu_count() * 0.75)) as pool:
dp_tasks = list(pool.map(self.create_dp_task, tasks_list))
valid_dp_tasks = []
invalid_tasks = defaultdict(int)
for t in dp_tasks:
if isinstance(t, dict):
valid_dp_tasks.append(t)
else:
invalid_tasks[t] += 1
logger.info(f"Removed invalid tasks out of {len(dp_tasks)}: {invalid_tasks}")
logger.info(f"Collecting results in a dataframe...")
self.tasks = pd.DataFrame(valid_dp_tasks)
logger.info(self.tasks.head())
def dump(
self,
path,
):
logger.info("Saving the privacy workload...")
path.parent.mkdir(parents=True, exist_ok=True)
self.tasks.to_csv(path, index=False)
logger.info(f"Saved {len(self.tasks)} tasks at {path}.")
OmegaConf.save(self.cfg, path.with_suffix(".cfg.yaml"))
def compute_budget(
self,
cpu_task: bool,
workload,
runtime,
avg_mem,
plan_gpu,
n_blocks,
alphas,
avg_gpu_wrk_mem,
):
# Rough range of delta. The final demands are in RDP.
dataset_size = n_blocks * self.cfg.avg_block_size
delta = 1 / dataset_size
if cpu_task == "True":
# Epsilon ~ RAM * runtime
epsilon = map_to_range(
value=avg_mem * runtime,
min_input=self.alibaba_trace.min_cpu_based_avg_mem_times_runtime,
max_input=self.alibaba_trace.max_cpu_based_avg_mem_times_runtime,
min_output=self.cfg.epsilon_min_cpu,
max_output=self.cfg.epsilon_max,
)
# Number of epochs ~ runtime
epochs = int(
map_to_range(
value=runtime,
min_input=self.alibaba_trace.min_cpu_based_runtime,
max_input=self.alibaba_trace.max_cpu_based_runtime,
min_output=self.cfg.epochs_min,
max_output=self.cfg.epochs_max,
)
)
# Map epsilon to an RDP curve with different mechanisms
mechanism = sample_from_frequencies_dict(
dict(self.cfg.cpu_mechanisms_frequencies)
)
params = {"epsilon": epsilon}
if mechanism == "laplace":
# Only one step for Laplace because they are already very costly RDP-wise
rdp_epsilons = compute_laplace_demands(
laplace_noise=1 / epsilon, alphas=alphas
)
elif mechanism == "gaussian":
rdp_epsilons = compute_gaussian_demands(
epsilon=epsilon, delta=delta, steps=epochs
)
params["delta"] = delta
params["epochs"] = epochs
elif mechanism == "subsampled_laplace":
batch_size = int(
map_to_range(
value=plan_gpu,
min_input=self.alibaba_trace.min_cpu_based_inst_num,
max_input=self.alibaba_trace.max_cpu_based_inst_num,
min_output=self.cfg.batch_size_min,
max_output=min(self.cfg.batch_size_max, dataset_size // 10),
)
)
_, rdp_epsilons = compute_noise_and_rdp_from_target_epsilon(
target_epsilon=epsilon,
target_delta=delta,
dataset_size=dataset_size,
batch_size=batch_size,
epochs=epochs,
alphas=alphas,
approx_ratio=0.1,
gaussian=False,
)
params["delta"] = delta
params["epochs"] = epochs
params["batch_size"] = batch_size
else:
# Epsilon ~ GPU mem * runtime
epsilon = map_to_range(
value=avg_gpu_wrk_mem * runtime,
min_input=self.alibaba_trace.min_gpu_based_avg_gpu_wrk_mem_times_runtime,
max_input=self.alibaba_trace.max_gpu_based_avg_gpu_wrk_mem_times_runtime,
min_output=self.cfg.epsilon_min_gpu,
max_output=self.cfg.epsilon_max,
)
# Batch size ~ GPU mem
batch_size = int(
map_to_range(
value=plan_gpu,
min_input=self.alibaba_trace.min_gpu_based_plan_gpu,
max_input=self.alibaba_trace.max_gpu_based_plan_gpu,
min_output=self.cfg.batch_size_min,
max_output=min(self.cfg.batch_size_max, dataset_size // 10),
)
)
# Epochs ~ runtime
epochs = int(
map_to_range(
value=runtime,
min_input=self.alibaba_trace.min_gpu_based_runtime,
max_input=self.alibaba_trace.max_gpu_based_runtime,
min_output=self.cfg.epochs_min,
max_output=self.cfg.epochs_max,
)
)
params = {"epsilon": epsilon}
params["delta"] = delta
mechanism = sample_from_frequencies_dict(
dict(self.cfg.gpu_mechanisms_frequencies)
)
if mechanism == "subsampled_gaussian":
sigma, rdp_epsilons = compute_noise_and_rdp_from_target_epsilon(
target_epsilon=epsilon,
target_delta=delta,
dataset_size=dataset_size,
batch_size=batch_size,
epochs=epochs,
alphas=alphas,
approx_ratio=0.1,
gaussian=True,
)
params["epochs"] = epochs
params["batch_size"] = batch_size
elif mechanism == "dp_ftrl":
rdp_epsilons = compute_gaussian_demands(
epsilon=epsilon,
delta=delta,
steps=int(np.ceil(np.log(dataset_size))),
)
param_string = ",".join([f"{k}={v:.2e}" for k, v in params.items()])
task_name = f"{mechanism}-{param_string}"
return rdp_epsilons, epsilon, delta, task_name
def compute_num_blocks(self, cpu_task, inst_num, runtime, read):
if cpu_task == "True":
min_input = self.alibaba_trace.min_cpu_based_read
max_input = self.alibaba_trace.max_cpu_based_read
n_blocks = int(
map_to_range(
value=read,
min_input=min_input,
max_input=max_input,
min_output=self.cfg.n_blocks_min,
max_output=self.cfg.n_blocks_max,
)
)
else:
min_input = self.alibaba_trace.min_gpu_based_read
max_input = self.alibaba_trace.max_gpu_based_read
n_blocks = int(
map_to_range(
value=read,
min_input=min_input,
max_input=max_input,
min_output=self.cfg.n_blocks_min,
max_output=self.cfg.n_blocks_max,
)
)
if n_blocks == 0:
n_blocks = 1
return n_blocks
def compute_profit(self, gpu_type_spec):
return 1
def compute_block_selection_policy(self):
return "LatestBlocksFirst"