Skip to content

Commit dc7976d

Browse files
[Misc] Upgrade more code to Python 3.10 (vllm-project#26463)
Signed-off-by: DarkLight1337 <[email protected]>
1 parent e479143 commit dc7976d

File tree

7 files changed

+16
-33
lines changed

7 files changed

+16
-33
lines changed

tests/entrypoints/openai/test_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ async def test_chat_completion_stream_options(
369369
assert chunk.usage is None
370370
else:
371371
assert chunk.usage is None
372-
final_chunk = await stream.__anext__()
372+
final_chunk = await anext(stream)
373373
assert final_chunk.usage is not None
374374
assert final_chunk.usage.prompt_tokens > 0
375375
assert final_chunk.usage.completion_tokens > 0

tests/entrypoints/test_context.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
from vllm.outputs import CompletionOutput, RequestOutput
1111

1212

13-
# Helper function for Python < 3.10 compatibility
14-
async def async_next(async_iterator):
15-
"""Compatibility function equivalent to Python 3.10's anext()."""
16-
return await async_iterator.__anext__()
17-
18-
1913
def create_mock_request_output(
2014
prompt_token_ids=None,
2115
output_token_ids=None,
@@ -129,7 +123,7 @@ async def test_multi_turn_token_counting():
129123
)
130124

131125
# First turn - initial prompt and response
132-
mock_output1 = await async_next(mock_generator)
126+
mock_output1 = await anext(mock_generator)
133127
context.append_output(mock_output1)
134128

135129
# At this point, we should have 5 prompt tokens and 3 output tokens
@@ -138,7 +132,7 @@ async def test_multi_turn_token_counting():
138132
assert context.num_tool_output_tokens == 0
139133

140134
# Second turn - after tool output
141-
mock_output2 = await async_next(mock_generator)
135+
mock_output2 = await anext(mock_generator)
142136
context.append_output(mock_output2)
143137
# Current prompt tokens (15) - last_turn_input_tokens (5) -
144138
# last_turn_output_tokens (3) = 7
@@ -150,7 +144,7 @@ async def test_multi_turn_token_counting():
150144
assert context.num_cached_tokens == 5
151145

152146
# Third turn - final response
153-
mock_output3 = await async_next(mock_generator)
147+
mock_output3 = await anext(mock_generator)
154148
context.append_output(mock_output3)
155149
# Additional tool output tokens from third turn:
156150
# Current prompt (20) - last_turn_input_tokens (15) -

tests/utils_/test_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ async def stream_output(generator: AsyncIterator[tuple[int, str]]):
7575

7676
for iterator in iterators:
7777
try:
78-
# Can use anext() in python >= 3.10
79-
await asyncio.wait_for(iterator.__anext__(), 1)
78+
await asyncio.wait_for(anext(iterator), 1)
8079
except StopAsyncIteration:
8180
# All iterators should be cancelled and print this message.
8281
print("Iterator was cancelled normally")

tests/v1/entrypoints/openai/test_completion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ async def test_completion_stream_options(client: openai.AsyncOpenAI, model_name:
420420
assert chunk.usage is None
421421
else:
422422
assert chunk.usage is None
423-
final_chunk = await stream.__anext__()
423+
final_chunk = await anext(stream)
424424
assert final_chunk.usage is not None
425425
assert final_chunk.usage.prompt_tokens > 0
426426
assert final_chunk.usage.completion_tokens > 0
@@ -450,7 +450,7 @@ async def test_completion_stream_options(client: openai.AsyncOpenAI, model_name:
450450
chunk.usage.prompt_tokens + chunk.usage.completion_tokens
451451
)
452452
if chunk.choices[0].finish_reason is not None:
453-
final_chunk = await stream.__anext__()
453+
final_chunk = await anext(stream)
454454
assert final_chunk.usage is not None
455455
assert final_chunk.usage.prompt_tokens > 0
456456
assert final_chunk.usage.completion_tokens > 0

vllm/benchmarks/serve.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import argparse
2020
import asyncio
21+
import contextlib
2122
import gc
2223
import importlib.util
2324
import json
@@ -605,17 +606,13 @@ async def benchmark(
605606

606607
pbar = None if disable_tqdm else tqdm(total=len(input_requests))
607608

608-
# This can be used once the minimum Python version is 3.10 or higher,
609-
# and it will simplify the code in limited_request_func.
610-
# semaphore = (asyncio.Semaphore(max_concurrency)
611-
# if max_concurrency else contextlib.nullcontext())
612-
semaphore = asyncio.Semaphore(max_concurrency) if max_concurrency else None
609+
semaphore = (
610+
asyncio.Semaphore(max_concurrency)
611+
if max_concurrency
612+
else contextlib.nullcontext()
613+
)
613614

614615
async def limited_request_func(request_func_input, session, pbar):
615-
if semaphore is None:
616-
return await request_func(
617-
request_func_input=request_func_input, session=session, pbar=pbar
618-
)
619616
async with semaphore:
620617
return await request_func(
621618
request_func_input=request_func_input, session=session, pbar=pbar

vllm/utils/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,6 @@ def _async_wrapper(*args: P.args, **kwargs: P.kwargs) -> asyncio.Future:
469469
return _async_wrapper
470470

471471

472-
def _next_task(iterator: AsyncGenerator[T, None], loop: AbstractEventLoop) -> Task:
473-
# Can use anext() in python >= 3.10
474-
return loop.create_task(iterator.__anext__()) # type: ignore[arg-type]
475-
476-
477472
async def merge_async_iterators(
478473
*iterators: AsyncGenerator[T, None],
479474
) -> AsyncGenerator[tuple[int, T], None]:
@@ -491,7 +486,7 @@ async def merge_async_iterators(
491486

492487
loop = asyncio.get_running_loop()
493488

494-
awaits = {_next_task(pair[1], loop): pair for pair in enumerate(iterators)}
489+
awaits = {loop.create_task(anext(it)): (i, it) for i, it in enumerate(iterators)}
495490
try:
496491
while awaits:
497492
done, _ = await asyncio.wait(awaits.keys(), return_when=FIRST_COMPLETED)
@@ -500,7 +495,7 @@ async def merge_async_iterators(
500495
try:
501496
item = await d
502497
i, it = pair
503-
awaits[_next_task(it, loop)] = pair
498+
awaits[loop.create_task(anext(it))] = pair
504499
yield i, item
505500
except StopAsyncIteration:
506501
pass

vllm/v1/serial_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ def __init__(self, t: Optional[Any] = None):
290290
_log_insecure_serialization_warning()
291291

292292
def decode(self, bufs: Union[bytestr, Sequence[bytestr]]) -> Any:
293-
if isinstance(bufs, (bytes, bytearray, memoryview, zmq.Frame)):
294-
# TODO - This check can become `isinstance(bufs, bytestr)`
295-
# as of Python 3.10.
293+
if isinstance(bufs, bytestr): # type: ignore
296294
return self.decoder.decode(bufs)
297295

298296
self.aux_buffers = bufs

0 commit comments

Comments
 (0)