-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRecPipeAccelSim.py
432 lines (346 loc) · 16.1 KB
/
RecPipeAccelSim.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import json
import numpy as np
import torch
import models.dlrm_data_pytorch as dp
from copy import deepcopy
import sys
import time
from utils import loadGenSleep
from utils import AccelInferenceQuery
import numpy as np
import random
import RecPipeAccelModel
from multiprocessing import Process, Queue
##########################################################################
# Accelerator simulation thread to measure the at-scale implications of
# different accelerator topologies. Input queries to accel_sim_thread
# are per-query and per-stage inference times.
##########################################################################
def accel_sim_thread(args, stage_id, engine_id, qInput, qOutput,
num_queries, num_threads_per_engine):
query_count = 0
time_total = 0
time_active = 0
time_active_start = 0
###################################################################
# Running inference thread
###################################################################
while True:
# Receive inference query
inference_query = qInput.get()
if query_count == 256:
time_total = time.time()
# Done processing all queries
if (inference_query == None):
time_total = (time.time() - time_total)
print("Accel {} util: {}".format(stage_id, time_active / float(time_total) * 100))
return
# Model/emulate inference time per query
inference_start = time.time()
time.sleep(inference_query.inference_times[stage_id])
inference_end = time.time()
if query_count >= 256:
time_active += (inference_end - inference_start)
# Send query to next set of modeled/emulated accelerator
inference_query.sim_inference_start_times.append(inference_start)
inference_query.sim_inference_end_times.append(inference_end)
inference_query.end_time = inference_end
qOutput.put(inference_query)
query_count += 1
return
##########################################################################
# Main simulation infrastructure for multi-stage recommendation accelerators.
# run_accel_sim sets up RecPipeAccel for each stage of the multi-stage pipeline
##########################################################################
def run_accel_sim( args,
num_stages,
num_queries,
num_threads_per_engine,
num_inference_engines,
stage_batch_sizes,
stage_items,
nepochs,
inference_query_runtimes,
accel_configs,
arrival_rates = [5.],
):
random.seed(args.numpy_rand_seed)
np.random.seed(args.numpy_rand_seed)
AccelInferenceEngines = [ [] for _ in range(num_stages) ]
AccelInferenceInputQueue = [ Queue() for _ in range(num_stages) ]
AccelOutputQueue = Queue()
# Model configuration for per-stage
model_configs = args.model_configs.split(",")
#################################################################
# Instantiate multi-stage inference processes
#################################################################
for i in range(num_stages):
# Create the specified number of inference engines
for j in range(num_inference_engines[i]):
engine_args = deepcopy(args)
engine_args.mini_batch_size = stage_batch_sizes[i]
engine_args.test_mini_batch_size = stage_batch_sizes[i]
engine_args.model_configs = model_configs[i]
engine_args.accel_configs = accel_configs[i]
# Process arguments for each stage
if i != num_stages - 1:
pargs = (engine_args, i, j, AccelInferenceInputQueue[i],
AccelInferenceInputQueue[i+1], num_queries,
num_threads_per_engine[i])
else:
pargs = (engine_args, i, j, AccelInferenceInputQueue[i],
AccelOutputQueue, num_queries,
num_threads_per_engine[i])
# Launch accelerator thread
p = Process( target = accel_sim_thread, args = pargs)
AccelInferenceEngines[i].append(p)
#################################################################
# Launch multi-stage inference processes
#################################################################
for i in range(num_stages):
for j in range(num_inference_engines[i]):
AccelInferenceEngines[i][j].start()
#################################################################
# Add input to drive the multi-stage ranking pipeline
# Iterate through all arrival rates based on RecPipe experiment sweep
# configuration
#################################################################
for arrival_rate in arrival_rates:
#Epochs can be used to model rnadom seeds for averaging results over many
#runs
for epoch in range(nepochs):
print("===================================================")
print("****Epoch ", epoch)
exp_start_time = time.time()
total_queries = 0
for i in range(num_queries):
q = AccelInferenceQuery( query_id=i,
inference_times=inference_query_runtimes[i],
start_time = time.time() )
AccelInferenceInputQueue[0].put(q)
# Modeling poisson arrival rate
arrival_time = np.random.poisson(lam = arrival_rate, size = 1)
loadGenSleep( arrival_time / 1000. )
total_queries += 1
sys.stdout.flush()
#################################################################
#Handle output
output_queries = []
while len(output_queries) != total_queries:
if AccelOutputQueue.qsize() != 0:
q = AccelOutputQueue.get()
if q is not None:
output_queries.append(q)
exp_end_time = time.time()
#################################################################
# Analyze time breakdown by type of operation
inference_times = []
tail_inference_times = []
for q in output_queries:
inference_times.append( np.sum(q.inference_times) )
tail_inference_times.append(q.end_time - q.start_time)
#print("Inference stages: ", q.inference_times)
#print("Tail inference times: ", tail_inference_times[-1])
exp_time = (exp_end_time - exp_start_time)
# Final experiment statistics
print("****Total experiment time (s): " , exp_time )
print("****Infer mean time (s): " , np.mean(inference_times) )
print("****Infer tail time (s): " , np.percentile(inference_times , 95) )
print("****Total mean time (s): " , np.mean(tail_inference_times) )
print("****Total tail time (s): " , np.percentile(tail_inference_times , 95) )
print("****Arrival rate: " , arrival_rate)
print("****model config: " , args.model_configs)
print("****batch_size: " , stage_batch_sizes)
print("****accel config: " , accel_configs)
print("****accel counts: " , num_inference_engines)
print("===================================================")
# Some cool off period from RecPipe infrastructure before next
# experiment
time.sleep(2)
# Once all queries and experiments are finished send done signal
for i in range(num_stages):
for _ in range(num_inference_engines[i]):
AccelInferenceInputQueue[i].put(None)
#Clean up inference engines once they are finished
for i in range(num_stages):
for j in range(num_inference_engines[i]):
AccelInferenceEngines[i][j].join()
while AccelOutputQueue.qsize() != 0:
q = AccelOutputQueue.get()
if q != None:
print("Warning: queries left in output queue")
return
##########################################################################
# Helper function to build dataset for accelerator experiments
##########################################################################
def construct_dataset(args, ):
use_gpu = False
device = torch.device("cpu")
### prepare training data ###
ln_bot = np.fromstring(args.arch_mlp_bot, dtype=int, sep="-")
# input data
if (args.data_generation == "dataset"):
test_data, test_ld = dp.make_criteo_data_and_loaders(args)
ln_emb = test_data.counts
# enforce maximum limit on number of vectors per embedding
if args.max_ind_range > 0:
ln_emb = np.array(list(map(
lambda x: x if x < args.max_ind_range else args.max_ind_range,
ln_emb
)))
m_den = test_data.m_den
ln_bot[0] = m_den
else:
print("Error need a dataset")
sys.exit()
print("Created dataset")
### parse command line arguments ###
m_spa = args.arch_sparse_feature_size
num_fea = ln_emb.size + 1 # num sparse + num dense features
m_den_out = ln_bot[ln_bot.size - 1]
if args.arch_interaction_op == "dot":
# approach 1: all
# num_int = num_fea * num_fea + m_den_out
# approach 2: unique
if args.arch_interaction_itself:
num_int = (num_fea * (num_fea + 1)) // 2 + m_den_out
else:
num_int = (num_fea * (num_fea - 1)) // 2 + m_den_out
elif args.arch_interaction_op == "cat":
num_int = num_fea * m_den_out
else:
sys.exit(
"ERROR: --arch-interaction-op="
+ args.arch_interaction_op
+ " is not supported"
)
arch_mlp_top_adjusted = str(num_int) + "-" + args.arch_mlp_top
ln_top = np.fromstring(arch_mlp_top_adjusted, dtype=int, sep="-")
# sanity check: feature sizes and mlp dimensions must match
if m_den != ln_bot[0]:
sys.exit(
"ERROR: arch-dense-feature-size "
+ str(m_den)
+ " does not match first dim of bottom mlp "
+ str(ln_bot[0])
)
if m_spa != m_den_out:
sys.exit(
"ERROR: arch-sparse-feature-size "
+ str(m_spa)
+ " does not match last dim of bottom mlp "
+ str(m_den_out)
)
if num_int != ln_top[0]:
sys.exit(
"ERROR: # of feature interactions "
+ str(num_int)
+ " does not match first dimension of top mlp "
+ str(ln_top[0])
)
ndevices = min(ngpus, args.mini_batch_size, num_fea - 1) if use_gpu else -1
return test_data, test_ld
##########################################################################
# Accelerator thread to measure the per-inference query times
# based on different accelerator (RecPipeAccel) topologies
##########################################################################
def accel_thread(args, stage_id, engine_id, qReady, qStart, qInput, qOutput,
num_queries, num_threads_per_engine,):
# Build model off of the configuration specified for model architecture
# parameters
if args.model_configs is not None:
with open(args.model_configs, 'r') as f:
config = json.load(f)
for key in config:
type_of = type(getattr(args, key))
setattr(args, key, type_of(config[key]))
### some basic setup ###
np.random.seed(args.numpy_rand_seed)
torch.manual_seed(args.numpy_rand_seed)
torch.set_num_threads(num_threads_per_engine)
### accelerator modeling setup ###
accelerator = RecPipeAccelModel.Accelerator(args.model_configs,
args.accel_configs,
args.data_set)
###################################################################
# Constructed inference thread first
###################################################################
use_gpu = args.use_gpu and torch.cuda.is_available()
if args.data_set == "kaggle":
test_data, test_ld = construct_dataset(args)
#Shape of np_X_int and np_X_cat is (length, features). For the full test
#set length = 3274330 (1million); X_int features is 13 while X_cat features
#is 26
np_X_int_test_total = np.array(deepcopy(test_data.X_int))
np_X_cat_test_total = np.array(deepcopy(test_data.X_cat))
np_y = np.array(deepcopy(test_data.y))
# Delete test_data and test_ld for memory saving
del test_data
del test_ld
# Warm up caches with full test-set inference
for i in range( int((int(len(np_X_cat_test_total)) /1024)-1)):
start_id = i * 1024
end_id = (i+1) * 1024
# accelerator inference to warm up caches
# Input continuous features: np_X_int_test_total[start_id:end_id]
# Input cateogorical features: np_X_cat_test_total[start_id:end_id]
accelerator.warm_cache(np_X_cat_test_total[start_id:end_id])
# Send signal to parent process that DLRM model has been constructed and
# ready to run inferences.
qReady.put(None)
# Wait for start signal from parent process
qStart.get()
###################################################################
# Running inference thread
###################################################################
while True:
inference_query = qInput.get()
if inference_query == None:
# If we have received the termination signal infernece process can
# begin to exit
#print("[Stage/engine {}/{}] got termination packet".format(stage_id, engine_id))
qOutput.put(None)
return
pre_process_start = time.time()
# Retrieve sorted IDs from query for continuous and categorical features
#np_X_int_test = np_X_int_test_total[inference_query.sorted_ids]
if args.data_set == "kaggle":
np_X_cat_test = np_X_cat_test_total[inference_query.sorted_ids]
else:
np_X_cat_test = []
data_time = time.time()
# Inference
# Return out -> scores (placeholder), preprocess_time, data_times, inference_times, fetch_times
# preprocess_time = Actual timestamp of preocessping data (Not really important)
# data_times = Time to read inputs from data (Not really important)
# inference_times = Time for actual inference (Very important)
# fetch_times = Time to fetch data from accelerator (Important for return of data from PCIe)
items_per_query = args.mini_batch_size
top_n = 0
(scores,
preprocess_time,
data_times,
inference_times,
fetch_times) = accelerator.run_query(items_per_query, top_n, np_X_cat_test)
sim_time = time.time()
loadGenSleep(inference_times) # Sleep to model accelerator tradeoff
end_time = time.time()
#print("Time to read data: ", data_time - pre_process_start)
#print("Simulation time: ", sim_time - data_time)
inference_query.query_end_time = end_time
inference_query.inference_end_time.append(end_time)
inference_query.inference_start_time.append(pre_process_start)
# Convert inference_times into
if args.data_set == "kaggle":
inference_query.sorted_scores = np_y[inference_query.sorted_ids]
else:
inference_query.sorted_scores = []
# Send queries over to next sorting stage
inference_query.preprocess_times.append(0)
inference_query.data_times.append(0)
inference_query.inference_times.append(inference_times)
inference_query.fetch_times.append(0)
inference_query.sort_times.append(0)
dump_query_time = time.time()
qOutput.put(inference_query)
return