-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlatency.py
More file actions
444 lines (383 loc) · 13.4 KB
/
Copy pathlatency.py
File metadata and controls
444 lines (383 loc) · 13.4 KB
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
from __future__ import annotations
# pyright: reportAny=false, reportExplicitAny=false, reportUnknownMemberType=false, reportUnknownVariableType=false, reportUnknownArgumentType=false, reportMissingTypeStubs=false, reportPrivateLocalImportUsage=false, reportUnannotatedClassAttribute=false, reportUnusedCallResult=false, reportUnusedParameter=false, reportAttributeAccessIssue=false, reportImplicitStringConcatenation=false
import argparse
import json
import math
import sys
import time
import warnings
from pathlib import Path
from typing import Any
import torch
PROJECT_ROOT = Path(__file__).resolve().parents[2]
DEFAULT_CONCURRENCY = (1, 2, 4, 8)
DEFAULT_MAX_NEW_TOKENS = 16
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Measure continuous-batching request latency."
)
parser.add_argument("--model", required=True, help="Model name or path")
parser.add_argument(
"--offload-dir",
required=True,
help="Directory used for MoE expert offload storage",
)
parser.add_argument(
"--concurrency",
nargs="+",
type=int,
default=list(DEFAULT_CONCURRENCY),
help="Concurrency levels to measure",
)
parser.add_argument(
"--num-rounds",
type=int,
default=5,
help="Measurement rounds per concurrency level",
)
parser.add_argument(
"--prompt-length",
type=int,
default=128,
help="Prompt length in tokens",
)
parser.add_argument(
"--max-new-tokens",
type=int,
default=DEFAULT_MAX_NEW_TOKENS,
help="Generated tokens per request",
)
parser.add_argument(
"--baseline-json",
default="baseline_results.json",
help="Optional baseline_performance.py output JSON for comparison",
)
parser.add_argument(
"--output-json",
default="latency_results.json",
help="Path to write the benchmark summary JSON",
)
return parser.parse_args()
def environment_info() -> dict[str, Any]:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
cuda_available = torch.cuda.is_available()
cuda_device_count = torch.cuda.device_count()
info: dict[str, Any] = {
"torch_version": getattr(torch, "__version__", "unknown"),
"torch_cuda_version": getattr(torch.version, "cuda", None),
"cuda_available": cuda_available,
"cuda_device_count": cuda_device_count,
}
if cuda_available and cuda_device_count > 0:
info["cuda_device_names"] = [
torch.cuda.get_device_name(idx) for idx in range(cuda_device_count)
]
return info
def write_json(path: Path, payload: dict[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as f:
json.dump(payload, f, indent=2, ensure_ascii=False)
f.write("\n")
def _repeat_to_length(token_ids: list[int], target_length: int) -> list[int]:
if target_length <= 0:
raise ValueError(f"target_length must be > 0, got {target_length}")
if not token_ids:
return [0] * target_length
output: list[int] = []
while len(output) < target_length:
output.extend(token_ids)
return output[:target_length]
def build_prompt_input_ids(
tokenizer: Any, target_length: int, batch_size: int
) -> torch.Tensor:
if batch_size <= 0:
raise ValueError(f"batch_size must be > 0, got {batch_size}")
base_text = (
"MoE-Infinity continuous batching latency benchmark prompt. "
"Keep this text deterministic for stable measurements."
)
encoded = tokenizer.encode(base_text, add_special_tokens=False)
prompt_ids = _repeat_to_length(encoded, target_length)
batch_input = [list(prompt_ids) for _ in range(batch_size)]
return torch.tensor(batch_input, dtype=torch.long, device="cuda")
def load_model_and_tokenizer(
model_name: str, offload_dir: str
) -> tuple[Any, Any]:
try:
from transformers import AutoTokenizer
except Exception as exc:
raise RuntimeError(f"transformers import failed: {exc}") from exc
try:
from moe_infinity import MoE
except Exception as exc:
raise RuntimeError(f"moe_infinity import failed: {exc}") from exc
tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True,
)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
config = {
"offload_path": offload_dir,
"device_memory_ratio": 0.75,
}
model = MoE(model_name, config)
return model, tokenizer
class BatchTimingStreamer:
def __init__(self, batch_size: int) -> None:
self._seen_prompt = False
self.first_token_at: list[float | None] = [None] * batch_size
def put(self, value: Any) -> None:
_ = value
if not self._seen_prompt:
self._seen_prompt = True
return
now = time.perf_counter()
for index, token_time in enumerate(self.first_token_at):
if token_time is None:
self.first_token_at[index] = now
def end(self) -> None:
return None
def _to_float(value: object) -> float | None:
if isinstance(value, bool):
return None
if isinstance(value, (int, float)):
return float(value)
return None
def percentile(values: list[float], p: float) -> float | None:
if not values:
return None
sorted_values = sorted(values)
if len(sorted_values) == 1:
return sorted_values[0]
position = (p / 100.0) * (len(sorted_values) - 1)
lower = int(math.floor(position))
upper = int(math.ceil(position))
if lower == upper:
return sorted_values[lower]
lower_value = sorted_values[lower]
upper_value = sorted_values[upper]
fraction = position - lower
return lower_value + (upper_value - lower_value) * fraction
def run_one_round(
model: Any,
tokenizer: Any,
*,
concurrency: int,
prompt_length: int,
max_new_tokens: int,
) -> tuple[list[float], list[float]]:
input_ids = build_prompt_input_ids(tokenizer, prompt_length, concurrency)
streamer = BatchTimingStreamer(concurrency)
if torch.cuda.is_available():
torch.cuda.synchronize()
start = time.perf_counter()
output_ids = model.generate(
input_ids,
max_new_tokens=max_new_tokens,
do_sample=False,
streamer=streamer,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
if torch.cuda.is_available():
torch.cuda.synchronize()
end = time.perf_counter()
total_latency_ms = (end - start) * 1000.0
generated_per_request = max(
int(output_ids.shape[-1] - input_ids.shape[-1]), 1
)
ttft_samples: list[float] = []
itl_samples: list[float] = []
for token_time in streamer.first_token_at:
if token_time is None:
ttft_ms = total_latency_ms
else:
ttft_ms = (token_time - start) * 1000.0
decode_ms = max(total_latency_ms - ttft_ms, 0.0)
if generated_per_request <= 1:
itl_ms = decode_ms
else:
itl_ms = decode_ms / (generated_per_request - 1)
ttft_samples.append(ttft_ms)
itl_samples.append(itl_ms)
return ttft_samples, itl_samples
def run_sweep(
model: Any,
tokenizer: Any,
*,
concurrency_levels: list[int],
num_rounds: int,
prompt_length: int,
max_new_tokens: int,
) -> dict[str, dict[str, float | None]]:
measurements: dict[str, dict[str, float | None]] = {}
for concurrency in concurrency_levels:
all_ttft: list[float] = []
all_itl: list[float] = []
for _ in range(num_rounds):
ttft_samples, itl_samples = run_one_round(
model,
tokenizer,
concurrency=concurrency,
prompt_length=prompt_length,
max_new_tokens=max_new_tokens,
)
all_ttft.extend(ttft_samples)
all_itl.extend(itl_samples)
measurements[str(concurrency)] = {
"ttft_p50_ms": percentile(all_ttft, 50.0),
"ttft_p90_ms": percentile(all_ttft, 90.0),
"ttft_p99_ms": percentile(all_ttft, 99.0),
"itl_p50_ms": percentile(all_itl, 50.0),
"itl_p90_ms": percentile(all_itl, 90.0),
"itl_p99_ms": percentile(all_itl, 99.0),
}
return measurements
def load_baseline(path: Path) -> dict[str, Any] | None:
if not path.exists():
return None
try:
with path.open("r", encoding="utf-8") as f:
payload = json.load(f)
except Exception:
return None
if isinstance(payload, dict):
return payload
return None
def baseline_reference(
baseline_payload: dict[str, Any] | None,
) -> dict[str, float | None]:
if baseline_payload is None:
return {
"ttft_ms": None,
"per_token_latency_ms": None,
}
measurement = baseline_payload.get("measurement")
if not isinstance(measurement, dict):
return {
"ttft_ms": None,
"per_token_latency_ms": None,
}
return {
"ttft_ms": _to_float(measurement.get("ttft_ms")),
"per_token_latency_ms": _to_float(
measurement.get("per_token_latency_ms")
),
}
def main() -> int:
args = parse_args()
if args.num_rounds <= 0:
raise ValueError("--num-rounds must be > 0")
if args.prompt_length <= 0:
raise ValueError("--prompt-length must be > 0")
if args.max_new_tokens <= 0:
raise ValueError("--max-new-tokens must be > 0")
concurrency_levels = [value for value in args.concurrency if value > 0]
if not concurrency_levels:
raise ValueError(
"--concurrency must include at least one positive value"
)
env = environment_info()
output_path = Path(args.output_json)
baseline = baseline_reference(load_baseline(Path(args.baseline_json)))
print("=== MoE-Infinity Continuous Batching Latency ===")
print(f"Project root: {PROJECT_ROOT}")
print(f"CUDA available: {env['cuda_available']}")
if not env["cuda_available"]:
print("BLOCKED: No CUDA. Run on GPU hardware.")
payload = {
"status": "BLOCKED",
"reason": "No CUDA device",
"environment": env,
"measurement": {
str(level): {
"ttft_p50_ms": None,
"ttft_p90_ms": None,
"ttft_p99_ms": None,
"itl_p50_ms": None,
"itl_p90_ms": None,
"itl_p99_ms": None,
}
for level in concurrency_levels
},
"baseline": baseline,
"requested_model": args.model,
"offload_dir": args.offload_dir,
"num_rounds": args.num_rounds,
}
write_json(output_path, payload)
return 0
try:
model, tokenizer = load_model_and_tokenizer(
args.model, args.offload_dir
)
except Exception as exc:
print(f"BLOCKED: {type(exc).__name__}: {exc}")
payload = {
"status": "BLOCKED",
"reason": f"{type(exc).__name__}: {exc}",
"environment": env,
"measurement": {
str(level): {
"ttft_p50_ms": None,
"ttft_p90_ms": None,
"ttft_p99_ms": None,
"itl_p50_ms": None,
"itl_p90_ms": None,
"itl_p99_ms": None,
}
for level in concurrency_levels
},
"baseline": baseline,
"requested_model": args.model,
"offload_dir": args.offload_dir,
"num_rounds": args.num_rounds,
}
write_json(output_path, payload)
return 0
measurements = run_sweep(
model,
tokenizer,
concurrency_levels=concurrency_levels,
num_rounds=args.num_rounds,
prompt_length=args.prompt_length,
max_new_tokens=args.max_new_tokens,
)
comparison: dict[str, dict[str, float | None]] = {}
for level, result in measurements.items():
baseline_ttft = baseline.get("ttft_ms")
baseline_itl = baseline.get("per_token_latency_ms")
ttft_p50 = result.get("ttft_p50_ms")
itl_p50 = result.get("itl_p50_ms")
comparison[level] = {
"ttft_delta_ms_vs_baseline": (
None
if baseline_ttft is None or ttft_p50 is None
else ttft_p50 - baseline_ttft
),
"itl_delta_ms_vs_baseline": (
None
if baseline_itl is None or itl_p50 is None
else itl_p50 - baseline_itl
),
}
payload = {
"status": "PASS",
"environment": env,
"measurement": measurements,
"baseline": baseline,
"comparison": comparison,
"requested_model": args.model,
"offload_dir": args.offload_dir,
"num_rounds": args.num_rounds,
"prompt_length": args.prompt_length,
"max_new_tokens": args.max_new_tokens,
}
write_json(output_path, payload)
return 0
if __name__ == "__main__":
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
raise SystemExit(main())