Merge f03f3b0899206fbe34458494d86b880a417c7e05 into 553741557a57df44a46fb3ab72b6aafeaf945337

This commit is contained in:
lhy027 2026-10-27 14:03:11 +08:00 committed by GitHub
commit 5072084f1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 1 deletions

View File

@ -157,13 +157,23 @@ class LLM:
prompt_token_ids = np.asarray(prompt_token_ids, dtype=np.int32)
if multi_modal_data is None and self.llm_engine.multi_modal_processor is not None:
multi_modal_data = dict(image_bytes=[], image_sizes=[])
return self.llm_engine.add_request(
sequence = self.llm_engine.add_request(
user_id="0", # HACK 随便加一个 user_id
request_id=request_id,
sampling_params=sampling_params,
prompt_token_ids=prompt_token_ids,
multi_modal_data=multi_modal_data,
)
# add_request returns None when it rejects the request (e.g. out-of-range
# prompt token ids, see the engine log for details). Silently dropping it
# would make generate() return fewer results than the input prompts,
# misaligning results[i] with prompts[i], so fail loudly instead.
if sequence is None:
raise ValueError(
f"Request {request_id} was rejected by the engine and returned None "
"(see engine log for the reason, e.g. out-of-range prompt token ids)"
)
return sequence
def _run_engine(self, use_tqdm: bool) -> list[list[RequestOutput] | str]:
# Initialize tqdm.

View File

@ -0,0 +1,38 @@
import itertools
from types import SimpleNamespace
import pytest
from deepinfer.entrypoints.llm import LLM
from deepinfer.sampling_params import SamplingParams
SENTINEL_SEQUENCE = object()
def _llm(add_request_result: object) -> LLM:
"""A bare LLM whose engine add_request always returns the given result."""
llm = object.__new__(LLM)
llm.request_counter = itertools.count()
llm.llm_engine = SimpleNamespace(
multi_modal_processor=None,
add_request=lambda **kwargs: add_request_result,
)
return llm
def test_add_request_returns_engine_sequence() -> None:
llm = _llm(SENTINEL_SEQUENCE)
sequence = llm._add_request(prompt=None, sampling_params=SamplingParams(), prompt_token_ids=[1, 2, 3])
assert sequence is SENTINEL_SEQUENCE
def test_add_request_raises_when_engine_rejects_request() -> None:
# add_request returns None when the engine drops the request (e.g. out-of-range
# prompt token ids). generate() must fail loudly instead of silently returning
# fewer outputs than input prompts, which would misalign results with prompts.
llm = _llm(None)
with pytest.raises(ValueError, match="rejected by the engine"):
llm._add_request(prompt=None, sampling_params=SamplingParams(), prompt_token_ids=[1, 2, 3])