forked from waybarrios/vllm-mlx
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_batching_deterministic.py
More file actions
460 lines (360 loc) · 15.5 KB
/
test_batching_deterministic.py
File metadata and controls
460 lines (360 loc) · 15.5 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# SPDX-License-Identifier: Apache-2.0
"""
Deterministic tests for continuous batching system.
These tests use temperature=0 to ensure reproducible outputs.
Run with: pytest tests/test_batching_deterministic.py -v
"""
import asyncio
import time
import pytest
# Model to use for tests - small model for fast testing
TEST_MODEL = "mlx-community/Llama-3.2-1B-Instruct-4bit"
@pytest.fixture(scope="module")
def model_and_tokenizer():
"""Load model once for all tests in this module."""
try:
from mlx_lm import load
model, tokenizer = load(TEST_MODEL)
return model, tokenizer
except Exception as e:
pytest.skip(f"Could not load model {TEST_MODEL}: {e}")
@pytest.fixture
def sampling_params():
"""Deterministic sampling params (temperature=0)."""
from vllm_mlx import SamplingParams
return SamplingParams(max_tokens=10, temperature=0.0, top_p=1.0)
class TestDeterministicSingleRequest:
"""Test single request determinism."""
@pytest.mark.asyncio
async def test_same_prompt_same_output(self, model_and_tokenizer, sampling_params):
"""Same prompt should produce same output with temp=0."""
from vllm_mlx import AsyncEngineCore, EngineConfig, SchedulerConfig
model, tokenizer = model_and_tokenizer
config = EngineConfig(
scheduler_config=SchedulerConfig(
max_num_seqs=4,
prefill_batch_size=2,
completion_batch_size=4,
)
)
prompt = "What is 2+2? Answer:"
outputs = []
for _ in range(3): # Run 3 times
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
request_id = await engine.add_request(prompt, sampling_params)
async for output in engine.stream_outputs(request_id, timeout=30):
if output.finished:
outputs.append(output.output_text)
break
# All outputs should be identical
assert len(outputs) == 3
assert outputs[0] == outputs[1] == outputs[2], f"Outputs differ: {outputs}"
@pytest.mark.asyncio
async def test_token_streaming_order(self, model_and_tokenizer, sampling_params):
"""Tokens should stream in order."""
from vllm_mlx import AsyncEngineCore
model, tokenizer = model_and_tokenizer
async with AsyncEngineCore(model, tokenizer) as engine:
await asyncio.sleep(0.05)
request_id = await engine.add_request(
"Count from 1 to 5:",
sampling_params,
)
token_ids = []
async for output in engine.stream_outputs(request_id, timeout=30):
token_ids.extend(output.new_token_ids)
if output.finished:
# Final output should have all tokens
assert output.output_token_ids == token_ids
break
class TestDeterministicConcurrentRequests:
"""Test concurrent request handling with determinism."""
@pytest.mark.asyncio
async def test_concurrent_same_prompt(self, model_and_tokenizer):
"""Multiple concurrent requests with same prompt should get same output."""
from vllm_mlx import (
AsyncEngineCore,
EngineConfig,
SamplingParams,
SchedulerConfig,
)
model, tokenizer = model_and_tokenizer
config = EngineConfig(
scheduler_config=SchedulerConfig(
max_num_seqs=8,
prefill_batch_size=4,
completion_batch_size=8,
)
)
params = SamplingParams(max_tokens=10, temperature=0.0)
prompt = "The capital of France is"
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
# Send 4 identical requests
request_ids = []
for _ in range(4):
rid = await engine.add_request(prompt, params)
request_ids.append(rid)
# Collect outputs
async def get_output(rid):
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
return out.output_text
return None
results = await asyncio.gather(*[get_output(r) for r in request_ids])
# All should be the same
assert all(r == results[0] for r in results), f"Outputs differ: {results}"
@pytest.mark.asyncio
async def test_concurrent_different_prompts(self, model_and_tokenizer):
"""Different prompts should get different (but deterministic) outputs."""
from vllm_mlx import (
AsyncEngineCore,
EngineConfig,
SamplingParams,
SchedulerConfig,
)
model, tokenizer = model_and_tokenizer
config = EngineConfig(
scheduler_config=SchedulerConfig(
max_num_seqs=8,
prefill_batch_size=4,
)
)
params = SamplingParams(max_tokens=5, temperature=0.0)
prompts = [
"Capital of France:",
"Capital of Spain:",
"Capital of Italy:",
]
# Run twice to verify determinism
all_results = []
for run in range(2):
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
request_ids = []
for p in prompts:
rid = await engine.add_request(p, params)
request_ids.append(rid)
async def get_output(rid):
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
return out.output_text
return None
results = await asyncio.gather(*[get_output(r) for r in request_ids])
all_results.append(results)
# Each run should produce same results
assert all_results[0] == all_results[1], (
f"Results differ between runs: {all_results}"
)
class TestBatchingPerformance:
"""Test that batching improves throughput."""
@pytest.mark.asyncio
async def test_batched_faster_than_sequential(self, model_and_tokenizer):
"""Batched requests should be faster than sequential."""
from vllm_mlx import (
AsyncEngineCore,
EngineConfig,
SamplingParams,
SchedulerConfig,
)
model, tokenizer = model_and_tokenizer
config = EngineConfig(
scheduler_config=SchedulerConfig(
max_num_seqs=8,
prefill_batch_size=4,
completion_batch_size=8,
)
)
params = SamplingParams(max_tokens=10, temperature=0.0)
prompts = [f"Count to {i}:" for i in range(1, 5)]
async def run_sequential():
"""Run requests one at a time."""
total_tokens = 0
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
for prompt in prompts:
rid = await engine.add_request(prompt, params)
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
total_tokens += out.completion_tokens
break
return total_tokens
async def run_batched():
"""Run requests concurrently."""
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
request_ids = []
for prompt in prompts:
rid = await engine.add_request(prompt, params)
request_ids.append(rid)
async def get_output(rid):
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
return out.completion_tokens
return 0
tokens = await asyncio.gather(*[get_output(r) for r in request_ids])
return sum(tokens)
# Time sequential
start = time.perf_counter()
seq_tokens = await run_sequential()
seq_time = time.perf_counter() - start
# Time batched
start = time.perf_counter()
batch_tokens = await run_batched()
batch_time = time.perf_counter() - start
# Batched should be faster (at least 1.5x)
seq_throughput = seq_tokens / seq_time
batch_throughput = batch_tokens / batch_time
print(f"\nSequential: {seq_throughput:.1f} tok/s")
print(f"Batched: {batch_throughput:.1f} tok/s")
print(f"Speedup: {batch_throughput / seq_throughput:.2f}x")
# Batched should have better throughput (allow 10% tolerance for variance)
assert batch_throughput > seq_throughput * 0.9, (
f"Batched ({batch_throughput:.1f} tok/s) should be faster than "
f"sequential ({seq_throughput:.1f} tok/s)"
)
class TestRequestManagement:
"""Test request lifecycle management."""
@pytest.mark.asyncio
async def test_abort_request(self, model_and_tokenizer):
"""Test aborting a request mid-generation."""
from vllm_mlx import AsyncEngineCore, SamplingParams
model, tokenizer = model_and_tokenizer
params = SamplingParams(max_tokens=100, temperature=0.0)
async with AsyncEngineCore(model, tokenizer) as engine:
await asyncio.sleep(0.05)
# Start a long request
rid = await engine.add_request(
"Write a very long story about a dragon:",
params,
)
# Get a few tokens
token_count = 0
async for output in engine.stream_outputs(rid, timeout=30):
token_count += len(output.new_token_ids)
if token_count >= 5:
# Abort after 5 tokens
await engine.abort_request(rid)
break
# Request should be aborted
stats = engine.get_stats()
assert stats["active_requests"] == 0
@pytest.mark.asyncio
async def test_engine_stats(self, model_and_tokenizer):
"""Test engine statistics tracking."""
from vllm_mlx import (
AsyncEngineCore,
EngineConfig,
SamplingParams,
SchedulerConfig,
)
model, tokenizer = model_and_tokenizer
config = EngineConfig(scheduler_config=SchedulerConfig(max_num_seqs=4))
params = SamplingParams(max_tokens=5, temperature=0.0)
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
# Initial stats
stats = engine.get_stats()
assert stats["running"] is True
assert stats["num_waiting"] == 0
assert stats["num_running"] == 0
# Add and complete a request
rid = await engine.add_request("Hello", params)
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
break
# Check stats after completion
stats = engine.get_stats()
assert stats["num_requests_processed"] >= 1
assert stats["total_completion_tokens"] > 0
class TestSchedulerPolicy:
"""Test scheduler policies."""
@pytest.mark.asyncio
async def test_fcfs_ordering(self, model_and_tokenizer):
"""Test that FCFS policy processes requests in order."""
from vllm_mlx import (
AsyncEngineCore,
EngineConfig,
SamplingParams,
SchedulerConfig,
)
from vllm_mlx.scheduler import SchedulingPolicy
model, tokenizer = model_and_tokenizer
config = EngineConfig(
scheduler_config=SchedulerConfig(
max_num_seqs=2, # Small batch to test ordering
policy=SchedulingPolicy.FCFS,
)
)
params = SamplingParams(max_tokens=3, temperature=0.0)
async with AsyncEngineCore(model, tokenizer, config) as engine:
await asyncio.sleep(0.05)
# Add requests with small delay
rid1 = await engine.add_request("First:", params)
await asyncio.sleep(0.01)
rid2 = await engine.add_request("Second:", params)
await asyncio.sleep(0.01)
rid3 = await engine.add_request("Third:", params)
# Collect completion order
completion_order = []
async def track_completion(rid, name):
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
completion_order.append(name)
return
await asyncio.gather(
track_completion(rid1, "first"),
track_completion(rid2, "second"),
track_completion(rid3, "third"),
)
# All should complete (order may vary due to batching, but all should finish)
assert len(completion_order) == 3
class TestEdgeCases:
"""Test edge cases and error handling."""
@pytest.mark.asyncio
async def test_empty_prompt(self, model_and_tokenizer):
"""Test handling of empty prompt."""
from vllm_mlx import AsyncEngineCore, SamplingParams
model, tokenizer = model_and_tokenizer
params = SamplingParams(max_tokens=5, temperature=0.0)
async with AsyncEngineCore(model, tokenizer) as engine:
await asyncio.sleep(0.05)
rid = await engine.add_request("", params)
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
# Should complete even with empty prompt
assert out.finished
break
@pytest.mark.asyncio
async def test_very_short_max_tokens(self, model_and_tokenizer):
"""Test with max_tokens=1."""
from vllm_mlx import AsyncEngineCore, SamplingParams
model, tokenizer = model_and_tokenizer
params = SamplingParams(max_tokens=1, temperature=0.0)
async with AsyncEngineCore(model, tokenizer) as engine:
await asyncio.sleep(0.05)
rid = await engine.add_request("Hello", params)
token_count = 0
async for out in engine.stream_outputs(rid, timeout=30):
token_count += len(out.new_token_ids)
if out.finished:
break
# Should generate exactly 1 token
assert token_count == 1
@pytest.mark.asyncio
async def test_multiple_start_stop(self, model_and_tokenizer):
"""Test starting and stopping engine multiple times."""
from vllm_mlx import AsyncEngineCore, SamplingParams
model, tokenizer = model_and_tokenizer
params = SamplingParams(max_tokens=3, temperature=0.0)
for _ in range(3):
async with AsyncEngineCore(model, tokenizer) as engine:
await asyncio.sleep(0.05)
rid = await engine.add_request("Test:", params)
async for out in engine.stream_outputs(rid, timeout=30):
if out.finished:
assert out.completion_tokens > 0
break
if __name__ == "__main__":
pytest.main([__file__, "-v", "-s"])