forked from LMCache/lmcache-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
538 lines (467 loc) · 18.6 KB
/
driver.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from typing import List, Tuple
import pandas as pd
import time
import multiprocessing
from dataclasses import dataclass
import openai
from configs import BootstrapConfig, WorkloadConfig, Usecase
from test_cases import TestCase
from bootstrapper import CreateBootstrapper, Bootstrapper, LMCacheServerManager
from workload import CreateWorkloadGenerator, Request, MultiTurnWorkloadGenerator
from utils import read_gpu_memory, get_max_context_length
import log
logger = log.init_logger(__name__)
@dataclass
class ExperimentResult:
timestamp: str
engine_id: int
request_id: int
TTFT: float
throughput: float
latency: float
@dataclass
class ExperimentResultWithOutput:
timestamp: str
engine_id: int
request_id: int
TTFT: float
throughput: float
messages: str
class RequestExecutor:
def __init__(self):
pass
def schedule_requests(self, requests_list: List[List[Request]], clients: List[openai.Client], models: List[str]):
"""
Take in the list of requests and the clients, prepare the execution
"""
requests_list = [(cid, rid, req, client, model) \
for cid, (requests, client, model) in enumerate(zip(requests_list, clients, models))\
for rid, req in enumerate(requests)]
# Order the requests by the timestamps
self.pending_requests = sorted(requests_list, key=lambda x: x[2].timestamp)
def execute_one_request(
self,
client_id: int,
request_id: int,
request: Request,
client: openai.Client,
model: str,
queue: multiprocessing.Queue) -> Tuple[float, float]:
"""
Execute the request and put the result into the queue
"""
ttft, thp, latency = execute_openai_request(request, model, client)
logger.info(f"Request completed, TTFT = {ttft}, throughput = {thp}")
queue.put(ExperimentResult(request.timestamp, client_id, request_id, ttft, thp, latency))
def execute_one_request_with_output(
self,
client_id: int,
request_id: int,
request: Request,
client: openai.Client,
model: str,
queue: multiprocessing.Queue) -> Tuple[float, float]:
"""
Execute the request and put the result into the queue
"""
ttft, thp, messages = execute_openai_request_with_output(request, model, client)
logger.info(f"Request completed, TTFT = {ttft}, throughput = {thp}")
queue.put(ExperimentResultWithOutput(request.timestamp, client_id, request_id, ttft, thp, messages))
def execute_all(self) -> List[ExperimentResult]:
"""
Returns the list of expr results
"""
queue = multiprocessing.Queue()
start_time = time.time()
processes = []
try:
for client_id, request_id, request, client, model in self.pending_requests:
already_elapsed = time.time() - start_time
# Wait for the request to be ready
wait_time = request.timestamp - already_elapsed
if wait_time > 0:
time.sleep(wait_time)
# Execute the request by a new process
process = multiprocessing.Process(
target = self.execute_one_request,
args=(client_id, request_id, request, client, model, queue))
process.start()
processes.append(process)
# Wait for all the processes to finish
for process in processes:
process.join()
except Exception as e:
logger.error(f"Exception happend when sending request: {e}")
for process in processes:
process.terminate()
return []
return [queue.get() for _ in self.pending_requests]
def execute_all_with_output(self) -> List[ExperimentResultWithOutput]:
"""
Returns the list of expr results
"""
queue = multiprocessing.Queue()
start_time = time.time()
processes = []
try:
for client_id, request_id, request, client, model in self.pending_requests:
already_elapsed = time.time() - start_time
# Wait for the request to be ready
wait_time = request.timestamp - already_elapsed
if wait_time > 0:
time.sleep(wait_time)
# Execute the request by a new process
process = multiprocessing.Process(
target = self.execute_one_request_with_output,
args=(client_id, request_id, request, client, model, queue))
process.start()
processes.append(process)
# Wait for all the processes to finish
for process in processes:
process.join()
except Exception as e:
logger.error(f"Exception happend when sending request: {e}")
for process in processes:
process.terminate()
return []
return [queue.get() for _ in self.pending_requests]
def create_openai_client(port: int, model) -> openai.Client:
openai_api_key = "EMPTY"
# TODO: currently we assume the engines are open to localhost. Need to support different hostname in the future
openai_api_base = f"http://localhost:{port}/v1"
client = openai.OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
messages = [{
"role": "user",
"content": f"This is a warm up request" * 200
}]
chat_completion = client.chat.completions.create(
messages = messages,
model = model,
temperature = 0,
stream = False,
max_tokens = 1,
)
messages = [{
"role": "user",
"content": f"This is a warm up request" * 201
}]
chat_completion = client.chat.completions.create(
messages = messages,
model = model,
temperature = 0,
stream = False,
max_tokens = 1,
)
return client
def execute_openai_request(request: Request, model: str, client: openai.Client) -> Tuple[float, float]:
"""
Execute a single request to the OpenAI engine
Returns: TTFT (seconds) and throughput (tokens per second)
"""
messages = [{
"role": "user",
"content": f"{request.context} {request.question}"
}]
#import random
#t = random.randint(2, 8)
#time.sleep(t)
#return t, t
try:
logger.debug("Issusing a new request...")
chat_completion = client.chat.completions.create(
messages = messages,
model = model,
temperature = 0,
stream = True,
max_tokens = 200,
)
start_time = time.perf_counter()
first_token_time = None
ntokens = 0
messages = []
for chunk in chat_completion:
chunk_message = chunk.choices[0].delta.content
if chunk_message is not None:
if first_token_time is None and chunk_message != " " and chunk_message != "":
first_token_time = time.perf_counter()
messages.append(chunk_message)
ntokens += 1
end_time = time.perf_counter()
ttft = first_token_time - start_time
throughput = ntokens / (end_time - first_token_time)
latency = end_time - start_time
logger.debug(f"Response: {''.join(messages)}")
except Exception as e:
logger.error(f"OpenAI request failed: {e}")
return -1, -1, -1
return ttft, throughput, latency
def execute_openai_request_with_output(request: Request, model: str, client: openai.Client) -> Tuple[float, float, str]:
"""
Execute a single request to the OpenAI engine
Returns: TTFT (seconds) and throughput (tokens per second)
"""
messages = [
{
"role": "user",
"content": request.question
}
]
split_strings = request.context.split("<<splitter>>")
for split_string in split_strings:
if split_string != "":
messages.extend([
{
"role": "assistant",
"content": split_string
},
{
"role": "user",
"content": request.question
}
])
try:
logger.debug("Issusing a new request...")
chat_completion = client.chat.completions.create(
messages = messages,
model = model,
temperature = 0,
stream = True,
max_tokens = 2000,
)
start_time = time.perf_counter()
first_token_time = None
ntokens = 0
messages = []
for chunk in chat_completion:
chunk_message = chunk.choices[0].delta.content
if chunk_message is not None:
if first_token_time is None and chunk_message != " " and chunk_message != "":
first_token_time = time.perf_counter()
messages.append(chunk_message)
ntokens += 1
end_time = time.perf_counter()
ttft = first_token_time - start_time
throughput = ntokens / (end_time - first_token_time)
logger.debug(f"Response: {''.join(messages)}")
except Exception as e:
logger.error(f"OpenAI request failed: {e}")
return -1, -1, ""
return ttft, throughput, f"{''.join(messages)}"
def run_experiment(
workload_config: WorkloadConfig,
usecase: Usecase,
engine_configs: List[BootstrapConfig]) -> List[ExperimentResult]:
"""
Run a single experiment:
- Bootstrap the serving bootstrappers
- Generate workload for each engine
- Wait for engine ready
- (separate threads/processes) Send requests, measure TTFT and throughput
- (separate threads/processes) Measure GPU stats
- Close the bootstrappers
Returns:
- None if the experiment failed
- <workload desc> <timestamp> <engine id> <request id> <TTFT> <throughput> <GPU mem util>
"""
def cleanup(bootstrappers: List[Bootstrapper]):
logger.info("Cleanning up the engine processes")
for bootstrapper in bootstrappers:
bootstrapper.close()
LMCacheServerManager.close_servers()
logger.info(f"Running experiment: {workload_config.desc()} {usecase}")
# Create the workloads
workload_generators = []
for engine_config in engine_configs:
workload_generators.append(CreateWorkloadGenerator(workload_config, usecase, get_max_context_length(engine_config.vllm_config.model)))
workloads = [generator.generate() for generator in workload_generators]
# Start the serving engine
bootstrappers = []
for config in engine_configs:
bootstrappers.append(CreateBootstrapper(config))
bootstrappers[-1].start()
try:
# Wait for the engines to be ready
for bootstrapper in bootstrappers:
ready = bootstrapper.wait_until_ready(timeout = 300)
if not ready:
logger.error(f"Engine {bootstrapper} is not ready")
cleanup(bootstrappers)
return
# Create the clients
clients = [create_openai_client(config.vllm_config.port, config.vllm_config.model) for config in engine_configs]
models = [config.vllm_config.model for config in engine_configs]
# Execute the requests
executor = RequestExecutor()
executor.schedule_requests(workloads, clients, models)
results = executor.execute_all()
#print(results)
# Read GPU memory utilization
gpu_usage = read_gpu_memory()
except Exception as e:
logger.error(f"Experiment failed: {e}")
cleanup(bootstrappers)
return None
finally:
# Cleanup
cleanup(bootstrappers)
return results, gpu_usage
def run_multi_turn_experiment(
workload_config: WorkloadConfig,
usecase: Usecase,
engine_configs: List[BootstrapConfig]) -> List[ExperimentResultWithOutput]:
"""
Run a multi turn experiment:
- Bootstrap the serving bootstrappers
- Create workload generator for each engine
- Wait for engine ready
- (loop) Generate workload and send requests
- Close the bootstrappers
Returns:
- None if the experiment failed
- <workload desc> <timestamp> <engine id> <request id> <TTFT> <throughput> <GPU mem util>
"""
def cleanup(bootstrappers: List[Bootstrapper]):
logger.info("Cleanning up the engine processes")
for bootstrapper in bootstrappers:
bootstrapper.close()
LMCacheServerManager.close_servers()
logger.info(f"Running experiment: {workload_config.desc()} {usecase}")
# Create the workloads
workload_generators = [MultiTurnWorkloadGenerator(workload_config) for _ in engine_configs]
# Start the serving engine
bootstrappers = []
for config in engine_configs:
bootstrappers.append(CreateBootstrapper(config))
bootstrappers[-1].start()
try:
# Wait for the engines to be ready
for bootstrapper in bootstrappers:
ready = bootstrapper.wait_until_ready(timeout = 300)
if not ready:
logger.error(f"Engine {bootstrapper} is not ready")
cleanup(bootstrappers)
return
# Create the clients
clients = [create_openai_client(config.vllm_config.port, config.vllm_config.model) for config in engine_configs]
models = [config.vllm_config.model for config in engine_configs]
# Execute the requests
executor = RequestExecutor()
# Generation and execution in multi turns (hard-coded 10 turns)
results = []
gpu_usage = []
for i in range(10):
workloads = [generator.generate() for generator in workload_generators]
executor.schedule_requests(workloads, clients, models)
results.append(executor.execute_all_with_output())
[setattr(result, 'request_id', i) for result in results[i]]
gpu_usage.append(read_gpu_memory())
for result in results[i]:
workload_generators[result.engine_id].offset += 1 / workload_config.qps
workload_generators[result.engine_id].store(result.messages)
except Exception as e:
logger.error(f"Experiment failed: {e}")
cleanup(bootstrappers)
return None
finally:
# Cleanup
cleanup(bootstrappers)
return results, gpu_usage
def run_test_case(case: TestCase) -> pd.DataFrame:
"""
Run a single test case
"""
dfs = []
for expr_id, (workload_cfg, usecase) in enumerate(case.experiments):
if usecase == Usecase.MULTI:
results = run_multi_turn_experiment(workload_cfg, usecase, case.engines)
else:
results = run_experiment(workload_cfg, usecase, case.engines)
if results is None:
logger.error(f"Experiment failed: {workload_cfg.desc()} {usecase}")
continue
else:
results, gpu_usage = results
if usecase == Usecase.MULTI:
for idx, result in enumerate(results):
dataframe = pd.DataFrame([item.__dict__ for item in result])
dataframe = dataframe.drop(columns=['messages'])
dataframe = dataframe.sort_values(by=["timestamp", "engine_id", "request_id"])
dataframe["context_len"] = workload_cfg.context_length
dataframe["query_len"] = workload_cfg.query_length
#dataframe["workload"] = workload_cfg.desc()
dataframe["gpu_memory"] = gpu_usage[idx]
dataframe["expr_id"] = "-"
dfs.append(dataframe)
else:
dataframe = pd.DataFrame([item.__dict__ for item in results])
dataframe = dataframe.sort_values(by=["timestamp", "engine_id", "request_id"])
# TODO: need to handle the case when max context length is reached
if usecase == Usecase.VARY:
dataframe["context_len"] = [workload_cfg.context_length * (idx // 2 + 1) for idx in range(1, len(dataframe)+1)]
else:
dataframe["context_len"] = workload_cfg.context_length
dataframe["query_len"] = workload_cfg.query_length
#dataframe["workload"] = workload_cfg.desc()
dataframe["gpu_memory"] = gpu_usage
dataframe["expr_id"] = expr_id
dfs.append(dataframe)
return pd.concat(dfs)
def run_test_cases(cases: List[TestCase]) -> pd.DataFrame:
"""
Run multiple test cases
The returned dataframe will have a new column "case_id"
"""
dataframes = []
for case_id, case in enumerate(cases):
dataframe = run_test_case(case)
dataframe["case_id"] = case_id
dataframes.append(dataframe)
return pd.concat(dataframes)
#if __name__ == "__main__":
# # test the request executor
# #requests = [
# # [Request(1, "context1", "question1"), Request(3, "context1", "question2")],
# # [Request(2, "context2", "question1"), Request(4, "context2", "question2")],
# # ]
# #clients = ["client1", "client2"]
# #executor = RequestExecutor("model_name")
# #executor.schedule_requests(requests, clients)
# #print(executor.pending_requests)
# #for val in executor.execute_all():
# # print(val)
#
# from configs import VLLMConfig, VLLMOptionalConfig, LMCacheConfig, EngineType
# vllm_config1 = VLLMConfig(
# port = 8000,
# model = "mistralai/Mistral-7B-Instruct-v0.2",
# gpu_memory_utilization = 0.5,
# tensor_parallel_size = 1)
#
# vllm_config2 = VLLMConfig(
# port = 8001,
# model = "mistralai/Mistral-7B-Instruct-v0.2",
# gpu_memory_utilization = 0.5,
# tensor_parallel_size = 1)
#
# config = BootstrapConfig(
# engine_type = EngineType.LOCAL,
# vllm_config = vllm_config1,
# vllm_optional_config = VLLMOptionalConfig(),
# lmcache_config = LMCacheConfig("configs/example.yaml"),
# #lmcache_config = LMCacheConfig(None),
# envs = {"CUDA_VISIBLE_DEVICES": "0"})
#
# import copy
# config2 = copy.deepcopy(config)
# config2.vllm_config = vllm_config2
# config2.envs = {"CUDA_VISIBLE_DEVICES": "1"}
#
# workload_config = WorkloadConfig(1, 3, 1000, 100)
#
# test_case = TestCase([(workload_config, Usecase.DUMMY)], [config, config2])
# #run_experiment(workload_config, Usecase.DUMMY, [config, config2])
# results = run_test_case(test_case)
# print(results)